Skip to content

Commit 858b227

Browse files
author
Vincent Leclere
committed
[UPD] stock-example work for sdp and sddp. SDPoptimized adjusted to better interact with generic SPmodel.
1 parent 7b6ff51 commit 858b227

File tree

2 files changed

+71
-41
lines changed

2 files changed

+71
-41
lines changed

examples/stock-example.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,16 @@ paramSDDP = SDDPparameters(SOLVER, 10, 0, MAX_ITER) # 10 forward path, stop at M
7272

7373
######### Solving the problem via SDDP
7474
#V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations
75-
#lb = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V)
76-
#println("Lower bound obtained by SDDP: "*string(lb))
75+
#lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V)
76+
#println("Lower bound obtained by SDDP: "*string(lb_sddp))
7777

7878
######### Solving the problem via Dynamic Programming
7979

8080
stateSteps = [0.1]
8181
controlSteps = [0.1]
8282
infoStruct = "HD" # noise at time t is known before taking the decision at time t
8383

84-
paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct)
84+
paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct)
8585
V = sdp_optimize(spmodel,paramSDP)
8686

8787

src/SDPoptimize.jl

Lines changed: 68 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ end
118118

119119
"""
120120
Value iteration algorithm to compute optimal value functions in
121-
the Decision Hazard (DH) case
121+
the Decision Hazard (DH) as well as the Hazard Decision (HD) case
122122
123123
Parameters:
124124
- model (SPmodel)
@@ -137,10 +137,68 @@ Returns :
137137
of the system at each time step
138138
139139
"""
140-
function sdp_solve_DH(model::SPModel,
140+
function sdp_optimize(model::SPModel,
141141
param::SDPparameters,
142142
display=true::Bool)
143+
144+
function true_fun(x...)
145+
return true
146+
end
147+
function zero_fun(x...)
148+
return 0
149+
end
150+
151+
if isa(model,PiecewiseLinearCostSPmodel)||isa(model,LinearDynamicLinearCostSPmodel)
152+
if in(:finalCostFunction,fieldnames(model))
153+
SDPmodel = StochDynProgModel(model, model.finalCostFunction, true_fun)
154+
else
155+
SDPmodel = StochDynProgModel(model, zero_fun, true_fun)
156+
end
157+
elseif isa(model,StochDynProgModel)
158+
SDPmodel = model
159+
else
160+
error("cannot build StochDynProgModel from current SPmodel. Implement new
161+
StochDynProgModel constructor.")
162+
end
163+
164+
#Display start of the algorithm in DH and HD cases
165+
if (param.infoStructure == "DH")
166+
V = sdp_solve_DH(SDPmodel, param, display)
167+
elseif (param.infoStructure == "HD")
168+
V = sdp_solve_HD(SDPmodel, param, display)
169+
else
170+
error("param.infoStructure is neither 'DH' nor 'HD'")
171+
end
172+
173+
return V
174+
end
175+
176+
177+
"""
178+
Value iteration algorithm to compute optimal value functions in
179+
the Decision Hazard (DH) case
180+
181+
Parameters:
182+
- model (StochDynProgModel)
183+
the DPSPmodel of our problem
184+
185+
- param (SDPparameters)
186+
the parameters for the SDP algorithm
187+
188+
- display (Bool)
189+
the output display or verbosity parameter
190+
143191
192+
Returns :
193+
- value_functions (Array)
194+
the vector representing the value functions as functions of the state
195+
of the system at each time step
196+
197+
"""
198+
function sdp_solve_DH(model::StochDynProgModel,
199+
param::SDPparameters,
200+
display=true::Bool)
201+
144202
TF = model.stageNumber
145203
next_state = zeros(Float64, model.dimStates)
146204
law = model.noises
@@ -206,7 +264,8 @@ function sdp_solve_DH(model::SPModel,
206264
w_sample = samples[:, w]
207265
proba = probas[w]
208266
next_state = model.dynamics(t, x, u, w_sample)
209-
267+
268+
210269
if model.constraints(t, next_state, u, w_sample)
211270

212271
count_admissible_w = count_admissible_w + proba
@@ -243,7 +302,7 @@ Value iteration algorithm to compute optimal value functions in
243302
the Hazard Decision (HD) case
244303
245304
Parameters:
246-
- model (SPmodel)
305+
- model (StochDynProgModel)
247306
the DPSPmodel of our problem
248307
249308
- param (SDPparameters)
@@ -259,7 +318,7 @@ Returns :
259318
of the system at each time step
260319
261320
"""
262-
function sdp_solve_HD(model::SPModel,
321+
function sdp_solve_HD(model::StochDynProgModel,
263322
param::SDPparameters,
264323
display=true::Bool)
265324

@@ -307,6 +366,10 @@ function sdp_solve_HD(model::SPModel,
307366
count_admissible_w = 0.
308367

309368
#Tuning expectation computation parameters
369+
if param.expectation_computation!="MonteCarlo" && param.expectation_computation!="Exact"
370+
warn("param.expectation_computation should be 'MonteCarlo' or 'Exact'. Defaulted to 'exact'")
371+
param.expectation_computation="Exact"
372+
end
310373
if (param.expectation_computation=="MonteCarlo")
311374
sampling_size = param.monteCarloSize
312375
samples = [sampling(law,t) for i in 1:sampling_size]
@@ -358,40 +421,7 @@ function sdp_solve_HD(model::SPModel,
358421
return V
359422
end
360423

361-
"""
362-
Value iteration algorithm to compute optimal value functions in
363-
the Decision Hazard (DH) as well as the Hazard Decision (HD) case
364-
365-
Parameters:
366-
- model (SPmodel)
367-
the DPSPmodel of our problem
368-
369-
- param (SDPparameters)
370-
the parameters for the SDP algorithm
371-
372-
- display (Bool)
373-
the output display or verbosity parameter
374-
375-
376-
Returns :
377-
- value_functions (Array)
378-
the vector representing the value functions as functions of the state
379-
of the system at each time step
380-
381-
"""
382-
function sdp_optimize(model::SPModel,
383-
param::SDPparameters,
384-
display=true::Bool)
385-
386-
#Display start of the algorithm in DH and HD cases
387-
if (param.infoStructure == "DH")
388-
V = sdp_solve_DH(model, param, display)
389-
else
390-
V = sdp_solve_HD(model, param, display)
391-
end
392424

393-
return V
394-
end
395425

396426

397427
"""

0 commit comments

Comments
 (0)