Skip to content

Commit f667f51

Browse files
authored
Merge pull request #173 from mrchaos/master
Available with JuMP v0.19 or higher AND Julia 1.3.1 or higher available.
2 parents 8274e75 + 1db3ad0 commit f667f51

40 files changed

+536
-463
lines changed

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ language: julia
22
os:
33
- linux
44
julia:
5-
- 0.6
5+
- 1.3
6+
- 1.4
67
notifications:
78
email: false
89

9-
script:
10-
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
11-
- julia -e 'Pkg.clone(pwd()); Pkg.build("StochDynamicProgramming"); Pkg.test("StochDynamicProgramming"; coverage=true)'
1210
after_success:
13-
- echo $TRAVIS_JULIA_VERSION
14-
- julia -e 'Pkg.add("Coverage"); cd(Pkg.dir("StochDynamicProgramming")); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(Codecov.process_folder())'
11+
# push coverage results to Coveralls
12+
- julia -e 'using Pkg; cd(Pkg.dir("StochDynamicProgramming")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
13+
# push coverage results to Codecov
14+
- julia -e 'using Pkg; cd(Pkg.dir("StochDynamicProgramming")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'

Project.toml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name = "StochDynamicProgramming"
2+
uuid = "f6024387-75e5-5fdf-b095-327b05c0905f"
3+
authors = ["mrchaos <[email protected]>"]
4+
version = "0.5.0"
5+
6+
[deps]
7+
CutPruners = "65d46eb8-70e9-5a30-bf48-2afa3a021b8f"
8+
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
9+
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
10+
Interpolations = "a98d9a8b-a2ab-59e6-89dd-64a1c18fca59"
11+
ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca"
12+
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
13+
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
14+
Nullables = "4d1e1d77-625e-5b40-9113-a560ec7a8ecd"
15+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
16+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
17+
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
18+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
19+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
20+
SharedArrays = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
21+
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
22+
Clp = "e2554f3b-3117-50c0-817c-e040a3ddf72d"
23+
Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76"
24+
25+
[compat]
26+
julia = "^1.3.0"
27+
28+
[extras]
29+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
30+
31+
[targets]
32+
test = ["Test"]

REQUIRE

Lines changed: 0 additions & 7 deletions
This file was deleted.

examples/battery_storage_parallel.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131

3232

3333

34-
import StochDynamicProgramming, Distributions
34+
using StochDynamicProgramming, Distributions
35+
using Statistics
36+
using Distributed
37+
3538
println("library loaded")
3639

3740
# We have to define the instance on all the workers (processes)
@@ -62,7 +65,7 @@ println("library loaded")
6265

6366
# create law of noises
6467
proba = 1/N_XI*ones(N_XI) # uniform probabilities
65-
xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI))
68+
xi_support = collect(range(XI_MIN,stop=XI_MAX,length=N_XI))
6669
xi_law = StochDynamicProgramming.NoiseLaw(xi_support, proba)
6770
xi_laws = StochDynamicProgramming.NoiseLaw[xi_law for t in 1:N_STAGES-1]
6871

@@ -112,4 +115,3 @@ lb_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
112115
println("Value obtained by SDP: "*string(lb_sdp))
113116
costsdp, states, stocks = StochDynamicProgramming.forward_simulations(spmodel,paramSDP,Vs,scenarios)
114117
println(mean(costsdp))
115-

examples/dam.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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
2222

2323

2424
# COST:
25-
const COST = -66*2.7*(1 + .5*(rand(N_STAGES) - .5))
25+
const COST = -66*2.7*(1 .+ .5*(rand(N_STAGES) .- .5))
2626

2727
# Constants:
2828
const VOLUME_MAX = 100
@@ -42,7 +42,7 @@ const X0 = [90]
4242

4343
# Define aleas' space:
4444
const N_ALEAS = Int(round(Int, (W_MAX - W_MIN) / DW + 1))
45-
const ALEAS = linspace(W_MIN, W_MAX, N_ALEAS)
45+
const ALEAS = range(W_MIN,stop=W_MAX,length=N_ALEAS)
4646

4747

