23#include "absl/base/attributes.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/str_cat.h"
27#include "absl/types/optional.h"
30#include "ortools/linear_solver/linear_solver.pb.h"
33#include "ortools/pdlp/solve_log.pb.h"
34#include "ortools/pdlp/solvers.pb.h"
52 std::atomic<bool>* interrupt)
override {
53 const bool log_error = request->enable_internal_solver_output();
60 void Reset()
override;
68 double new_value,
double old_value)
override;
77 int64_t
nodes()
const override;
83 bool IsLP()
const override;
84 bool IsMIP()
const override;
106 void NonIncrementalChange();
108 pdlp::PrimalDualHybridGradientParams parameters_;
109 pdlp::SolveLog solve_log_;
110 std::atomic<bool> interrupt_solver_;
121 interrupt_solver_ =
false;
126 parameters_.set_verbosity_level(0);
128 parameters_.set_verbosity_level(3);
132 solver_->solver_specific_parameter_string_);
137 parameters_.mutable_termination_criteria()->set_time_sec_limit(
142 for (
int i = 0; i <
solver_->variables_.size(); ++i) {
145 for (
int i = 0; i <
solver_->constraints_.size(); ++i) {
149 MPModelProto model_proto;
151 MPModelRequest request;
152 *request.mutable_model() = std::move(model_proto);
153 if (!google::protobuf::TextFormat::PrintToString(
154 parameters_, request.mutable_solver_specific_parameters())) {
155 LOG(QFATAL) <<
"Error converting parameters to text format: "
159 std::move(request),
true, &interrupt_solver_);
161 if (!response.ok()) {
162 LOG(ERROR) <<
"Unexpected error solving with PDLP: " << response.status();
168 if (response->status() == MPSOLVER_CANCELLED_BY_USER) {
175 if (response->has_solver_specific_info()) {
176 if (!solve_log_.ParseFromString(response->solver_specific_info())) {
178 <<
"Unable to parse PDLP's SolveLog from solver_specific_info";
182 if (response->status() == MPSOLVER_FEASIBLE ||
183 response->status() == MPSOLVER_OPTIMAL) {
186 LOG(ERROR) <<
"LoadSolutionFromProto failed: " << result;
196 NonIncrementalChange();
200 NonIncrementalChange();
204 NonIncrementalChange();
208 NonIncrementalChange();
212 NonIncrementalChange();
216 NonIncrementalChange();
221 double new_value,
double old_value) {
222 NonIncrementalChange();
226 NonIncrementalChange();
231 NonIncrementalChange();
239 return solve_log_.iteration_count();
243 LOG(DFATAL) <<
"Number of nodes only available for discrete problems";
272 interrupt_solver_ =
true;
287 if (num_threads < 1) {
288 return absl::InvalidArgumentError(
289 absl::StrCat(
"Invalid number of threads: ", num_threads));
291 parameters_.set_num_threads(num_threads);
292 return absl::OkStatus();
308void PdlpInterface::NonIncrementalChange() {
void set_variable_as_extracted(int var_index, bool extracted)
void set_constraint_as_extracted(int ct_index, bool extracted)
void ResetExtractionInformation()
Resets the extraction information.
static constexpr int64_t kUnknownNumberOfNodes
void ExtractModel()
Extracts model stored in MPSolver.
bool quiet_
Boolean indicator for the verbosity of the solver output.
void SetCommonParameters(const MPSolverParameters ¶m)
Sets parameters common to LP and MIP in the underlying solver.
MPSolver::ResultStatus result_status_
SynchronizationStatus sync_status_
Indicates whether the model and the solution are synchronized.
int64_t time_limit() const
@ NOT_SOLVED
not been solved yet.
@ ABNORMAL
abnormal, i.e., error of some kind.
bool SetSolverSpecificParametersAsString(const std::string ¶meters)
void ExportModelToProto(MPModelProto *output_model) const
Exports model to protocol buffer.
absl::Status LoadSolutionFromProto(const MPSolutionResponse &response, double tolerance=std::numeric_limits< double >::infinity())
The class for variables of a Mathematical Programming (MP) model.
MPSolutionResponse DirectlySolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt) override
int64_t nodes() const override
void SetLpAlgorithm(int value) override
bool IsMIP() const override
Returns true if the problem is discrete and linear.
void * underlying_solver() override
bool SetSolverSpecificParametersAsString(const std::string ¶meters) override
void SetScalingMode(int value) override
Sets the scaling mode.
std::string SolverVersion() const override
Returns a string describing the underlying solver and its version.
~PdlpInterface() override
void SetConstraintBounds(int index, double lb, double ub) override
Modify bounds of an extracted variable.
void ClearObjective() override
Clears the objective from all its terms.
PdlpInterface(MPSolver *solver)
void SetPrimalTolerance(double value) override
These have no effect. Use SetSolverSpecificParametersAsString instead.
void SetVariableInteger(int index, bool integer) override
Modifies integrality of an extracted variable.
void SetDualTolerance(double value) override
bool InterruptSolve() override
void SetVariableBounds(int index, double lb, double ub) override
Modifies bounds of an extracted variable.
void Reset() override
--— Model modifications and extraction --—
void AddVariable(MPVariable *var) override
Add a variable.
MPSolver::ResultStatus Solve(const MPSolverParameters ¶m) override
--— Solve --—
bool IsContinuous() const override
--— Misc --—
void SetPresolveMode(int value) override
void SetOptimizationDirection(bool maximize) override
Sets the optimization direction (min/max).
bool IsLP() const override
Returns true if the problem is continuous and linear.
void ExtractObjective() override
Extracts the objective.
void AddRowConstraint(MPConstraint *ct) override
Adds a linear constraint.
void SetRelativeMipGap(double value) override
Sets each parameter in the underlying solver.
bool SupportsDirectlySolveProto(std::atomic< bool > *interrupt) const override
void ExtractNewVariables() override
Extracts the variables that have not been extracted yet.
void SetObjectiveOffset(double value) override
Changes the constant term in the linear objective.
int64_t iterations() const override
---— Query statistics on the solution and the solve ---—
void ExtractNewConstraints() override
Extracts the constraints that have not been extracted yet.
MPSolver::BasisStatus column_status(int variable_index) const override
Returns the basis status of a constraint.
void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override
Changes a coefficient in the linear objective.
MPSolver::BasisStatus row_status(int constraint_index) const override
Returns the basis status of a row.
void ClearConstraint(MPConstraint *constraint) override
Clears a constraint from all its terms.
void SetParameters(const MPSolverParameters ¶m) override
Sets all parameters in the underlying solver.
void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
Changes a coefficient in a constraint.
absl::Status SetNumThreads(int num_threads) override
Sets the number of threads to be used by the solver.
In SWIG mode, we don't want anything besides these top-level includes.
absl::StatusOr< MPSolutionResponse > PdlpSolveProto(LazyMutableCopy< MPModelRequest > request, const bool relax_integer_variables, const std::atomic< bool > *interrupt_solve)
MPSolverInterface * BuildPdlpInterface(MPSolver *const solver)
Register PDLP in the global linear solver factory.
std::string ProtobufDebugString(const P &message)
MPSolutionResponse ConvertStatusOrMPSolutionResponse(bool log_error, absl::StatusOr< MPSolutionResponse > response)
bool ProtobufTextFormatMergeFromString(absl::string_view proto_text_string, ProtoType *proto)