Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate testing of examples #1712

Merged
merged 19 commits into from
Jan 2, 2019
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
148 changes: 148 additions & 0 deletions examples/Manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[BinDeps]]
deps = ["Compat", "Libdl", "SHA", "URIParser"]
git-tree-sha1 = "12093ca6cdd0ee547c39b1870e0c9c3f154d9ca9"
uuid = "9e28174c-4ba2-5203-b857-d8d62c4213ee"
version = "0.8.10"

[[BinaryProvider]]
deps = ["Libdl", "Pkg", "SHA", "Test"]
git-tree-sha1 = "055eb2690182ebc31087859c3dd8598371d3ef9e"
uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
version = "0.5.3"

[[Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "ec61a16eed883ad0cfa002d7489b3ce6d039bb9a"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "1.4.0"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[Distributed]]
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[GLPK]]
deps = ["BinaryProvider", "Compat", "LinQuadOptInterface"]
git-tree-sha1 = "82f64bfd090a95b7670ed0d7c734220f32983386"
repo-rev = "master"
repo-url = "https://github.com/JuliaOpt/GLPK.jl.git"
uuid = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
version = "0.9.0+"

[[InteractiveUtils]]
deps = ["LinearAlgebra", "Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[Ipopt]]
deps = ["BinDeps", "BinaryProvider", "Compat", "Libdl", "MathOptInterface", "MathProgBase"]
git-tree-sha1 = "ab19d65dcb12fa5efd7707ca64bb12f42ce4ef62"
uuid = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
version = "0.4.5"

[[LibGit2]]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinQuadOptInterface]]
deps = ["Compat", "MathOptInterface"]
git-tree-sha1 = "d6692a09d0424e34c884cd819b8ed47943eefc20"
uuid = "f8899e07-120b-5979-ab1d-7b97bb9e1a48"
version = "0.6.0"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[MathOptInterface]]
deps = ["Compat", "Unicode"]
git-tree-sha1 = "e88e00ffef6e4cdd515b1b3f4be84b55ff664379"
uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
version = "0.8.0"

[[MathProgBase]]
deps = ["Compat"]
git-tree-sha1 = "3bf2e534e635df810e5f4b4f1a8b6de9004a0d53"
uuid = "fdba3010-5040-5b88-9595-932c9decdf73"
version = "0.7.7"

[[Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[Pkg]]
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[SCS]]
deps = ["BinaryProvider", "Compat", "Libdl", "MathOptInterface", "MathProgBase"]
git-tree-sha1 = "f3372fde752302d3cd38d00e2adaf3a7731f7061"
uuid = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
version = "0.5.1"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[[Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[URIParser]]
deps = ["Test", "Unicode"]
git-tree-sha1 = "6ddf8244220dfda2f17539fa8c9de20d6c575b69"
uuid = "30578b45-9adc-5946-b283-645ec420af67"
version = "0.4.0"

[[UUIDs]]
deps = ["Random"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"
4 changes: 4 additions & 0 deletions examples/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[deps]
GLPK = "60bf3e95-4087-53dc-ae20-288a0d20c6a6"
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
SCS = "c946c3f1-0d1f-5ce8-9dea-7daa1f7e2d13"
56 changes: 37 additions & 19 deletions examples/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,47 @@
# An algebraic modeling langauge 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)

m = Model(with_optimizer(Clp.Optimizer))
@objective(model, Max, 5x + 3y)
@constraint(model, 1x + 5y <= 3.0)

@variable(m, 0 <= x <= 2)
@variable(m, 0 <= y <= 30)
if verbose
print(model)
end

@objective(m, Max, 5x + 3y)
@constraint(m, 1x + 5y <= 3.0)
JuMP.optimize!(model)

print(m)
obj_value = JuMP.objective_value(model)
x_value = JuMP.value(x)
y_value = JuMP.value(y)

JuMP.optimize!(m)
if verbose
println("Objective value: ", obj_value)
println("x = ", x_value)
println("y = ", y_value)
end

println("Objective value: ", JuMP.objective_value(m))
println("x = ", JuMP.value(x))
println("y = ", JuMP.value(y))
@test obj_value ≈ 10.6
@test x_value ≈ 2
@test y_value ≈ 0.2
end
102 changes: 46 additions & 56 deletions examples/cannery.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,67 @@
# 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
#############################################################################

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

solver = Clp.Optimizer
#=
JuMP implementation of the cannery problem from Dantzig, Linear Programming and
Extensions, Princeton University Press, Princeton, NJ, 1963.

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
Author: Louis Luangkesorn
Date: January 30, 2015
=#

function solveCannery(plants, markets, capacity, demand, distance, freight)
numplants = length(plants)
nummarkets = length(markets)
cannery = Model(with_optimizer(solver))
function example_cannery(; verbose = true)
plants = ["Seattle", "San-Diego"]
markets = ["New-York", "Chicago", "Topeka"]

@variable(cannery, ship[1:numplants, 1:nummarkets] >= 0)
# Capacity and demand in cases.
capacity = [350, 600]
demand = [300, 300, 300]

# 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])
# Distance in thousand miles.
distance = [2.5 1.7 1.8; 2.5 1.8 1.4]

# Ship at least market demand
@constraint(cannery, demand_con[j in 1:nummarkets],
sum(ship[i,j] for i in 1:numplants) >= demand[j])
# Cost per case per thousand miles.
freight = 90

# Minimize transporatation cost
@objective(cannery, Min,
sum(distance[i,j]* freight * ship[i,j] for i in 1:numplants, j in 1:nummarkets))
num_plants = length(plants)
num_markets = length(markets)

JuMP.optimize!(cannery)
cannery = Model(with_optimizer(GLPK.Optimizer))

term_status = JuMP.termination_status(cannery)
primal_status = JuMP.primal_status(cannery)
is_optimal = term_status == MOI.Optimal

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]

# cost per case per thousand miles
freightcost = 90
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

solveCannery(plants, markets, capacitycases, demandcases, distanceKmiles, freightcost)
@test JuMP.termination_status(cannery) == MOI.OPTIMAL
@test JuMP.primal_status(cannery) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(cannery) == 151200.0
end
Loading