4848
# Define dynamic of the dam:
@@ -60,7 +60,7 @@ end
6060
in advance."""
6161
function solve_determinist_problem()
6262
println(alea_year)
63-
m = Model(solver=SOLVER)
63+
m = JuMP.Model(OPTIMIZER)
6464

6565
@variable(m, 0 <= x[1:N_STAGES] <= 100)
6666
@variable(m, 0. <= u[1:N_STAGES-1] <= 7)
@@ -74,10 +74,10 @@ function solve_determinist_problem()
7474

7575
@constraint(m, x[1] .==X0)
7676

77-
status = solve(m)
77+
status = JuMP.optimize!(m)
7878
println(status)
79-
println(getObjectiveValue(m))
80-
return getValue(u), getValue(x)
79+
println(JuMP.getobjectivevalue(m))
80+
return JuMP.value.(u), JuMP.value.(x)
8181
end
8282

8383

@@ -86,7 +86,7 @@ function build_aleas()
8686
aleas = zeros(N_ALEAS, N_STAGES)
8787

8888
# take into account seasonality effects:
89-
unorm_prob = linspace(1, N_ALEAS, N_ALEAS)
89+
unorm_prob = range(1,stop=N_ALEAS,length=N_ALEAS)
9090
proba1 = unorm_prob / sum(unorm_prob)
9191
proba2 = proba1[N_ALEAS:-1:1]
9292

@@ -149,8 +149,8 @@ function init_problem()
149149

150150
set_state_bounds(model, x_bounds)
151151

152-
solver = SOLVER
153-
params = StochDynamicProgramming.SDDPparameters(solver,
152+
optimizer = OPTIMIZER
153+
params = StochDynamicProgramming.SDDPparameters(optimizer,
154154
passnumber=N_SCENARIOS,
155155
gap=EPSILON,
156156
max_iterations=MAX_ITER)

examples/damsvalley.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
##################################################
66
# Set a seed for reproductability:
7-
srand(2713)
8-
9-
using StochDynamicProgramming, JuMP
7+
using Random
8+
using StochDynamicProgramming, JuMP, LinearAlgebra
9+
Random.seed!(2713)
1010

1111
include("solver.jl")
1212
##################################################
@@ -23,7 +23,7 @@ const N_ALEAS = 10
2323

2424
# Cost are negative as we sell the electricity produced by
2525
# dams (and we want to minimize our problem)
26-
const COST = -66*2.7*(1 + .5*(rand(N_STAGES) - .5))
26+
const COST = -66*2.7*(1 .+ .5*(rand(N_STAGES) .- .5))
2727

2828
# Constants:
2929
const VOLUME_MAX = 80
@@ -36,7 +36,7 @@ const CONTROL_MIN = 0
3636
const X0 = [40 for i in 1:N_DAMS]
3737

3838
# Dynamic of stocks:
39-
const A = eye(N_DAMS)
39+
const A = I(N_DAMS)
4040
# The problem has the following structure:
4141
# dam1 -> dam2 -> dam3 -> dam4 -> dam5
4242
# We need to define the corresponding dynamic:
@@ -76,7 +76,7 @@ function final_cost_dams(model, m)
7676
@JuMP.constraint(m, z3 >= 40 - xf[3])
7777
@JuMP.constraint(m, z4 >= 40 - xf[4])
7878
@JuMP.constraint(m, z5 >= 40 - xf[5])
79-
@JuMP.objective(m, Min, model.costFunctions(model.stageNumber-1, x, u, w) + 500.*(z1*z1+z2*z2+z3*z3+z4*z4+z5*z5))
79+
@JuMP.objective(m, Min, model.costFunctions(model.stageNumber-1, x, u, w) + 500. *(z1*z1+z2*z2+z3*z3+z4*z4+z5*z5))
8080
end
8181

8282
##################################################
@@ -92,13 +92,13 @@ const MAX_ITER = 40
9292
"""Build probability distribution at each timestep.
9393
Return a Vector{NoiseLaw}"""
9494
function generate_probability_laws()
95-
laws = Vector{NoiseLaw}(N_STAGES-1)
95+
laws = Vector{NoiseLaw}([])
9696
# uniform probabilities:
9797
proba = 1/N_ALEAS*ones(N_ALEAS)
9898

9999
for t=1:N_STAGES-1
100100
support = rand(0:9, N_DAMS, N_ALEAS)
101-
laws[t] = NoiseLaw(support, proba)
101+
push!(laws, NoiseLaw(support, proba))
102102
end
103103
return laws
104104
end
@@ -118,7 +118,7 @@ function init_problem()
118118
set_state_bounds(model, x_bounds)
119119

120120

121-
params = SDDPparameters(SOLVER,
121+
params = SDDPparameters(OPTIMIZER,
122122
passnumber=FORWARD_PASS,
123123
compute_ub=10,
124124
gap=EPSILON,
@@ -129,4 +129,3 @@ end
129129
# Solve the problem:
130130
model, params = init_problem()
131131
sddp = @time solve_SDDP(model, params, 2, 1)
132-

examples/dynamic_example.jl

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
# Source: Adrien Cassegrain
88
#############################################################################
99

10-
#srand(2713)
10+
using Random
11+
#Random.seed!(2713)
1112

1213
using StochDynamicProgramming, JuMP, Clp
1314

1415
#Constant that the user have to define himself
15-
const SOLVER = ClpSolver()
16+
const OPTIMIZER = optimizer_with_attributes(Clp.Optimizer,
17+
"LogLevel"=>0)
1618
# const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0)
1719

1820
const N_STAGES = 3
@@ -36,7 +38,7 @@ const DW = 1
3638

3739
# Define aleas' space:
3840
const N_ALEAS = Int(round(Int, (W_MAX - W_MIN) / DW + 1))
39-
const ALEAS = linspace(W_MIN, W_MAX, N_ALEAS)
41+
const ALEAS = range(W_MIN,stop=W_MAX,length=N_ALEAS)
4042

4143
const EPSILON = .05
4244
const MAX_ITER = 20
@@ -86,7 +88,7 @@ function build_aleas()
8688
aleas = zeros(N_ALEAS, N_STAGES)
8789

8890
# take into account seasonality effects:
89-
unorm_prob = linspace(1, N_ALEAS, N_ALEAS)
91+
unorm_prob = range(1,stop=N_ALEAS,length=N_ALEAS)
9092
proba1 = unorm_prob / sum(unorm_prob)
9193
proba2 = proba1[N_ALEAS:-1:1]
9294

@@ -120,13 +122,13 @@ Return a Vector{NoiseLaw}"""
120122
function generate_probability_laws()
121123
aleas = build_scenarios(N_SCENARIOS, build_aleas())
122124

