20#include "absl/functional/any_invocable.h"
21#include "absl/log/check.h"
22#include "absl/memory/memory.h"
23#include "absl/status/status.h"
24#include "absl/status/statusor.h"
25#include "absl/synchronization/mutex.h"
43absl::StatusOr<SolveResult> CallSolve(
44 BaseSolver& solver,
const ModelStorage*
const expected_storage,
45 const SolveArguments& arguments, SolveInterrupter& local_canceller) {
46 RETURN_IF_ERROR(arguments.CheckModelStorageAndCallback(expected_storage));
50 absl::Status cb_status;
51 if (arguments.callback !=
nullptr) {
52 cb = [&](
const CallbackDataProto& callback_data_proto) {
53 const CallbackData data(expected_storage, callback_data_proto);
54 const CallbackResult result = arguments.callback(data);
55 if (
const absl::Status
status =
56 result.CheckModelStorage(expected_storage);
61 builder <<
"invalid CallbackResult returned by user callback";
64 const absl::MutexLock lock(&mutex);
65 cb_status.Update(builder);
69 local_canceller.Interrupt();
73 CallbackResultProto result_proto;
74 result_proto.set_terminate(
true);
77 return result.Proto();
81 const absl::StatusOr<SolveResultProto> solve_result_proto = solver.Solve(
82 {.parameters = arguments.parameters.Proto(),
83 .model_parameters = arguments.model_parameters.Proto(),
84 .message_callback = arguments.message_callback,
85 .callback_registration = arguments.callback_registration.Proto(),
86 .user_cb = std::move(cb),
87 .interrupter = arguments.interrupter});
93 const absl::MutexLock lock(&mutex);
97 if (!solve_result_proto.ok()) {
98 return solve_result_proto.status();
101 return SolveResult::FromProto(expected_storage, solve_result_proto.value());
104absl::StatusOr<ComputeInfeasibleSubsystemResult> CallComputeInfeasibleSubsystem(
105 BaseSolver& solver,
const ModelStorage*
const expected_storage,
106 const ComputeInfeasibleSubsystemArguments& arguments,
107 SolveInterrupter& local_canceller) {
109 const ComputeInfeasibleSubsystemResultProto compute_result_proto,
110 solver.ComputeInfeasibleSubsystem(
111 {.parameters = arguments.parameters.Proto(),
112 .message_callback = arguments.message_callback,
113 .interrupter = arguments.interrupter}));
116 compute_result_proto);
127 user_canceller, [&]() { local_canceller.
Interrupt(); });
129 const std::unique_ptr<BaseSolver> solver,
132 return CallSolve(*solver,
model.storage(), solve_args, local_canceller);
142 user_canceller, [&]() { local_canceller.
Interrupt(); });
144 const std::unique_ptr<BaseSolver> subprocess_solver,
147 return CallComputeInfeasibleSubsystem(*subprocess_solver,
model.storage(),
148 compute_args, local_canceller);
151absl::StatusOr<std::unique_ptr<IncrementalSolverImpl>>
155 const bool remove_names) {
156 if (
model ==
nullptr) {
157 return absl::InvalidArgumentError(
"input model can't be null");
159 auto local_canceller = std::make_shared<SolveInterrupter>();
160 auto user_canceller_cb =
161 std::make_unique<const ScopedSolveInterrupterCallback>(
163 [local_canceller]() { local_canceller->Interrupt(); });
164 std::unique_ptr<UpdateTracker> update_tracker =
model->NewUpdateTracker();
166 update_tracker->ExportModel(remove_names));
168 std::unique_ptr<BaseSolver> solver,
170 local_canceller.get()));
172 std::move(solver_factory),
solver_type, remove_names,
173 std::move(local_canceller), std::move(user_canceller_cb),
174 model->storage(), std::move(update_tracker), std::move(solver)));
177IncrementalSolverImpl::IncrementalSolverImpl(
179 const bool remove_names, std::shared_ptr<SolveInterrupter> local_canceller,
180 std::unique_ptr<const ScopedSolveInterrupterCallback> user_canceller_cb,
181 const ModelStorage*
const expected_storage,
182 std::unique_ptr<UpdateTracker> update_tracker,
183 std::unique_ptr<BaseSolver> solver)
184 : solver_factory_(
std::move(solver_factory)),
185 solver_type_(solver_type),
186 remove_names_(remove_names),
187 local_canceller_(
std::move(local_canceller)),
188 user_canceller_cb_(
std::move(user_canceller_cb)),
189 expected_storage_(expected_storage),
190 update_tracker_(
std::move(update_tracker)),
191 solver_(
std::move(solver)) {}
200absl::StatusOr<ComputeInfeasibleSubsystemResult>
211 update_tracker_->ExportModelUpdate(remove_names_));
212 if (!model_update.has_value()) {
217 solver_->Update(*std::move(model_update)),
218 _ <<
"update failed");
226 update_tracker_->ExportModel(remove_names_));
229 solver_factory_(
EnumToProto(solver_type_), std::move(model_proto),
230 local_canceller_.get()),
231 _ <<
"solver re-creation failed");
239 return CallSolve(*solver_, expected_storage_, arguments, *local_canceller_);
242absl::StatusOr<ComputeInfeasibleSubsystemResult>
246 return CallComputeInfeasibleSubsystem(*solver_, expected_storage_, arguments,
#define ASSIGN_OR_RETURN(lhs, rexpr)
#define RETURN_IF_ERROR(expr)
std::function< CallbackResultProto(const CallbackDataProto &)> Callback
Callback function type for MIP/LP callbacks.
absl::StatusOr< SolveResult > SolveWithoutUpdate() const
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystemWithoutUpdate() const
absl::StatusOr< SolveResult > Solve()
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystem()
Incremental solve of a model.
absl::StatusOr< UpdateResult > Update() override
SolverType solver_type() const override
Returns the underlying solver used.
static absl::StatusOr< std::unique_ptr< IncrementalSolverImpl > > New(BaseSolverFactory solver_factory, Model *model, SolverType solver_type, const SolveInterrupter *user_canceller, bool remove_names)
absl::StatusOr< ComputeInfeasibleSubsystemResult > ComputeInfeasibleSubsystemImpl(const BaseSolverFactory solver_factory, const Model &model, const SolverType solver_type, const ComputeInfeasibleSubsystemArguments &compute_args, const SolveInterrupter *const user_canceller, const bool remove_names)
absl::AnyInvocable< absl::StatusOr< std::unique_ptr< BaseSolver > >( SolverTypeProto solver_type, ModelProto model, SolveInterrupter *local_canceller) const > BaseSolverFactory
absl::StatusOr< SolveResult > SolveImpl(const BaseSolverFactory solver_factory, const Model &model, const SolverType solver_type, const SolveArguments &solve_args, const SolveInterrupter *const user_canceller, const bool remove_names)
SolverType
The solvers supported by MathOpt.
Enum< E >::Proto EnumToProto(std::optional< E > value)
Arguments passed to ComputeInfeasibleSubsystem() to control the solver.
static absl::StatusOr< ComputeInfeasibleSubsystemResult > FromProto(const ModelStorage *model, const ComputeInfeasibleSubsystemResultProto &result_proto)
Result of the Update() on an incremental solver.
#define OR_ASSIGN_OR_RETURN3(lhs, rexpr, error_expression)