19#include "absl/log/check.h"
20#include "absl/memory/memory.h"
21#include "absl/status/status.h"
22#include "absl/status/statusor.h"
23#include "absl/strings/str_cat.h"
25#include "ortools/math_opt/callback.pb.h"
32#include "ortools/math_opt/infeasible_subsystem.pb.h"
33#include "ortools/math_opt/model.pb.h"
34#include "ortools/math_opt/model_update.pb.h"
35#include "ortools/math_opt/parameters.pb.h"
36#include "ortools/math_opt/result.pb.h"
52absl::Status ToInternalError(
const absl::Status original) {
57 return absl::InternalError(original.message());
62absl::Status PreviousFatalFailureOccurred() {
63 return absl::InvalidArgumentError(
64 "a previous call to Solve(), ComputeInfeasibleSubsystem(), or Update() "
65 "failed, the Solver can't be used anymore");
71 const ModelProto&
model,
const SolverTypeProto solver_type,
75 return solver->Solve(solve_args);
78Solver::Solver(std::unique_ptr<SolverInterface> underlying_solver,
80 : underlying_solver_(
std::move(underlying_solver)),
81 model_summary_(
std::move(model_summary)) {
82 CHECK(underlying_solver_ !=
nullptr);
89 const SolverTypeProto solver_type,
const ModelProto&
model,
94 auto underlying_solver,
96 auto result = absl::WrapUnique(
97 new Solver(std::move(underlying_solver), std::move(summary)));
105 if (fatal_failure_occurred_) {
106 return PreviousFatalFailureOccurred();
108 CHECK(underlying_solver_ !=
nullptr);
111 fatal_failure_occurred_ =
true;
118 <<
"invalid parameters";
121 <<
"invalid model_parameters";
124 if (arguments.
user_cb !=
nullptr) {
127 cb = [&](
const CallbackDataProto& callback_data)
128 -> absl::StatusOr<CallbackResultProto> {
131 auto callback_result = arguments.
user_cb(callback_data);
133 callback_result, callback_data.event(),
135 return callback_result;
140 underlying_solver_->Solve(arguments.
parameters,
154 fatal_failure_occurred_ =
false;
162 if (fatal_failure_occurred_) {
163 return PreviousFatalFailureOccurred();
165 CHECK(underlying_solver_ !=
nullptr);
168 fatal_failure_occurred_ =
true;
172 underlying_solver_->Update(model_update));
178 underlying_solver_ =
nullptr;
182 fatal_failure_occurred_ =
false;
187absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
193 if (fatal_failure_occurred_) {
194 return PreviousFatalFailureOccurred();
196 CHECK(underlying_solver_ !=
nullptr);
199 fatal_failure_occurred_ =
true;
202 <<
"invalid parameters";
205 underlying_solver_->ComputeInfeasibleSubsystem(
215 fatal_failure_occurred_ =
false;
219absl::StatusOr<ComputeInfeasibleSubsystemResultProto>
221 const ModelProto&
model,
const SolverTypeProto solver_type,
226 return solver->ComputeInfeasibleSubsystem(compute_infeasible_subsystem_args);
232 const SolverTypeProto solver_type) {
233 if (solver_type == SOLVER_TYPE_UNSPECIFIED) {
234 return absl::InvalidArgumentError(
235 "can't use SOLVER_TYPE_UNSPECIFIED as solver_type parameter");
240 return absl::InvalidArgumentError(
241 absl::StrCat(
"input non_streamable init arguments are for ",
246 return absl::OkStatus();
#define ASSIGN_OR_RETURN(lhs, rexpr)
#define RETURN_IF_ERROR(expr)
static AllSolversRegistry * Instance()
static absl::StatusOr< ConcurrentCallsGuard > TryAcquire(Tracker &tracker)
std::function< absl::StatusOr< CallbackResultProto >( const CallbackDataProto &)> Callback
static absl::StatusOr< SolveResultProto > NonIncrementalSolve(const ModelProto &model, SolverTypeProto solver_type, const InitArgs &init_args, const SolveArgs &solve_args)
A shortcut for calling Solver::New() and then Solver::Solve().
absl::StatusOr< bool > Update(ModelUpdateProto model_update) override
absl::StatusOr< ComputeInfeasibleSubsystemResultProto > ComputeInfeasibleSubsystem(const ComputeInfeasibleSubsystemArgs &arguments) override
Computes an infeasible subsystem of model (including all updates).
absl::StatusOr< SolveResultProto > Solve(const SolveArgs &arguments) override
Solves the current model (including all updates).
static absl::StatusOr< ComputeInfeasibleSubsystemResultProto > NonIncrementalComputeInfeasibleSubsystem(const ModelProto &model, SolverTypeProto solver_type, const InitArgs &init_args, const ComputeInfeasibleSubsystemArgs &compute_infeasible_subsystem_args)
static absl::StatusOr< std::unique_ptr< Solver > > New(SolverTypeProto solver_type, const ModelProto &model, const InitArgs &arguments)
std::atomic< int64_t > debug_num_solver
absl::Status ValidateInitArgs(const Solver::InitArgs &init_args, const SolverTypeProto solver_type)
absl::Status ValidateCallbackResultProto(const CallbackResultProto &callback_result, const CallbackEventProto callback_event, const CallbackRegistrationProto &callback_registration, const ModelSummary &model_summary)
void UpgradeSolveResultProtoForStatsMigration(SolveResultProto &solve_result_proto)
absl::Status ValidateModelUpdate(const ModelUpdateProto &model_update, ModelSummary &model_summary)
absl::Status ValidateComputeInfeasibleSubsystemResult(const ComputeInfeasibleSubsystemResultProto &result, const ModelSummary &model_summary)
absl::Status ValidateCallbackDataProto(const CallbackDataProto &cb_data, const CallbackRegistrationProto &callback_registration, const ModelSummary &model_summary)
absl::Status ValidateResult(const SolveResultProto &result, const ModelSolveParametersProto ¶meters, const ModelSummary &model_summary)
Validates the input result.
absl::Status ValidateModelSolveParameters(const ModelSolveParametersProto ¶meters, const ModelSummary &model_summary)
absl::StatusOr< ModelSummary > ValidateModel(const ModelProto &model, const bool check_names)
absl::Status ValidateSolveParameters(const SolveParametersProto ¶meters)
absl::Status ValidateCallbackRegistration(const CallbackRegistrationProto &callback_registration, const ModelSummary &model_summary)
Checks that CallbackRegistrationProto is valid given a valid model summary.
In SWIG mode, we don't want anything besides these top-level includes.
std::string ProtoEnumToString(ProtoEnumType enum_value)
Arguments used when calling ComputeInfeasibleSubsystem().
const SolveInterrupter * interrupter
MessageCallback message_callback
SolveParametersProto parameters
Arguments used when calling Solve() to solve the problem.
SolveParametersProto parameters
ModelSolveParametersProto model_parameters
const SolveInterrupter * interrupter
CallbackRegistrationProto callback_registration
MessageCallback message_callback
virtual SolverTypeProto solver_type() const =0
Returns the type of solver that the implementation is for.
Initialization arguments.
const NonStreamableSolverInitArguments * non_streamable