123-
laws = Vector{NoiseLaw}(N_STAGES)
125+
laws = Vector{NoiseLaw}([])
124126

125127
# uniform probabilities:
126128
proba = 1/N_SCENARIOS*ones(N_SCENARIOS)
127129

128130
for t=1:N_STAGES
129-
laws[t] = NoiseLaw(aleas[:, t], proba)
131+
push!(laws, NoiseLaw(aleas[:, t], proba))
130132
end
131133

132134
return laws
@@ -142,15 +144,17 @@ function init_problem()
142144
#Define bounds for the control
143145
u_bounds = [(CONTROL_MIN, CONTROL_MAX) for i in 1:DIM_CONTROLS]
144146

145-
model = LinearDynamicLinearCostSPmodel(N_STAGES,
146-
u_bounds,
147-
x0,
148-
cost_t,
149-
dynamic,
150-
aleas)
151-
152-
params = SDDPparameters(SOLVER, N_SCENARIOS, EPSILON, MAX_ITER)
153-
147+
model = LinearSPModel(N_STAGES,
148+
u_bounds,
149+
x0,
150+
cost_t,
151+
dynamic,
152+
aleas)
153+
154+
params = SDDPparameters(OPTIMIZER,
155+
passnumber=N_SCENARIOS,
156+
gap=EPSILON,
157+
max_iterations=MAX_ITER)
154158
return model, params
155159
end
156160

@@ -181,6 +185,7 @@ i = 0
181185
nb_iter = 10
182186

183187
while i<nb_iter
188+
global model,params,i
184189
sol, firstControl, status = extensive_formulation(model,params)
185190
if (status == :Optimal)
186191
unsolve = false
@@ -207,4 +212,3 @@ else
207212
println("firstControl =", firstControl)
208213
println("V0 = ", b[1].lambdas[1,:]*X0+b[1].betas[1])
209214
end
210-

examples/multistock-example.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# u^i_t choosen knowing xi_1 .. xi_t
1313
#############################################################################
1414

