Skip to content

Commit 77d7b06

Browse files
committed
Merge pull request #66 from leclere/integrate-ef
Integrate ef
2 parents c035625 + e9b7754 commit 77d7b06

File tree

3 files changed

+25
-41
lines changed

3 files changed

+25
-41
lines changed

examples/dynamic_example.jl

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,21 @@ const SOLVER = ClpSolver()
1717
# const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0)
1818

1919
const N_STAGES = 5
20-
const N_SCENARIOS = 3
20+
const N_SCENARIOS = 1
2121

22-
const DIM_STATES = 2
23-
const DIM_CONTROLS = 2
22+
const DIM_STATES = 1
23+
const DIM_CONTROLS = 1
2424
const DIM_ALEAS = 1
2525

2626

2727

2828
#Constants that the user does not have to define
2929
const T0 = 1
3030

31-
# Constants:
32-
const VOLUME_MAX = 100
33-
const VOLUME_MIN = 0
34-
35-
const CONTROL_MAX = round(Int, .4/7. * VOLUME_MAX) + 1
31+
const CONTROL_MAX = round(Int, .4/7. * 100) + 1
3632
const CONTROL_MIN = 0
3733

38-
const W_MAX = round(Int, .5/7. * VOLUME_MAX)
34+
const W_MAX = round(Int, .5/7. * 100)
3935
const W_MIN = 0
4036
const DW = 1
4137

@@ -46,9 +42,7 @@ const ALEAS = linspace(W_MIN, W_MAX, N_ALEAS)
4642
const EPSILON = .05
4743
const MAX_ITER = 20
4844

49-
alea_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])
50-
51-
const X0 = [50, 50]
45+
const X0 = 50*ones(DIM_STATES)
5246

5347
Ax=[]
5448
Au=[]
@@ -146,23 +140,17 @@ function init_problem()
146140
x0 = X0
147141
aleas = generate_probability_laws()
148142

149-
x_bounds = [(VOLUME_MIN, VOLUME_MAX), (VOLUME_MIN, VOLUME_MAX)]
150-
u_bounds = []
151-
for u = 1:DIM_CONTROLS
152-
u_bounds = push!(u_bounds, (CONTROL_MIN, CONTROL_MAX))
153-
end
154-
143+
#Define bounds for the control
144+
u_bounds = [(CONTROL_MIN, CONTROL_MAX) for i in 1:DIM_CONTROLS]
145+
155146
model = LinearDynamicLinearCostSPmodel(N_STAGES,
156147
u_bounds,
157148
x0,
158149
cost_t,
159150
dynamic,
160151
aleas)
161152

162-
#set_state_bounds(model, x_bounds)
163-
164-
solver = SOLVER
165-
params = SDDPparameters(solver, N_SCENARIOS, EPSILON, MAX_ITER)
153+
params = SDDPparameters(SOLVER, N_SCENARIOS, EPSILON, MAX_ITER)
166154

167155
return model, params
168156
end
@@ -177,10 +165,7 @@ function solve_dams(model,params,display=false)
177165

178166
V, pbs = solve_SDDP(model, params, display)
179167

180-
aleas = simulate_scenarios(model.noises,
181-
(model.stageNumber,
182-
params.forwardPassNumber,
183-
model.dimNoises))
168+
aleas = simulate_scenarios(model.noises,params.forwardPassNumber)
184169

185170
params.forwardPassNumber = 1
186171

@@ -189,7 +174,7 @@ function solve_dams(model,params,display=false)
189174
return stocks, V
190175
end
191176

192-
#Solve the problem and try nb_iter times to generate radom data in case of infeasibility
177+
#Solve the problem and try nb_iter times to generate random data in case of infeasibility
193178
unsolve = true
194179
sol = 0
195180
i = 0
@@ -219,5 +204,6 @@ if (unsolve)
219204
else
220205
a,b = solve_dams(modelbis,paramsbis)
221206
println("solution =",sol)
207+
println("V0 = ", b[1].lambdas[1,:]*X0+b[1].betas[1])
222208
end
223209

