23#include "absl/base/attributes.h"
24#include "absl/log/check.h"
25#include "absl/status/status.h"
26#include "absl/status/statusor.h"
27#include "absl/strings/str_cat.h"
28#include "absl/types/optional.h"
29#include "google/protobuf/text_format.h"
32#include "ortools/linear_solver/linear_solver.pb.h"
50 return interrupt ==
nullptr;
53 std::atomic<bool>* interrupt)
override {
54 DCHECK_EQ(interrupt,
nullptr);
55 const bool log_error = request->enable_internal_solver_output();
61 void Reset()
override;
69 double new_value,
double old_value)
override;
78 int64_t
nodes()
const override;
84 bool IsLP()
const override;
85 bool IsMIP()
const override;
106 void NonIncrementalChange();
108 const bool solve_as_a_mip_;
129 solver_->solver_specific_parameter_string_);
139 for (
int i = 0; i <
solver_->variables_.size(); ++i) {
142 for (
int i = 0; i <
solver_->constraints_.size(); ++i) {
146 MPModelProto model_proto;
148 MPModelRequest request;
149 *request.mutable_model() = std::move(model_proto);
150 request.set_solver_type(solve_as_a_mip_
151 ? MPModelRequest::HIGHS_MIXED_INTEGER_PROGRAMMING
152 : MPModelRequest::HIGHS_LINEAR_PROGRAMMING);
155 absl::StatusOr<MPSolutionResponse> response =
158 if (!response.ok()) {
159 LOG(ERROR) <<
"Unexpected error solving with Highs: " << response.status();
166 LOG_IF(DFATAL, !response->has_solver_specific_info()) << *response;
168 if (response->status() == MPSOLVER_FEASIBLE ||
169 response->status() == MPSOLVER_OPTIMAL) {
172 LOG(ERROR) <<
"LoadSolutionFromProto failed: " << result;
182 NonIncrementalChange();
186 NonIncrementalChange();
190 NonIncrementalChange();
194 NonIncrementalChange();
198 NonIncrementalChange();
202 NonIncrementalChange();
207 double new_value,
double old_value) {
208 NonIncrementalChange();
212 NonIncrementalChange();
217 NonIncrementalChange();
221 NonIncrementalChange();
231 LOG(DFATAL) <<
"Number of nodes only available for discrete problems";
270 if (num_threads < 1) {
271 return absl::InvalidArgumentError(
272 absl::StrCat(
"Invalid number of threads: ", num_threads));
275 return absl::OkStatus();
292void 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)
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
@ 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.
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)