def solve_via_data( self, data: Dict[str, Any], warm_start: bool, verbose: bool, solver_opts: Dict[str, Any], solver_cache: Dict = None, ) -> Solution: """Returns the result of the call to the solver.""" from google.protobuf import text_format from ortools.glop import parameters_pb2 from ortools.linear_solver import linear_solver_pb2, pywraplp response = linear_solver_pb2.MPSolutionResponse() solver = pywraplp.Solver.CreateSolver('GLOP') solver.LoadModelFromProto(data[self.MODEL_PROTO]) if verbose: solver.EnableOutput() if "parameters_proto" in solver_opts: proto = solver_opts["parameters_proto"] if not isinstance(proto, parameters_pb2.GlopParameters): log.error("parameters_proto must be a GlopParameters") return {"status": s.SOLVER_ERROR} proto_str = text_format.MessageToString(proto) if not solver.SetSolverSpecificParametersAsString(proto_str): return {"status": s.SOLVER_ERROR} if "time_limit_sec" in solver_opts: solver.SetTimeLimit(1000 * float(solver_opts["time_limit_sec"])) solver.Solve() solver.FillSolutionResponseProto(response) solution = {} solution["value"] = response.objective_value solution["status"] = self._status_map(response) has_primal = data["num_vars"] == 0 or len(response.variable_value) > 0 if has_primal: solution["primal"] = array(response.variable_value) else: solution["primal"] = None has_dual = data["num_constraints"] == 0 or len(response.dual_value) > 0 if has_dual: solution["dual"] = array(response.dual_value) else: solution["dual"] = None # Make solution status more precise depending on whether a solution is # present. if solution["status"] == s.SOLVER_ERROR and has_primal and has_dual: solution["status"] = s.USER_LIMIT return solution
def test_proto(): input_proto = linear_solver_pb2.MPModelRequest() text_format.Merge(text_model, input_proto) solver = pywraplp.Solver('solveFromProto', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) print(input_proto) # For now, create the model from the proto by parsing the proto solver.LoadModelFromProto(input_proto.model) solver.EnableOutput() solver.Solve() # Fill solution solution = linear_solver_pb2.MPSolutionResponse() solver.FillSolutionResponseProto(solution) print(solution)
def test_proto(self): input_proto = linear_solver_pb2.MPModelProto() text_format.Merge(TEXT_MODEL, input_proto) solver = pywraplp.Solver('solveFromProto', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING) # For now, create the model from the proto by parsing the proto errors = solver.LoadModelFromProto(input_proto) self.assertFalse(errors) solver.Solve() # Fill solution solution = linear_solver_pb2.MPSolutionResponse() solver.FillSolutionResponseProto(solution) self.assertEqual(solution.objective_value, 3.0) self.assertEqual(solution.variable_value[0], 1.0) self.assertEqual(solution.variable_value[1], 1.0) self.assertEqual(solution.best_objective_bound, 3.0)
def testSolveFromProto(self): solver = pywraplp.Solver('', pywraplp.Solver.GLOP_LINEAR_PROGRAMMING) solver.LoadSolutionFromProto(linear_solver_pb2.MPSolutionResponse())