Google OR-Tools: ortools/linear_solver/highs_interface.cc Source File

1

2

3

4

5

6

7

8

9

10

11

12

13

14#include <atomic>

15#include <cstdint>

16#include <optional>

17#include <string>

18#include <utility>

19#include <vector>

20

21#include "absl/base/attributes.h"

22#include "absl/log/check.h"

23#include "absl/status/status.h"

24#include "absl/status/statusor.h"

25#include "absl/strings/str_cat.h"

32

34

36 public:

39

40

42

43

45 return interrupt == nullptr;

46 }

48 std::atomic<bool>* interrupt) override {

49 DCHECK_EQ(interrupt, nullptr);

50 const bool log_error = request->enable_internal_solver_output();

53 }

54

55

56 void Reset() override;

64 double new_value, double old_value) override;

67 double coefficient) override;

70

71

73 int64_t nodes() const override;

76

77

79 bool IsLP() const override;

80 bool IsMIP() const override;

81

84

88

97 const std::string& parameters) override;

98 absl::Status SetNumThreads(int num_threads) override;

99

100 private:

101 void NonIncrementalChange();

102

103 const bool solve_as_a_mip_;

104 std::optional<HighsSolveInfo> solve_info_;

105};

106

109

111

113

116

117

118 for (int i = 0; i < solver_->variables_.size(); ++i) {

120 }

121 for (int i = 0; i < solver_->constraints_.size(); ++i) {

123 }

124

126 solver_->ExportModelToProto(&model_proto);

128 *request.mutable_model() = std::move(model_proto);

132

136 solver_->solver_specific_parameter_string_);

137 if (solver_->time_limit()) {

139 static_cast<double>(solver_->time_limit()) / 1000.0);

140 }

141

142

144 absl::StatusOr<MPSolutionResponse> response =

146

147 if (!response.ok()) {

148 LOG(ERROR) << "Unexpected error solving with Highs: " << response.status();

150 }

151

152

155

158 const absl::Status result = solver_->LoadSolutionFromProto(*response);

159 if (!result.ok()) {

160 LOG(ERROR) << "LoadSolutionFromProto failed: " << result;

161 }

162 }

163

165}

166

169 solve_info_.reset();

170}

171

173 NonIncrementalChange();

174}

175

177 NonIncrementalChange();

178}

179

181 NonIncrementalChange();

182}

183

185 NonIncrementalChange();

186}

187

189 NonIncrementalChange();

190}

191

193 NonIncrementalChange();

194}

195

198 double new_value, double old_value) {

199 NonIncrementalChange();

200}

201

203 NonIncrementalChange();

204}

205

207 double coefficient) {

208 NonIncrementalChange();

209}

210

212 NonIncrementalChange();

213}

214

216

220

222 QCHECK(solve_info_.has_value())

223 << "Number of nodes only available after solve";

224 return solve_info_->mip_node_count;

225}

226

232

238

240

242

244

246

247

248

250

252

254

256

260

262 if (num_threads < 1) {

263 return absl::InvalidArgumentError(

264 absl::StrCat("Invalid number of threads: ", num_threads));

265 }

266

267 return absl::OkStatus();

268}

269

270

277

279 const std::string& parameters) {

280

281 return false;

282}

283

284void HighsInterface::NonIncrementalChange() {

285

287}

288

289namespace {

290

291

292const void* const kRegisterHighsLp ABSL_ATTRIBUTE_UNUSED = [] {

294 [](MPSolver* solver) { return new HighsInterface(solver, false); },

296 return nullptr;

297}();

298

299

300const void* const kRegisterHighsMip ABSL_ATTRIBUTE_UNUSED = [] {

304 return nullptr;

305}();

306

307}

308

309}

MPSolver::BasisStatus row_status(int constraint_index) const override

Definition highs_interface.cc:227

void SetConstraintBounds(int index, double lb, double ub) override

Definition highs_interface.cc:184

absl::Status SetNumThreads(int num_threads) override

Definition highs_interface.cc:261

int64_t nodes() const override

Definition highs_interface.cc:221

bool IsContinuous() const override

Definition highs_interface.cc:239

void AddVariable(MPVariable *var) override

Definition highs_interface.cc:192

void SetObjectiveCoefficient(const MPVariable *variable, double coefficient) override

