Skip to content

Add LP Base.read! and tests #1713

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

Merged
merged 12 commits into from
Jan 24, 2022
477 changes: 474 additions & 3 deletions src/FileFormats/LP/LP.jl

Large diffs are not rendered by default.

148 changes: 145 additions & 3 deletions test/FileFormats/LP/LP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,152 @@ minobjective: 1.0*x*x
)
end

function test_read()
function test_read_example_lo1()
model = LP.Model()
exception = ErrorException("read! is not implemented for LP files.")
@test_throws exception MOI.read_from_file(model, LP_TEST_FILE)
MOI.read_from_file(model, joinpath(@__DIR__, "models", "example_lo1.lp"))
@test MOI.get(model, MOI.NumberOfVariables()) == 4
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test (MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) in
constraints
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
constraints
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
constraints
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
return nothing
end

function test_read_model2()
model = LP.Model()
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model2.lp"))
@test MOI.get(model, MOI.NumberOfVariables()) == 8
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
constraints
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
constraints
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
@test (MathOptInterface.VariableIndex, MathOptInterface.Integer) in
constraints
@test (MathOptInterface.VariableIndex, MathOptInterface.ZeroOne) in
constraints
# Adicionar testes dos bounds de V8
@test MOI.get(model, MOI.VariableName(), MOI.VariableIndex(8)) == "V8"
@test model.variables.lower[8] == -Inf
@test model.variables.upper[8] == -3
obj_type = MOI.get(model, MOI.ObjectiveFunctionType())
obj_func = MOI.get(model, MOI.ObjectiveFunction{obj_type}())
@test obj_func.constant == 2.5
return nothing
end

function test_read_model1_tricky()
model = LP.Model()
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model1_tricky.lp"))
@test MOI.get(model, MOI.NumberOfVariables()) == 8
var_names = MOI.get.(model, MOI.VariableName(), MOI.VariableIndex.(1:8))
@test Set(var_names) ==
Set(["Var4", "V5", "V1", "V2", "V3", "V6", "V7", "V8"])
return nothing
end

function test_read_corrupt()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "corrupt.lp"),
)
return nothing
end

function test_read_invalid_variable_name()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_variable_name.lp"),
)
return nothing
end

function test_read_invalid_affine_term_objective()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_affine_term_objective.lp"),
)
return nothing
end

function test_read_invalid_affine_term_constraint()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_affine_term_constraint.lp"),
)
return nothing
end

function test_read_invalid_sos_set()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_sos_set.lp"),
)
return nothing
end

function test_read_invalid_sos_constraint()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_sos_constraint.lp"),
)
return nothing
end

function test_read_invalid_bound()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_bound.lp"),
)
return nothing
end

function test_read_invalid_constraint()
model = LP.Model()
@test_throws ErrorException MOI.read_from_file(
model,
joinpath(@__DIR__, "models", "invalid_constraint.lp"),
)
return nothing
end

function test_read_model1()
model = LP.Model()
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model1.lp"))
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
constraints
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
constraints
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
@test (MathOptInterface.VariableIndex, MathOptInterface.Integer) in
constraints
@test (MathOptInterface.VariableIndex, MathOptInterface.ZeroOne) in
constraints
@test (
MathOptInterface.VectorOfVariables,
MathOptInterface.SOS1{Float64},
) in constraints
@test (
MathOptInterface.VectorOfVariables,
MathOptInterface.SOS2{Float64},
) in constraints
return nothing
end

