Skip to content

Commit

Permalink
update to work for current version of JuMP
Browse files Browse the repository at this point in the history
  • Loading branch information
yeesian committed Mar 23, 2017
1 parent e161a36 commit 35545ff
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
JuMP 0.14 0.15
JuMP 0.15 0.17
2 changes: 1 addition & 1 deletion src/uncsets_basic_cut.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function get_worst_case_value(us::BasicUncertaintySet, rm::Model, idx::Int)
con = rmext.unc_constraints[idx]
# Update the cutting plane problem's objective, and solve
cut_sense, unc_obj_coeffs, lhs_const = JuMPeR.build_cut_objective_sparse(rm, con)
@objective(us.cut_model, cut_sense, sum{u[2]*us.cut_vars[u[1]], u=unc_obj_coeffs})
@objective(us.cut_model, cut_sense, sum(u[2]*us.cut_vars[u[1]] for u=unc_obj_coeffs))
cut_solve_status = solve(us.cut_model, suppress_warnings=true)
cut_solve_status != :Optimal &&
error("BasicUncertaintySet: cutting plane problem is infeasible or unbounded!")
Expand Down
16 changes: 8 additions & 8 deletions test/adp_inventory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# "Adjustable Robust Solutions of Uncertain Linear Programs"
#-----------------------------------------------------------------------

using JuMP, JuMPeR
using JuMP, JuMPeR, GLPKMathProgInterface
using BaseTestNext

const TOL = 1e-4
Expand Down Expand Up @@ -51,19 +51,19 @@ print_with_color(:yellow, "Adaptive Inventory Model...\n")
@constraint(rm, p[i,t] <= P)
end
for i in 1:I
@constraint(rm, sum{p[i,t], t=1:T} <= Q)
@constraint(rm, sum(p[i,t] for t=1:T) <= Q)
end
# Constraint: cannot exceed inventory limits
for t in 1:T
@constraint(rm,
v1 + sum{p[i,s], i=1:I, s=1:t} - sum{d[s],s=1:t} >= Vmin)
v1 + sum(p[i,s] for i=1:I, s=1:t) - sum(d[s] for s=1:t) >= Vmin)
@constraint(rm,
v1 + sum{p[i,s], i=1:I, s=1:t} - sum{d[s],s=1:t} <= Vmax)
v1 + sum(p[i,s] for i=1:I, s=1:t) - sum(d[s] for s=1:t) <= Vmax)
end
end

@testset "Affine, manual" begin
rm = RobustModel()
rm = RobustModel(solver=GLPKSolverLP())
# Uncertain parameter: demand at each time stage
@uncertain(rm, d_nom[t]*(1-θ) <= d[t=1:T] <= d_nom[t]*(1+θ))
# Decision: how much to produce at each factory at each time
Expand All @@ -76,21 +76,21 @@ print_with_color(:yellow, "Adaptive Inventory Model...\n")
end
# Objective: total cost of production
@variable(rm, F); @objective(rm, Min, F)
@constraint(rm, sum{c[i,t] * p[i,t], i=1:I, t=1:T} <= F)
@constraint(rm, sum(c[i,t] * p[i,t] for i=1:I, t=1:T) <= F)
add_constraints(rm, p, d)
solve(rm)
@test isapprox(getvalue(F), 44272.82749, atol=TOL)
end

@testset "Affine, auto" begin
rm = RobustModel()
rm = RobustModel(solver=GLPKSolverLP())
# Uncertain parameter: demand at each time stage
@uncertain(rm, d_nom[t]*(1-θ) <= d[t=1:T] <= d_nom[t]*(1+θ))
# Decision: how much to produce at each factory at each time
@adaptive(rm, p[i=1:I,t=1:T], policy=Affine, depends_on=d[1:t-1])
# Objective: total cost of production
@variable(rm, F); @objective(rm, Min, F)
@constraint(rm, sum{c[i,t] * p[i,t], i=1:I, t=1:T} <= F)
@constraint(rm, sum(c[i,t] * p[i,t] for i=1:I, t=1:T) <= F)
add_constraints(rm, p, d)
solve(rm)
@test isapprox(getvalue(F), 44272.82749, atol=TOL)
Expand Down
10 changes: 5 additions & 5 deletions test/adp_newsvendor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# D ∈ { ‖(D - μ)/σ‖₁ ≤ ⌊√N⌋, ‖(D - μ)/σ‖∞ ≤ 1 }
#-----------------------------------------------------------------------

