Skip to content

Commit caef765

Browse files
committed
Add DCP_WARNINGS constant
1 parent d142c10 commit caef765

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

docs/src/advanced.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
Advanced Features
22
=================
33

4+
DCP warnings
5+
------------
6+
7+
When an expression is created which is not of [DCP
8+
form](https://dcp.stanford.edu/), a warning is emitted. For example,
9+
10+
```repl
11+
x = Variable()
12+
y = Variable()
13+
x*y
14+
```
15+
16+
To disable this, set the module-level parameter `DCP_WARNINGS` via
17+
18+
```julia
19+
Convex.DCP_WARNINGS[] = false
20+
```
21+
22+
423
Dual Variables
524
--------------
625

src/Convex.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ export Positive, Negative, ComplexSign, NoSign
3434
# Problems
3535
export add_constraint!, add_constraints!, maximize, minimize, Problem, satisfy, solve!
3636

37+
"""
38+
const DCP_WARNINGS = Ref(true)
39+
40+
Controls whether or not warnings are emitted for when an expression fails to be
41+
of disciplined convex form. To turn warnings off, run
42+
43+
Convex.DCP_WARNINGS[] = false
44+
"""
45+
const DCP_WARNINGS = Ref(true)
46+
3747
### modeling framework
3848
include("dcp.jl")
3949
include("expressions.jl")

src/dcp.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ struct ConcaveVexity <: Vexity end
2121

2222
struct NotDcp <: Vexity
2323
function NotDcp()
24-
@warn "Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior."
24+
if DCP_WARNINGS[]
25+
@warn "Expression not DCP compliant. Trying to solve non-DCP compliant problems can lead to unexpected behavior."
26+
end
2527
return new()
2628
end
2729
end

test/test_utilities.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,15 @@ using Convex: AbstractExpr, ConicObj
250250
# x.value = [1 2 3; 4 5 6]
251251
# @fact evaluate(s) --> 21
252252
# end
253+
254+
@testset "DCP warnings" begin
255+
# default is to log
256+
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()
257+
258+
Convex.DCP_WARNINGS[] = false
259+
@test_logs Convex.NotDcp()
260+
Convex.DCP_WARNINGS[] = true
261+
@test_logs (:warn, r"not DCP compliant") Convex.NotDcp()
262+
263+
end
253264
end

0 commit comments

Comments
 (0)