Skip to content
Permalink
Browse files

Automate testing of examples (#1712)

* Tidy basic.jl

* Tidy cannery.jl

* Tidy optcontrol.jl

* Tidy rosenbrock.jl

* Add CI tests for examples

* Add minellipse.jl and qcp.jl

* Add docstrings to examples

* Remove Manifest.toml

* Update examples

* Update sudoku.jl

* Update urbanplan.jl

* Update robust_uncertainty.jl

* Update cluster.jl and other examples

* Update examples

* More updates

* Add diet.jl

* Add steelT3 and multi

* Add prod.jl

* Update copyright notice and generate EXAMPLES programatically
  • Loading branch information...
odow committed Jan 2, 2019
1 parent a7fe2c1 commit 9483613d981df2c6090e294c3b5bae1ebd149b4f
@@ -5,3 +5,4 @@
*.sublime*
*.opf
*.cov
examples/Manifest.toml
@@ -29,3 +29,9 @@ jobs:
- julia --project=docs -e 'using Pkg; Pkg.instantiate(); Pkg.add(PackageSpec(path=pwd()))'
- julia --project=docs --color=yes docs/make.jl
after_success: skip
- stage: "Examples"
julia: 1.0
os: linux
script:
- julia --project=examples -e 'using Pkg; Pkg.instantiate(); Pkg.add(PackageSpec(path=pwd()))'
- julia --project=examples --color=yes examples/run_examples.jl
@@ -0,0 +1,4 @@
[deps]
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
@@ -4,32 +4,51 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#############################################################################
# JuMP
# An algebraic modeling langauge for Julia
# An algebraic modeling language for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# basic.jl
#
# Solves a simple LP:
# max 5x + 3y
# st 1x + 5y <= 3
# 0 <= x <= 2
# 0 <= y <= 30
#############################################################################

using JuMP, Clp
using JuMP, GLPK, Test

"""
example_basic([; verbose = true])
Formulate and solve a simple LP:
max 5x + 3y
st 1x + 5y <= 3
0 <= x <= 2
0 <= y <= 30
If `verbose = true`, print the model and the solution.
"""
function example_basic(; verbose = true)
model = Model(with_optimizer(GLPK.Optimizer))

@variable(model, 0 <= x <= 2)
@variable(model, 0 <= y <= 30)

@objective(model, Max, 5x + 3y)
@constraint(model, 1x + 5y <= 3.0)

m = Model(with_optimizer(Clp.Optimizer))
if verbose
print(model)
end

@variable(m, 0 <= x <= 2)
@variable(m, 0 <= y <= 30)
JuMP.optimize!(model)

@objective(m, Max, 5x + 3y)
@constraint(m, 1x + 5y <= 3.0)
obj_value = JuMP.objective_value(model)
x_value = JuMP.value(x)
y_value = JuMP.value(y)

print(m)
if verbose
println("Objective value: ", obj_value)
println("x = ", x_value)
println("y = ", y_value)
end

JuMP.optimize!(m)
@test obj_value ≈ 10.6
@test x_value ≈ 2
@test y_value ≈ 0.2
end

println("Objective value: ", JuMP.objective_value(m))
println("x = ", JuMP.value(x))
println("y = ", JuMP.value(y))
example_basic(verbose = false)
@@ -1,79 +1,75 @@
#############################################################################
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# 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/.
#
# JuMP implementation of the cannery problem
# Dantzig, Linear Programming and Extensions,
# Princeton University Press, Princeton, NJ, 1963.
#
# Author: Louis Luangkesorn
# Date January 30, 2015
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################

using JuMP, Clp
using JuMP, GLPK, Test
const MOI = JuMP.MathOptInterface

solver = Clp.Optimizer

function PrintSolution(is_optimal, plants, markets, ship)
println("RESULTS:")
if is_optimal
for i in 1:length(plants)
for j in 1:length(markets)
println(" $(plants[i]) $(markets[j]) = $(JuMP.value(ship[i,j]))")
end
end
else
println("The solver did not find an optimal solution.")
end
end
"""
example_cannery(; verbose = true)
function solveCannery(plants, markets, capacity, demand, distance, freight)
numplants = length(plants)
nummarkets = length(markets)
cannery = Model(with_optimizer(solver))
JuMP implementation of the cannery problem from Dantzig, Linear Programming and
Extensions, Princeton University Press, Princeton, NJ, 1963.
@variable(cannery, ship[1:numplants, 1:nummarkets] >= 0)
Author: Louis Luangkesorn
Date: January 30, 2015
"""
function example_cannery(; verbose = true)
plants = ["Seattle", "San-Diego"]
markets = ["New-York", "Chicago", "Topeka"]

# Ship no more than plant capacity
@constraint(cannery, capacity_con[i in 1:numplants],
sum(ship[i,j] for j in 1:nummarkets) <= capacity[i])
# Capacity and demand in cases.
capacity = [350, 600]
demand = [300, 300, 300]

# Ship at least market demand
@constraint(cannery, demand_con[j in 1:nummarkets],
sum(ship[i,j] for i in 1:numplants) >= demand[j])
# Distance in thousand miles.
distance = [2.5 1.7 1.8; 2.5 1.8 1.4]

# Minimize transporatation cost
@objective(cannery, Min,
sum(distance[i,j]* freight * ship[i,j] for i in 1:numplants, j in 1:nummarkets))
# Cost per case per thousand miles.
freight = 90

JuMP.optimize!(cannery)
num_plants = length(plants)
num_markets = length(markets)

term_status = JuMP.termination_status(cannery)
primal_status = JuMP.primal_status(cannery)
is_optimal = term_status == MOI.Optimal
cannery = Model(with_optimizer(GLPK.Optimizer))

PrintSolution(is_optimal, plants, markets, ship)
return is_optimal
end
@variable(cannery, ship[1:num_plants, 1:num_markets] >= 0)

# Ship no more than plant capacity
@constraint(cannery, capacity_con[i in 1:num_plants],
sum(ship[i,j] for j in 1:num_markets) <= capacity[i]
)

# PARAMETERS
# Ship at least market demand
@constraint(cannery, demand_con[j in 1:num_markets],
sum(ship[i,j] for i in 1:num_plants) >= demand[j]
)

plants = ["Seattle", "San-Diego"]
markets = ["New-York", "Chicago", "Topeka"]
# Minimize transporatation cost
@objective(cannery, Min, sum(distance[i, j] * freight * ship[i, j]
for i in 1:num_plants, j in 1:num_markets)
)

# capacity and demand in cases
capacitycases = [350, 600]
demandcases = [300, 300, 300]
JuMP.optimize!(cannery)

# distance in thousand miles
distanceKmiles = [2.5 1.7 1.8;
2.5 1.8 1.4]
if verbose
println("RESULTS:")
for i in 1:num_plants
for j in 1:num_markets
println(" $(plants[i]) $(markets[j]) = $(JuMP.value(ship[i, j]))")
end
end
end

# cost per case per thousand miles
freightcost = 90
@test JuMP.termination_status(cannery) == MOI.OPTIMAL
@test JuMP.primal_status(cannery) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(cannery) == 151200.0
end

solveCannery(plants, markets, capacitycases, demandcases, distanceKmiles, freightcost)
example_cannery(verbose = false)
@@ -0,0 +1,55 @@
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors
# 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/.
#############################################################################
# JuMP
# An algebraic modeling language for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################

using JuMP, Ipopt, Test
const MOI = JuMP.MathOptInterface

"""
clnlbeam
Based on AMPL model
Copyright (C) 2001 Princeton University
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided that
the above copyright notice appear in all copies and that the copyright notice
and this permission notice appear in all supporting documentation.
Source: H. Maurer and H.D. Mittelman, "The non-linear beam via optimal control
with bound state variables", Optimal Control Applications and Methods 12, pp.
19-31, 1991.
"""
function example_clnlbeam()
N = 1000
h = 1/N
alpha = 350
model = Model(with_optimizer(Ipopt.Optimizer, print_level = 0))
@variables(model, begin
-1 <= t[1:(N + 1)] <= 1
-0.05 <= x[1:(N + 1)] <= 0.05
u[1:(N + 1)]
end)
@NLobjective(model, Min, sum(0.5 * h * (u[i + 1]^2 + u[i]^2) +
0.5 * alpha * h * (cos(t[i + 1]) + cos(t[i])) for i in 1:N)
)
@NLconstraint(model, [i = 1:N],
x[i + 1] - x[i] - 0.5 * h * (sin(t[i + 1]) + sin(t[i])) == 0
)
@constraint(model, [i = 1:N],
t[i + 1] - t[i] - 0.5 * h * u[i + 1] - 0.5 * h * u[i] == 0
)
JuMP.optimize!(model)

@test JuMP.termination_status(model) == MOI.LOCALLY_SOLVED
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(model) ≈ 350.0
end

example_clnlbeam()

0 comments on commit 9483613

Please sign in to comment.
You can’t perform that action at this time.