using JuMP, JuMPeR
using JuMP, JuMPeR, GLPKMathProgInterface
using BaseTestNext

const TOL = 1e-4
Expand Down Expand Up @@ -56,7 +56,7 @@ print_with_color(:yellow, "Adaptive Newsvendor Model...\n")
end

@testset "Static, manual" begin
m = RobustModel()
m = RobustModel(solver=GLPKSolverLP())
D = add_set(m)
@variable(m, x >= 0)
@variable(m, S[1:N] >= 0)
Expand All @@ -70,7 +70,7 @@ print_with_color(:yellow, "Adaptive Newsvendor Model...\n")
end # "Static, manual"

@testset "Static, auto" begin
m = RobustModel()
m = RobustModel(solver=GLPKSolverLP())
D = add_set(m)
@variable(m, x >= 0)
@adaptive(m, S[1:N] >= 0, policy=Static, depends_on=D)
Expand All @@ -84,7 +84,7 @@ print_with_color(:yellow, "Adaptive Newsvendor Model...\n")
end # "Static, auto"

@testset "Affine, manual" begin
m = RobustModel()
m = RobustModel(solver=GLPKSolverLP())
D = add_set(m)
@variable(m, x >= 0)
@variable(m, S_aff[1:N,0:N])
Expand All @@ -105,7 +105,7 @@ print_with_color(:yellow, "Adaptive Newsvendor Model...\n")
end

@testset "Affine, auto" begin
m = RobustModel()
m = RobustModel(solver=GLPKSolverLP())
D = add_set(m)
@variable(m, x >= 0)
@adaptive(m, S[1:N] >= 0, policy=Affine, depends_on=D)
Expand Down
8 changes: 4 additions & 4 deletions test/macro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ end
@constraint(rm, 0 <= u - 5)
@test lastus(rm) == "-u $le -5"

@constraint(rm, u == sum{i*v[i], i=1:3})
@constraint(rm, u == sum(i*v[i] for i=1:3))
@test lastus(rm) == "u - v[1] - 2 v[2] - 3 v[3] $eq 0"
@constraint(rm, sum{i*v[i], i=1:3} + u >= 10)
@constraint(rm, sum(i*v[i] for i=1:3) + u >= 10)
@test lastus(rm) == "v[1] + 2 v[2] + 3 v[3] + u $ge 10"
end

Expand Down Expand Up @@ -82,10 +82,10 @@ end
@constraint(rm, (u+w)*x + 2 + w*x <= u*z + 3)
@test lastuc(rm) == "u x + w x + w x + -u z $le 1"

@constraint(rm, sum{v[i]*y[i], i=1:5; i!=3} <= 9)
@constraint(rm, sum(v[i]*y[i] for i=1:5 if i!=3) <= 9)
@test lastuc(rm) == "v[1] y[1] + v[2] y[2] + v[4] y[4] + v[5] y[5] $le 9"

@constraint(rm, sum{i*(u+v[i])*(y[i]+x), i=1:2:5} <= 0)
@constraint(rm, sum(i*(u+v[i])*(y[i]+x) for i=1:2:5) <= 0)
@test lastuc(rm) == "(u + v[1]) y[1] + (u + v[1]) x + (3 u + 3 v[3]) y[3] + (3 u + 3 v[3]) x + (5 u + 5 v[5]) y[5] + (5 u + 5 v[5]) x $le 0"

foo = u*x
Expand Down
68 changes: 34 additions & 34 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -522,60 +522,60 @@ rm = RobustModel()
us = JuMPeR.get_robust(rm).default_uncset
nc = us.norm_constraints
@uncertain(rm, u[1:3])
@constraint(us, norm1{u[i],i=1:3} <= 1)
@constraint(us, norm((u[i] for i=1:3),1) <= 1)
@test string(nc[end]) == "‖u[1],u[2],u[3]‖₁ $leq 1"
@constraint(us, norm2{u[i],i=1:3} <= 2)
@constraint(us, norm(u[i] for i=1:3) <= 2)
@test string(nc[end]) == "‖u[1],u[2],u[3]‖₂ $leq 2"
@constraint(us, norm∞{u[i],i=1:3} <= 9)
@constraint(us, norm((u[i] for i=1:3),Inf) <= 9)
@test string(nc[end]) == "‖u[1],u[2],u[3]‖∞ $leq 9"

