Skip to content

Commit 28523b3

Browse files
authored
Merge pull request #106 from JuliaOpt/cleaning
Cleaning stock-example
2 parents e0e58fe + 2375bfa commit 28523b3

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

examples/stock-example.jl

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@
1414
using StochDynamicProgramming, JuMP, Clp, Distributions
1515
println("library loaded")
1616

17-
run_sddp = true
18-
run_sdp = true
17+
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
19+
run_ef = true # false if you don't want to run extensive formulation
1920

2021
######## Optimization parameters ########
2122
# choose the LP solver used.
2223
const SOLVER = ClpSolver()
2324
# const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0) # require "using CPLEX"
2425

2526
# convergence test
26-
const MAX_ITER = 100 # maximum iteration of SDDP
27+
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,33 +56,43 @@ function cost_t(t, x, u, w)
5556
end
5657

5758
######## Setting up the SPmodel
58-
s_bounds = [(0, 1)]
59-
u_bounds = [(CONTROL_MIN, CONTROL_MAX)]
60-
spmodel = LinearDynamicLinearCostSPmodel(N_STAGES,u_bounds,[S0],cost_t,dynamic,xi_laws)
61-
set_state_bounds(spmodel, s_bounds)
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
66-
paramSDDP = SDDPparameters(SOLVER, 2, 0, MAX_ITER) # 10 forward pass, stop at MAX_ITER
67+
println("Starting resolution by SDDP")
68+
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)
69-
println("Lower bound obtained by SDDP: "*string(lb_sddp))
71+
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
7072
end
7173

7274
######### Solving the problem via Dynamic Programming
7375
if run_sdp
74-
stateSteps = [0.01]
75-
controlSteps = [0.01]
76+
println("Starting resolution by SDP")
77+
stateSteps = [0.01] # discretization step of the state
78+
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)
80-
lb_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
81-
println("Value obtained by SDP: "*string(lb_sdp))
82+
value_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
83+
println("Value obtained by SDP: "*string(round(value_sdp,4)))
8284
end
8385

84-
######### Comparing the solution
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)
@@ -90,5 +101,6 @@ if run_sdp
90101
costsdp, states, stocks =sdp_forward_simulation(spmodel,paramSDP,scenarios,Vs)
91102
end
92103
if run_sddp && run_sdp
93-
println(mean(costsddp-costsdp))
104+
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)