def test_objective(self): lp_str = """ Maximize obj: x1 + 2 x2 + 3 x3 + x4 + 10 Subject To c1: - x1 + x2 + x3 + 10 x4 <= 20 c2: x1 - 3 x2 + x3 <= 30 c3: x2 - 3.5 x4 = 0 Bounds 0 <= x1 <= 40 2 <= x4 <= 3 General x4 Binary x2 End """ lp_problem = LpReader.read(io.StringIO(lp_str)) assert lp_problem.lp_objective.to_lp_terms() == [ 'x1', '+ 2 x2', '+ 3 x3', '+ x4', '+ 10.0' ] assert lp_problem.lp_objective.name == 'obj' assert lp_problem.lp_objective.const == 10
def test_constraints(self): lp_str = """ Maximize obj: x1 + 2 x2 + 3 x3 + x4 + 10 Subject To c1: - x1 + x2 + x3 + 10 x4 <= 20 c2: x1 - 3 x2 + x3 <= 30 c3: x2 - 3.5 x4 = 0 Bounds 0 <= x1 <= 40 2 <= x4 <= 3 General x4 Binary x2 End """ lp_problem = LpReader.read(io.StringIO(lp_str)) assert lp_problem.lp_constraints['c1'].to_lp_terms() == [ '- x1', '+ x2', '+ x3', '+ 10 x4', '<=', '20.0' ] assert lp_problem.lp_constraints['c2'].to_lp_terms() == [ 'x1', '- 3 x2', '+ x3', '<=', '30.0' ] assert lp_problem.lp_constraints['c3'].to_lp_terms() == [ 'x2', '- 3.5 x4', '=', '0.0' ]
def test_constraints_exceptions(self): lp_str = """ Maximize obj: x1 + 2 x2 + 3 x3 + x4 + 10 Subject To c1: - x1 + x2 + x3 + 10 x4 c2: x1 - 3 x2 + x3 <= 30 c3: x2 - 3.5 x4 = 0 Bounds 0 <= x1 <= 40 2 <= x4 <= 3 General x4 Binary x2 End """ with pytest.raises(Exception) as e: lp_problem = LpReader.read(io.StringIO(lp_str)) assert str( e.value ) == "constraint c1: - x1 + x2 + x3 + 10 x4\n c2: x1 - 3 x2 + x3 <= 30 doesn't appear to be valid"
def test_variable_types(self): lp_str = """ Maximize obj: x1 + 2 x2 + 3 x3 + x4 + 10 Subject To c1: - x1 + x2 + x3 + 10 x4 <= 20 c2: x1 - 3 x2 + x3 <= 30 c3: x2 - 3.5 x4 = 0 Bounds 0 <= x1 <= 40 2 <= x4 <= 3 x3 <= 3 General x4 Binary x2 End """ lp_problem = LpReader.read(io.StringIO(lp_str)) assert lp_problem.lp_variables['x1'].low_bound == 0 assert lp_problem.lp_variables['x1'].up_bound == 40 assert lp_problem.lp_variables['x1'].var_type == VarType.Continuous assert lp_problem.lp_variables['x2'].low_bound == 0 assert lp_problem.lp_variables['x2'].up_bound == 1 assert lp_problem.lp_variables['x2'].var_type == VarType.Binary assert lp_problem.lp_variables['x3'].low_bound == 0 assert lp_problem.lp_variables['x3'].up_bound == 3.0 assert lp_problem.lp_variables['x3'].var_type == VarType.Continuous assert lp_problem.lp_variables['x4'].low_bound == 2 assert lp_problem.lp_variables['x4'].up_bound == 3 assert lp_problem.lp_variables['x4'].var_type == VarType.Integer