Skip to content

Commit 9ef6efe

Browse files
Add LP Base.read! and tests (#1713)
1 parent 5f8846c commit 9ef6efe

14 files changed

Lines changed: 824 additions & 6 deletions

src/FileFormats/LP/LP.jl

Lines changed: 474 additions & 3 deletions
Large diffs are not rendered by default.

test/FileFormats/LP/LP.jl

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,152 @@ minobjective: 1.0*x*x
234234
)
235235
end
236236

237-
function test_read()
237+
function test_read_example_lo1()
238238
model = LP.Model()
239-
exception = ErrorException("read! is not implemented for LP files.")
240-
@test_throws exception MOI.read_from_file(model, LP_TEST_FILE)
239+
MOI.read_from_file(model, joinpath(@__DIR__, "models", "example_lo1.lp"))
240+
@test MOI.get(model, MOI.NumberOfVariables()) == 4
241+
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
242+
@test (MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) in
243+
constraints
244+
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
245+
constraints
246+
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
247+
constraints
248+
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
249+
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
250+
return nothing
251+
end
252+
253+
function test_read_model2()
254+
model = LP.Model()
255+
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model2.lp"))
256+
@test MOI.get(model, MOI.NumberOfVariables()) == 8
257+
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
258+
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
259+
constraints
260+
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
261+
constraints
262+
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
263+
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
264+
@test (MathOptInterface.VariableIndex, MathOptInterface.Integer) in
265+
constraints
266+
@test (MathOptInterface.VariableIndex, MathOptInterface.ZeroOne) in
267+
constraints
268+
# Adicionar testes dos bounds de V8
269+
@test MOI.get(model, MOI.VariableName(), MOI.VariableIndex(8)) == "V8"
270+
@test model.variables.lower[8] == -Inf
271+
@test model.variables.upper[8] == -3
272+
obj_type = MOI.get(model, MOI.ObjectiveFunctionType())
273+
obj_func = MOI.get(model, MOI.ObjectiveFunction{obj_type}())
274+
@test obj_func.constant == 2.5
275+
return nothing
276+
end
277+
278+
function test_read_model1_tricky()
279+
model = LP.Model()
280+
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model1_tricky.lp"))
281+
@test MOI.get(model, MOI.NumberOfVariables()) == 8
282+
var_names = MOI.get.(model, MOI.VariableName(), MOI.VariableIndex.(1:8))
283+
@test Set(var_names) ==
284+
Set(["Var4", "V5", "V1", "V2", "V3", "V6", "V7", "V8"])
285+
return nothing
286+
end
287+
288+
function test_read_corrupt()
289+
model = LP.Model()
290+
@test_throws ErrorException MOI.read_from_file(
291+
model,
292+
joinpath(@__DIR__, "models", "corrupt.lp"),
293+
)
294+
return nothing
295+
end
296+
297+
function test_read_invalid_variable_name()
298+
model = LP.Model()
299+
@test_throws ErrorException MOI.read_from_file(
300+
model,
301+
joinpath(@__DIR__, "models", "invalid_variable_name.lp"),
302+
)
303+
return nothing
304+
end
305+
306+
function test_read_invalid_affine_term_objective()
307+
model = LP.Model()
308+
@test_throws ErrorException MOI.read_from_file(
309+
model,
310+
joinpath(@__DIR__, "models", "invalid_affine_term_objective.lp"),
311+
)
312+
return nothing
313+
end
314+
315+
function test_read_invalid_affine_term_constraint()
316+
model = LP.Model()
317+
@test_throws ErrorException MOI.read_from_file(
318+
model,
319+
joinpath(@__DIR__, "models", "invalid_affine_term_constraint.lp"),
320+
)
321+
return nothing
322+
end
323+
324+
function test_read_invalid_sos_set()
325+
model = LP.Model()
326+
@test_throws ErrorException MOI.read_from_file(
327+
model,
328+
joinpath(@__DIR__, "models", "invalid_sos_set.lp"),
329+
)
330+
return nothing
331+
end
332+
333+
function test_read_invalid_sos_constraint()
334+
model = LP.Model()
335+
@test_throws ErrorException MOI.read_from_file(
336+
model,
337+
joinpath(@__DIR__, "models", "invalid_sos_constraint.lp"),
338+
)
339+
return nothing
340+
end
341+
342+
function test_read_invalid_bound()
343+
model = LP.Model()
344+
@test_throws ErrorException MOI.read_from_file(
345+
model,
346+
joinpath(@__DIR__, "models", "invalid_bound.lp"),
347+
)
348+
return nothing
349+
end
350+
351+
function test_read_invalid_constraint()
352+
model = LP.Model()
353+
@test_throws ErrorException MOI.read_from_file(
354+
model,
355+
joinpath(@__DIR__, "models", "invalid_constraint.lp"),
356+
)
357+
return nothing
358+
end
359+
360+
function test_read_model1()
361+
model = LP.Model()
362+
MOI.read_from_file(model, joinpath(@__DIR__, "models", "model1.lp"))
363+
constraints = MOI.get(model, MOI.ListOfConstraintTypesPresent())
364+
@test (MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) in
365+
constraints
366+
@test (MOI.ScalarAffineFunction{Float64}, MOI.LessThan{Float64}) in
367+
constraints
368+
@test (MOI.VariableIndex, MOI.GreaterThan{Float64}) in constraints
369+
@test (MOI.VariableIndex, MOI.Interval{Float64}) in constraints
370+
@test (MathOptInterface.VariableIndex, MathOptInterface.Integer) in
371+
constraints
372+
@test (MathOptInterface.VariableIndex, MathOptInterface.ZeroOne) in
373+
constraints
374+
@test (
375+
MathOptInterface.VectorOfVariables,
376+
MathOptInterface.SOS1{Float64},
377+
) in constraints
378+
@test (
379+
MathOptInterface.VectorOfVariables,
380+
MathOptInterface.SOS2{Float64},
381+
) in constraints
382+
return nothing
241383
end
242384

