1414using StochDynamicProgramming, JuMP, Clp, Distributions
1515println (" 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.
2223const 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
3334const CONTROL_MIN = 0
3435
35- const XI_MAX = 0.3
36+ const XI_MAX = 0.3 # bounds on the noise
3637const 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
4243proba = 1 / N_XI* ones (N_XI) # uniform probabilities
@@ -55,33 +56,43 @@ function cost_t(t, x, u, w)
5556end
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
6566if 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 ) ))
7072end
7173
7274# ######## Solving the problem via Dynamic Programming
7375if 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 ) ))
8284end
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.
8596scenarios = StochDynamicProgramming. simulate_scenarios (xi_laws,1000 )
8697if 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)
91102end
92103if 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 ) ))
94105end
106+
0 commit comments