Skip to content

Commit b1efb84

Browse files
author
Vincent Leclere
committed
[UPD] stock-example run for both sdp and sddp. compare only lower bounds.
1 parent 858b227 commit b1efb84

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

examples/stock-example.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const S0 = 0.5
4141
proba = 1/N_XI*ones(N_XI) # uniform probabilities
4242
xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI))
4343
xi_law = NoiseLaw(xi_support, proba)
44-
xi_laws = Vector{NoiseLaw}(N_STAGES)
44+
xi_laws = Vector{NoiseLaw}(N_STAGES-1)
4545
for t=1:N_STAGES-1
4646
xi_laws[t] = xi_law
4747
end
@@ -71,9 +71,9 @@ set_state_bounds(spmodel, s_bounds)
7171
paramSDDP = SDDPparameters(SOLVER, 10, 0, MAX_ITER) # 10 forward path, stop at MAX_ITER
7272

7373
######### Solving the problem via SDDP
74-
#V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations
75-
#lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V)
76-
#println("Lower bound obtained by SDDP: "*string(lb_sddp))
74+
V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations
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

@@ -83,10 +83,11 @@ infoStruct = "HD" # noise at time t is known before taking the decision at time
8383

8484
paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct)
8585
V = sdp_optimize(spmodel,paramSDP)
86-
86+
lb_sdp = StochDynamicProgramming.get_value(spmodel,paramSDP,V)
87+
println("Lower bound obtained by SDP: "*string(lb_sdp))
8788

8889

8990
######### Comparing the solution
90-
#scenarios = generate_scenarios(xi_laws,1000)
91+
#scenarios = StochDynamicProgramming.generate_scenarios(xi_laws,1)
9192
#costs, stocks = forward_simulations(spmodel, params, V, pbs, scenarios)
9293

src/SDPoptimize.jl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ function sdp_optimize(model::SPModel,
157157
elseif isa(model,StochDynProgModel)
158158
SDPmodel = model
159159
else
160-
error("cannot build StochDynProgModel from current SPmodel. Implement new
161-
StochDynProgModel constructor.")
160+
error("cannot build StochDynProgModel from current SPmodel. You need to implement
161+
a new StochDynProgModel constructor.")
162162
end
163163

164164
#Display start of the algorithm in DH and HD cases
@@ -421,7 +421,26 @@ function sdp_solve_HD(model::StochDynProgModel,
421421
return V
422422
end
423423

424+
"""
425+
Get the optimal value of the problem from the optimal Bellman Function
426+
427+
Parameters:
428+
- model (SPmodel)
429+
the DPSPmodel of our problem
430+
431+
- param (SDPparameters)
432+
the parameters for the SDP algorithm
433+
434+
- V (Array{Float64})
435+
the Bellman Functions
424436
437+
Returns :
438+
- V(x0) (Float64)
439+
"""
440+
function get_value(model::SPModel,param::SDPparameters,V::Array{Float64})
441+
ind_x0 = index_from_variable(model.initialState, model.xlim, param.stateSteps)
442+
return V[ind_x0...,1]
443+
end
425444

426445

427446
"""

src/noises.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,12 @@ function generate_scenarios(laws::Vector{NoiseLaw}, n::Int64)
173173
if n <= 0
174174
error("negative number of simulations")
175175
end
176-
Tf = length(law)
176+
Tf = length(laws)
177177
scenarios = Array{Vector{Float64}}(n,Tf)
178178
for i = 1:n#TODO can be parallelized
179179
scenario = []
180180
for t=1:Tf
181-
new_val = laws[t].support[:, rand(Categorical(law[t].proba))]
181+
new_val = laws[t].support[:, rand(Categorical(laws[t].proba))]
182182
push!(scenario, new_val)
183183
end
184184
scenarios[i,:]=scenario

0 commit comments

Comments
 (0)