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)
@@ -3,44 +3,47 @@
# An algebraic modeling langauge for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# corr_sdp.jl
#
# Given three random variables A,B,C and given bounds on two of the three
# correlation coefficients:
# -0.2 <= ρ_AB <= -0.1
# 0.4 <= ρ_BC <= 0.5
# We can use the following property of the correlations:
# [ 1 ρ_AB ρ_AC ]
# [ ρ_AB 1 ρ_BC ] ≽ 0
# [ ρ_AC ρ_BC 1 ]
# To determine bounds on ρ_AC by solving a SDP
#############################################################################

using JuMP, CSDP
using JuMP, SCS, Test

"""
example_corr_sdp()
Given three random variables A,B,C and given bounds on two of the three
correlation coefficients:
-0.2 <= ρ_AB <= -0.1
0.4 <= ρ_BC <= 0.5
m = Model(with_optimizer(CSDP.Optimizer))
We can use the following property of the correlations to determine bounds on
ρ_AC by solving a SDP:
| 1 ρ_AB ρ_AC |
| ρ_AB 1 ρ_BC | ≽ 0
| ρ_AC ρ_BC 1 |
"""
function example_corr_sdp()
model = Model(with_optimizer(SCS.Optimizer, verbose = 0))
@variable(model, X[1:3, 1:3], PSD)

@variable(m, X[1:3,1:3], PSD)
# Diagonal is 1s
@constraint(model, X[1, 1] == 1)
@constraint(model, X[2, 2] == 1)
@constraint(model, X[3, 3] == 1)

# Diagonal is 1s
@constraint(m, X[1,1] == 1)
@constraint(m, X[2,2] == 1)
@constraint(m, X[3,3] == 1)
# Bounds on the known correlations
@constraint(model, X[1, 2] >= -0.2)
@constraint(model, X[1, 2] <= -0.1)
@constraint(model, X[2, 3] >= 0.4)
@constraint(model, X[2, 3] <= 0.5)

# Bounds on the known correlations
@constraint(m, X[1,2] >= -0.2)
@constraint(m, X[1,2] <= -0.1)
@constraint(m, X[2,3] >= 0.4)
@constraint(m, X[2,3] <= 0.5)
# Find upper bound
@objective(model, Max, X[1, 3])
JuMP.optimize!(model)
@test JuMP.value(X[1, 3]) ≈ 0.87195 atol = 1e-4

# Find upper bound
@objective(m, Max, X[1,3])
JuMP.optimize!(m)
println("Maximum value is ", JuMP.value.(X)[1,3])
@assert +0.8719 <= JuMP.value.(X)[1,3] <= +0.8720
# Find lower bound
@objective(model, Min, X[1, 3])
JuMP.optimize!(model)
@test JuMP.value(X[1, 3])-0.978 atol = 1e-3
end

# Find lower bound
@objective(m, Min, X[1,3])
JuMP.optimize!(m)
println("Minimum value is ", JuMP.value.(X)[1,3])
@assert -0.9779 >= JuMP.value.(X)[1,3] >= -0.9799
example_corr_sdp()
File renamed without changes.
@@ -7,37 +7,40 @@
# An algebraic modeling langauge for Julia
# See http://github.com/JuliaOpt/JuMP.jl
#############################################################################
# knapsack.jl
#
# Solves a simple knapsack problem:
# max sum(p_j x_j)
# st sum(w_j x_j) <= C
# x binary
#############################################################################

using JuMP, GLPK, LinearAlgebra

# Maximization problem
m = Model(with_optimizer(GLPK.Optimizer))

@variable(m, x[1:5], Bin)

profit = [ 5, 3, 2, 7, 4 ]
weight = [ 2, 8, 4, 2, 5 ]
capacity = 10

# Objective: maximize profit
@objective(m, Max, dot(profit, x))

# Constraint: can carry all
@constraint(m, dot(weight, x) <= capacity)

# Solve problem using MIP solver
JuMP.optimize!(m)

println("Objective is: ", JuMP.objective_value(m))
println("Solution is:")
for i = 1:5
print("x[$i] = ", JuMP.value(x[i]))
println(", p[$i]/w[$i] = ", profit[i]/weight[i])
using JuMP, GLPK, Test

"""
example_knapsack(; verbose = true)
Formulate and solve a simple knapsack problem:
max sum(p_j x_j)
st sum(w_j x_j) <= C
x binary
"""
function example_knapsack(; verbose = true)
profit = [5, 3, 2, 7, 4]
weight = [2, 8, 4, 2, 5]
capacity = 10
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model, x[1:5], Bin)
# Objective: maximize profit
@objective(model, Max, profit' * x)
# Constraint: can carry all
@constraint(model, weight' * x <= capacity)
# Solve problem using MIP solver
JuMP.optimize!(model)
if verbose
println("Objective is: ", JuMP.objective_value(model))
println("Solution is:")
for i in 1:5
print("x[$i] = ", JuMP.value(x[i]))
println(", p[$i]/w[$i] = ", profit[i] / weight[i])
end
end
@test JuMP.termination_status(model) == MOI.OPTIMAL
@test JuMP.primal_status(model) == MOI.FEASIBLE_POINT
@test JuMP.objective_value(model) == 16.0
end

example_knapsack(verbose = false)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.