OR-Tools  9.1
solver_interface.cc
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
15
16#include <algorithm>
17#include <memory>
18#include <string>
19#include <utility>
20#include <vector>
21
23#include "absl/container/flat_hash_map.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/str_cat.h"
27#include "absl/strings/str_join.h"
28#include "absl/synchronization/mutex.h"
30#include "ortools/math_opt/model.pb.h"
31#include "ortools/math_opt/parameters.pb.h"
33
34namespace operations_research {
35namespace math_opt {
36
38 static AllSolversRegistry* const instance = new AllSolversRegistry;
39 return instance;
40}
41
42void AllSolversRegistry::Register(const SolverType solver_type,
44 bool inserted;
45 {
46 const absl::MutexLock lock(&mutex_);
47 inserted =
48 registered_solvers_.emplace(solver_type, std::move(factory)).second;
49 }
50 CHECK(inserted) << "Solver type: " << ProtoEnumToString(solver_type)
51 << " already registered.";
52}
53
54absl::StatusOr<std::unique_ptr<SolverInterface>> AllSolversRegistry::Create(
55 SolverType solver_type, const ModelProto& model,
56 const SolverInitializerProto& initializer) const {
57 const SolverInterface::Factory* factory = nullptr;
58 {
59 const absl::MutexLock lock(&mutex_);
60 factory = gtl::FindOrNull(registered_solvers_, solver_type);
61 }
62 if (factory == nullptr) {
63 return absl::InvalidArgumentError(
64 absl::StrCat("Solver type: ", ProtoEnumToString(solver_type),
65 " is not registered."));
66 }
67 return (*factory)(model, initializer);
68}
69
70bool AllSolversRegistry::IsRegistered(const SolverType solver_type) const {
71 const absl::MutexLock lock(&mutex_);
72 return registered_solvers_.contains(solver_type);
73}
74
75std::vector<SolverType> AllSolversRegistry::RegisteredSolvers() const {
76 std::vector<SolverType> result;
77 {
78 const absl::MutexLock lock(&mutex_);
79 for (const auto& kv_pair : registered_solvers_) {
80 result.push_back(kv_pair.first);
81 }
82 }
83 std::sort(result.begin(), result.end());
84 return result;
85}
86
88 std::vector<std::string> solver_names;
89 {
90 const absl::MutexLock lock(&mutex_);
91 for (const auto& kv_pair : registered_solvers_) {
92 solver_names.push_back(ProtoEnumToString(kv_pair.first));
93 }
94 }
95 std::sort(solver_names.begin(), solver_names.end());
96 return absl::StrCat("[", absl::StrJoin(solver_names, ","), "]");
97}
98
99} // namespace math_opt
100} // namespace operations_research
#define CHECK(condition)
Definition: base/logging.h:491
std::vector< SolverType > RegisteredSolvers() const
bool IsRegistered(SolverType solver_type) const
void Register(SolverType solver_type, SolverInterface::Factory factory)
absl::StatusOr< std::unique_ptr< SolverInterface > > Create(SolverType solver_type, const ModelProto &model, const SolverInitializerProto &initializer) const
std::function< absl::StatusOr< std::unique_ptr< SolverInterface > >(const ModelProto &model, const SolverInitializerProto &initializer)> Factory
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)