Skip to content

Commit d2083e2

Browse files
committed
[FIX] extensive formulation
1 parent 2375bfa commit d2083e2

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

examples/stock-example.jl

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ const SOLVER = ClpSolver()
2424
# const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0) # require "using CPLEX"
2525

2626
# convergence test
27-
const MAX_ITER = 100 # number of iterations of SDDP
27+
const MAX_ITER = 10 # number of iterations of SDDP
2828

2929
######## Stochastic Model Parameters ########
30-
const N_STAGES = 5 # number of stages of the SP problem
30+
const N_STAGES = 6 # number of stages of the SP problem
3131
const COSTS = rand(N_STAGES) # generating deterministic costs
3232

3333
const CONTROL_MAX = 0.5 # bounds on the control
@@ -65,8 +65,8 @@ println("Model set up")
6565
######### Solving the problem via SDDP
6666
if run_sddp
6767
println("Starting resolution by SDDP")
68-
paramSDDP = SDDPparameters(SOLVER, 2, 0, MAX_ITER) # 2 forward pass, stop at MAX_ITER
69-
V, pbs = solve_SDDP(spmodel, paramSDDP, 10) # display information every 10 iterations
68+
paramSDDP = SDDPparameters(SOLVER, 10, 0, MAX_ITER) # 10 forward pass, stop at MAX_ITER
69+
V, pbs = solve_SDDP(spmodel, paramSDDP, 2) # display information every 2 iterations
7070
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, V)
7171
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
7272
end
@@ -85,11 +85,10 @@ end
8585

8686
if run_ef
8787
println("Starting resolution by Extensive Formulation")
88-
println( extensive_formulation(spmodel, paramSDDP))
8988
value_ef = extensive_formulation(spmodel, paramSDDP)[1]
9089
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))
90+
println("Relative error of SDP value: "*string(100*round(value_sdp/value_ef-1,4))*"%")
91+
println("Relative error of SDDP value: "*string(100*round(lb_sddp/value_ef-1,4))*"%")
9392
end
9493

9594
######### Comparing the solutions on simulated scenarios.
@@ -101,6 +100,6 @@ if run_sdp
101100
costsdp, states, stocks =sdp_forward_simulation(spmodel,paramSDP,scenarios,Vs)
102101
end
103102
if run_sddp && run_sdp
104-
println("Relative difference between sddp and sdp: "*string(2*round(mean(costsddp-costsdp)/mean(costsddp+costsdp),4)))
103+
println("Simulated relative difference between sddp and sdp: "
104+
*string(200*round(mean(costsddp-costsdp)/mean(costsddp+costsdp),4))*"%")
105105
end
106-

src/extensiveFormulation.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,16 @@ function extensive_formulation(model, param)
5353
end
5454
end
5555
end
56-
57-
#Instantiate the problem creating dynamic constraint at each node
58-
for t = 1 : (T)
56+
#Add state constraints
57+
for t = 1 :(T+1)
5958
for n = 1 : N[t]
6059
@constraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .>= [model.xlim[k][1] for k = 1:DIM_STATE])
6160
@constraint(mod,[x[t,DIM_STATE*(n-1)+k] for k = 1:DIM_STATE] .<= [model.xlim[k][2] for k = 1:DIM_STATE])
61+
end
62+
end
63+
#Instantiate the problem creating dynamic constraint at each node
64+
for t = 1 : (T)
65+
for n = 1 : N[t]
6266
for xi = 1 : laws[t].supportSize
6367
m = (n-1)*laws[t].supportSize+xi
6468

@@ -87,12 +91,18 @@ function extensive_formulation(model, param)
8791
@constraint(mod, [x[1,k] for k = 1:DIM_STATE] .== X_init)
8892

8993
#Define the objective of the function
90-
@objective(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)})
94+
@objective(mod, Min,
95+
sum{
96+
sum{ proba[t][laws[t].supportSize*(n-1)+k]*c[t,laws[t].supportSize*(n-1)+k],
97+
k = 1:laws[t].supportSize},
98+
t = 1:T, n=1:div(N[t+1],laws[t].supportSize)}
99+
)
91100

92101
status = solve(mod)
93102
solved = (status == :Optimal)
94103

95104
if solved
105+
println("EF value: "*string(getobjectivevalue(mod)))
96106
firstControl = collect(values(getvalue(u)))[1:DIM_CONTROL*laws[1].supportSize]
97107
return getobjectivevalue(mod), firstControl, status
98108
else

0 commit comments

Comments
 (0)