Skip to content

Commit

Permalink
Merge pull request #95 from odow/testsets
Browse files Browse the repository at this point in the history
Change to testsets
  • Loading branch information
mlubin authored Mar 28, 2017
2 parents 6d9f039 + a23eb1a commit 4f83c1e
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 362 deletions.
48 changes: 25 additions & 23 deletions test/env.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using Gurobi, MathProgBase
using Gurobi, MathProgBase, Base.Test

env = Gurobi.Env()
@testset "Env" begin
env = Gurobi.Env()

# Test supplying environment manually
# Test supplying environment manually

solver = GurobiSolver(env)
# Check that the env for each model is the same
m1 = MathProgBase.LinearQuadraticModel(solver)
@assert m1.inner.env === env
m2 = MathProgBase.LinearQuadraticModel(solver)
@assert m2.inner.env === env
# Check that finalizer doesn't touch env when manually provided
finalize(m1.inner)
@assert Gurobi.is_valid(env)
solver = GurobiSolver(env)
# Check that the env for each model is the same
m1 = MathProgBase.LinearQuadraticModel(solver)
@test m1.inner.env === env
m2 = MathProgBase.LinearQuadraticModel(solver)
@test m2.inner.env === env
# Check that finalizer doesn't touch env when manually provided
finalize(m1.inner)
@test Gurobi.is_valid(env)

# Test creating environment automatically
# Test creating environment automatically

solver = GurobiSolver()
# Check that the env for each model is different
m3 = MathProgBase.LinearQuadraticModel(solver)
@assert m3.inner.env !== env
m4 = MathProgBase.LinearQuadraticModel(solver)
@assert m4.inner.env !== env
@assert m3.inner.env !== m4.inner.env
# Check that env is finalized with model when not supplied manually
finalize(m3.inner)
@assert !Gurobi.is_valid(m3.inner.env)
solver = GurobiSolver()
# Check that the env for each model is different
m3 = MathProgBase.LinearQuadraticModel(solver)
@test m3.inner.env !== env
m4 = MathProgBase.LinearQuadraticModel(solver)
@test m4.inner.env !== env
@test m3.inner.env !== m4.inner.env
# Check that env is finalized with model when not supplied manually
finalize(m3.inner)
@test !Gurobi.is_valid(m3.inner.env)
end
51 changes: 25 additions & 26 deletions test/lp_01a.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,38 @@
#
# solution: x = 45, y = 6.25, objv = 51.25

using Gurobi
using Gurobi, Base.Test

env = Gurobi.Env()
@testset "LP 01a" begin
env = Gurobi.Env()

method = getparam(env, "Method")
println("method = $method")
method = getparam(env, "Method")
println("method = $method")

model = Gurobi.Model(env, "lp_01", :maximize)
model = Gurobi.Model(env, "lp_01", :maximize)

# add variables
add_cvar!(model, 1.0, 45., Inf) # x
add_cvar!(model, 1.0, 5., Inf) # y
update_model!(model)
# add variables
add_cvar!(model, 1.0, 45., Inf) # x
add_cvar!(model, 1.0, 5., Inf) # y
update_model!(model)

# add constraints
add_constr!(model, [50., 24.], '<', 2400.)
add_constr!(model, [30., 33.], '<', 2100.)
update_model!(model)
# add constraints
add_constr!(model, [50., 24.], '<', 2400.)
add_constr!(model, [30., 33.], '<', 2100.)
update_model!(model)

println(model)
println(model)

# perform optimization
optimize(model)
# perform optimization
optimize(model)

# show results
info = get_optiminfo(model)
println()
println(info)
# show results
info = get_optiminfo(model)
println()
println(info)

sol = get_solution(model)
println("soln = $(sol)")
@test get_solution(model) == [45, 6.25]
@test get_objval(model) == 51.25

objv = get_objval(model)
println("objv = $(objv)")

gc() # test finalizers
gc() # test finalizers
end
53 changes: 28 additions & 25 deletions test/lp_01b.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@
#
# solution: x = 45, y = 6.25, objv = 51.25

using Gurobi
using Gurobi, Base.Test

env = Gurobi.Env()
setparams!(env, Method=2) # using barrier method
@testset "LP 01b" begin

method = getparam(env, "Method")
println("method = $method")
env = Gurobi.Env()
setparams!(env, Method=2) # using barrier method

model = Gurobi.Model(env, "lp_01", :maximize)
method = getparam(env, "Method")
println("method = $method")

# add variables
add_cvars!(model, [1., 1.], [45., 5.], Inf)
update_model!(model)
model = Gurobi.Model(env, "lp_01", :maximize)

