Skip to content

Commit e5b218f

Browse files
authored
Tidy src/variable.jl (#583)
1 parent 0e42f97 commit e5b218f

File tree

9 files changed

+240
-253
lines changed

9 files changed

+240
-253
lines changed

docs/src/examples/general_examples/basic_usage.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ evaluate(y)
104104
#
105105

106106
using GLPK
107-
x = Variable(4, :Int)
107+
x = Variable(4, IntVar)
108108
p = minimize(sum(x), x >= 0.5)
109109
solve!(p, GLPK.Optimizer; silent_solver = true)
110110
evaluate(x)

docs/src/examples/mixed_integer/binary_knapsack.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ n = length(w)
2222
#-
2323

2424
using Convex, GLPK
25-
x = Variable(n, :Bin)
25+
x = Variable(n, BinVar)
2626
problem = maximize(dot(p, x), dot(w, x) <= C)
2727
solve!(problem, GLPK.Optimizer)
2828
evaluate(x)

docs/src/examples/mixed_integer/n_queens.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include(aux("antidiag.jl"))
66

77
n = 8
88
# We encode the locations of the queens with a matrix of binary random variables.
9-
x = Variable((n, n), :Bin)
9+
x = Variable((n, n), BinVar)
1010

1111
# Now we impose the constraints: at most one queen on any anti-diagonal, at most one queen on any diagonal, and we must have exactly one queen per row and per column.
1212
## At most one queen on any anti-diagonal

docs/src/examples/mixed_integer/section_allocation.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
# 10,000 or a large number would mean the student can not attend that section).
88
#
99
# The goal will be to get an allocation matrix $X$, where $X_{ij} = 1$ if
10-
# student $i$ is assigned to section $j$ and $0$ otherwise.
10+
# student $i$ is assigned to section $j$ and $0$ otherwise.
1111

1212
using Convex, GLPK
1313
aux(str) = joinpath(@__DIR__, "aux_files", str) # path to auxiliary files
1414

1515
# Load our preference matrix, `P`
1616
include(aux("data.jl"))
1717

18-
X = Variable(size(P), :Bin)
18+
X = Variable(size(P), BinVar)
1919

2020
# We want every student to be assigned to exactly one section. So, every row
2121
# must have exactly one non-zero entry. In other words, the sum of all the

src/deprecations.jl

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,72 @@ function Base.in(x::AbstractExpr, y::Symbol)
4949
)
5050
return isposdef(x)
5151
end
52+
53+
# Compatability with old `sets` model.
54+
#
55+
# Only dispatch to these methods when at least one set is given.
56+
function Variable(
57+
size::Tuple{Int,Int},
58+
sign::Sign,
59+
set::Symbol,
60+
sets::Symbol...,
61+
)
62+
@warn(
63+
"Using symbols in `Variable` constructor is deprecated. Use " *
64+
"`Convex.BinVar` or `Convex.IntVar` instead.",
65+
maxlog = 1,
66+
)
67+
sets = [set, sets...]
68+
x = if :Bin in sets
69+
Variable(size, sign, BinVar)
70+
elseif :Int in sets
71+
Variable(size, sign, IntVar)
72+
else
73+
Variable(size, sign, ContVar)
74+
end
75+
if :Semidefinite in sets
76+
add_constraint!(x, x 0)
77+
end
78+
return x
79+
end
80+
81+
function Variable(sign::Sign, set::Symbol, sets::Symbol...)
82+
return Variable((1, 1), sign, set, sets...)
83+
end
84+
85+
function Variable(size::Tuple{Int,Int}, set::Symbol, sets::Symbol...)
86+
return Variable(size, NoSign(), set, sets...)
87+
end
88+
89+
function Variable(m::Int, set::Symbol, sets::Symbol...)
90+
return Variable((m, 1), NoSign(), set, sets...)
91+
end
92+
93+
function Variable(set::Symbol, sets::Symbol...)
94+
return Variable((1, 1), NoSign(), set, sets...)
95+
end
96+
97+
function ComplexVariable(size::Tuple{Int,Int}, set::Symbol, sets::Symbol...)
98+
@warn(
99+
"Using symbols in `ComplexVariable` constructor is deprecated. Use " *
100+
"`isposdef(x)` to enforce semidefiniteness instead of `:Semidefinite.",
101+
maxlog = 1,
102+
)
103+
sets = [set, sets...]
104+
if :Bin in sets
105+
throw(ArgumentError("Complex variables cannot be restricted to binary"))
106+
elseif :Int in sets
107+
throw(
108+
ArgumentError("Complex variables cannot be restricted to integer"),
109+
)
110+
end
111+
x = ComplexVariable(size)
112+
if :Semidefinite in sets
113+
add_constraint!(x, x 0)
114+
end
115+
return x
116+
end
117+
118+
function ComplexVariable(set::Symbol, sets::Symbol...)
119+
return ComplexVariable((1, 1), set, sets...)
120+
end

