Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
solver_interface.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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/log/check.h"
24#include "absl/status/statusor.h"
25#include "absl/strings/ascii.h"
26#include "absl/strings/match.h"
27#include "absl/strings/str_cat.h"
28#include "absl/strings/str_join.h"
29#include "absl/synchronization/mutex.h"
32#include "ortools/math_opt/model.pb.h"
33#include "ortools/math_opt/parameters.pb.h"
35
36namespace operations_research {
37namespace math_opt {
38namespace {} // namespace
39
41 static AllSolversRegistry* const instance = new AllSolversRegistry;
42 return instance;
43}
44
45void AllSolversRegistry::Register(const SolverTypeProto solver_type,
47 bool inserted;
48 {
49 const absl::MutexLock lock(&mutex_);
50 inserted =
51 registered_solvers_.emplace(solver_type, std::move(factory)).second;
52 }
53 CHECK(inserted) << "Solver type: " << ProtoEnumToString(solver_type)
54 << " already registered.";
55}
56
57absl::StatusOr<std::unique_ptr<SolverInterface>> AllSolversRegistry::Create(
58 SolverTypeProto solver_type, const ModelProto& model,
59 const SolverInterface::InitArgs& init_args) const {
60 const SolverInterface::Factory* factory = nullptr;
61 {
62 const absl::MutexLock lock(&mutex_);
63 factory = gtl::FindOrNull(registered_solvers_, solver_type);
64 }
65 if (factory == nullptr) {
66 std::string name = SolverTypeProto_Name(solver_type);
67 if (name.empty()) {
68 name = absl::StrCat("unknown(", static_cast<int>(solver_type), ")");
69 }
71 << "solver type " << name << " is not registered"
72 << ", support for this solver has not been compiled";
73 }
74 return (*factory)(model, init_args);
75}
76
77bool AllSolversRegistry::IsRegistered(const SolverTypeProto solver_type) const {
78 const absl::MutexLock lock(&mutex_);
79 return registered_solvers_.contains(solver_type);
80}
81
82std::vector<SolverTypeProto> AllSolversRegistry::RegisteredSolvers() const {
83 std::vector<SolverTypeProto> result;
84 {
85 const absl::MutexLock lock(&mutex_);
86 for (const auto& kv_pair : registered_solvers_) {
87 result.push_back(kv_pair.first);
88 }
89 }
90 std::sort(result.begin(), result.end());
91 return result;
92}
93
95 std::vector<std::string> solver_names;
96 {
97 const absl::MutexLock lock(&mutex_);
98 for (const auto& kv_pair : registered_solvers_) {
99 solver_names.push_back(ProtoEnumToString(kv_pair.first));
100 }
101 }
102 std::sort(solver_names.begin(), solver_names.end());
103 return absl::StrCat("[", absl::StrJoin(solver_names, ","), "]");
104}
105
106} // namespace math_opt
107} // namespace operations_research
bool IsRegistered(SolverTypeProto solver_type) const
Whether a solver type is supported.
std::vector< SolverTypeProto > RegisteredSolvers() const
List all supported solver types.
void Register(SolverTypeProto solver_type, SolverInterface::Factory factory)
std::string RegisteredSolversToString() const
Returns a human-readable list of supported solver types.
absl::StatusOr< std::unique_ptr< SolverInterface > > Create(SolverTypeProto solver_type, const ModelProto &model, const SolverInterface::InitArgs &init_args) const
std::function< absl::StatusOr< std::unique_ptr< SolverInterface > >( const ModelProto &model, const InitArgs &init_args)> Factory
const std::string name
A name for logging purposes.
GRBmodel * model
const Collection::value_type::second_type * FindOrNull(const Collection &collection, const typename Collection::value_type::first_type &key)
Definition map_util.h:65
In SWIG mode, we don't want anything besides these top-level includes.
std::string ProtoEnumToString(ProtoEnumType enum_value)
Definition proto_utils.h:50
StatusBuilder InvalidArgumentErrorBuilder()