Skip to content

Commit

Permalink
Update for current version of JuMP (#74)
Browse files Browse the repository at this point in the history
Update for JuMP v0.6
  • Loading branch information
yeesian authored Oct 20, 2017
1 parent e161a36 commit 090ce1d
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 173 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ It is embedded in the [Julia programming language], and is an extension to the [
- The source code is well-commented, if you'd like to understand more.
- [Checkout the examples](example/).

#### JuMPeR v0.4

JuMPeR was recently updated to version v0.4. This changed the syntax to match the changes in JuMP v0.13. Unlike JuMP v0.13, there are no deprecation warnings. However, it should generally be a simple find-and-replace, e.g. `@defUnc` to `@uncertain`. See the documentation for examples.

[Julia programming language]: http://julialang.org/
[JuMP]: https://github.com/JuliaOpt/JuMP.jl
[JuMPeR]: https://github.com/IainNZ/JuMPeR.jl
Expand Down
3 changes: 2 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
julia 0.5
JuMP 0.14 0.15
JuMP 0.16.2 0.17
MathProgBase 0.6.4 0.7
2 changes: 1 addition & 1 deletion src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function _solve_robust(rm::Model, suppress_warnings::Bool,
# 2. Setup for uncertainty sets
#-------------------------------------------------------------------
# Prepare to pass through preferences to a sets
prefs = Dict{Symbol,Any}([name => value for (name,value) in kwargs])
prefs = Dict{Symbol,Any}(name => value for (name,value) in kwargs)
# Build mapping from uncertainty sets to constraints
uncsets_to_con_idxs = Dict{AbstractUncertaintySet,Vector{Int}}()
for idx in 1:length(rmext.constraint_uncsets)
Expand Down
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
1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
BaseTestNext
GLPKMathProgInterface
GLPK 0.4 0.4.2
ECOS
16 changes: 8 additions & 8 deletions test/adp_inventory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using JuMP, JuMPeR
using BaseTestNext

const TOL = 1e-4
const TOL = 5e-3

if !(:lp_solvers in names(Main))
print_with_color(:magenta, "Loading solvers...\n")
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=solver)
# 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=solver)
# 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
8 changes: 4 additions & 4 deletions test/adp_newsvendor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ print_with_color(:yellow, "Adaptive Newsvendor Model...\n")
end

@testset "Static, manual" begin
m = RobustModel()
m = RobustModel(solver=solver)
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=solver)
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=solver)
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=solver)
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
64 changes: 29 additions & 35 deletions test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@

using JuMP, JuMPeR
using BaseTestNext
import JuMP: REPLMode, IJuliaMode
import JuMP: repl, ijulia
import JuMP: repl

# Helper function to test IO methods work correctly
function io_test(mode, obj, exp_str; repl=:both)
if mode == REPLMode
repl != :show && @test sprint(print, obj) == exp_str
repl != :print && @test sprint(show, obj) == exp_str
else
@test sprint(writemime, "text/latex", obj) == "\$\$ $exp_str \$\$"
end
function io_test(obj, exp_str; repl=:both)
@test sprint(print, obj) == exp_str
end

@testset "Printing" begin
Expand Down Expand Up @@ -65,27 +59,27 @@ print_with_color(:yellow, "Printing...\n")
# @constraint(mod_1, norm([a,b]) <= 1)
# ‖a,b‖₂ $le 1

io_test(REPLMode, mod_1, """
io_test(mod_1, """
Max 2 vars[1]
Subject to
vars[10] $le 10
vars[i] free $fa i $inset {1,2,$dots,9,10}
vars[10] 10
vars[i] ∀ i ∈ {1,2,,9,10}
Uncertain constraints:
a vars[5] $le 5
a vars[5] 5
Uncertain parameters:
bnd_free[i] free $fa i $inset {2,3,4,5}
bnd_lowb[i] $ge 2 $fa i $inset {2,3,4,5}
bnd_high[i] $le 5 $fa i $inset {2,3,4,5}
2 $le bnd_both[i] $le 5 $fa i $inset {2,3,4,5}
mat2d[i,j] free $fa i $inset {1,2,3}, j $inset {1,2,3}
mat3d[i,j,k] free $fa i $inset {1,2,3}, j $inset {1,2,3}, k $inset {1,2,3}
a $ge 1
b $le 1
-1 $le c $le 1
a1 $ge 1, integer
b1 $le 1, integer
-1 $le c1 $le 1, integer
x $inset {0,1}
bnd_free[i] free i {2,3,4,5}
bnd_lowb[i] 2 i {2,3,4,5}
bnd_high[i] 5 i {2,3,4,5}
2 bnd_both[i] 5 i {2,3,4,5}
mat2d[i,j] free i {1,2,3}, j {1,2,3}
mat3d[i,j,k] free i {1,2,3}, j {1,2,3}, k {1,2,3}
a 1
b 1
-1 c 1
a1 1, integer
b1 1, integer
-1 c1 1, integer
x {0,1}
y free
z free, integer
""", repl=:print)
Expand All @@ -107,15 +101,15 @@ end # "RobustModel"
@uncertain(m, i <= bnd_difflo_with_up[i=2:5] <= 5)
@uncertain(m, 2 <= bnd_diffup_with_lo[i=2:5] <= i)

io_test(REPLMode, bnd_free, "bnd_free[i] free $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_lowb, "bnd_lowb[i] $ge 2 $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_high, "bnd_high[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_both, "2 $le bnd_both[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_difflo, "bnd_difflo[i] $ge $dots $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_diffup, "bnd_diffup[i] $le $dots $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_diffbo, "$dots $le bnd_diffbo[i] $le $dots $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_difflo_with_up, "$dots $le bnd_difflo_with_up[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(REPLMode, bnd_diffup_with_lo, "2 $le bnd_diffup_with_lo[i] $le $dots $fa i $inset {2,3,4,5}")
io_test(bnd_free, "bnd_free[i] free $fa i $inset {2,3,4,5}")
io_test(bnd_lowb, "bnd_lowb[i] $ge 2 $fa i $inset {2,3,4,5}")
io_test(bnd_high, "bnd_high[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(bnd_both, "2 $le bnd_both[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(bnd_difflo, "bnd_difflo[i] $ge $dots $fa i $inset {2,3,4,5}")
io_test(bnd_diffup, "bnd_diffup[i] $le $dots $fa i $inset {2,3,4,5}")
io_test(bnd_diffbo, "$dots $le bnd_diffbo[i] $le $dots $fa i $inset {2,3,4,5}")
io_test(bnd_difflo_with_up, "$dots $le bnd_difflo_with_up[i] $le 5 $fa i $inset {2,3,4,5}")
io_test(bnd_diffup_with_lo, "2 $le bnd_diffup_with_lo[i] $le $dots $fa i $inset {2,3,4,5}")
end # "JuMPContainer{Uncertain}"

end # "Printing"
Loading

0 comments on commit 090ce1d

Please sign in to comment.