Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Python Ortools Lp Parser
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
kassings
Python Ortools Lp Parser
Commits
30745c3d
Commit
30745c3d
authored
May 07, 2020
by
kassings
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bugfix same variable twice on a line
parent
69a98663
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
2 deletions
+73
-2
ortoolslpparser/ortoolslpparser.py
ortoolslpparser/ortoolslpparser.py
+9
-2
tests/test_basic.py
tests/test_basic.py
+64
-0
No files found.
ortoolslpparser/ortoolslpparser.py
View file @
30745c3d
...
...
@@ -73,6 +73,8 @@ def _parse_declaration(solver: pywraplp.Solver, core_line: str, line_nr: int, va
if
not
re
.
match
(
_REGEXP_SINGLE_VAR_NAME
,
raw_var
.
strip
()):
raise
ValueError
(
"Non-permitted variable name (
\"
%s
\"
) on line %d."
%
(
raw_var
,
line_nr
))
clean_var
=
raw_var
.
strip
()
if
clean_var
in
var_names
:
raise
ValueError
(
"Variable
\"
%s
\"
declared again on line %d."
%
(
clean_var
,
line_nr
))
var_names
.
add
(
clean_var
)
solver
.
IntVar
(
-
solver
.
infinity
(),
solver
.
infinity
(),
clean_var
)
...
...
@@ -85,6 +87,9 @@ def _set_coefficients(solver: pywraplp.Solver, objective_or_constraint, coeffici
if
len
(
remainder
)
==
0
:
raise
ValueError
(
"No variables present in equation on line %d."
%
line_nr
)
# All variables found
var_names_found
=
set
()
running_constant_sum
=
0.0
had_at_least_one_variable
=
False
while
len
(
remainder
)
!=
0
:
...
...
@@ -125,6 +130,9 @@ def _set_coefficients(solver: pywraplp.Solver, objective_or_constraint, coeffici
# Retrieve clean variable name
clean_var
=
var_name_match
.
group
()
var_names
.
add
(
clean_var
)
if
clean_var
in
var_names_found
:
raise
ValueError
(
"Variable
\"
%s
\"
found more than once on line %d."
%
(
clean_var
,
line_nr
))
var_names_found
.
add
(
clean_var
)
solver_var
=
solver
.
LookupVariable
(
clean_var
)
if
solver_var
is
None
:
solver_var
=
solver
.
NumVar
(
-
solver
.
infinity
(),
solver
.
infinity
(),
clean_var
)
...
...
@@ -368,8 +376,7 @@ def parse_lp_file(lp_filename: str, use_solver=pywraplp.Solver.GLOP_LINEAR_PROGR
Read in a linear program defined in a LP file.
The LP specification format is similar to LPSolve 5.1.
For the accepted format, consult:
https://github.com/snkas/python-ortools-lp-parser
For the accepted format, consult the README of the python-ortools-lp-parser repository.
:param lp_filename: .lp file name (i.e. "/path/to/file.lp")
:param use_solver: Solver instance to use (optional)
...
...
tests/test_basic.py
View file @
30745c3d
...
...
@@ -989,3 +989,67 @@ class TestInvalid(unittest.TestCase):
self
.
assertEqual
(
status
,
solver
.
OPTIMAL
)
self
.
assertEqual
(
solver
.
LookupVariable
(
"x1"
).
solution_value
(),
30
)
self
.
assertEqual
(
solver
.
LookupVariable
(
"x2"
).
solution_value
(),
50.1
)
def
test_twice_same_var_objective
(
self
):
write_lp_program
([
"max: x1 + x2 + x1;"
,
"x1 >= 0.0;"
,
"x1 <= 30.9;"
,
"24.4 >= x2 >= 29.8;"
,
])
try
:
ortoolslpparser
.
parse_lp_file
(
TEMP_FILE
)
self
.
assertTrue
(
False
)
except
ValueError
:
self
.
assertTrue
(
True
)
def
test_twice_same_var_constraint
(
self
):
write_lp_program
([
"max: x1 + x2;"
,
"x1 + 2x1 >= 0.0;"
,
"x1 <= 30.9;"
,
"24.4 >= x2 >= 29.8;"
,
])
try
:
ortoolslpparser
.
parse_lp_file
(
TEMP_FILE
)
self
.
assertTrue
(
False
)
except
ValueError
:
self
.
assertTrue
(
True
)
def
test_twice_declaration
(
self
):
write_lp_program
([
"max: x1 + x2;"
,
"x1 >= 0.0;"
,
"x1 <= 30.9;"
,
"24.4 >= x2 >= 29.8;"
,
"int x1;"
,
"int x1;"
])
try
:
ortoolslpparser
.
parse_lp_file
(
TEMP_FILE
)
self
.
assertTrue
(
False
)
except
ValueError
:
self
.
assertTrue
(
True
)
def
test_twice_declaration_same_line
(
self
):
write_lp_program
([
"max: x1 + x2;"
,
"x1 >= 0.0;"
,
"x1 <= 30.9;"
,
"24.4 >= x2 >= 29.8;"
,
"int x2;"
,
"int x1, x1;"
])
try
:
ortoolslpparser
.
parse_lp_file
(
TEMP_FILE
)
self
.
assertTrue
(
False
)
except
ValueError
:
self
.
assertTrue
(
True
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment