def main(): # Create the linear solver with the GLOP backend. solver = Solver('simple_lp_program', Solver.GLOP_LINEAR_PROGRAMMING) # Create the variables x and y. x = solver.NumVar(0, 1, 'x') y = solver.NumVar(0, 2, 'y') print('Number of variables =', solver.NumVariables()) # Create a linear constraint, 0 <= x + y <= 2. ct = solver.Constraint(0, 2, 'ct') ct.SetCoefficient(x, 1) ct.SetCoefficient(y, 1) print('Number of constraints =', solver.NumConstraints()) # Create the objective function, 3 * x + y. objective = solver.Objective() objective.SetCoefficient(x, 3) objective.SetCoefficient(y, 1) objective.SetMaximization() solver.Solve() print('Solution:') print('Objective value =', objective.Value()) print('x =', x.solution_value()) print('y =', y.solution_value())
def _parse_constraint(solver: pywraplp.Solver, core_line: str, line_nr: int, var_names: set): # We don't care about the coefficient name before the colon constraint_part = core_line spl_colon = core_line.split(":", maxsplit=1) if len(spl_colon) > 1: constraint_part = spl_colon[1].strip() # Equality constraint if constraint_part.find("=") >= 0 and constraint_part.find( "<=") == -1 and constraint_part.find(">=") == -1: equality_spl = constraint_part.split("=") if len(equality_spl) > 2: raise ValueError( "Equality constraint on line %d has multiple equal signs." % line_nr) if not _is_valid_constant_float(equality_spl[1]): raise ValueError( "Right hand side (\"%s\") of equality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (equality_spl[1], line_nr)) equal_value = float(equality_spl[1]) constraint = solver.Constraint(equal_value, equal_value) constant = _set_coefficients(solver, constraint, equality_spl[0], line_nr, var_names) constraint.SetLb(constraint.Lb() - constant) constraint.SetUb(constraint.Ub() - constant) _attempt_to_improve_var_bounds_two_hs(solver, equality_spl[0], True, equality_spl[1], equality_spl[1]) # Inequality constraints else: # Replace all of these inequality signs, because they are equivalent constraint_part = constraint_part.replace("<=", "<").replace(">=", ">") # lower bound < ... < upper bound if constraint_part.count("<") == 2: spl = constraint_part.split("<") if not _is_valid_constant_float(spl[0]): raise ValueError( "Left hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[0], line_nr)) if not _is_valid_constant_float(spl[2]): raise ValueError( "Right hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[2], line_nr)) constraint = solver.Constraint(float(spl[0]), float(spl[2])) constant = _set_coefficients(solver, constraint, spl[1], line_nr, var_names) constraint.SetLb(constraint.Lb() - constant) constraint.SetUb(constraint.Ub() - constant) _attempt_to_improve_var_bounds_two_hs(solver, spl[1], True, spl[0], spl[2]) # upper bound > ... > lower bound elif constraint_part.count(">") == 2: spl = constraint_part.split(">") if not _is_valid_constant_float(spl[0]): raise ValueError( "Left hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[0], line_nr)) if not _is_valid_constant_float(spl[2]): raise ValueError( "Right hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[2], line_nr)) constraint = solver.Constraint(float(spl[2]), float(spl[0])) constant = _set_coefficients(solver, constraint, spl[1], line_nr, var_names) constraint.SetLb(constraint.Lb() - constant) constraint.SetUb(constraint.Ub() - constant) _attempt_to_improve_var_bounds_two_hs(solver, spl[1], False, spl[0], spl[2]) # ... < upper bound elif constraint_part.count("<") == 1: spl = constraint_part.split("<") if not _is_valid_constant_float(spl[1]): raise ValueError( "Right hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[1], line_nr)) constraint = solver.Constraint(-solver.infinity(), float(spl[1])) constant = _set_coefficients(solver, constraint, spl[0], line_nr, var_names) constraint.SetUb(constraint.Ub() - constant) _attempt_to_improve_var_bounds_one_hs(solver, spl[0], True, spl[1]) # ... > lower bound elif constraint_part.count(">") == 1: spl = constraint_part.split(">") if not _is_valid_constant_float(spl[1]): raise ValueError( "Right hand side (\"%s\") of inequality constraint on line %d is not a float " "(e.g., variables are not allowed there!)." % (spl[1], line_nr)) constraint = solver.Constraint(float(spl[1]), solver.infinity()) constant = _set_coefficients(solver, constraint, spl[0], line_nr, var_names) constraint.SetLb(constraint.Lb() - constant) _attempt_to_improve_var_bounds_one_hs(solver, spl[0], False, spl[1]) # ... elif constraint_part.count(">") == 0 and constraint_part.count( "<") == 0: raise ValueError( "No (in)equality sign present for constraint on line %d." % line_nr) # Some strange combination else: raise ValueError( "Too many (in)equality signs present for constraint on line %d." % line_nr)