OR-Tools  9.6
solver_interface.cc
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 
15 
16 #include <algorithm>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
25 #include "absl/strings/str_cat.h"
26 #include "absl/strings/str_join.h"
27 #include "absl/synchronization/mutex.h"
28 #include "ortools/base/logging.h"
29 #include "ortools/base/map_util.h"
31 #include "ortools/math_opt/model.pb.h"
32 #include "ortools/math_opt/parameters.pb.h"
34 
35 namespace operations_research {
36 namespace math_opt {
37 
39  static AllSolversRegistry* const instance = new AllSolversRegistry;
40  return instance;
41 }
42 
43 void AllSolversRegistry::Register(const SolverTypeProto solver_type,
44  SolverInterface::Factory factory) {
45  bool inserted;
46  {
47  const absl::MutexLock lock(&mutex_);
48  inserted =
49  registered_solvers_.emplace(solver_type, std::move(factory)).second;
50  }
51  CHECK(inserted) << "Solver type: " << ProtoEnumToString(solver_type)
52  << " already registered.";
53 }
54 
55 absl::StatusOr<std::unique_ptr<SolverInterface>> AllSolversRegistry::Create(
56  SolverTypeProto solver_type, const ModelProto& model,
57  const SolverInterface::InitArgs& init_args) const {
58  const SolverInterface::Factory* factory = nullptr;
59  {
60  const absl::MutexLock lock(&mutex_);
61  factory = gtl::FindOrNull(registered_solvers_, solver_type);
62  }
63  if (factory == nullptr) {
64  std::string name = SolverTypeProto_Name(solver_type);
65  if (name.empty()) {
66  name = absl::StrCat("unknown(", static_cast<int>(solver_type), ")");
67  }
69  << "solver type " << name << " is not registered";
70  }
71  return (*factory)(model, init_args);
72 }
73 
74 bool AllSolversRegistry::IsRegistered(const SolverTypeProto solver_type) const {
75  const absl::MutexLock lock(&mutex_);
76  return registered_solvers_.contains(solver_type);
77 }
78 
79 std::vector<SolverTypeProto> AllSolversRegistry::RegisteredSolvers() const {
80  std::vector<SolverTypeProto> result;
81  {
82  const absl::MutexLock lock(&mutex_);
83  for (const auto& kv_pair : registered_solvers_) {
84  result.push_back(kv_pair.first);
85  }
86  }
87  std::sort(result.begin(), result.end());
88  return result;
89 }
90 
92  std::vector<std::string> solver_names;
93  {
94  const absl::MutexLock lock(&mutex_);
95  for (const auto& kv_pair : registered_solvers_) {
96  solver_names.push_back(ProtoEnumToString(kv_pair.first));
97  }
98  }
99  std::sort(solver_names.begin(), solver_names.end());
100  return absl::StrCat("[", absl::StrJoin(solver_names, ","), "]");
101 }
102 
103 } // namespace math_opt
104 } // namespace operations_research
void Register(SolverTypeProto solver_type, SolverInterface::Factory factory)
absl::StatusOr< std::unique_ptr< SolverInterface > > Create(SolverTypeProto solver_type, const ModelProto &model, const SolverInterface::InitArgs &init_args) const
bool IsRegistered(SolverTypeProto solver_type) const
std::vector< SolverTypeProto > RegisteredSolvers() const
std::function< absl::StatusOr< std::unique_ptr< SolverInterface > >(const ModelProto &model, const InitArgs &init_args)> Factory
const std::string name
GRBmodel * model
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition: map_util.h:60
Collection of objects used to extend the Constraint Solver library.
std::string ProtoEnumToString(ProtoEnumType enum_value)
StatusBuilder InvalidArgumentErrorBuilder()