@constraint(us, 2*norm1{u[i],i=1:3} <= 1)
@constraint(us, 2*norm((u[i] for i=1:3),1) <= 1)
@test string(nc[end]) == "2‖u[1],u[2],u[3]‖₁ $leq 1"
@constraint(us, -1*norm1{u[i],i=1:3} >= -1)
@constraint(us, -1*norm((u[i] for i=1:3),1) >= -1)
@test string(nc[end]) == "‖u[1],u[2],u[3]‖₁ $leq 1"

@constraint(us, 1 + norm1{u[i],i=1:3} <= 1)
@constraint(us, 1 + norm((u[i] for i=1:3),1) <= 1)
@test string(nc[end]) == "‖u[1],u[2],u[3]‖₁ $leq 0"

@variable(rm, x)

@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} + u[1] <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} - u[1] <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} * u[1] <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} / u[1] <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) + u[1] <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) - u[1] <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) * u[1] <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) / u[1] <= 1)

@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} + (2*u[1]) <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} - (2*u[1]) <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} * (2*u[1]) <= 1)
@test_throws MethodError @constraint(us, norm1{u[i],i=1:3} / (2*u[1]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) + (2*u[1]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) - (2*u[1]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) * (2*u[1]) <= 1)
@test_throws MethodError @constraint(us, norm((u[i] for i=1:3),1) / (2*u[1]) <= 1)
# MethodError: `/` has no method matching /(::Int64, ::JuMP.GenericAffExpr{Float64,JuMPeR.Uncertain})

@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} + (2*u[1]*x+u[2]) <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} - (2*u[1]*x+u[2]) <= 1)
@test_throws ErrorException @constraint(us, norm1{u[i],i=1:3} * (2*u[1]*x+u[2]) <= 1)
@test_throws MethodError @constraint(us, norm1{u[i],i=1:3} / (2*u[1]*x+u[2]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) + (2*u[1]*x+u[2]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) - (2*u[1]*x+u[2]) <= 1)
@test_throws ErrorException @constraint(us, norm((u[i] for i=1:3),1) * (2*u[1]*x+u[2]) <= 1)
@test_throws MethodError @constraint(us, norm((u[i] for i=1:3),1) / (2*u[1]*x+u[2]) <= 1)

@test_throws ErrorException @constraint(us, x + norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, x - norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2x) + norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2x) - norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, x + norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, x - norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2x) + norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2x) - norm((u[i] for i=1:3),1) <= 1)

@test_throws ErrorException @constraint(us, (u[1]) + norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (u[1]) - norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (u[1]) * norm1{u[i],i=1:3} <= 1)
# @constraint(us, (u[1]) / norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (u[1]) + norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (u[1]) - norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (u[1]) * norm((u[i] for i=1:3),1) <= 1)
# @constraint(us, (u[1]) / norm((u[i] for i=1:3),1) <= 1)
# UndefVarError: i not defined

@test_throws ErrorException @constraint(us, (2*u[1]) + norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]) - norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]) * norm1{u[i],i=1:3} <= 1)
# @constraint(us, (2*u[1]) / norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]) + norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]) - norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]) * norm((u[i] for i=1:3),1) <= 1)
# @constraint(us, (2*u[1]) / norm((u[i] for i=1:3),1) <= 1)
# UndefVarError: i not defined

@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) + norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) - norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) * norm1{u[i],i=1:3} <= 1)
# @constraint(us, (2*u[1]*x+u[2]) / norm1{u[i],i=1:3} <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) + norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) - norm((u[i] for i=1:3),1) <= 1)
@test_throws ErrorException @constraint(us, (2*u[1]*x+u[2]) * norm((u[i] for i=1:3),1) <= 1)
# @constraint(us, (2*u[1]*x+u[2]) / norm((u[i] for i=1:3),1) <= 1)
# UndefVarError: i not defined