15-
using StochDynamicProgramming, Clp
15+
using StochDynamicProgramming, Clp, JuMP
1616
println("library loaded")
1717

1818
run_sddp = true # false if you don't want to run sddp
@@ -21,7 +21,8 @@ test_simulation = false # false if you don't want to test your strategies
2121

2222
######## Optimization parameters ########
2323
# choose the LP solver used.
24-
const SOLVER = ClpSolver() # require "using Clp"
24+
const OPTIMIZER = optimizer_with_attributes(Clp.Optimizer,
25+
"LogLevel"=>0) # require "using Clp"
2526
#const SOLVER = CplexSolver(CPX_PARAM_SIMDISPLAY=0) # require "using CPLEX"
2627

2728
# convergence test
@@ -49,7 +50,7 @@ const S0 = [0.5 for i=1:N_STOCKS] # initial stock
4950

5051
# create law of noises
5152
proba = 1/N_XI*ones(N_XI) # uniform probabilities
52-
xi_support = collect(linspace(XI_MIN,XI_MAX,N_XI))
53+
xi_support = collect(range(XI_MIN,stop=XI_MAX,length=N_XI))
5354
xi_law = StochDynamicProgramming.noiselaw_product([NoiseLaw(xi_support, proba) for i=1:N_STOCKS]...)
5455
xi_laws = NoiseLaw[xi_law for t in 1:N_STAGES-1]
5556

@@ -77,22 +78,23 @@ println("Model set up")
7778

7879
######### Solving the problem via SDDP
7980
if run_sddp
80-
tic()
81+
@time begin
8182
println("Starting resolution by SDDP")
8283
# 10 forward pass, stop at MAX_ITER
83-
paramSDDP = SDDPparameters(SOLVER,
84+
paramSDDP = SDDPparameters(OPTIMIZER,
8485
passnumber=1,
8586
max_iterations=MAX_ITER)
8687
sddp = @time solve_SDDP(spmodel, paramSDDP, 2, # display information every 2 iterations
8788
stopcrit=IterLimit(MAX_ITER))
8889
lb_sddp = StochDynamicProgramming.get_lower_bound(spmodel, paramSDDP, sddp.bellmanfunctions)
89-
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,4)))
90-
toc(); println();
90+
println("Lower bound obtained by SDDP: "*string(round(lb_sddp,digits=4)))
91+
end
92+
println();
9193
end
9294

9395
######### Solving the problem via Dynamic Programming
9496
if run_sdp
95-
tic()
97+
@time begin
9698
println("Starting resolution by SDP")
9799
stateSteps = [step for i=1:N_STOCKS] # discretization step of the state
98100
controlSteps = [step for i=1:N_STOCKS] # discretization step of the control
@@ -104,8 +106,9 @@ if run_sdp
104106

105107
Vs = solve_dp(spmodel_sdp, paramSDP, 1)
106108
value_sdp = StochDynamicProgramming.get_bellman_value(spmodel,paramSDP,Vs)
107-
println("Value obtained by SDP: "*string(round(value_sdp,4)))
108-
toc(); println();
109+
println("Value obtained by SDP: "*string(round(value_sdp,digits=4)))
110+
end
111+
println();
109112
end
110113

111114
######### Comparing the solutions on simulated scenarios.
@@ -115,6 +118,5 @@ if run_sddp && run_sdp && test_simulation
115118
costsddp, stocks = forward_simulations(spmodel, paramSDDP, sddp.solverinterface, scenarios)
116119
costsdp, states, controls = forward_simulations(spmodel,paramSDP, Vs, scenarios)
117120
println("Simulated relative gain of sddp over sdp: "
118-
*string(round(200*mean(costsdp-costsddp)/abs(mean(costsddp+costsdp)),3))*"%")
121+
*string(round(200*mean(costsdp-costsddp)/abs(mean(costsddp+costsdp)),digits=3))*"%")
119122
end
120-

examples/parallel_sddp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11

2-
import StochDynamicProgramming
3-
2+
using StochDynamicProgramming
3+
using Distributed
44

55
"""
6-
Solve SDDP in parallel, dispatching both forward and backward passes to process,
6+
Solve SDDP in parallel, dispatching both forward and backward passes to process,
77
which is not the most standard parallelization of SDDP.
88
99
# Arguments

0 commit comments

Comments
 (0)