OR-Tools  9.6
glop_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2022 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #ifndef OR_TOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
15 #define OR_TOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
16 
17 #include <stdint.h>
18 
19 #include <atomic>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/status/status.h"
27 #include "absl/status/statusor.h"
28 #include "absl/types/span.h"
29 #include "ortools/glop/lp_solver.h"
30 #include "ortools/glop/parameters.pb.h"
33 #include "ortools/math_opt/callback.pb.h"
37 #include "ortools/math_opt/model.pb.h"
38 #include "ortools/math_opt/model_parameters.pb.h"
39 #include "ortools/math_opt/model_update.pb.h"
40 #include "ortools/math_opt/parameters.pb.h"
41 #include "ortools/math_opt/result.pb.h"
42 #include "ortools/math_opt/solution.pb.h"
43 #include "ortools/math_opt/sparse_containers.pb.h"
44 
45 namespace operations_research {
46 namespace math_opt {
47 
48 class GlopSolver : public SolverInterface {
49  public:
50  static absl::StatusOr<std::unique_ptr<SolverInterface>> New(
51  const ModelProto& model, const InitArgs& init_args);
52 
53  absl::StatusOr<SolveResultProto> Solve(
54  const SolveParametersProto& parameters,
55  const ModelSolveParametersProto& model_parameters,
56  MessageCallback message_cb,
57  const CallbackRegistrationProto& callback_registration, Callback cb,
58  SolveInterrupter* interrupter) override;
59  absl::StatusOr<bool> Update(const ModelUpdateProto& model_update) override;
60 
61  // Returns the merged parameters and a list of warnings from any parameter
62  // settings that are invalid for this solver.
63  static absl::StatusOr<glop::GlopParameters> MergeSolveParameters(
64  const SolveParametersProto& solver_parameters, bool setting_initial_basis,
65  bool has_message_callback);
66 
67  private:
68  GlopSolver();
69 
70  void AddVariables(const VariablesProto& variables);
71  void AddLinearConstraints(const LinearConstraintsProto& linear_constraints);
72 
73  void DeleteVariables(absl::Span<const int64_t> ids_to_delete);
74  void DeleteLinearConstraints(absl::Span<const int64_t> ids_to_delete);
75 
76  void SetOrUpdateObjectiveCoefficients(
77  const SparseDoubleVectorProto& linear_objective_coefficients);
78  void SetOrUpdateConstraintMatrix(
79  const SparseDoubleMatrixProto& linear_constraint_matrix);
80 
81  void UpdateVariableBounds(const VariableUpdatesProto& variable_updates);
82  void UpdateLinearConstraintBounds(
83  const LinearConstraintUpdatesProto& linear_constraint_updates);
84 
85  // Returns the ids of variables and linear constraints with inverted bounds.
86  InvertedBounds ListInvertedBounds() const;
87 
88  void FillSolution(glop::ProblemStatus status,
89  const ModelSolveParametersProto& model_parameters,
90  SolveResultProto& solve_result);
91  absl::StatusOr<SolveResultProto> MakeSolveResult(
93  const ModelSolveParametersProto& model_parameters,
94  const SolveInterrupter* interrupter, absl::Duration solve_time);
95 
96  absl::Status FillSolveStats(const glop::ProblemStatus status,
97  absl::Duration solve_time,
98  SolveStatsProto& solve_stats);
99 
100  void SetGlopBasis(const BasisProto& basis);
101 
102  glop::LinearProgram linear_program_;
103  glop::LPSolver lp_solver_;
104 
105  absl::flat_hash_map<int64_t, glop::ColIndex> variables_;
106  absl::flat_hash_map<int64_t, glop::RowIndex> linear_constraints_;
107 };
108 
109 } // namespace math_opt
110 } // namespace operations_research
111 
112 #endif // OR_TOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
absl::StatusOr< bool > Update(const ModelUpdateProto &model_update) override
Definition: glop_solver.cc:883
static absl::StatusOr< std::unique_ptr< SolverInterface > > New(const ModelProto &model, const InitArgs &init_args)
Definition: glop_solver.cc:860
absl::StatusOr< SolveResultProto > Solve(const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters, MessageCallback message_cb, const CallbackRegistrationProto &callback_registration, Callback cb, SolveInterrupter *interrupter) override
Definition: glop_solver.cc:795
static absl::StatusOr< glop::GlopParameters > MergeSolveParameters(const SolveParametersProto &solver_parameters, bool setting_initial_basis, bool has_message_callback)
Definition: glop_solver.cc:282
std::function< void(const std::vector< std::string > &)> MessageCallback
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
SatParameters parameters
absl::Status status
Definition: g_gurobi.cc:41
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.