src/problem_depot/problems/mip.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ end
4646
@test p.optval 5 atol = atol rtol = rtol
4747
end
4848

49-
x = Variable(2, :Int)
49+
x = Variable(2, IntVar)
5050
p = minimize(sum(x), x >= 4.3; numeric_type = T)
5151

5252
if test
@@ -69,7 +69,7 @@ end
6969
@test p.optval 12 atol = atol rtol = rtol
7070
end
7171

72-
x = Variable(2, :Int)
72+
x = Variable(2, IntVar)
7373
p = minimize(norm(x, 1), x[1] >= 4.3; numeric_type = T)
7474

7575
if test
@@ -80,7 +80,7 @@ end
8080
@test p.optval 5 atol = atol rtol = rtol
8181
end
8282

83-
x = Variable(2, :Int)
83+
x = Variable(2, IntVar)
8484
p = minimize(sum(x), x[1] >= 4.3, x >= 0; numeric_type = T)
8585

8686
if test
@@ -91,7 +91,7 @@ end
9191
@test p.optval 5 atol = atol rtol = rtol
9292
end
9393

94-
x = Variable(2, :Int)
94+
x = Variable(2, IntVar)
9595
p = minimize(sum(x), x >= 0.5; numeric_type = T)
9696

9797
if test
@@ -110,7 +110,7 @@ end
110110
rtol,
111111
::Type{T},
112112
) where {T,test}
113-
x = Variable(2, :Bin)
113+
x = Variable(2, BinVar)
114114
p = minimize(sum(x), x >= 0.5; numeric_type = T)
115115

116116
if test
@@ -121,7 +121,7 @@ end
121121
@test p.optval 2 atol = atol rtol = rtol
122122
end
123123

124-
x = Variable(2, :Bin)
124+
x = Variable(2, BinVar)
125125
p = minimize(sum(x), x[1] >= 0.5, x >= 0; numeric_type = T)
126126

127127
if test

src/problem_depot/problems/sdp.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
rtol,
77
::Type{T},
88
) where {T,test}
9-
y = Variable((2, 2), :Semidefinite)
9+
y = Semidefinite((2, 2))
1010
p = minimize(y[1, 1]; numeric_type = T)
1111

1212
# @fact problem_vexity(p) --> ConvexVexity()
@@ -15,7 +15,7 @@
1515
@test p.optval 0 atol = atol rtol = rtol
1616
end
1717

18-
y = Variable((3, 3), :Semidefinite)
18+
y = Semidefinite((3, 3))
1919
p = minimize(y[1, 1], y[2, 2] == 1; numeric_type = T)
2020

2121
# @fact problem_vexity(p) --> ConvexVexity()
@@ -27,7 +27,7 @@
2727
# Solution is obtained as y[2,2] -> infinity
2828
# This test fails on Mosek. See
2929
# https://github.com/JuliaOpt/Mosek.jl/issues/29
30-
# y = Variable((2, 2), :Semidefinite)
30+
# y = Semidefinite((2, 2))
3131
# p = minimize(y[1, 1], y[1, 2] == 1; numeric_type = T)
3232

3333
# # @fact problem_vexity(p) --> ConvexVexity()
@@ -43,7 +43,7 @@
4343
@test p.optval 1 atol = atol rtol = rtol
4444
end
4545

46-
y = Variable((3, 3), :Semidefinite)
46+
y = Semidefinite((3, 3))
4747
p = minimize(tr(y), y[2, 1] <= 4, y[2, 2] >= 3; numeric_type = T)
4848

4949
# @fact problem_vexity(p) --> ConvexVexity()

0 commit comments

Comments
 (0)