src/StochDynamicProgramming.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ module StochDynamicProgramming
1212

1313
using JuMP, Distributions
1414

15-
export solve_SDDP, NoiseLaw, simulate_scenarios,
15+
export solve_SDDP,
16+
NoiseLaw, simulate_scenarios,
1617
SDDPparameters, LinearDynamicLinearCostSPmodel, set_state_bounds,
17-
PiecewiseLinearCostSPmodel,
18+
PiecewiseLinearCostSPmodel, extensive_formulation,
1819
PolyhedralFunction, NextStep, forward_simulations,
1920
StochDynProgModel, SDPparameters, solve_DP,
2021
sdp_forward_simulation, sampling, get_control, get_bellman_value

src/extensiveFormulation.jl

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# License, v. 2.0. If a copy of the MPL was not distributed with this
44
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
#############################################################################
6-
# Define the extensive formulation to check the results of small problems
6+
# Define the extensive formulation to check the results of small problems.
7+
# The problem is instantiate on a tree.
78
#############################################################################
89

910
"""
@@ -12,17 +13,14 @@ Contruct the scenario tree and solve the problem with measurability constraints
1213
function extensive_formulation(model,
1314
param)
1415

15-
1616
#Recover all the constant in the model or in param
1717
laws = model.noises
18-
N_NOISES = laws[1].supportSize
1918

2019
DIM_STATE = model.dimStates
2120
DIM_CONTROL = model.dimControls
2221

2322
X_init = model.initialState
2423

25-
2624
T = model.stageNumber-1
2725

2826
mod = Model(solver=param.solver)
@@ -41,22 +39,21 @@ function extensive_formulation(model,
4139
#At each node, we have as many variables as nodes
4240
@defVar(mod, u[t=1:T,n=1:DIM_CONTROL*N[t+1]])
4341
@defVar(mod, x[t=1:T+1,n=1:DIM_STATE*N[t]])
44-
@defVar(mod, c[t=1:T,n=1:N_NOISES*N[t]])
42+
@defVar(mod, c[t=1:T,n=1:laws[t].supportSize*N[t]])
4543

4644

4745
#Computes the total probability of each node from the conditional probabilities
48-
proba = []
49-
push!(proba, laws[1].proba)
46+
proba = Vector{typeof(laws[1].proba)}(T)
47+
proba[1] = laws[1].proba
5048
for t = 2 : T
51-
push!(proba, zeros(N[t+1]))
49+
proba[t] = zeros(N[t+1])
5250
for j = 1 : N[t]
53-
for k = 1 : N_NOISES
54-
proba[t][N_NOISES*(j-1)+k] = laws[t].proba[k]*proba[t-1][j]
51+
for k = 1 : laws[t].supportSize
52+
proba[t][laws[t].supportSize*(j-1)+k] = laws[t].proba[k]*proba[t-1][j]
5553
end
5654
end
5755
end
5856

59-
6057
#Instantiate the problem creating dynamic constraint at each node
6158
for t = 1 : (T)
6259
for n = 1 : N[t]
@@ -91,7 +88,7 @@ function extensive_formulation(model,
9188

9289

9390
#Define the objective of the function
94-
@setObjective(mod, Min, sum{ sum{proba[t][N_NOISES*(n-1)+k]*c[t,N_NOISES*(n-1)+k],k = 1:N_NOISES} , t = 1:T, n=1:div(N[t+1],N_NOISES)})
91+
@setObjective(mod, Min, sum{ sum{proba[t][laws[t].supportSize*(n-1)+k]*c[t,laws[t].supportSize*(n-1)+k],k = 1:laws[t].supportSize} , t = 1:T, n=1:div(N[t+1],laws[t].supportSize)})
9592

9693
status = solve(mod)
9794

0 commit comments

Comments
 (0)