Skip to content

Commit 9c19bd8

Browse files
committed
git push origin dev-release-v0.1.2Merge branch 'update-jump-interface' into dev-release-v0.1.2
2 parents db45e60 + d1c8191 commit 9c19bd8

File tree

10 files changed

+121
-95
lines changed

10 files changed

+121
-95
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
julia 0.4
2+
JuMP 0.13
23
Distributions
3-
JuMP
44
ProgressMeter
55
Interpolations
66
Iterators

examples/benchmark.jl

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

5252

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

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

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

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

6868
status = solve(m)
69-
return getObjectiveValue(m)
69+
return getobjectivevalue(m)
7070
end
7171

7272

examples/dam.jl

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

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

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

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

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

7878
status = solve(m)
7979
println(status)

examples/damsvalley.jl

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

6666

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

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

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

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

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

src/SDDPoptimize.jl

Lines changed: 45 additions & 35 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
@@ -310,10 +310,13 @@ function initialize_value_functions( model::SPModel,
310310
# Build scenarios according to distribution laws:
311311
aleas = simulate_scenarios(model.noises, param.forwardPassNumber)
312312

313-
V[end] = model.finalCost
314-
315313
# Add final costs to solverProblems:
316-
build_terminal_cost!(model, solverProblems[end], V[end])
314+
if isa(model.finalCost, PolyhedralFunction)
315+
V[end] = model.finalCost
316+
build_terminal_cost!(model, solverProblems[end], V[end])
317+
elseif isa(model.finalCost, Function)
318+
model.finalCost(model, solverProblems[end])
319+
end
317320

318321
stockTrajectories = forward_simulations(model,
319322
param,
@@ -353,9 +356,16 @@ function hotstart_SDDP(model::SPModel, param::SDDPparameters, V::Vector{Polyhedr
353356

354357
solverProblems = build_models(model, param)
355358

356-
for t in 1:model.stageNumber-1
359+
for t in 1:model.stageNumber-2
357360
add_cuts_to_model!(model, t, solverProblems[t], V[t+1])
358361
end
362+
363+
# Take care of final cost:
364+
if isa(model.finalCost, PolyhedralFunction)
365+
add_cuts_to_model!(model, model.stageNumber-1, solverProblems[end], V[end])
366+
else
367+
model.finalCost(model, solverProblems[end])
368+
end
359369
return solverProblems
360370
end
361371

@@ -388,16 +398,16 @@ function get_bellman_value(model::SPModel, param::SDDPparameters,
388398
t::Int64, Vt::PolyhedralFunction, xt::Vector{Float64})
389399

390400
m = Model(solver=param.solver)
391-
@defVar(m, alpha)
401+
@variable(m, alpha)
392402

393403
for i in 1:Vt.numCuts
394404
lambda = vec(Vt.lambdas[i, :])
395-
@addConstraint(m, Vt.betas[i] + dot(lambda, xt) <= alpha)
405+
@constraint(m, Vt.betas[i] + dot(lambda, xt) <= alpha)
396406
end
397407

398-
@setObjective(m, Min, alpha)
408+
@objective(m, Min, alpha)
399409
solve(m)
400-
return getValue(alpha)
410+
return getvalue(alpha)
401411
end
402412

403413

@@ -474,14 +484,14 @@ Parameters:
474484
475485
"""
476486
function add_cuts_to_model!(model::SPModel, t::Int64, problem::JuMP.Model, V::PolyhedralFunction)
477-
alpha = getVar(problem, :alpha)
478-
x = getVar(problem, :x)
479-
u = getVar(problem, :u)
480-
w = getVar(problem, :w)
487+
alpha = getvariable(problem, :alpha)
488+
x = getvariable(problem, :x)
489+
u = getvariable(problem, :u)
490+
w = getvariable(problem, :w)
481491

482492
for i in 1:V.numCuts
483493
lambda = vec(V.lambdas[i, :])
484-
@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)
485495
end
486496
end
487497

@@ -547,19 +557,19 @@ Return:
547557
function is_cut_relevant(model::SPModel, k::Int, Vt::PolyhedralFunction, solver)
548558

549559
m = Model(solver=solver)
550-
@defVar(m, alpha)
551-
@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])
552562

553563
for i in 1:Vt.numCuts
554564
if i!=k
555565
lambda = vec(Vt.lambdas[i, :])
556-
@addConstraint(m, Vt.betas[i] + dot(lambda, x) <= alpha)
566+
@constraint(m, Vt.betas[i] + dot(lambda, x) <= alpha)
557567
end
558568
end
559569

560570
λ_k = vec(Vt.lambdas[k, :])
561-
@setObjective(m, Min, alpha - dot(λ_k, x) - Vt.betas[k])
571+
@objective(m, Min, alpha - dot(λ_k, x) - Vt.betas[k])
562572
solve(m)
563-
return getObjectiveValue(m) < 0.
573+
return getobjectivevalue(m) < 0.
564574
end
565575

src/extensiveFormulation.jl

Lines changed: 15 additions & 11 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,19 +84,23 @@ 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+
<<<<<<< HEAD
99+
return getobjectivevalue(mod), status
100+
=======
98101
firstControl = collect(values(getValue(u)))[1:DIM_CONTROL*laws[1].supportSize]
99102
return getObjectiveValue(mod), firstControl, status
103+
>>>>>>> dev-release-v0.1.2
100104
else
101105
error("Extensive formulation not solved to optimality. Change the model")
102106
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/objects.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type LinearDynamicLinearCostSPmodel <: SPModel
3737
dynamics::Function
3838
noises::Vector{NoiseLaw}
3939

40-
finalCost::PolyhedralFunction
40+
finalCost
4141

4242
function LinearDynamicLinearCostSPmodel(nstage, ubounds, x0, cost, dynamic, aleas, Vfinal=nothing)
4343

@@ -47,10 +47,10 @@ type LinearDynamicLinearCostSPmodel <: SPModel
4747

4848
# First step: process terminal costs.
4949
# If not specified, default value is null function
50-
if isa(Vfinal, Void)
51-
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
52-
else
50+
if isa(Vfinal, Function) || isa(Vfinal, PolyhedralFunction)
5351
Vf = Vfinal
52+
else
53+
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
5454
end
5555

5656
xbounds = []
@@ -79,17 +79,17 @@ type PiecewiseLinearCostSPmodel <: SPModel
7979
costFunctions::Vector{Function}
8080
dynamics::Function
8181
noises::Vector{NoiseLaw}
82-
finalCost::PolyhedralFunction
82+
finalCost
8383

8484
function PiecewiseLinearCostSPmodel(nstage, ubounds, x0, costs, dynamic, aleas, Vfinal=nothing)
8585
dimStates = length(x0)
8686
dimControls = length(ubounds)
8787
dimNoises = length(aleas[1].support[:, 1])
8888

89-
if isa(Vfinal, Void)
90-
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
91-
else
89+
if isa(Vfinal, Function) || isa(Vfinal, PolyhedralFunction)
9290
Vf = Vfinal
91+
else
92+
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
9393
end
9494

9595
xbounds = []

0 commit comments

Comments
 (0)