# add constraints
add_constrs!(model, Cint[1, 3], Cint[1, 2, 1, 2],
[50., 24., 30., 33.], '<', [2400., 2100.])
update_model!(model)
# add variables
add_cvars!(model, [1., 1.], [45., 5.], Inf)
update_model!(model)

println(model)
# add constraints
add_constrs!(model, Cint[1, 3], Cint[1, 2, 1, 2],
[50., 24., 30., 33.], '<', [2400., 2100.])
update_model!(model)

# perform optimization
optimize(model)
println(model)

# show results
info = get_optiminfo(model)
println()
println(info)
# perform optimization
optimize(model)

sol = get_solution(model)
println("soln = $(sol)")
# show results
info = get_optiminfo(model)
println()
println(info)

objv = get_objval(model)
println("objv = $(objv)")
sol = get_solution(model)
@test sol == [45, 6.25]

gc() # test finalizers
objv = get_objval(model)
@test objv == 51.25

gc() # test finalizers
end
33 changes: 17 additions & 16 deletions test/lp_02.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,29 @@
# x - 1.5y >= 0 (i.e. -x + 1.5 y <= 0)
# 12 x + 8 y <= 1000
# 1000 x + 300 y <= 70000
#
#
# solution: (59.0909, 36.3636)
# objv: 71818.1818
#

using MathProgBase
using Gurobi
using MathProgBase, Gurobi, Base.Test

@testset "LP 02" begin

env = Gurobi.Env()
env = Gurobi.Env()

model = gurobi_model(env;
name="lp_02",
sense=:maximize,
f = [1000., 350.],
A = [-1. 1.5; 12. 8.; 1000. 300.],
b = [0., 1000., 70000.],
lb = [0., 30.])
model = gurobi_model(env;
name="lp_02",
sense=:maximize,
f = [1000., 350.],
A = [-1. 1.5; 12. 8.; 1000. 300.],
b = [0., 1000., 70000.],
lb = [0., 30.])

println(model)
println(model)

optimize(model)
optimize(model)

println()
println("soln = $(get_solution(model))")
println("objv = $(get_objval(model))")
@test_approx_eq_eps get_solution(model) [59.0909, 36.3636] 1e-4
@test_approx_eq_eps get_objval(model) 71818.1818 1e-4
end
82 changes: 40 additions & 42 deletions test/lp_03.jl
Original file line number Diff line number Diff line change
@@ -1,58 +1,56 @@
# Test get/set objective coefficients in LP

using Gurobi
using Base.Test
using Gurobi, Base.Test

env = Gurobi.Env()
@testset "LP 03" begin
env = Gurobi.Env()

# original model
#
# maximize 2x + 2y
#
# s.t. 0.2 <= x, y <= 1
#
# original model
#
# maximize 2x + 2y
#
# s.t. 0.2 <= x, y <= 1
#

model = gurobi_model(env;
name="lp_03",
sense=:maximize,
f=[2.0, 2.0],
lb=[0.2, 0.2],
ub=[1.0, 1.0])
model = gurobi_model(env;
name="lp_03",
sense=:maximize,
f=[2.0, 2.0],
lb=[0.2, 0.2],
ub=[1.0, 1.0])

println(model)
println(model)

lb_ = lowerbounds(model)
ub_ = upperbounds(model)
c_ = objcoeffs(model)
lb_ = lowerbounds(model)
ub_ = upperbounds(model)
c_ = objcoeffs(model)

@test lb_ == [0.2, 0.2]
@test ub_ == [1.0, 1.0]
@test c_ == [2.0, 2.0]
@test lb_ == [0.2, 0.2]
@test ub_ == [1.0, 1.0]
@test c_ == [2.0, 2.0]

optimize(model)
optimize(model)

println()
println("soln = $(get_solution(model))")
println("objv = $(get_objval(model))")
@test get_solution(model) == [1.0, 1.0]
@test get_objval(model) == 4.0

# change objective (warm start)
#
# maximize x - y
#
# s.t. 0.2 <= x, y <= 1
#

# change objective (warm start)
#
# maximize x - y
#
# s.t. 0.2 <= x, y <= 1
#
set_objcoeffs!(model, [1, -1])
update_model!(model)

set_objcoeffs!(model, [1, -1])
update_model!(model)
c_ = objcoeffs(model)
@test c_ == [1.0, -1.0]

c_ = objcoeffs(model)
@test c_ == [1.0, -1.0]
optimize(model)

optimize(model)
@test get_solution(model) == [1.0, 0.2]
@test get_objval(model) == 0.8

println()
println("soln = $(get_solution(model))")
println("objv = $(get_objval(model))")

gc()
gc()
end
Loading

0 comments on commit 4f83c1e

Please sign in to comment.