243385
function runtests()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
max
2+
obj: 1 x
3+
Subject To
4+
Bounds
5+
x free
6+
End
7+
C: 1 x <= 2
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ File: lo1.lp
2+
maximize
3+
obj: 3 x1 + x2 + 5 x3 + x4
4+
subject to
5+
c1: 3 x1 + x2 + 2 x3 = 30
6+
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
7+
c3: 2 x2 + 3 x4 <= 25
8+
bounds
9+
0 <= x1 <= +infinity
10+
0 <= x2 <= 10
11+
0 <= x3 <= +infinity
12+
0 <= x4 <= +infinity
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ File: lo1.lp
2+
maximize
3+
obj: 3 x1 + x2 + 5 x3 + x4
4+
subject to
5+
c1: 3 x1 + x2 + 2 x3 = 30
6+
c2: 2 x1 + x2 + - 3 x3 + x4 >= 15
7+
c3: 2 x2 + 3 x4 <= 25
8+
bounds
9+
0 <= x1 <= +infinity
10+
0 <= x2 <= 10
11+
0 <= x3 <= +infinity
12+
0 <= x4 <= +infinity
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ File: lo1.lp
2+
maximize
3+
obj: 3 x1 + + x2 + 5 x3 + x4
4+
subject to
5+
c1: 3 x1 + x2 + 2 x3 = 30
6+
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
7+
c3: 2 x2 + 3 x4 <= 25
8+
bounds
9+
0 <= x1 <= +infinity
10+
0 <= x2 <= 10
11+
0 <= x3 <= +infinity
12+
0 <= x4 <= +infinity
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ File: lo1.lp
2+
maximize
3+
obj: 3 x1 + x2 + 5 x3 + x4
4+
subject to
5+
c1: 3 x1 + x2 + 2 x3 = 30
6+
c2: 2 x1 + x2 + 3 x3 + x4 >= 15
7+
c3: 2 x2 + 3 x4 <= 25
8+
bounds
9+
0 <= x1 <= +infinity
10+
0 >= x2 <= 10
11+
0 <= x3 <= +infinity
12+
0 <= x4 <= +infinity
13+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\ File: lo1.lp
2+
maximize
3+
obj: 3 x1 + x2 + 5 x3 + x4
4+
subject to
5+
c1: 3 x1 + x2 + 2 x3 = 30
6+
c2: 2 x1 + x2 + c2: + 3 x3 + x4 >= 15
7+
c3: 2 x2 + 3 x4 <= 25
8+
bounds
9+
0 <= x1 <= +infinity
10+
0 <= x2 <= 10
11+
0 <= x3 <= +infinity
12+
0 <= x4 <= +infinity
13+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Maximize
2+
obj: -1 V4 + 1 V5
3+
Subject To
4+
CON1: 1 V1 >= 0.0
5+
CON2: 1 V2 >= 2.0
6+
CON3: 1 V3 <= 2.5
7+
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
8+
csos1: S1:: V1:1 V3:2 V5:3
9+
csos2: S2::
10+
Bounds
11+
-inf <= V1 <= 3
12+
-inf <= V2 <= 3
13+
-inf <= V3 <= 3
14+
5.5 <= V4 <= +inf
15+
0 <= V5 <= 1
16+
0 <= V6 <= 1
17+
0 <= V7 <= 1
18+
0 <= V8 <= 1
19+
General
20+
V4
21+
Binary
22+
V8
23+
End
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Maximize
2+
obj: -1 V4 + 1 V5
3+
Subject To
4+
CON1: 1 V1 >= 0.0
5+
CON2: 1 V2 >= 2.0
6+
CON3: 1 V3 <= 2.5
7+
CON4: 1 V5 + 1 V6 + 1 V7 <= 1.0
8+
csos1: S1:: V1:1 V3:2 V5:3
9+
csos2: S3:: V2:2 V4:1 V5:2.5
10+
Bounds
11+
-inf <= V1 <= 3
12+
-inf <= V2 <= 3
13+
-inf <= V3 <= 3
14+
5.5 <= V4 <= +inf
15+
0 <= V5 <= 1
16+
0 <= V6 <= 1
17+
0 <= V7 <= 1
18+
0 <= V8 <= 1
19+
General
20+
V4
21+
Binary
22+
V8
23+
End

0 commit comments

Comments
 (0)