OR-Tools  9.0
glop_solver.h
Go to the documentation of this file.
1 // Copyright 2010-2021 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 <memory>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 #include "absl/container/flat_hash_map.h"
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/types/span.h"
28 #include "ortools/glop/lp_solver.h"
32 #include "ortools/math_opt/callback.pb.h"
33 #include "ortools/math_opt/model.pb.h"
34 #include "ortools/math_opt/model_parameters.pb.h"
35 #include "ortools/math_opt/model_update.pb.h"
36 #include "ortools/math_opt/parameters.pb.h"
37 #include "ortools/math_opt/result.pb.h"
39 #include "ortools/math_opt/sparse_containers.pb.h"
40 
41 namespace operations_research {
42 namespace math_opt {
43 
44 class GlopSolver : public SolverInterface {
45  public:
46  static absl::StatusOr<std::unique_ptr<SolverInterface>> New(
47  const ModelProto& model, const SolverInitializerProto& initializer);
48 
49  absl::StatusOr<SolveResultProto> Solve(
50  const SolveParametersProto& parameters,
51  const ModelSolveParametersProto& model_parameters,
52  const CallbackRegistrationProto& callback_registration,
53  Callback cb) override;
54  absl::Status Update(const ModelUpdateProto& model_update) override;
55  bool CanUpdate(const ModelUpdateProto& model_update) override;
56 
57  // Returns the merged parameters and a list of warnings from any parameter
58  // settings that are invalid for this solver.
59  static std::pair<glop::GlopParameters, std::vector<std::string>>
61  const CommonSolveParametersProto& common_solver_parameters,
62  const glop::GlopParameters& glop_parameters);
63 
64  private:
65  GlopSolver();
66 
67  void AddVariables(const VariablesProto& variables);
68  void AddLinearConstraints(const LinearConstraintsProto& linear_constraints);
69 
70  void DeleteVariables(absl::Span<const int64_t> ids_to_delete);
71  void DeleteLinearConstraints(absl::Span<const int64_t> ids_to_delete);
72 
73  void SetOrUpdateObjectiveCoefficients(
74  const SparseDoubleVectorProto& linear_objective_coefficients);
75  void SetOrUpdateConstraintMatrix(
76  const SparseDoubleMatrixProto& linear_constraint_matrix);
77 
78  void UpdateVariableBounds(const VariableUpdatesProto& variable_updates);
79  void UpdateLinearConstraintBounds(
80  const LinearConstraintUpdatesProto& linear_constraint_updates);
81 
82  void FillSolveResult(glop::ProblemStatus status,
83  const ModelSolveParametersProto& model_parameters,
84  SolveResultProto& solve_result);
85 
86  glop::LinearProgram linear_program_;
87  glop::LPSolver lp_solver_;
88 
89  absl::flat_hash_map<int64_t, glop::ColIndex> variables_;
90  absl::flat_hash_map<int64_t, glop::RowIndex> linear_constraints_;
91 };
92 
93 } // namespace math_opt
94 } // namespace operations_research
95 
96 #endif // OR_TOOLS_MATH_OPT_SOLVERS_GLOP_SOLVER_H_
bool CanUpdate(const ModelUpdateProto &model_update) override
Definition: glop_solver.cc:317
static std::pair< glop::GlopParameters, std::vector< std::string > > MergeCommonParameters(const CommonSolveParametersProto &common_solver_parameters, const glop::GlopParameters &glop_parameters)
Definition: glop_solver.cc:221
absl::Status Update(const ModelUpdateProto &model_update) override
Definition: glop_solver.cc:453
absl::StatusOr< SolveResultProto > Solve(const SolveParametersProto &parameters, const ModelSolveParametersProto &model_parameters, const CallbackRegistrationProto &callback_registration, Callback cb) override
Definition: glop_solver.cc:408
static absl::StatusOr< std::unique_ptr< SolverInterface > > New(const ModelProto &model, const SolverInitializerProto &initializer)
Definition: glop_solver.cc:436
std::function< absl::StatusOr< CallbackResultProto >(const CallbackDataProto &)> Callback
SatParameters parameters
GRBmodel * model
Collection of objects used to extend the Constraint Solver library.