function runtests()
Expand Down
7 changes: 7 additions & 0 deletions test/FileFormats/LP/models/corrupt.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
max
obj: 1 x
Subject To
Bounds
x free
End
C: 1 x <= 2
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/example_lo1.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 x1 + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 <= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/invalid_affine_term_constraint.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 x1 + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + - 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 <= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/invalid_affine_term_objective.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 x1 + + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 <= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/invalid_bound.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 x1 + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 >= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/invalid_constraint.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 x1 + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + c2: + 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 <= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
23 changes: 23 additions & 0 deletions test/FileFormats/LP/models/invalid_sos_constraint.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Maximize
obj: -1 V4 + 1 V5
Subject To
CON1: 1 V1 >= 0.0
CON2: 1 V2 >= 2.0
CON3: 1 V3 <= 2.5
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
csos1: S1:: V1:1 V3:2 V5:3
csos2: S2::
Bounds
-inf <= V1 <= 3
-inf <= V2 <= 3
-inf <= V3 <= 3
5.5 <= V4 <= +inf
0 <= V5 <= 1
0 <= V6 <= 1
0 <= V7 <= 1
0 <= V8 <= 1
General
V4
Binary
V8
End
23 changes: 23 additions & 0 deletions test/FileFormats/LP/models/invalid_sos_set.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Maximize
obj: -1 V4 + 1 V5
Subject To
CON1: 1 V1 >= 0.0
CON2: 1 V2 >= 2.0
CON3: 1 V3 <= 2.5
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
csos1: S1:: V1:1 V3:2 V5:3
csos2: S3:: V2:2 V4:1 V5:2.5
Bounds
-inf <= V1 <= 3
-inf <= V2 <= 3
-inf <= V3 <= 3
5.5 <= V4 <= +inf
0 <= V5 <= 1
0 <= V6 <= 1
0 <= V7 <= 1
0 <= V8 <= 1
General
V4
Binary
V8
End
13 changes: 13 additions & 0 deletions test/FileFormats/LP/models/invalid_variable_name.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\ File: lo1.lp
maximize
obj: 3 1x1 + x2 + 5 x3 + x4
subject to
c1: 3 x1 + x2 + 2 x3 = 30
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
c3: 2 x2 + 3 x4 <= 25
bounds
0 <= x1 <= +infinity
0 <= x2 <= 10
0 <= x3 <= +infinity
0 <= x4 <= +infinity
end
23 changes: 23 additions & 0 deletions test/FileFormats/LP/models/model1.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Maximize
obj: -1 V4 + 1 V5
Subject To
CON1: 1 V1 >= 0.0
CON2: 1 V2 >= 2.0
CON3: 1 V3 <= 2.5
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
csos1: S1:: V1:1 V3:2 V5:3
csos2: S2:: V2:2 V4:1 V5:2.5
Bounds
-inf <= V1 <= 3
-inf <= V2 <= 3
-inf <= V3 <= 3
5.5 <= V4 <= +inf
0 <= V5 <= 1
0 <= V6 <= 1
0 <= V7 <= 1
0 <= V8 <= 1
General
V4
Binary
V8
End
30 changes: 30 additions & 0 deletions test/FileFormats/LP/models/model1_tricky.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
\ a comment to begin with
\ weirdly, V ar4 and Va r4 are the same variable...
\ we're going to disallow this and say the file is corrupt
\ its a terrible idea
Max \ this problem is a maximisation!

obj: -1 Var4
+ 1 V5
Subject To
CON1: 1 V1 >= 0.0
1 V2 >= 2.0 \ not named
CON3: 1 V3 <= 2.5
CON4: 1 V5 + 1 V6 \ split constraint. we know it hasn't ended as missing operator
+ 1 V7 <= 1.0
Bounds
-inf <= V1 <= 3
V2 <= 3
V3 >= -3
5.5 <= Var4 <= +inf
V5 = 1 \ fixed variable

V6 free
0 <= V7 < 1 \ stupidly allow < as <=
0 <= V8 <= 1
General
Var4 V5 \ integer variables can be listed (MOSEK)
V6 \ or each new line
Binary
V8
End
21 changes: 21 additions & 0 deletions test/FileFormats/LP/models/model2.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Minimize
obj: - 2 - 1 V4 + 1 V5 + 3 + 2 - 0.5
Subject To
CON1: 1 V1 >= 0.0
CON2: 1 V2 >= 2.0
CON3: 1 V3 <= 2.5
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
Bounds
-inf <= V1 <= 3
-inf <= V2 <= 3
-inf <= V3 <= 3
5.5 <= V4 <= +inf
0 <= V5 <= 1
0 <= V6 <= 1
0 <= V7 <= 1
V8 <= -3
General
V4
Binary
V8
End