Skip to content

Commit 2375bfa

Browse files
committed
[DEV] working on examples
1 parent 4c26981 commit 2375bfa

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

examples/stock-example.jl

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ using StochDynamicProgramming, JuMP, Clp, Distributions
1515
println("library loaded")
1616

1717
run_sddp = true # false if you don't want to run sddp
18-
run_sdp = true # false if you don't want to run sdp
18+
run_sdp = true # false if you don't want to run sdp
19+
run_ef = true # false if you don't want to run extensive formulation
1920

2021
######## Optimization parameters ########
2122
# choose the LP solver used.
@@ -26,17 +27,17 @@ const SOLVER = ClpSolver()
2627
const MAX_ITER = 100 # number of iterations of SDDP
2728

2829
######## Stochastic Model Parameters ########
29-
const N_STAGES = 5
30-
const COSTS = rand(N_STAGES)
30+
const N_STAGES = 5 # number of stages of the SP problem
31+
const COSTS = rand(N_STAGES) # generating deterministic costs
3132

32-
const CONTROL_MAX = 0.5
33+
const CONTROL_MAX = 0.5 # bounds on the control
3334
const CONTROL_MIN = 0
3435

35-
const XI_MAX = 0.3
36+
const XI_MAX = 0.3 # bounds on the noise
3637
const XI_MIN = 0
37-
const N_XI = 10
38-
# initial stock
39-
const S0 = 0.5
38+
const N_XI = 10 # discretization of the noise
39+
40+
const S0 = 0.5 # initial stock
4041

4142
# create law of noises
4243
proba = 1/N_XI*ones(N_XI) # uniform probabilities
@@ -55,14 +56,15 @@ function cost_t(t, x, u, w)
5556
end
5657

5758
######## Setting up the SPmodel
58-
s_bounds = [(0, 1)] # bounds on the state
59-
u_bounds = [(CONTROL_MIN, CONTROL_MAX)] # bounds on controls
60-
spmodel = LinearDynamicLinearCostSPmodel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws)
61-
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
62-
59+
s_bounds = [(0, 1)] # bounds on the state
60+
u_bounds = [(CONTROL_MIN, CONTROL_MAX)] # bounds on controls
61+
spmodel = LinearDynamicLinearCostSPmodel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws)
62+
set_state_bounds(spmodel, s_bounds) # adding the bounds to the model
63+
println("Model set up")
6364

6465
######### Solving the problem via SDDP
6566
if run_sddp
67+
println("Starting resolution by SDDP")
6668
paramSDDP = SDDPparameters(SOLVER, 2, 0, MAX_ITER) # 2 forward pass, stop at MAX_ITER
6769
V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations
6870
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V)
@@ -71,17 +73,26 @@ end
7173

7274
######### Solving the problem via Dynamic Programming
7375
if run_sdp
76+
println("Starting resolution by SDP")
7477
stateSteps = [0.01] # discretization step of the state
7578
controlSteps = [0.01] # discretization step of the control
7679
infoStruct = "HD" # noise at time t is known before taking the decision at time t
77-
7880
paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct)
7981
Vs = solve_DP(spmodel,paramSDP, 1)
8082
value_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
8183
println("Value obtained by SDP: "*string(round(value_sdp,4)))
8284
end
8385

84-
######### Comparing the solutions
86+
if run_ef
87+
println("Starting resolution by Extensive Formulation")
88+
println( extensive_formulation(spmodel, paramSDDP))
89+
value_ef = extensive_formulation(spmodel, paramSDDP)[1]
90+
println("Value obtained by Extensive Formulation: "*string(round(value_ef,4)))
91+
println(round(value_sdp/value_ef,4))
92+
println(round(lb_sddp/value_ef,4))
93+
end
94+
95+
######### Comparing the solutions on simulated scenarios.
8596
scenarios = StochDynamicProgramming.simulate_scenarios(xi_laws,1000)
8697
if run_sddp
8798
costsddp, stocks = forward_simulations(spmodel, paramSDDP, pbs, scenarios)
@@ -92,3 +103,4 @@ end
92103
if run_sddp && run_sdp
93104
println("Relative difference between sddp and sdp: "*string(2*round(mean(costsddp-costsdp)/mean(costsddp+costsdp),4)))
94105
end
106+

src/extensiveFormulation.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ measurability constraints.
1313
# Arguments:
1414
* `model::SPModel`
1515
* `param::SDDPparameters`
16+
# Returns
17+
* `objective value`
18+
* `first control`
19+
* `status of optimization problem`
1620
"""
1721
function extensive_formulation(model, param)
1822

0 commit comments

Comments
 (0)