Skip to content

Commit 4d5b34d

Browse files
committed
Merge pull request #60 from leclere/dev_speedups
Fixes + unit tests
2 parents 198ff8e + 0e0205d commit 4d5b34d

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

examples/benchmark.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function benchmark_sdp()
328328
timing = zeros(n_benchmark)
329329
for n in 1:n_benchmark
330330
tic()
331-
V_sdp = solve_DP(modelSDP, paramsSDP,1);
331+
V_sdp = solve_DP(modelSDP, paramsSDP,0);
332332
timing[n] = toq()
333333
end
334334
@show timing

examples/stock-example.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ if run_sdp
7676
infoStruct = "HD" # noise at time t is known before taking the decision at time t
7777

7878
paramSDP = SDPparameters(spmodel, stateSteps, controlSteps, infoStruct)
79-
Vs = sdp_optimize(spmodel,paramSDP)
80-
lb_sdp = StochDynamicProgramming.get_value(spmodel,paramSDP,Vs)
79+
Vs = solve_DP(spmodel,paramSDP, 1)
80+
lb_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
8181
println("Value obtained by SDP: "*string(lb_sdp))
8282
end
8383

src/SDPoptimize.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ function build_sdpmodel_from_spmodel(model::SPModel)
144144
if isa(model,PiecewiseLinearCostSPmodel)||isa(model,LinearDynamicLinearCostSPmodel)
145145
function cons_fun(t,x,u,w)
146146
for i in 1:model.dimStates
147-
if (x[i]<=model.xlim[i][1]) || (x[i]>=model.xlim[i][2])
147+
if (x[i]<model.xlim[i][1]) || (x[i]>model.xlim[i][2])
148148
return false
149149
end
150150
end
@@ -518,13 +518,13 @@ function sdp_forward_simulation(model::SPModel,
518518
nb_scenarios = size(scenarios)[2]
519519

520520
costs = zeros(nb_scenarios)
521-
states = zeros(TF,nb_scenarios)
522-
controls = zeros(TF-1,nb_scenarios)
521+
states = zeros(TF,nb_scenarios,model.dimStates)
522+
controls = zeros(TF-1,nb_scenarios,model.dimControls)
523523

524524

525525
for k = 1:nb_scenarios
526-
costs[k], states[:,k], controls[:,k] = sdp_forward_single_simulation(SDPmodel,
527-
param,scenarios[:,k],model.initialState,value,display)
526+
costs[k], states[:,k,:], controls[:,k,:] = sdp_forward_single_simulation(SDPmodel,
527+
param,scenarios[:,k],model.initialState,V,display)
528528
end
529529

530530
return costs, states, controls

test/runtests.jl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,8 @@ facts("SDP algorithm") do
432432

433433
@fact test_costs --> true
434434

435+
@fact convertedSDPmodel.constraints(1,x,u,w) --> true
436+
435437
end
436438

437439
context("Solve and simulate using SDP") do
@@ -445,6 +447,14 @@ facts("SDP algorithm") do
445447
aleas_scen, x0,
446448
V_sdp, true )
447449

450+
451+
costs_sdp2, stocks_sdp2, controls_sdp2 = StochDynamicProgramming.sdp_forward_simulation(modelSDP,
452+
paramsSDP,
453+
aleas_scen,
454+
V_sdp, true )
455+
456+
@fact costs_sdp2[1] --> costs_sdp
457+
448458
x = x0
449459
V_sdp = solve_DP(modelSDP, paramsSDP, false);
450460
V_sdp2 = StochDynamicProgramming.sdp_solve_HD(modelSDP, paramsSDP, false);
@@ -461,6 +471,15 @@ facts("SDP algorithm") do
461471
@fact v1 --> v2
462472
@fact (v1<=v3) --> true
463473

474+
paramsSDP.infoStructure = "DH"
475+
costs_sdp3, stocks_sdp3, controls_sdp3 = StochDynamicProgramming.sdp_forward_simulation(modelSDP,
476+
paramsSDP,
477+
aleas_scen,
478+
V_sdp3, true )
479+
paramsSDP.infoStructure = "HD"
480+
481+
@fact costs_sdp3[1]>=costs_sdp2[1] --> true
482+
464483
a,b = StochDynamicProgramming.generate_grid(modelSDP, paramsSDP)
465484

466485
x_bounds = modelSDP.xlim
@@ -484,11 +503,15 @@ facts("SDP algorithm") do
484503
state_ref = zeros(2)
485504
state_ref[1] = stocks_sdp[2,1,1]
486505
state_ref[2] = stocks_sdp[2,1,2]
506+
w = [4]
487507

488508
@fact_throws get_control(modelSDP,paramsSDP,V_sdp3, 1, x)
509+
@fact (get_control(modelSDP,paramsSDP,V_sdp3, 1, x, w)[1] >= CONTROL_MIN) --> true
510+
@fact (get_control(modelSDP,paramsSDP,V_sdp3, 1, x, w)[1] <= CONTROL_MAX) --> true
511+
489512
paramsSDP.infoStructure = "DH"
490513
@fact (get_control(modelSDP,paramsSDP,V_sdp3, 1, x)[1] >= CONTROL_MIN) --> true
491-
@fact (get_control(modelSDP,paramsSDP,V_sdp3, 1, x)[1] >= CONTROL_MIN) --> true
514+
@fact (get_control(modelSDP,paramsSDP,V_sdp3, 1, x)[1] <= CONTROL_MAX) --> true
492515

493516
@fact size(stocks_sdp) --> (3,1,2)
494517
@fact size(controls_sdp) --> (2,1,2)

0 commit comments

Comments
 (0)