22#include "absl/base/attributes.h"
23#include "absl/log/check.h"
24#include "absl/status/status.h"
25#include "absl/status/statusor.h"
26#include "absl/strings/str_cat.h"
29#include "ortools/linear_solver/linear_solver.pb.h"
46 return interrupt ==
nullptr;
49 std::atomic<bool>* interrupt)
override {
50 DCHECK_EQ(interrupt,
nullptr);
51 const bool log_error = request->enable_internal_solver_output();
57 void Reset()
override;
65 double new_value,
double old_value)
override;
68 double coefficient)
override;
74 int64_t
nodes()
const override;
80 bool IsLP()
const override;
81 bool IsMIP()
const override;
98 const std::string& parameters)
override;
102 void NonIncrementalChange();
104 const bool solve_as_a_mip_;
124 solver_->SetSolverSpecificParametersAsString(
125 solver_->solver_specific_parameter_string_);
129 VLOG(1) <<
"Setting time limit = " <<
solver_->time_limit() <<
" ms.";
135 for (
int i = 0; i <
solver_->variables_.size(); ++i) {
138 for (
int i = 0; i <
solver_->constraints_.size(); ++i) {
142 MPModelProto model_proto;
143 solver_->ExportModelToProto(&model_proto);
144 MPModelRequest request;
145 *request.mutable_model() = std::move(model_proto);
146 request.set_solver_type(solve_as_a_mip_
147 ? MPModelRequest::HIGHS_MIXED_INTEGER_PROGRAMMING
148 : MPModelRequest::HIGHS_LINEAR_PROGRAMMING);
151 absl::StatusOr<MPSolutionResponse> response =
154 if (!response.ok()) {
155 LOG(ERROR) <<
"Unexpected error solving with Highs: " << response.status();
163 if (response->status() == MPSOLVER_FEASIBLE ||
164 response->status() == MPSOLVER_OPTIMAL) {
165 const absl::Status result =
solver_->LoadSolutionFromProto(*response);
167 LOG(ERROR) <<
"LoadSolutionFromProto failed: " << result;
177 NonIncrementalChange();
181 NonIncrementalChange();
185 NonIncrementalChange();
189 NonIncrementalChange();
193 NonIncrementalChange();
197 NonIncrementalChange();
202 double new_value,
double old_value) {
203 NonIncrementalChange();
207 NonIncrementalChange();
211 double coefficient) {
212 NonIncrementalChange();
216 NonIncrementalChange();
226 LOG(DFATAL) <<
"Number of nodes only available for discrete problems";
265 if (num_threads < 1) {
266 return absl::InvalidArgumentError(
267 absl::StrCat(
"Invalid number of threads: ", num_threads));
270 return absl::OkStatus();
282 const std::string& parameters) {
287void HighsInterface::NonIncrementalChange() {
MPSolver::BasisStatus row_status(int constraint_index) const override
Returns the basis status of a row.
void SetConstraintBounds(int index, double lb, double ub) override
Modify bounds of an extracted variable.
absl::Status SetNumThreads(int num_threads) override
Sets the number of threads to be used by the solver.
int64_t nodes() const override
bool IsContinuous() const override
--— Misc --—
void AddVariable(MPVariable *var) override
Add a variable.
void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override
Changes a coefficient in the linear objective.
bool IsMIP() const override
Returns true if the problem is discrete and linear.
bool SupportsDirectlySolveProto(std::atomic< bool > *interrupt) const override
--— Directly solve proto is supported without interrupt —
bool IsLP() const override
Returns true if the problem is continuous and linear.
MPSolver::ResultStatus Solve(const MPSolverParameters ¶m) override
--— Solve --—
void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override
Changes a coefficient in a constraint.
MPSolutionResponse DirectlySolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt) override
bool SetSolverSpecificParametersAsString(const std::string ¶meters) override
int64_t iterations() const override
---— Query statistics on the solution and the solve ---—
void SetPresolveMode(int value) override
void SetVariableInteger(int index, bool integer) override
Modifies integrality of an extracted variable.
void SetScalingMode(int value) override
Sets the scaling mode.
void SetVariableBounds(int index, double lb, double ub) override
Modifies bounds of an extracted variable.
void * underlying_solver() override
std::string SolverVersion() const override
Returns a string describing the underlying solver and its version.
MPSolver::BasisStatus column_status(int variable_index) const override
Returns the basis status of a constraint.
void SetDualTolerance(double value) override
void SetRelativeMipGap(double value) override
Sets each parameter in the underlying solver.
void ClearObjective() override
Clears the objective from all its terms.
HighsInterface(MPSolver *solver, bool solve_as_a_mip)
void SetParameters(const MPSolverParameters ¶m) override
Sets all parameters in the underlying solver.
~HighsInterface() override
void Reset() override
--— Model modifications and extraction --—
void ExtractNewVariables() override
Extracts the variables that have not been extracted yet.
void AddRowConstraint(MPConstraint *ct) override
Adds a linear constraint.
void SetOptimizationDirection(bool maximize) override
Sets the optimization direction (min/max).
void SetPrimalTolerance(double value) override
These have no effect. Use SetSolverSpecificParametersAsString instead.
void ClearConstraint(MPConstraint *constraint) override
Clears a constraint from all its terms.
void ExtractObjective() override
Extracts the objective.
void ExtractNewConstraints() override
Extracts the constraints that have not been extracted yet.
void SetLpAlgorithm(int value) override
void SetObjectiveOffset(double value) override
Changes the constant term in the linear objective.
void set_variable_as_extracted(int var_index, bool extracted)
friend class MPConstraint
To access the maximize_ bool and the MPSolver.
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.
MPSolverInterface(MPSolver *solver)
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.
@ ABNORMAL
abnormal, i.e., error of some kind.
The class for variables of a Mathematical Programming (MP) model.
In SWIG mode, we don't want anything besides these top-level includes.
MPSolverInterface * BuildHighsInterface(bool mip, MPSolver *const solver)
Register PDLP in the global linear solver factory.
absl::StatusOr< MPSolutionResponse > HighsSolveProto(LazyMutableCopy< MPModelRequest > request)
Solve the input MIP model with the HIGHS solver.
MPSolutionResponse ConvertStatusOrMPSolutionResponse(bool log_error, absl::StatusOr< MPSolutionResponse > response)