Skip to content

Commit f8853df

Browse files
authored
Merge pull request #107 from JuliaOpt/ef-fix
Ef fix
2 parents 28523b3 + 181ac9e commit f8853df

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
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 lower bound: "*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(round(200*mean(costsddp-costsdp)/mean(costsddp+costsdp),3))*"%")
105105
end
106-

src/SDDPoptimize.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ fulfilled.
3939
function solve_SDDP(model::SPModel, param::SDDPparameters, display=0::Int64)
4040
# initialize value functions:
4141
V, problems = initialize_value_functions(model, param)
42+
println("initial value function intialized")
4243
# Run SDDP upon example:
4344
sddp_stats = run_SDDP!(model, param, V, problems, display)
4445
return V, problems, sddp_stats
@@ -128,10 +129,16 @@ function run_SDDP!(model::SPModel,
128129
push!(stats.upper_bounds, upb)
129130

130131
if (display > 0) && (iteration_count%display==0)
131-
println("Pass number ", iteration_count,
132+
if upb < Inf
133+
println("Pass number ", iteration_count,
132134
"\tUpper-bound: ", upb,
133135
"\tLower-bound: ", round(stats.lower_bounds[end], 4),
134136
"\tTime: ", round(stats.exectime[end], 2),"s")
137+
else
138+
println("Pass number ", iteration_count,
139+
"\tLower-bound: ", round(stats.lower_bounds[end], 4),
140+
"\tTime: ", round(stats.exectime[end], 2),"s")
141+
end
135142
end
136143

137144
end
@@ -313,7 +320,7 @@ function initialize_value_functions(model::SPModel,
313320
param::SDDPparameters)
314321

315322
solverProblems = build_models(model, param)
316-
323+
println("model builded")
317324
V = PolyhedralFunction[
318325
PolyhedralFunction([], Array{Float64}(0, model.dimStates), 0) for i in 1:model.stageNumber]
319326

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)