Skip to content

Commit

Permalink
Merge pull request #372 from JuliaOpt/DCP_warnings
Browse files Browse the repository at this point in the history
Allow disabling DCP warnings
  • Loading branch information
ericphanson authored Mar 5, 2020
2 parents e8b5897 + 8d3692a commit 6a5ffd8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
19 changes: 19 additions & 0 deletions docs/src/advanced.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
Advanced Features
=================

DCP warnings
------------

When an expression is created which is not of [DCP
form](https://dcp.stanford.edu/), a warning is emitted. For example,

```repl
x = Variable()
y = Variable()
x*y
```

To disable this, set the module-level parameter `DCP_WARNINGS` via

```julia
Convex.DCP_WARNINGS[] = false
```


Dual Variables
--------------

Expand Down
31 changes: 31 additions & 0 deletions src/Convex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ export Positive, Negative, ComplexSign, NoSign
# Problems
export add_constraint!, add_constraints!, maximize, minimize, Problem, satisfy, solve!


# Module level globals

"""
DCP_WARNINGS
Controls whether or not warnings are emitted for when an expression fails to be
of disciplined convex form. To turn warnings off, run
Convex.DCP_WARNINGS[] = false
"""
const DCP_WARNINGS = Threads.Atomic{Bool}(true)

"""
MAXDEPTH
Controls depth of tree printing globally for Convex.jl; defaults to 3. Set via
Convex.MAXDEPTH[] = 5
"""
const MAXDEPTH = Threads.Atomic{Int}(3)

"""
MAXWIDTH
Controls width of tree printing globally for Convex.jl; defaults to 15. Set via
Convex.MAXWIDTH[] = 15
"""
const MAXWIDTH= Threads.Atomic{Int}(15)

### modeling framework
include("dcp.jl")
include("expressions.jl")
Expand Down
4 changes: 3 additions & 1 deletion src/dcp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ struct ConcaveVexity <: Vexity end

struct NotDcp <: Vexity
function NotDcp()
@warn "Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior."
if DCP_WARNINGS[]
@warn "Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior."
end
return new()
end
end
Expand Down
14 changes: 0 additions & 14 deletions src/utilities/show.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
import Base.show, Base.summary
using .TreePrint

"""
const MAXDEPTH = Ref(3)
Controls depth of tree printing globally for Convex.jl
"""
const MAXDEPTH = Ref(3)

"""
const MAXWIDTH = Ref(15)
Controls width of tree printing globally for Convex.jl
"""
const MAXWIDTH= Ref(15)


"""
show_id(io::IO, x::Union{AbstractExpr, Constraint}; digits = 3)
Expand Down
11 changes: 11 additions & 0 deletions test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,15 @@ using Convex: AbstractExpr, ConicObj
# x.value = [1 2 3; 4 5 6]
# @fact evaluate(s) --> 21
# end

@testset "DCP warnings" begin
# default is to log
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()

Convex.DCP_WARNINGS[] = false
@test_logs Convex.NotDcp()
Convex.DCP_WARNINGS[] = true
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()

end
end

0 comments on commit 6a5ffd8

Please sign in to comment.