Skip to content

Commit

Permalink
Tidy up examples and reduce amount of printing in tests (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Apr 8, 2018
1 parent 2c487cb commit be885b2
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ application. Here we categorize the examples by functionality.
- [Simple Conic](https://github.com/odow/SDDP.jl/tree/master/examples/Conic/simple_conic.jl)

## Conic
- [Simple Conic](https://github.com/odow/SDDP.jl/tree/master/examples/Conic/simple_conic.jl)
- [Simple Conic](https://github.com/odow/SDDP.jl/tree/master/examples/Nonlinear/simple_conic.jl)

## Termination Criteria

Expand Down
4 changes: 2 additions & 2 deletions examples/Agriculture/mccardle_farm.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -137,7 +137,7 @@ end

srand(111)
m = mccardle_farm_model()
solution = solve(m, max_iterations=20)
solution = solve(m, max_iterations=20, print_level=0)
@test isapprox(getbound(m), 4074.1391, atol=1e-5)

# results = simulate(m, # Simulate the policy
Expand Down
8 changes: 5 additions & 3 deletions examples/AssetManagement/asset_management.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -48,6 +48,8 @@ m = SDDPModel(
end

srand(111)
@time solve(m, max_iterations = 25,
print_level = 0)
solve(m,
max_iterations = 25,
print_level = 0
)
@test isapprox(getbound(m), 1.514, atol=1e-4)
4 changes: 2 additions & 2 deletions examples/AssetManagement/asset_management_stagewise.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -52,7 +52,7 @@ m = SDDPModel(
end

srand(111)
@time status = solve(m,
status = solve(m,
max_iterations = 100,
simulation = MonteCarloSimulation(
frequency = 5,
Expand Down
4 changes: 2 additions & 2 deletions examples/BinaryProblems/airconditioning.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -51,5 +51,5 @@ end

srand(1234)
m = airconditioningmodel()
solve(m, max_iterations=16, print_level=1)
solve(m, max_iterations=16, print_level=2)
@test isapprox(getbound(m), 62_500.0)
4 changes: 2 additions & 2 deletions examples/BinaryProblems/booking_management.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Eyob Zewdie, Oscar Dowson
# Copyright 2018, Eyob Zewdie, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -88,5 +88,5 @@ m_1_2_5 = bookingmanagementmodel(1, 2, 5)

srand(1234)
m_2_2_3 = bookingmanagementmodel(2, 2, 3)
@test solve(m_2_2_3, max_iterations = 40, print_level = 2) == :max_iterations
@test solve(m_2_2_3, max_iterations = 40, print_level = 0) == :max_iterations
@test isapprox(getbound(m_2_2_3), 6.13, atol=0.001)
4 changes: 2 additions & 2 deletions examples/BinaryProblems/vehicle_location.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson, Eyob Zewdie
# Copyright 2018, Oscar Dowson, Eyob Zewdie
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -93,7 +93,7 @@ function vehiclelocationmodel(nvehicles, baselocations, requestlocations)
end

ambulancemodel = vehiclelocationmodel(3, [0, 20, 40, 60, 80, 100], 0:2:100)
@test solve(ambulancemodel, max_iterations=50) == :max_iterations
@test solve(ambulancemodel, max_iterations=50, print_level=0) == :max_iterations
@test isapprox(getbound(ambulancemodel), 1700.0, atol=5)
# N = 100
# results = simulate(m, N, [:dispatch, :q, :shift])
Expand Down
9 changes: 5 additions & 4 deletions examples/HydroValleys/hydro_valley_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include(joinpath(dirname(@__FILE__), "hydro_valley.jl"))

# deterministic
deterministic_model = hydrovalleymodel(hasmarkovprice=false,hasstagewiseinflows=false)
SDDP.solve(deterministic_model, max_iterations=10, cut_selection_frequency=1)
SDDP.solve(deterministic_model, max_iterations=10, cut_selection_frequency=1, print_level=0)
@test isapprox(getbound(deterministic_model), 835.0, atol=1e-3)

# stagewise inflows
Expand Down Expand Up @@ -50,7 +50,8 @@ SDDP.solve(worst_case_model,
min = 20,
step = 10,
max = 50
)
),
print_level=0
)

@test isapprox(getbound(worst_case_model), -780.867, atol=1e-3)
Expand All @@ -68,10 +69,10 @@ SDDP.solve(cutselection_model,

# Distributionally robust Optimization
dro_model = hydrovalleymodel(hasmarkovprice=false, riskmeasure=DRO(sqrt(2/3)-1e-6))
SDDP.solve(dro_model, max_iterations = 10)
SDDP.solve(dro_model, max_iterations = 10, print_level=0)
@test isapprox(getbound(dro_model), 835.0, atol=1e-3)
dro_model = hydrovalleymodel(hasmarkovprice=false, riskmeasure=DRO(1/6))
SDDP.solve(dro_model, max_iterations = 20)
SDDP.solve(dro_model, max_iterations = 20, print_level=0)
@test isapprox(getbound(dro_model), 836.695, atol=1e-3)
# (Note) radius ≈ sqrt(2/3), will set all noise probabilities to zero except the worst case noise
# (Why?):
Expand Down
2 changes: 1 addition & 1 deletion examples/Newsvendor/async_newsvendor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ end
m = createmodel(EAVaR(beta = 0.6, lambda = 0.5))


@test_throws Exception SDDP.solve(m, max_iterations=30, solve_type=Asynchronous(slaves=[2]))
@test_throws Exception SDDP.solve(m, print_level=0, max_iterations=30, solve_type=Asynchronous(slaves=[2]))

# slave processes
slaves = workers()
Expand Down
4 changes: 2 additions & 2 deletions examples/Newsvendor/newsvendor.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -95,7 +95,7 @@ news1 = newsvendormodel()
news2 = newsvendormodel(riskmeasure=EAVaR(beta=0.6,lambda=0.5))
@test SDDP.solve(news2,
max_iterations = 50,
print_level = 0,
print_level = 0,
simulation = MonteCarloSimulation(
frequency = 10,
min = 500,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
Volume I (3rd ed.). Bellmont, MA: Athena Scientific.
=#

using SDDP, JuMP, Gurobi, Base.Test
using SDDP, JuMP, Ipopt, Base.Test

m = SDDPModel(
stages = 3,
sense = :Min,
solver = GurobiSolver(OutputFlag=0),
solver = IpoptSolver(print_level=0),
objective_bound = 0.0
) do sp, t
@state(sp, 0 <= x <= 2, x0 == 0)
Expand Down
27 changes: 27 additions & 0 deletions examples/Nonlinear/logs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################

using JuMP, SDDP, Ipopt

m = SDDPModel(
stages = 2,
sense = :Max,
objective_bound = 2.0,
solver = IpoptSolver(print_level=0)
) do sp, t

@state(sp, x′ >= 1, x==1)
if t == 1
@rhsnoise(sp, w=[1,2,3], x′ == w)
@stageobjective(sp, 0.0)
else
@variable(sp, y)
@NLconstraint(sp, y <= log(x))
@stageobjective(sp, y)
end
end
solve(m, max_iterations=10, print_level=0)
@test isapprox(getbound(m), log(6) / 3, atol=1e-4)
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################

using SDDP, JuMP, Gurobi, Base.Test
using SDDP, JuMP, Ipopt, Base.Test

"""
A trivial two-stage stochastic programming problem to work out the
Expand All @@ -13,11 +13,11 @@ and the second-stage evaluates the decision.
"""
function least_squares(data::Vector{Float64},radius=0.0)
m = SDDPModel(
stages = 2,
sense = :Min,
stages = 2,
sense = :Min,
objective_bound = 0.0,
solver=GurobiSolver(OutputFlag=0),
risk_measure = DRO(radius)
solver = IpoptSolver(print_level=0),
risk_measure = DRO(radius)
) do sp, t

@state(sp, x′>=0, x==0)
Expand Down
4 changes: 2 additions & 2 deletions examples/PriceInterpolation/simple_contracting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ end
# dynamic interpolation
m = contracting_example()
srand(123)
SDDP.solve(m, max_iterations = 50, cut_selection_frequency=10, print_level=2)
SDDP.solve(m, max_iterations = 50, cut_selection_frequency=10, print_level=0)
@test SDDP.getbound(m) <= 175.0

# historical simulation
Expand Down Expand Up @@ -153,7 +153,7 @@ results = simulate(m3, 500)
# 5 fixed ribs
m5 = contracting_example(5)
srand(123)
SDDP.solve(m5, max_iterations = 10)
SDDP.solve(m5, max_iterations = 10, print_level=0)
@test SDDP.getbound(m5) <= 150.0

# historical simulation
Expand Down
7 changes: 4 additions & 3 deletions examples/PriceInterpolation/widget_producer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ end
m = widget_producer_example()
srand(123)
status = SDDP.solve(m,
time_limit = 20.0,
time_limit = 5.0,
simulation = MonteCarloSimulation(
frequency = 100, min=100, step=100,max=1000
)
),
print_level = 0
)
@test status == :time_limit
results = simulate(m, 1000, [:C, :S, :price, :s, :d, :b])
@test length(results) == 1000
@test getbound(m) <= 12.0
@test getbound(m) <= 15.0

# SDDP.save!("results.julia", results)
# plt = SDDP.newplot()
Expand Down
4 changes: 2 additions & 2 deletions examples/StochDynamicProgramming.jl/multistock-example.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#==
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -46,7 +46,7 @@ m = SDDPModel(
@stageobjective(sp, (sin(3 * stage) - 1) * sum(control))
end

@time status = SDDP.solve(m, max_iterations = 100,
status = SDDP.solve(m, max_iterations = 100,
print_level = 0)
@test isapprox(SDDP.getbound(m), -4.349, atol=0.01)

Expand Down
4 changes: 2 additions & 2 deletions examples/StochDynamicProgramming.jl/stock-example.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#==
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -39,7 +39,7 @@ m = SDDPModel(
@stageobjective(sp, (sin(3 * stage) - 1) * control)
end

@time status = SDDP.solve(m, max_iterations = 50,
status = SDDP.solve(m, max_iterations = 50,
print_level = 0)
@test isapprox(SDDP.getbound(m), -1.471, atol=0.001)

Expand Down
28 changes: 14 additions & 14 deletions examples/StructDualDynProg.jl/prob5.2_2stages.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand All @@ -10,22 +10,22 @@

using SDDP, JuMP, Base.Test, Clp

n = 4
m = 3
ic = [16, 5, 32, 2]
C = [25, 80, 6.5, 160]
T = [8760, 7000, 1500] / 8760
D2 = [diff([0, 3919, 7329, 10315]) diff([0, 7086, 9004, 11169])]
p2 = [0.9, 0.1]

mod = SDDPModel(
sense = :Min,
stages = 2,
solver = ClpSolver(),
objective_bound = 0,
# noise_probability = [ Float64[], p2, p2 ]
objective_bound = 0
) do sp, t

n = 4
m = 3
ic = [16, 5, 32, 2]
C = [25, 80, 6.5, 160]
T = [8760, 7000, 1500] / 8760
D2 = [diff([0, 3919, 7329, 10315]) diff([0, 7086, 9004, 11169])]
p2 = [0.9, 0.1]


@state(sp, x[i=1:n] >= 0, x0 == 0)

@variables(sp, begin
Expand All @@ -52,10 +52,10 @@ mod = SDDPModel(
end
end

@time status = SDDP.solve(mod,
status = SDDP.solve(mod,
max_iterations = 50,
print_level = 0,
simulation = MonteCarloSimulation(
print_level = 0,
simulation = MonteCarloSimulation(
frequency = 10,
min = 100,
step = 1,
Expand Down
23 changes: 11 additions & 12 deletions examples/StructDualDynProg.jl/prob5.2_3stages.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, Oscar Dowson
# Copyright 2018, Oscar Dowson
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand All @@ -10,20 +10,19 @@

using SDDP, JuMP, Base.Test, Clp

n = 4
m = 3
ic = [16, 5, 32, 2]
C = [25, 80, 6.5, 160]
T = [8760, 7000, 1500] / 8760
D2 = [diff([0, 3919, 7329, 10315]) diff([0, 7086, 9004, 11169])]
p2 = [0.9, 0.1]

mod = SDDPModel(
sense = :Min,
stages = 3,
solver = ClpSolver(),
objective_bound = 0
) do sp, t
n = 4
m = 3
ic = [16, 5, 32, 2]
C = [25, 80, 6.5, 160]
T = [8760, 7000, 1500] / 8760
D2 = [diff([0, 3919, 7329, 10315]) diff([0, 7086, 9004, 11169])]
p2 = [0.9, 0.1]

@state(sp, x[i=1:n] >= 0, x0 == 0)

Expand Down Expand Up @@ -52,10 +51,10 @@ mod = SDDPModel(
end
end

@time status = SDDP.solve(mod,
status = SDDP.solve(mod,
max_iterations = 30,
print_level = 0,
simulation = MonteCarloSimulation(
print_level = 0,
simulation = MonteCarloSimulation(
frequency = 10,
min = 100,
step = 1,
Expand Down
Loading

0 comments on commit be885b2

Please sign in to comment.