Definition highs_interface.cc:206

bool IsMIP() const override

Definition highs_interface.cc:243

bool SupportsDirectlySolveProto(std::atomic< bool > *interrupt) const override

Definition highs_interface.cc:44

bool IsLP() const override

Definition highs_interface.cc:241

MPSolver::ResultStatus Solve(const MPSolverParameters &param) override

Definition highs_interface.cc:112

void SetCoefficient(MPConstraint *constraint, const MPVariable *variable, double new_value, double old_value) override

Definition highs_interface.cc:196

MPSolutionResponse DirectlySolveProto(LazyMutableCopy< MPModelRequest > request, std::atomic< bool > *interrupt) override

Definition highs_interface.cc:47

bool SetSolverSpecificParametersAsString(const std::string &parameters) override

Definition highs_interface.cc:278

int64_t iterations() const override

Definition highs_interface.cc:217

void SetPresolveMode(int value) override

Definition highs_interface.cc:276

void SetVariableInteger(int index, bool integer) override

Definition highs_interface.cc:180

void SetScalingMode(int value) override

Definition highs_interface.cc:273

void SetVariableBounds(int index, double lb, double ub) override

Definition highs_interface.cc:176

void * underlying_solver() override

Definition highs_interface.cc:249

std::string SolverVersion() const override

Definition highs_interface.cc:245

MPSolver::BasisStatus column_status(int variable_index) const override

Definition highs_interface.cc:233

void SetDualTolerance(double value) override

Definition highs_interface.cc:272

void SetRelativeMipGap(double value) override

Definition highs_interface.cc:275

void ClearObjective() override

Definition highs_interface.cc:215

HighsInterface(MPSolver *solver, bool solve_as_a_mip)

Definition highs_interface.cc:107

void SetParameters(const MPSolverParameters &param) override

Definition highs_interface.cc:257

~HighsInterface() override

Definition highs_interface.cc:110

void Reset() override

Definition highs_interface.cc:167

void ExtractNewVariables() override

Definition highs_interface.cc:251

void AddRowConstraint(MPConstraint *ct) override

Definition highs_interface.cc:188

void SetOptimizationDirection(bool maximize) override

Definition highs_interface.cc:172

void SetPrimalTolerance(double value) override

Definition highs_interface.cc:271

void ClearConstraint(MPConstraint *constraint) override

Definition highs_interface.cc:202

void ExtractObjective() override

Definition highs_interface.cc:255

void ExtractNewConstraints() override

Definition highs_interface.cc:253

void SetLpAlgorithm(int value) override

Definition highs_interface.cc:274

void SetObjectiveOffset(double value) override

Definition highs_interface.cc:211

void set_enable_internal_solver_output(bool value)

void set_solver_specific_parameters(Arg_ &&arg, Args_... args)

static constexpr SolverType HIGHS_LINEAR_PROGRAMMING

static constexpr SolverType HIGHS_MIXED_INTEGER_PROGRAMMING

::operations_research::MPModelProto *PROTOBUF_NONNULL mutable_model()

void set_solver_type(::operations_research::MPModelRequest_SolverType value)

void set_solver_time_limit_seconds(double value)

static MPSolverInterfaceFactoryRepository * GetInstance()

void Register(MPSolverInterfaceFactory factory, MPSolver::OptimizationProblemType problem_type, std::function< bool()> is_runtime_ready={})

void set_variable_as_extracted(int var_index, bool extracted)

friend class MPConstraint

void set_constraint_as_extracted(int ct_index, bool extracted)

void ResetExtractionInformation()

MPSolverInterface(MPSolver *solver)

void SetCommonParameters(const MPSolverParameters &param)

MPSolver::ResultStatus result_status_

SynchronizationStatus sync_status_

@ ABNORMAL

abnormal, i.e., error of some kind.

@ HIGHS_MIXED_INTEGER_PROGRAMMING

@ HIGHS_LINEAR_PROGRAMMING

The class for variables of a Mathematical Programming (MP) model.

absl::StatusOr< MPSolutionResponse > HighsSolveProto(LazyMutableCopy< MPModelRequest > request, HighsSolveInfo *solve_info)

MPSolutionResponse ConvertStatusOrMPSolutionResponse(bool log_error, absl::StatusOr< MPSolutionResponse > response)