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
@@ -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"
@@ -7,29 +7,48 @@
# 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])
This conversation was marked as resolved by odow

This comment has been minimized.

Copy link
@mlubin

mlubin Dec 29, 2018

Member

Why not format as a docstring?

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)
@@ -3,77 +3,70 @@
# 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
"""
example_cannery(; verbose = true)
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 example_cannery(; verbose = true)
plants = ["Seattle", "San-Diego"]
markets = ["New-York", "Chicago", "Topeka"]

function solveCannery(plants, markets, capacity, demand, distance, freight)
numplants = length(plants)
nummarkets = length(markets)
cannery = Model(with_optimizer(solver))
# Capacity and demand in cases.
capacity = [350, 600]
demand = [300, 300, 300]

@variable(cannery, ship[1:numplants, 1:nummarkets] >= 0)
# Distance in thousand miles.
distance = [2.5 1.7 1.8; 2.5 1.8 1.4]

# 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])
# Cost per case per thousand miles.
freight = 90

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

# Minimize transporatation cost
@objective(cannery, Min,
sum(distance[i,j]* freight * ship[i,j] for i in 1:numplants, j in 1:nummarkets))
cannery = Model(with_optimizer(GLPK.Optimizer))

JuMP.optimize!(cannery)
@variable(cannery, ship[1:num_plants, 1:num_markets] >= 0)

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
# 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]
)

# 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]
)

# PARAMETERS
# 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)
)

plants = ["Seattle", "San-Diego"]
markets = ["New-York", "Chicago", "Topeka"]
JuMP.optimize!(cannery)

# capacity and demand in cases
capacitycases = [350, 600]
demandcases = [300, 300, 300]

# 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,50 @@
# 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/.

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()
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.