Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy src/variable.jl #583

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/examples/general_examples/basic_usage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ evaluate(y)
#

using GLPK
x = Variable(4, :Int)
x = Variable(4, IntVar)
p = minimize(sum(x), x >= 0.5)
solve!(p, GLPK.Optimizer; silent_solver = true)
evaluate(x)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/examples/mixed_integer/binary_knapsack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ n = length(w)
#-

using Convex, GLPK
x = Variable(n, :Bin)
x = Variable(n, BinVar)
problem = maximize(dot(p, x), dot(w, x) <= C)
solve!(problem, GLPK.Optimizer)
evaluate(x)
2 changes: 1 addition & 1 deletion docs/src/examples/mixed_integer/n_queens.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include(aux("antidiag.jl"))

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

# 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.
## At most one queen on any anti-diagonal
Expand Down
4 changes: 2 additions & 2 deletions docs/src/examples/mixed_integer/section_allocation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
# 10,000 or a large number would mean the student can not attend that section).
#
# The goal will be to get an allocation matrix $X$, where $X_{ij} = 1$ if
# student $i$ is assigned to section $j$ and $0$ otherwise.
# student $i$ is assigned to section $j$ and $0$ otherwise.

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

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

X = Variable(size(P), :Bin)
X = Variable(size(P), BinVar)

# We want every student to be assigned to exactly one section. So, every row
# must have exactly one non-zero entry. In other words, the sum of all the
Expand Down
69 changes: 69 additions & 0 deletions src/deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,72 @@ function Base.in(x::AbstractExpr, y::Symbol)
)
return isposdef(x)
end

# Compatability with old `sets` model.
#
# Only dispatch to these methods when at least one set is given.
function Variable(
size::Tuple{Int,Int},
sign::Sign,
set::Symbol,
sets::Symbol...,
)
@warn(
"Using symbols in `Variable` constructor is deprecated. Use " *
"`Convex.BinVar` or `Convex.IntVar` instead.",
maxlog = 1,
)
sets = [set, sets...]
x = if :Bin in sets
Variable(size, sign, BinVar)
elseif :Int in sets
Variable(size, sign, IntVar)
else
Variable(size, sign, ContVar)
end
if :Semidefinite in sets
add_constraint!(x, x ⪰ 0)
end
return x
end

function Variable(sign::Sign, set::Symbol, sets::Symbol...)
return Variable((1, 1), sign, set, sets...)
end

function Variable(size::Tuple{Int,Int}, set::Symbol, sets::Symbol...)
return Variable(size, NoSign(), set, sets...)
end

function Variable(m::Int, set::Symbol, sets::Symbol...)
return Variable((m, 1), NoSign(), set, sets...)
end

function Variable(set::Symbol, sets::Symbol...)
return Variable((1, 1), NoSign(), set, sets...)
end

function ComplexVariable(size::Tuple{Int,Int}, set::Symbol, sets::Symbol...)
@warn(
"Using symbols in `ComplexVariable` constructor is deprecated. Use " *
"`isposdef(x)` to enforce semidefiniteness instead of `:Semidefinite.",
maxlog = 1,
)
sets = [set, sets...]
if :Bin in sets
throw(ArgumentError("Complex variables cannot be restricted to binary"))
elseif :Int in sets
throw(
ArgumentError("Complex variables cannot be restricted to integer"),
)
end
x = ComplexVariable(size)
if :Semidefinite in sets
add_constraint!(x, x ⪰ 0)
end
return x
end

function ComplexVariable(set::Symbol, sets::Symbol...)
return ComplexVariable((1, 1), set, sets...)
end
12 changes: 6 additions & 6 deletions src/problem_depot/problems/mip.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ end
@test p.optval ≈ 5 atol = atol rtol = rtol
end

x = Variable(2, :Int)
x = Variable(2, IntVar)
p = minimize(sum(x), x >= 4.3; numeric_type = T)

if test
Expand All @@ -69,7 +69,7 @@ end
@test p.optval ≈ 12 atol = atol rtol = rtol
end

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

if test
Expand All @@ -80,7 +80,7 @@ end
@test p.optval ≈ 5 atol = atol rtol = rtol
end

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

if test
Expand All @@ -91,7 +91,7 @@ end
@test p.optval ≈ 5 atol = atol rtol = rtol
end

x = Variable(2, :Int)
x = Variable(2, IntVar)
p = minimize(sum(x), x >= 0.5; numeric_type = T)

if test
Expand All @@ -110,7 +110,7 @@ end
rtol,
::Type{T},
) where {T,test}
x = Variable(2, :Bin)
x = Variable(2, BinVar)
p = minimize(sum(x), x >= 0.5; numeric_type = T)

if test
Expand All @@ -121,7 +121,7 @@ end
@test p.optval ≈ 2 atol = atol rtol = rtol
end

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

if test
Expand Down
8 changes: 4 additions & 4 deletions src/problem_depot/problems/sdp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
rtol,
::Type{T},
) where {T,test}
y = Variable((2, 2), :Semidefinite)
y = Semidefinite((2, 2))
p = minimize(y[1, 1]; numeric_type = T)

# @fact problem_vexity(p) --> ConvexVexity()
Expand All @@ -15,7 +15,7 @@
@test p.optval ≈ 0 atol = atol rtol = rtol
end

y = Variable((3, 3), :Semidefinite)
y = Semidefinite((3, 3))
p = minimize(y[1, 1], y[2, 2] == 1; numeric_type = T)

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

# # @fact problem_vexity(p) --> ConvexVexity()
Expand All @@ -43,7 +43,7 @@
@test p.optval ≈ 1 atol = atol rtol = rtol
end

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

# @fact problem_vexity(p) --> ConvexVexity()
Expand Down
Loading
Loading