Commit 30745c3d authored by kassings's avatar kassings

Bugfix same variable twice on a line

parent 69a98663
......@@ -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)
......
......@@ -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)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment