Skip to content

Commit aa730cf

Browse files
committed
[DEV] Define final cost in SPModel
Final cost is now defined in SPMOdel as a PolyhedralFunction
1 parent 77d7b06 commit aa730cf

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

src/SDDPoptimize.jl

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,18 @@ Returns :
4040
function solve_SDDP(model::SPModel,
4141
param::SDDPparameters,
4242
display=0::Int64,
43-
V=nothing,
44-
V_final=nothing)
43+
V=nothing)
4544

46-
# First step: process terminal costs.
47-
# If not specified, default value is null functions
48-
if isa(V_final, Void)
49-
Vf = PolyhedralFunction(zeros(1), zeros(1, model.dimStates), 1)
50-
else
51-
Vf = V_final
52-
end
53-
54-
# Second step: process value functions if hotstart is called
45+
# First step: process value functions if hotstart is called
5546
if isa(V, Vector{PolyhedralFunction})
5647
# If V is already specified, then call hotstart:
5748
problems = hotstart_SDDP(model, param, V)
5849
else
5950
# Otherwise, initialize value functions:
60-
V, problems = initialize_value_functions(model, param, Vf)
51+
V, problems = initialize_value_functions(model, param)
6152
end
6253

54+
# Run SDDP upon example:
6355
run_SDDP!(model, param, V, problems, display)
6456
return V, problems
6557
end
@@ -207,8 +199,6 @@ Parameter:
207199
208200
"""
209201
function build_terminal_cost!(model::SPModel, problem::JuMP.Model, Vt::PolyhedralFunction)
210-
alpha = getVar(problem, :alpha)
211-
212202
# if shape is PolyhedralFunction, build terminal cost with it:
213203
alpha = getVar(problem, :alpha)
214204
x = getVar(problem, :x)
@@ -307,12 +297,9 @@ Return:
307297
the initialization of linear problems used to approximate
308298
each value function
309299
310-
- V_final (PolydrehalFunction)
311-
Terminal cost to penalize final state.
312-
313300
"""
314301
function initialize_value_functions( model::SPModel,
315-
param::SDDPparameters, V_final::PolyhedralFunction)
302+
param::SDDPparameters)
316303

317304
solverProblems = build_models(model, param)
318305
solverProblems_null = build_models(model, param)
@@ -323,7 +310,10 @@ function initialize_value_functions( model::SPModel,
323310
# Build scenarios according to distribution laws:
324311
aleas = simulate_scenarios(model.noises, param.forwardPassNumber)
325312

326-
V[end] = V_final
313+
V[end] = model.finalCost
314+
315+
# Add final costs to solverProblems:
316+
build_terminal_cost!(model, solverProblems[end], V[end])
327317

328318
stockTrajectories = forward_simulations(model,
329319
param,
@@ -332,8 +322,6 @@ function initialize_value_functions( model::SPModel,
332322
aleas,
333323
false, true, false)[2]
334324

335-
build_terminal_cost!(model, solverProblems[end], V[end])
336-
337325
backward_pass!(model,
338326
param,
339327
V,

src/forwardBackwardIterations.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ function forward_simulations(model::SPModel,
111111

112112
if returnCosts
113113
costs[k] += nextstep.cost - nextstep.cost_to_go
114+
if t==T-1
115+
costs[k] += nextstep.cost_to_go
116+
end
114117
end
115118
end
116119
end

src/objects.jl

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ include("noises.jl")
1111
abstract SPModel
1212

1313

14+
type PolyhedralFunction
15+
#function defined by max_k betas[k] + lambdas[k,:]*x
16+
betas::Vector{Float64}
17+
lambdas::Array{Float64,2} #lambdas[k,:] is the subgradient
18+
# number of cuts:
19+
numCuts::Int64
20+
end
21+
22+
1423
type LinearDynamicLinearCostSPmodel <: SPModel
1524
# problem dimension
1625
stageNumber::Int64
@@ -28,18 +37,28 @@ type LinearDynamicLinearCostSPmodel <: SPModel
2837
dynamics::Function
2938
noises::Vector{NoiseLaw}
3039

31-
function LinearDynamicLinearCostSPmodel(nstage, ubounds, x0, cost, dynamic, aleas)
40+
finalCost::PolyhedralFunction
41+
42+
function LinearDynamicLinearCostSPmodel(nstage, ubounds, x0, cost, dynamic, aleas, Vfinal=nothing)
3243

3344
dimStates = length(x0)
3445
dimControls = length(ubounds)
3546
dimNoises = length(aleas[1].support[:, 1])
3647

48+
# First step: process terminal costs.
49+
# If not specified, default value is null function
50+
if isa(Vfinal, Void)
51+
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
52+
else
53+
Vf = Vfinal
54+
end
55+
3756
xbounds = []
3857
for i = 1:dimStates
3958
push!(xbounds, (-Inf, Inf))
4059
end
4160

42-
return new(nstage, dimControls, dimStates, dimNoises, xbounds, ubounds, x0, cost, dynamic, aleas)
61+
return new(nstage, dimControls, dimStates, dimNoises, xbounds, ubounds, x0, cost, dynamic, aleas, Vf)
4362
end
4463
end
4564

@@ -60,17 +79,24 @@ type PiecewiseLinearCostSPmodel <: SPModel
6079
costFunctions::Vector{Function}
6180
dynamics::Function
6281
noises::Vector{NoiseLaw}
82+
finalCost::PolyhedralFunction
6383

64-
function PiecewiseLinearCostSPmodel(nstage, ubounds, x0, costs, dynamic, aleas)
84+
function PiecewiseLinearCostSPmodel(nstage, ubounds, x0, costs, dynamic, aleas, Vfinal=nothing)
6585
dimStates = length(x0)
6686
dimControls = length(ubounds)
6787
dimNoises = length(aleas[1].support[:, 1])
6888

89+
if isa(Vfinal, Void)
90+
Vf = PolyhedralFunction(zeros(1), zeros(1, dimStates), 1)
91+
else
92+
Vf = Vfinal
93+
end
94+
6995
xbounds = []
7096
for i = 1:dimStates
7197
push!(xbounds, (-Inf, Inf))
7298
end
73-
return new(nstage, dimControls, dimStates, dimNoises, xbounds, ubounds, x0, costs, dynamic, aleas)
99+
return new(nstage, dimControls, dimStates, dimNoises, xbounds, ubounds, x0, costs, dynamic, aleas, Vf)
74100
end
75101
end
76102

@@ -196,13 +222,6 @@ function set_max_iterations(param::SDDPparameters, n_iter::Int)
196222
end
197223

198224

199-
type PolyhedralFunction
200-
#function defined by max_k betas[k] + lambdas[k,:]*x
201-
betas::Vector{Float64}
202-
lambdas::Array{Float64,2} #lambdas[k,:] is the subgradient
203-
# number of cuts:
204-
numCuts::Int64
205-
end
206225

207226

208227
type NextStep

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ facts("SDDP algorithm: 1D case") do
123123
V, pbs = solve_SDDP(model, params, 0)
124124
@fact typeof(V) --> Vector{StochDynamicProgramming.PolyhedralFunction}
125125
@fact typeof(pbs) --> Vector{JuMP.Model}
126+
@fact length(pbs) --> n_stages - 1
127+
@fact length(V) --> n_stages
126128

127129
# Test if the first subgradient has the same dimension as state:
128130
@fact length(V[1].lambdas[1, :]) --> model.dimStates

0 commit comments

Comments
 (0)