end # "Unc. set norms"
Expand Down
12 changes: 6 additions & 6 deletions test/uncsets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Test UncertainySet interface & tools.
#-----------------------------------------------------------------------

using JuMP, JuMPeR
using JuMP, JuMPeR, GLPKMathProgInterface
using BaseTestNext

if !(:lp_solvers in names(Main))
Expand All @@ -25,18 +25,18 @@ print_with_color(:yellow, "UncertainySet...\n")

@testset "Check interface throws" begin
eval(:(type IncompleteSet <: JuMPeR.AbstractUncertaintySet end)) # In global scope
@test_throws ErrorException JuMPeR.setup_set(IncompleteSet(), RobustModel(), Int[], false, nothing)
@test_throws ErrorException JuMPeR.generate_reform(IncompleteSet(), RobustModel(), Int[])
@test_throws ErrorException JuMPeR.generate_cut(IncompleteSet(), RobustModel(), Int[])
@test_throws ErrorException JuMPeR.generate_scenario(IncompleteSet(), RobustModel(), Int[])
@test_throws ErrorException JuMPeR.setup_set(IncompleteSet(), RobustModel(solver=GLPKSolverLP()), Int[], false, nothing)
@test_throws ErrorException JuMPeR.generate_reform(IncompleteSet(), RobustModel(solver=GLPKSolverLP()), Int[])
@test_throws ErrorException JuMPeR.generate_cut(IncompleteSet(), RobustModel(solver=GLPKSolverLP()), Int[])
@test_throws ErrorException JuMPeR.generate_scenario(IncompleteSet(), RobustModel(solver=GLPKSolverLP()), Int[])
end

