Skip to content

Commit 69ee991

Browse files
committed
[UPD] Update JuMP syntax (issue #82)
1 parent 893ad8d commit 69ee991

File tree

8 files changed

+84
-85
lines changed

8 files changed

+84
-85
lines changed

REQUIRE

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
julia 0.4
2-
MathProgBase
32
Clp
43
Distributions
5-
JuMP
4+
JuMP 0.13
65
FactCheck
76
ProgressMeter
87
Interpolations

examples/benchmark.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,23 @@ function solve_anticipative_problem(model, scenario)
5151
m = Model(solver=SOLVER)
5252

5353

54-
@defVar(m, model.xlim[1][1] <= x1[1:(N_STAGES)] <= model.xlim[1][2])
55-
@defVar(m, model.xlim[2][1] <= x2[1:(N_STAGES)] <= model.xlim[2][2])
56-
@defVar(m, model.ulim[1][1] <= u1[1:N_STAGES-1] <= model.ulim[1][2])
57-
@defVar(m, model.ulim[2][1] <= u2[1:N_STAGES-1] <= model.ulim[2][2])
54+
@variable(m, model.xlim[1][1] <= x1[1:(N_STAGES)] <= model.xlim[1][2])
55+
@variable(m, model.xlim[2][1] <= x2[1:(N_STAGES)] <= model.xlim[2][2])
56+
@variable(m, model.ulim[1][1] <= u1[1:N_STAGES-1] <= model.ulim[1][2])
57+
@variable(m, model.ulim[2][1] <= u2[1:N_STAGES-1] <= model.ulim[2][2])
5858

59-
@setObjective(m, Min, sum{COST[i]*(u1[i] + u2[i]), i = 1:N_STAGES-1})
59+
@objective(m, Min, sum{COST[i]*(u1[i] + u2[i]), i = 1:N_STAGES-1})
6060

6161
for i in 1:N_STAGES-1
62-
@addConstraint(m, x1[i+1] - x1[i] + u1[i] - scenario[i] == 0)
63-
@addConstraint(m, x2[i+1] - x2[i] + u2[i] - u1[i] == 0)
62+
@constraint(m, x1[i+1] - x1[i] + u1[i] - scenario[i] == 0)
63+
@constraint(m, x2[i+1] - x2[i] + u2[i] - u1[i] == 0)
6464
end
6565

66-
@addConstraint(m, x1[1] == model.initialState[1])
67-
@addConstraint(m, x2[1] == model.initialState[2])
66+
@constraint(m, x1[1] == model.initialState[1])
67+
@constraint(m, x2[1] == model.initialState[2])
6868

6969
status = solve(m)
70-
return getObjectiveValue(m)
70+
return getobjectivevalue(m)
7171
end
7272

7373

examples/dam.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ function solve_determinist_problem()
6565
println(alea_year)
6666
m = Model(solver=SOLVER)
6767

68-
@defVar(m, 0 <= x[1:N_STAGES] <= 100)
69-
@defVar(m, 0. <= u[1:N_STAGES-1] <= 7)
70-
@defVar(m, 0. <= s[1:N_STAGES-1] <= 7)
68+
@variable(m, 0 <= x[1:N_STAGES] <= 100)
69+
@variable(m, 0. <= u[1:N_STAGES-1] <= 7)
70+
@variable(m, 0. <= s[1:N_STAGES-1] <= 7)
7171

72-
@setObjective(m, Min, sum{COST[i]*u[i], i = 1:N_STAGES-1})
72+
@objective(m, Min, sum{COST[i]*u[i], i = 1:N_STAGES-1})
7373

7474
for i in 1:(N_STAGES-1)
75-
@addConstraint(m, x[i+1] - x[i] + u[i] + s[i] - alea_year[i] == 0)
75+
@constraint(m, x[i+1] - x[i] + u[i] + s[i] - alea_year[i] == 0)
7676
end
7777

78-
@addConstraint(m, x[1] .==X0)
78+
@constraint(m, x[1] .==X0)
7979

8080
status = solve(m)
8181
println(status)

examples/damsvalley.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,20 @@ function solve_determinist_problem()
6565
m = Model(solver=SOLVER)
6666

6767

68-
@defVar(m, VOLUME_MIN <= x1[1:N_STAGES] <= VOLUME_MAX)
69-
@defVar(m, VOLUME_MIN <= x2[1:N_STAGES] <= VOLUME_MAX)
70-
@defVar(m, CONTROL_MIN <= u1[1:N_STAGES-1] <= CONTROL_MAX)
71-
@defVar(m, CONTROL_MIN <= u2[1:N_STAGES-1] <= CONTROL_MAX)
68+
@variable(m, VOLUME_MIN <= x1[1:N_STAGES] <= VOLUME_MAX)
69+
@variable(m, VOLUME_MIN <= x2[1:N_STAGES] <= VOLUME_MAX)
70+
@variable(m, CONTROL_MIN <= u1[1:N_STAGES-1] <= CONTROL_MAX)
71+
@variable(m, CONTROL_MIN <= u2[1:N_STAGES-1] <= CONTROL_MAX)
7272

73-
@setObjective(m, Min, sum{COST[i]*(u1[i] + u2[i]), i = 1:N_STAGES})
73+
@objective(m, Min, sum{COST[i]*(u1[i] + u2[i]), i = 1:N_STAGES})
7474

7575
for i in 1:N_STAGES-1
76-
@addConstraint(m, x1[i+1] - x1[i] + u1[i] - alea_year[i] == 0)
77-
@addConstraint(m, x2[i+1] - x2[i] + u2[i] - u1[i] == 0)
76+
@constraint(m, x1[i+1] - x1[i] + u1[i] - alea_year[i] == 0)
77+
@constraint(m, x2[i+1] - x2[i] + u2[i] - u1[i] == 0)
7878
end
7979

80-
@addConstraint(m, x1[1] == X0[1])
81-
@addConstraint(m, x2[1] == X0[2])
80+
@constraint(m, x1[1] == X0[1])
81+
@constraint(m, x2[1] == X0[2])
8282

8383
status = solve(m)
8484
println(status)

src/SDDPoptimize.jl

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -200,18 +200,18 @@ Parameter:
200200
"""
201201
function build_terminal_cost!(model::SPModel, problem::JuMP.Model, Vt::PolyhedralFunction)
202202
# if shape is PolyhedralFunction, build terminal cost with it:
203-
alpha = getVar(problem, :alpha)
204-
x = getVar(problem, :x)
205-
u = getVar(problem, :u)
206-
w = getVar(problem, :w)
203+
alpha = getvariable(problem, :alpha)
204+
x = getvariable(problem, :x)
205+
u = getvariable(problem, :u)
206+
w = getvariable(problem, :w)
207207
t = model.stageNumber -1
208208
if isa(Vt, PolyhedralFunction)
209209
for i in 1:Vt.numCuts
210210
lambda = vec(Vt.lambdas[i, :])
211-
@addConstraint(problem, Vt.betas[i] + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
211+
@constraint(problem, Vt.betas[i] + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
212212
end
213213
else
214-
@addConstraint(problem, alpha >= 0)
214+
@constraint(problem, alpha >= 0)
215215
end
216216
end
217217

@@ -247,26 +247,26 @@ function build_models(model::SPModel, param::SDDPparameters)
247247
nu = model.dimControls
248248
nw = model.dimNoises
249249

250-
@defVar(m, model.xlim[i][1] <= x[i=1:nx] <= model.xlim[i][2])
251-
@defVar(m, model.ulim[i][1] <= u[i=1:nu] <= model.ulim[i][2])
252-
@defVar(m, model.xlim[i][1] <= xf[i=1:nx]<= model.xlim[i][2])
253-
@defVar(m, alpha)
250+
@variable(m, model.xlim[i][1] <= x[i=1:nx] <= model.xlim[i][2])
251+
@variable(m, model.ulim[i][1] <= u[i=1:nu] <= model.ulim[i][2])
252+
@variable(m, model.xlim[i][1] <= xf[i=1:nx]<= model.xlim[i][2])
253+
@variable(m, alpha)
254254

255-
@defVar(m, w[1:nw] == 0)
256-
m.ext[:cons] = @addConstraint(m, state_constraint, x .== 0)
255+
@variable(m, w[1:nw] == 0)
256+
m.ext[:cons] = @constraint(m, state_constraint, x .== 0)
257257

258-
@addConstraint(m, xf .== model.dynamics(t, x, u, w))
258+
@constraint(m, xf .== model.dynamics(t, x, u, w))
259259

260260
if typeof(model) == LinearDynamicLinearCostSPmodel
261-
@setObjective(m, Min, model.costFunctions(t, x, u, w) + alpha)
261+
@objective(m, Min, model.costFunctions(t, x, u, w) + alpha)
262262

263263
elseif typeof(model) == PiecewiseLinearCostSPmodel
264-
@defVar(m, cost)
264+
@variable(m, cost)
265265

266266
for i in 1:length(model.costFunctions)
267-
@addConstraint(m, cost >= model.costFunctions[i](t, x, u, w))
267+
@constraint(m, cost >= model.costFunctions[i](t, x, u, w))
268268
end
269-
@setObjective(m, Min, cost + alpha)
269+
@objective(m, Min, cost + alpha)
270270
end
271271

272272
models[t] = m
@@ -398,16 +398,16 @@ function get_bellman_value(model::SPModel, param::SDDPparameters,
398398
t::Int64, Vt::PolyhedralFunction, xt::Vector{Float64})
399399

400400
m = Model(solver=param.solver)
401-
@defVar(m, alpha)
401+
@variable(m, alpha)
402402

403403
for i in 1:Vt.numCuts
404404
lambda = vec(Vt.lambdas[i, :])
405-
@addConstraint(m, Vt.betas[i] + dot(lambda, xt) <= alpha)
405+
@constraint(m, Vt.betas[i] + dot(lambda, xt) <= alpha)
406406
end
407407

408-
@setObjective(m, Min, alpha)
408+
@objective(m, Min, alpha)
409409
solve(m)
410-
return getValue(alpha)
410+
return getvalue(alpha)
411411
end
412412

413413

@@ -484,14 +484,14 @@ Parameters:
484484
485485
"""
486486
function add_cuts_to_model!(model::SPModel, t::Int64, problem::JuMP.Model, V::PolyhedralFunction)
487-
alpha = getVar(problem, :alpha)
488-
x = getVar(problem, :x)
489-
u = getVar(problem, :u)
490-
w = getVar(problem, :w)
487+
alpha = getvariable(problem, :alpha)
488+
x = getvariable(problem, :x)
489+
u = getvariable(problem, :u)
490+
w = getvariable(problem, :w)
491491

492492
for i in 1:V.numCuts
493493
lambda = vec(V.lambdas[i, :])
494-
@addConstraint(problem, V.betas[i] + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
494+
@constraint(problem, V.betas[i] + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
495495
end
496496
end
497497

@@ -557,19 +557,19 @@ Return:
557557
function is_cut_relevant(model::SPModel, k::Int, Vt::PolyhedralFunction, solver)
558558

559559
m = Model(solver=solver)
560-
@defVar(m, alpha)
561-
@defVar(m, model.xlim[i][1] <= x[i=1:model.dimStates] <= model.xlim[i][2])
560+
@variable(m, alpha)
561+
@variable(m, model.xlim[i][1] <= x[i=1:model.dimStates] <= model.xlim[i][2])
562562

563563
for i in 1:Vt.numCuts
564564
if i!=k
565565
lambda = vec(Vt.lambdas[i, :])
566-
@addConstraint(m, Vt.betas[i] + dot(lambda, x) <= alpha)
566+
@constraint(m, Vt.betas[i] + dot(lambda, x) <= alpha)
567567
end
568568
end
569569

570570
λ_k = vec(Vt.lambdas[k, :])
571-
@setObjective(m, Min, alpha - dot(λ_k, x) - Vt.betas[k])
571+
@objective(m, Min, alpha - dot(λ_k, x) - Vt.betas[k])
572572
solve(m)
573-
return getObjectiveValue(m) < 0.
573+
return getobjectivevalue(m) < 0.
574574
end
575575

src/extensiveFormulation.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ function extensive_formulation(model,
3737

3838
#Define the variables for the extensive formulation
3939
#At each node, we have as many variables as nodes
40-
@defVar(mod, u[t=1:T,n=1:DIM_CONTROL*N[t+1]])
41-
@defVar(mod, x[t=1:T+1,n=1:DIM_STATE*N[t]])
42-
@defVar(mod, c[t=1:T,n=1:laws[t].supportSize*N[t]])
40+
@variable(mod, u[t=1:T,n=1:DIM_CONTROL*N[t+1]])
41+
@variable(mod, x[t=1:T+1,n=1:DIM_STATE*N[t]])
42+
@variable(mod, c[t=1:T,n=1:laws[t].supportSize*N[t]])
4343

4444

4545
#Computes the total probability of each node from the conditional probabilities
@@ -57,24 +57,24 @@ function extensive_formulation(model,
5757
#Instantiate the problem creating dynamic constraint at each node
5858
for t = 1 : (T)
5959
for n = 1 : N[t]
60-
@addConstraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .>= [model.xlim[k][1] for k = 1:DIM_STATE])
61-
@addConstraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .<= [model.xlim[k][2] for k = 1:DIM_STATE])
60+
@constraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .>= [model.xlim[k][1] for k = 1:DIM_STATE])
61+
@constraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .<= [model.xlim[k][2] for k = 1:DIM_STATE])
6262
for xi = 1 : laws[t].supportSize
6363
m = (n-1)*laws[t].supportSize+xi
6464

6565
#Add bounds constraint on the control
66-
@addConstraint(mod,[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL] .>= [model.ulim[k][1] for k = 1:DIM_CONTROL])
67-
@addConstraint(mod,[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL] .<= [model.ulim[k][2] for k = 1:DIM_CONTROL])
66+
@constraint(mod,[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL] .>= [model.ulim[k][1] for k = 1:DIM_CONTROL])
67+
@constraint(mod,[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL] .<= [model.ulim[k][2] for k = 1:DIM_CONTROL])
6868

6969
#Add dynamic constraints
70-
@addConstraint(mod,
70+
@constraint(mod,
7171
[x[t+1,DIM_STATE*(m-1)+k] for k = 1:DIM_STATE] .== model.dynamics(t,
7272
[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE],
7373
[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL],
7474
laws[t].support[xi]))
7575

7676
#Add constraints to define the cost at each node
77-
@addConstraint(mod,
77+
@constraint(mod,
7878
c[t,m] == model.costFunctions(t,
7979
[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE],
8080
[u[t,DIM_CONTROL*(m-1)+k] for k = 1:DIM_CONTROL],
@@ -84,18 +84,18 @@ function extensive_formulation(model,
8484
end
8585

8686
#Initial state
87-
@addConstraint(mod, [x[1,k] for k = 1:DIM_STATE] .== X_init)
87+
@constraint(mod, [x[1,k] for k = 1:DIM_STATE] .== X_init)
8888

8989

9090
#Define the objective of the function
91-
@setObjective(mod, Min, sum{ sum{proba[t][laws[t].supportSize*(n-1)+k]*c[t,laws[t].supportSize*(n-1)+k],k = 1:laws[t].supportSize} , t = 1:T, n=1:div(N[t+1],laws[t].supportSize)})
91+
@objective(mod, Min, sum{ sum{proba[t][laws[t].supportSize*(n-1)+k]*c[t,laws[t].supportSize*(n-1)+k],k = 1:laws[t].supportSize} , t = 1:T, n=1:div(N[t+1],laws[t].supportSize)})
9292

9393
status = solve(mod)
9494

9595
solved = (status == :Optimal)
9696

9797
if solved
98-
return getObjectiveValue(mod), status
98+
return getobjectivevalue(mod), status
9999
else
100100
return -1., status
101101
end

src/forwardBackwardIterations.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ subgradient of the cut to add
173173
"""
174174
function add_cut_to_model!(model::SPModel, problem::JuMP.Model,
175175
t::Int64, beta::Float64, lambda::Vector{Float64})
176-
alpha = getVar(problem, :alpha)
177-
x = getVar(problem, :x)
178-
u = getVar(problem, :u)
179-
w = getVar(problem, :w)
180-
@addConstraint(problem, beta + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
176+
alpha = getvariable(problem, :alpha)
177+
x = getvariable(problem, :x)
178+
u = getvariable(problem, :u)
179+
w = getvariable(problem, :w)
180+
@constraint(problem, beta + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
181181
end
182182

183183

src/oneStepOneAleaProblem.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ function solve_one_step_one_alea(model,
5858
xi::Vector{Float64},
5959
init=false::Bool)
6060
# Get var defined in JuMP.model:
61-
u = getVar(m, :u)
62-
w = getVar(m, :w)
63-
alpha = getVar(m, :alpha)
61+
u = getvariable(m, :u)
62+
w = getvariable(m, :w)
63+
alpha = getvariable(m, :alpha)
6464

6565
# Update value of w:
66-
setValue(w, xi)
66+
setvalue(w, xi)
6767

6868
# If this is the first call to the solver, value-to-go are approximated
6969
# with null function:
7070
if init
71-
@addConstraint(m, alpha >= 0)
71+
@constraint(m, alpha >= 0)
7272
end
7373
# Update constraint x == xt
7474
for i in 1:model.dimStates
75-
chgConstrRHS(m.ext[:cons][i], xt[i])
75+
JuMP.setRHS(m.ext[:cons][i], xt[i])
7676
end
7777

7878

@@ -81,14 +81,14 @@ function solve_one_step_one_alea(model,
8181
solved = (status == :Optimal)
8282

8383
if solved
84-
optimalControl = getValue(u)
84+
optimalControl = getvalue(u)
8585
# Return object storing results:
8686
result = NextStep(
8787
model.dynamics(t, xt, optimalControl, xi),
8888
optimalControl,
89-
[getDual(m.ext[:cons][i]) for i in 1:model.dimStates],
90-
getObjectiveValue(m),
91-
getValue(alpha))
89+
[getdual(m.ext[:cons][i]) for i in 1:model.dimStates],
90+
getobjectivevalue(m),
91+
getvalue(alpha))
9292
else
9393
# If no solution is found, then return nothing
9494
result = nothing

0 commit comments

Comments
 (0)