Skip to content

Commit 893ad8d

Browse files
committed
[DEV] Add definition of final cost with JuMP.Model
1 parent aa730cf commit 893ad8d

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

src/SDDPoptimize.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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 = []

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ facts("SDDP algorithm: 1D case") do
180180
@fact isactive2 --> false
181181
end
182182

183+
# Test definition of final cost with a JuMP.Model:
184+
context("Final cost") do
185+
function fcost(model, m)
186+
alpha = getVar(m, :alpha)
187+
@addConstraint(m, alpha == 0.)
188+
end
189+
# Store final cost in model:
190+
model.finalCost = fcost
191+
V, pbs = solve_SDDP(model, params, 0)
192+
V, pbs = solve_SDDP(model, params, 0, V)
193+
end
194+
183195
context("Piecewise linear cost") do
184196
# Test Piecewise linear costs:
185197
model = StochDynamicProgramming.PiecewiseLinearCostSPmodel(n_stages,

0 commit comments

Comments
 (0)