# build_cut_objective
# build_cut_objective_sparse
# build_certain_constraint
# is_constraint_violated
@testset "Utilities" begin
rm = RobustModel()
rm = RobustModel(solver=GLPKSolverLP())
@variable(rm, x[1:4] >= 0)
@uncertain(rm, u[1:5])
@constraint(rm, (3*u[1] + 2.0) * x[1] + # u1: 3*x1 = 3*2 = 6, c: 2*x1 = 2*2 =4
Expand Down
10 changes: 5 additions & 5 deletions test/uncsets_basic_L1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ print_with_color(:yellow, "BasicUncertaintySet L1 norm...\n")
m = RobustModel(solver=solver)
@variable(m, 0 <= x[i=1:5] <= 2*i)
@uncertain(m, 0 <= u[i=1:5] <= i+4)
@objective(m, Max, sum{(6-i)*x[i], i=1:5})
!flip && @constraint(m, sum{u[i]*x[i], i=1:5} <= 100)
flip && @constraint(m, -sum{u[i]*x[i], i=1:5} >= -100)
@objective(m, Max, sum((6-i)*x[i] for i=1:5))
!flip && @constraint(m, sum(u[i]*x[i] for i=1:5) <= 100)
flip && @constraint(m, -sum(u[i]*x[i] for i=1:5) >= -100)
a = Float64[3, 0, 0, 2, 1];
c = Float64[5, 0, 0, 5, 5]
I = [1, 5, 4]
z = convert(Vector{JuMPeR.UncExpr}, a.*u-c)
!macr && @constraint(m, norm(z, 1) <= 1)
macr && @constraint(m, norm1{a[i]*u[i]-c[i],i=I} <= 1)
macr && @constraint(m, norm((a[i]*u[i]-c[i] for i=I),1) <= 1)
@test solve(m, suppress_warnings=true, prefer_cuts=cuts) == :Optimal
# u = [5, 6, 7, 5, 6] (2,3 are unrestricted)
# x = [2, 4, 6, 8, ?]
Expand All @@ -78,7 +78,7 @@ print_with_color(:yellow, "BasicUncertaintySet L1 norm...\n")
@constraint(m, u[1] == 5.0*z[1] + 10.0)
@constraint(m, u[2] == 3.0*z[1] - 2.0*z[2] + 3.0)
!macr && @constraint(m, norm(z,1) <= 1)
macr && @constraint(m, norm1{z[i],i=1:2} <= 1)
macr && @constraint(m, norm((z[i] for i=1:2),1) <= 1)
@test solve(m, suppress_warnings=true, prefer_cuts=cuts) == :Optimal
@test isapprox(getvalue(x[1]), 1.0, atol=TOL)
@test isapprox(getvalue(x[2]), 0.0, atol=TOL)
Expand Down
8 changes: 4 additions & 4 deletions test/uncsets_basic_L2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ print_with_color(:yellow, "BasicUncertaintySet L2 norm...\n")
m = RobustModel(solver=solver)
@variable(m, 0 <= x[i=1:5] <= 2*i)
@uncertain(m, 0 <= u[i=1:5] <= i+4)
@objective(m, Max, sum{(6-i)*x[i], i=1:5})
!flip && @constraint(m, sum{u[i]*x[i], i=1:5} <= 100)
flip && @constraint(m, -sum{u[i]*x[i], i=1:5} >= -100)
@objective(m, Max, sum((6-i)*x[i] for i=1:5))
!flip && @constraint(m, sum(u[i]*x[i] for i=1:5) <= 100)
flip && @constraint(m, -sum(u[i]*x[i] for i=1:5) >= -100)
a = [3, 0, 0, 2, 1];
I = [1, 5, 4]
@constraint(m, norm2{a[i]*u[i]-5,i=I} <= 1)
@constraint(m, norm(a[i]*u[i]-5 for i=I) <= 1)
@test solve(m, suppress_warnings=true, prefer_cuts=cuts) == :Optimal
@test isapprox(getvalue(x[1]), 2.0, atol=TOL)
@test isapprox(getvalue(x[2]), 4.0, atol=TOL)
Expand Down
10 changes: 5 additions & 5 deletions test/uncsets_basic_Linf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ print_with_color(:yellow, "BasicUncertaintySet L∞ norm...\n")
m = RobustModel(solver=solver)
@variable(m, 0 <= x[i=1:5] <= 2*i)
@uncertain(m, 0 <= u[i=1:5] <= i+4)
@objective(m, Max, sum{(6-i)*x[i], i=1:5})
!flip && @constraint(m, sum{u[i]*x[i], i=1:5} <= 100)
flip && @constraint(m, -sum{u[i]*x[i], i=1:5} >= -100)
@objective(m, Max, sum((6-i)*x[i] for i=1:5))
!flip && @constraint(m, sum(u[i]*x[i] for i=1:5) <= 100)
flip && @constraint(m, -sum(u[i]*x[i] for i=1:5) >= -100)
a = Float64[2, 0, 0, 2, 2];
c = Float64[5, 0, 0, 5, 5]
I = [1, 4, 5]
z = convert(Vector{JuMPeR.UncExpr}, a.*u-c)
!macr && @constraint(m, norm(z, Inf) <= 2)
macr && @constraint(m, norm∞{a[i]*u[i]-c[i],i=I} <= 2)
macr && @constraint(m, norm((a[i]*u[i]-c[i] for i=I),Inf) <= 2)
solve(m, suppress_warnings=true, prefer_cuts=cuts, cut_tol=1e-4)
# max_u = [5.0, 6, 7.0, 8.0, 9.0]
# u = [3.5, 6, 7.0, 3.5, 3.5]
Expand Down Expand Up @@ -82,7 +82,7 @@ print_with_color(:yellow, "BasicUncertaintySet L∞ norm...\n")
@constraint(m, u[1] == 5.0*z[1] + 10.0)
@constraint(m, u[2] == 3.0*z[1] - 2.0*z[2] + 3.0)
!macr && @constraint(m, norm(z,Inf) <= 1)
macr && @constraint(m, norm∞{z[i],i=1:2} <= 1)
macr && @constraint(m, norm((z[i] for i=1:2),Inf) <= 1)
@test solve(m, suppress_warnings=true, prefer_cuts=cuts) == :Optimal
@test isapprox(getvalue(x[1]), 1.0, atol=TOL/10)
@test isapprox(getvalue(x[2]), 0.0, atol=TOL/10)
Expand Down

0 comments on commit 35545ff

Please sign in to comment.