Skip to content

Commit 10fe948

Browse files
committed
[UPD] Synchronize different interfaces
1 parent 1fab604 commit 10fe948

File tree

6 files changed

+172
-162
lines changed

6 files changed

+172
-162
lines changed

src/SDDPoptimize.jl

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ function solve_SDDP(model::SPModel,
4141
param::SDDPparameters,
4242
display=0::Int64,
4343
V=nothing,
44-
V_final=nothing,
45-
returnValueFunctions=true::Bool)
44+
V_final=nothing)
4645

4746
# First step: process terminal costs.
4847
# If not specified, default value is null functions
@@ -55,13 +54,13 @@ function solve_SDDP(model::SPModel,
5554
# Second step: process value functions if hotstart is called
5655
if isa(V, Vector{PolyhedralFunction})
5756
# If V is already specified, then call hotstart:
58-
problems = hotstart(model, param, V)
57+
problems = SDDP_hotstart(model, param, V)
5958
else
6059
# Otherwise, initialize value functions:
6160
V, problems = initialize_value_functions(model, param, Vf)
6261
end
6362

64-
return run_SDDP(model, param, V, problems, display, returnValueFunctions)
63+
return run_SDDP(model, param, V, problems, display)
6564
end
6665

6766

@@ -70,10 +69,9 @@ end
7069
"""
7170
function run_SDDP(model::SPModel,
7271
param::SDDPparameters,
73-
V::Array{PolyhedralFunction, 1},
72+
V::Vector{PolyhedralFunction},
7473
problems::Vector{JuMP.Model},
75-
display=0::Int64,
76-
returnValueFunctions=true::Bool)
74+
display=0::Int64)
7775

7876
# Evaluation of initial cost:
7977
V0::Float64 = 0
@@ -110,14 +108,11 @@ function run_SDDP(model::SPModel,
110108
problems,
111109
stockTrajectories,
112110
model.noises,
113-
false,
114-
returnValueFunctions)
111+
false)
115112

116113
iteration_count += 1
117114

118115

119-
120-
121116
if (display > 0) && (iteration_count%display==0)
122117
println("Pass number ", iteration_count,
123118
"\tLower-bound: ", round(get_bellman_value(model, param, 1, V[1], model.initialState),4),
@@ -169,14 +164,14 @@ Return:
169164
Float64 (estimation of the upper bound)
170165
171166
"""
172-
function estimate_upper_bound(model, param, V, problems, n_simulation=1000)
167+
function estimate_upper_bound(model::SPmodel, param::SDDPparameters, V::Vector{PolyhedralFunction}, problem::Vector{JuMP.Model}, n_simulation=1000::Int)
173168

174169
aleas = simulate_scenarios(model.noises, n_simulation)
175170

176171
costs, stockTrajectories, _ = forward_simulations(model,
177172
param,
178173
V,
179-
problems,
174+
problem,
180175
aleas)
181176

182177
return upper_bound(costs), costs
@@ -210,7 +205,7 @@ Parameter:
210205
Else, terminal cost is null
211206
212207
"""
213-
function build_terminal_cost!(model::SPModel, problem::JuMP.Model, Vt)
208+
function build_terminal_cost!(model::SPModel, problem::JuMP.Model, Vt::PolyhedralFunction)
214209
alpha = getVar(problem, :alpha)
215210

216211
# if shape is PolyhedralFunction, build terminal cost with it:
@@ -370,7 +365,7 @@ Parameters:
370365
Estimation of bellman functions as Polyhedral functions
371366
372367
"""
373-
function hotstart(model::SPModel, param::SDDPparameters, V::Vector{PolyhedralFunction})
368+
function SDDP_hotstart(model::SPModel, param::SDDPparameters, V::Vector{PolyhedralFunction})
374369

375370
solverProblems = build_models(model, param)
376371

@@ -470,7 +465,7 @@ Return:
470465
Vector{Float64}: optimal control at time t
471466
472467
"""
473-
function get_control(model::SPModel, param::SDDPparameters, lpproblem::Vector{JuMP.Model}, t, xt, xi)
468+
function get_control(model::SPModel, param::SDDPparameters, lpproblem::Vector{JuMP.Model}, t::Int, xt::Vector{Float64}, xi::Vector{Float64})
474469
return solve_one_step_one_alea(model, param, lpproblem[t], t, xt, xi)[2].optimal_control
475470
end
476471

@@ -504,3 +499,4 @@ function add_cuts_to_model!(model::SPModel, t::Int64, problem::JuMP.Model, V::Po
504499
@addConstraint(problem, V.betas[i] + dot(lambda, model.dynamics(t, x, u, w)) <= alpha)
505500
end
506501
end
502+

src/SDPoptimize.jl

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using ProgressMeter
1212
using Iterators
1313
using Interpolations
1414

15+
1516
"""
1617
Convert the state and control float tuples (stored as arrays or tuples) of the
1718
problem into integer tuples that can be used as indexes for the value function
@@ -39,6 +40,7 @@ function index_from_variable( variable,
3940
return tuple([ 1 + floor(Int64,(1e-10+( variable[i] - bounds[i][1] )/ variable_steps[i] )) for i in 1:length(variable)]...)
4041
end
4142

43+
4244
"""
4345
Convert the state and control float tuples (stored as arrays or tuples) of the
4446
problem into float tuples that can be used as indexes for the interpolated
@@ -67,6 +69,7 @@ function real_index_from_variable( variable,
6769
return tuple([1 + ( variable[i] - bounds[i][1] )/variable_steps[i] for i in 1:length(variable)]...)
6870
end
6971

72+
7073
"""
7174
Compute interpolation of the value function at time t
7275
@@ -91,6 +94,7 @@ function value_function_interpolation( model::SPModel,
9194
return interpolate(V[[Colon() for i in 1:model.dimStates]...,time], BSpline(Linear()), OnGrid())
9295
end
9396

97+
9498
"""
9599
Compute interpolation of the value function at time t
96100
@@ -113,9 +117,9 @@ function generate_grid(model::SPModel, param::SDPparameters)
113117
product_controls = product([model.ulim[i][1]:param.controlSteps[i]:model.ulim[i][2] for i in 1:model.dimControls]...)
114118

115119
return product_states, product_controls
116-
117120
end
118121

122+
119123
"""
120124
Transform a general SPmodel into a StochDynProgModel
121125
@@ -161,6 +165,7 @@ function build_sdpmodel_from_spmodel(model::SPModel)
161165
return SDPmodel
162166
end
163167

168+
164169
"""
165170
Value iteration algorithm to compute optimal value functions in
166171
the Decision Hazard (DH) as well as the Hazard Decision (HD) case
@@ -325,6 +330,7 @@ function sdp_solve_DH(model::StochDynProgModel,
325330
return V
326331
end
327332

333+
328334
"""
329335
Value iteration algorithm to compute optimal value functions in
330336
the Hazard Decision (HD) case
@@ -450,6 +456,7 @@ function sdp_solve_HD(model::StochDynProgModel,
450456
return V
451457
end
452458

459+
453460
"""
454461
Get the optimal value of the problem from the optimal Bellman Function
455462
@@ -465,13 +472,15 @@ Parameters:
465472
466473
Returns :
467474
- V(x0) (Float64)
475+
468476
"""
469477
function get_value(model::SPModel,param::SDPparameters,V::Array{Float64})
470478
ind_x0 = real_index_from_variable(model.initialState, model.xlim, param.stateSteps)
471479
Vi = value_function_interpolation(model, V, 1)
472480
return Vi[ind_x0...,1]
473481
end
474482

483+
475484
"""
476485
Simulation of optimal trajectories given model and Bellman functions
477486
@@ -503,6 +512,7 @@ Returns :
503512
504513
- controls (Array{Float64})
505514
the controls applied to the system at each time step
515+
506516
"""
507517
function sdp_forward_simulation(model::SPModel,
508518
param::SDPparameters,
@@ -521,13 +531,14 @@ function sdp_forward_simulation(model::SPModel,
521531

522532
for k = 1:nb_scenarios
523533
#println(k)
524-
costs[k],states[:,k], controls[:,k] = sdp_forward_single_simulation(SDPmodel,
534+
costs[k], states[:,k], controls[:,k] = sdp_forward_single_simulation(SDPmodel,
525535
param,scenarios[:,k],model.initialState,value,display)
526536
end
527537

528-
return costs, controls, states
538+
return costs, states, controls
529539
end
530540

541+
531542
"""
532543
Get the optimal control at time t knowing the state of the system in the decision hazard case
533544
@@ -552,6 +563,7 @@ the alea realization
552563
553564
Returns :
554565
- V(x0) (Float64)
566+
555567
"""
556568
function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::Int64, x::Array)
557569

@@ -608,6 +620,7 @@ function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::I
608620
return best_control
609621
end
610622

623+
611624
"""
612625
Get the optimal control at time t knowing the state of the system and the alea in the hazard decision case
613626
@@ -632,6 +645,7 @@ the alea realization
632645
633646
Returns :
634647
- V(x0) (Float64)
648+
635649
"""
636650
function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::Int64, x::Array, w::Array)
637651

@@ -670,6 +684,7 @@ function get_control(model::SPModel,param::SDPparameters,V::Array{Float64}, t::I
670684

671685
end
672686

687+
673688
"""
674689
Simulation of optimal control given an initial state and an alea scenario
675690

src/extensiveFormulation.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
Contruct the scenario tree and solve the problem with measurability constraints
1111
"""
1212
function extensive_formulation(model,
13-
params)
13+
param)
1414

1515

1616
#Recover all the constant in the model or in param

0 commit comments

Comments
 (0)