77# Source: Adrien Cassegrain
88# ############################################################################
99
10-
1110using StochDynamicProgramming, JuMP
1211
1312include (" solver.jl" )
13+
1414const EPSILON = .05
1515const MAX_ITER = 20
1616
@@ -20,35 +20,26 @@ const N_SCENARIOS = 10
2020
2121alea_year = Array ([7.0 7.0 8.0 3.0 1.0 1.0 3.0 4.0 3.0 2.0 6.0 5.0 2.0 6.0 4.0 7.0 3.0 4.0 1.0 1.0 6.0 2.0 2.0 8.0 3.0 7.0 3.0 1.0 4.0 2.0 4.0 1.0 3.0 2.0 8.0 1.0 5.0 5.0 2.0 1.0 6.0 7.0 5.0 1.0 7.0 7.0 7.0 4.0 3.0 2.0 8.0 7.0 ])
2222
23-
2423# COST:
2524const COST = - 66 * 2.7 * (1 .+ .5 * (rand (N_STAGES) .- .5 ))
26-
2725# Constants:
2826const VOLUME_MAX = 100
2927const VOLUME_MIN = 0
30-
3128const CONTROL_MAX = round (Int, .4 / 7. * VOLUME_MAX) + 1
3229const CONTROL_MIN = 0
33-
3430const W_MAX = round (Int, .5 / 7. * VOLUME_MAX)
3531const W_MIN = 0
3632const DW = 1
37-
3833const T0 = 1
3934const HORIZON = 52
40-
4135const X0 = [90 ]
4236
4337# Define aleas' space:
4438const N_ALEAS = Int (round (Int, (W_MAX - W_MIN) / DW + 1 ))
4539const ALEAS = range (W_MIN,stop= W_MAX,length= N_ALEAS)
4640
47-
4841# Define dynamic of the dam:
49- function dynamic (t, x, u, w)
50- return [x[1 ] - u[1 ] - u[2 ] + w[1 ]]
51- end
42+ dynamic (t, x, u, w) = [x[1 ] - u[1 ] - u[2 ] + w[1 ]]
5243
5344# Define cost corresponding to each timestep:
5445function cost_t (t, x, u, w)
5950""" Solve the problem assuming the aleas are known
6051in advance."""
6152function solve_determinist_problem ()
62- println (alea_year)
6353 m = JuMP. Model (OPTIMIZER)
64-
6554 @variable (m, 0 <= x[1 : N_STAGES] <= 100 )
6655 @variable (m, 0. <= u[1 : N_STAGES- 1 ] <= 7 )
6756 @variable (m, 0. <= s[1 : N_STAGES- 1 ] <= 7 )
68-
6957 @objective (m, Min, sum (COST[i]* u[i] for i = 1 : N_STAGES- 1 ))
70-
7158 for i in 1 : (N_STAGES- 1 )
7259 @constraint (m, x[i+ 1 ] - x[i] + u[i] + s[i] - alea_year[i] == 0 )
7360 end
74-
7561 @constraint (m, x[1 ] .== X0)
76-
7762 status = JuMP. optimize! (m)
78- println (status)
7963 println (JuMP. getobjectivevalue (m))
8064 return JuMP. value .(u), JuMP. value .(x)
8165end
8468""" Build aleas probabilities for each month."""
8569function build_aleas ()
8670 aleas = zeros (N_ALEAS, N_STAGES)
87-
8871 # take into account seasonality effects:
8972 unorm_prob = range (1 ,stop= N_ALEAS,length= N_ALEAS)
9073 proba1 = unorm_prob / sum (unorm_prob)
9174 proba2 = proba1[N_ALEAS: - 1 : 1 ]
92-
9375 for t in 1 : (N_STAGES- 1 )
9476 aleas[:, t] = (1 - sin (pi * t/ N_STAGES)) * proba1 + sin (pi * t/ N_STAGES) * proba2
9577 end
@@ -104,7 +86,6 @@ function build_scenarios(n_scenarios::Int64, probabilities)
10486 for scen in 1 : n_scenarios
10587 for t in 1 : (N_STAGES- 1 )
10688 Pcum = cumsum (probabilities[:, t])
107-
10889 n_random = rand ()
10990 prob = findfirst (x -> x > n_random, Pcum)
11091 scenarios[scen, t] = prob
@@ -119,16 +100,12 @@ end
119100Return a Vector{NoiseLaw}"""
120101function generate_probability_laws ()
121102 aleas = build_scenarios (N_SCENARIOS, build_aleas ())
122-
123- laws = Vector {NoiseLaw} (N_STAGES- 1 )
124-
103+ laws = NoiseLaw[]
125104 # uniform probabilities:
126105 proba = 1 / N_SCENARIOS* ones (N_SCENARIOS)
127-
128106 for t= 1 : (N_STAGES- 1 )
129- laws[t] = NoiseLaw (aleas[:, t], proba)
107+ push! ( laws, NoiseLaw (aleas[:, t], proba) )
130108 end
131-
132109 return laws
133110end
134111
@@ -138,35 +115,24 @@ function init_problem()
138115 # Instantiate model:
139116 x0 = X0
140117 aleas = generate_probability_laws ()
141-
142118 x_bounds = [(0 , 100 )]
143119 u_bounds = [(0 , 7 ), (0 , 7 )]
144120 model = StochDynamicProgramming. LinearSPModel (N_STAGES,
145121 u_bounds,
146122 x0,
147123 cost_t,
148124 dynamic, aleas)
149-
150125 set_state_bounds (model, x_bounds)
151-
152126 optimizer = OPTIMIZER
153127 params = StochDynamicProgramming. SDDPparameters (optimizer,
154- passnumber= N_SCENARIOS ,
128+ passnumber= 1 ,
155129 gap= EPSILON,
156130 max_iterations= MAX_ITER)
157-
158131 return model, params
159132end
160133
161-
162- """ Solve the problem."""
163- function solve_dams (display= 0 )
164- model, params = init_problem ()
165-
166- sddp = solve_SDDP (model, params, display)
167- aleas = simulate_scenarios (model. noises, params. forwardPassNumber)
168-
169- costs, stocks = forward_simulations (model, params, sddp. solverinterface, aleas)
170- println (" SDDP cost: " , costs)
171- return stocks
172- end
134+ model, params = init_problem ()
135+ sddp = solve_SDDP (model, params, 1 )
136+ aleas = simulate_scenarios (model. noises, params. forwardPassNumber)
137+ costs, stocks = forward_simulations (model, params, sddp. solverinterface, aleas)
138+ println (" SDDP cost: " , costs)
0 commit comments