Google OR-Tools: ortools/linear_solver/bop_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 <memory>

17#include <string>

18#include <utility>

19#include <vector>

20

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

26

28namespace {

29

31 switch (status) {

42 }

43 LOG(DFATAL) << "Invalid bop::BopSolveStatus";

45}

46

47}

48

50 public:

53

54

56

57

58 void Reset() override;

66 double new_value, double old_value) override;

69 double coefficient) override;

72

73

75 int64_t nodes() const override;

78

79

81 bool IsLP() const override;

82 bool IsMIP() const override;

83

87

91

100 const std::string& parameters) override;

101

102 private:

103 void NonIncrementalChange();

104

107 std::vector<MPSolver::BasisStatus> column_status_;

108 std::vector<MPSolver::BasisStatus> row_status_;

110 std::atomic<bool> interrupt_solver_;

111};

112

115 linear_program_(),

116 bop_solver_(),

117 column_status_(),

118 row_status_(),

119 parameters_(),

120 interrupt_solver_(false) {}

121

123

125

126 if (interrupt_solver_) {

128

129

132 }

133

134

138

139 linear_program_.SetMaximizationProblem(maximize_);

140 linear_program_.CleanUp();

141

142

143 if (solver_->time_limit()) {

144 VLOG(1) << "Setting time limit = " << solver_->time_limit() << " ms.";

145 parameters_.set_max_time_in_seconds(

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

147 }

148 parameters_.set_log_search_progress(!quiet());

149

151 if (!solver_->solution_hint_.empty()) {

152 const int num_vars = solver_->variables_.size();

153 if (solver_->solution_hint_.size() != num_vars) {

154 LOG(WARNING) << "Bop currently doesn't handle partial solution hints. "

155 << "Filling the missing positions with zeros...";

156 }

158 for (const std::pair<const MPVariable*, double>& p :

159 solver_->solution_hint_) {

160 initial_solution[glop::ColIndex(p.first->index())] =

162 }

163 }

164

165 solver_->SetSolverSpecificParametersAsString(

166 solver_->solver_specific_parameter_string_);

167 bop_solver_.SetParameters(parameters_);

168 std::unique_ptr<TimeLimit> time_limit =

170 time_limit->RegisterExternalBooleanAsLimit(&interrupt_solver_);

172 initial_solution.empty()

173 ? bop_solver_.SolveWithTimeLimit(linear_program_, time_limit.get())

174 : bop_solver_.SolveWithTimeLimit(linear_program_, initial_solution,

175 time_limit.get());

176

177

182

185

186

187 const size_t num_vars = solver_->variables_.size();

189 for (int var_id = 0; var_id < num_vars; ++var_id) {

191 const glop::ColIndex lp_solver_var_id(var->index());

193 bop_solver_.variable_values()[lp_solver_var_id];

195 }

196

197

198 const size_t num_constraints = solver_->constraints_.size();

200 }

201

203}

204

207 linear_program_.Clear();

208 interrupt_solver_ = false;

209}

210

212 NonIncrementalChange();

213}

214

216 NonIncrementalChange();

217}

218

220 NonIncrementalChange();

221}

222

224 NonIncrementalChange();

225}

226

228 NonIncrementalChange();

229}

230

232 NonIncrementalChange();

233}

234

237 double new_value, double old_value) {

238 NonIncrementalChange();

239}

240

242 NonIncrementalChange();

243}

244

246 double coefficient) {

247 NonIncrementalChange();

248}

249

251

253

255 LOG(DFATAL) << "Number of iterations not available";

257}

258

260 LOG(DFATAL) << "Number of nodes not available";

262}

263

265 return row_status_[constraint_index];

266}

267

269 return column_status_[variable_index];

270}

271

275

277

278 return "Bop-0.0";

279}

280

282 interrupt_solver_ = true;

283 return true;

284}

285

287

288

292

293 const glop::ColIndex num_cols(solver_->variables_.size());

296 const glop::ColIndex new_col = linear_program_.CreateNewVariable();

297 DCHECK_EQ(new_col, col);

299 linear_program_.SetVariableBounds(col, var->lb(), var->ub());

301 linear_program_.SetVariableType(

303 }

304 }

305}

306

307

310

311 const glop::RowIndex num_rows(solver_->constraints_.size());

312 for (glop::RowIndex row(0); row < num_rows; ++row) {

315

316 const double lb = ct->lb();

317 const double ub = ct->ub();

318 const glop::RowIndex new_row = linear_program_.CreateNewConstraint();

319 DCHECK_EQ(new_row, row);

320 linear_program_.SetConstraintBounds(row, lb, ub);

321

322 for (const auto& entry : ct->coefficients_) {

323 const int var_index = entry.first->index();

325 const glop::ColIndex col(var_index);

326 const double coeff = entry.second;

327 linear_program_.SetCoefficient(row, col, coeff);

328 }

329 }

330}

331

332

334 linear_program_.SetObjectiveOffset(solver_->Objective().offset());

335 for (const auto& entry : solver_->objective_->coefficients_) {

336 const int var_index = entry.first->index();

337 const glop::ColIndex col(var_index);

338 const double coeff = entry.second;

339 linear_program_.SetObjectiveCoefficient(col, coeff);

340 }

341}

342

344 parameters_.Clear();

346}

347

348

354

356 switch (value) {

358

359 break;

361

362 break;

363 default:

366 }

367 }

368}

369

371 const std::string& parameters) {

372 const bool ok =

373 google::protobuf::TextFormat::MergeFromString(parameters, &parameters_);

374 bop_solver_.SetParameters(parameters_);

375 return ok;

376}

377

378void BopInterface::NonIncrementalChange() {

379

381}

382

383namespace {

384

385

386const void* const kRegisterBop ABSL_ATTRIBUTE_UNUSED = [] {

388 [](MPSolver* const solver) { return new BopInterface(solver); },

390 return nullptr;

391}();

392

393}

394

395}

bool IsMIP() const override

Definition bop_interface.cc:274

void SetParameters(const MPSolverParameters &param) override

Definition bop_interface.cc:343

bool InterruptSolve() override

Definition bop_interface.cc:281

void SetRelativeMipGap(double value) override

Definition bop_interface.cc:353

void SetScalingMode(int value) override

Definition bop_interface.cc:351

bool IsLP() const override

Definition bop_interface.cc:273

MPSolver::BasisStatus row_status(int constraint_index) const override

Definition bop_interface.cc:264

void AddVariable(MPVariable *var) override

Definition bop_interface.cc:231

void Reset() override

Definition bop_interface.cc:205

void * underlying_solver() override

Definition bop_interface.cc:286

int64_t nodes() const override

Definition bop_interface.cc:259

void SetVariableInteger(int index, bool integer) override

Definition bop_interface.cc:219

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

Definition bop_interface.cc:245

void ExtractNewVariables() override

Definition bop_interface.cc:289

std::string SolverVersion() const override

Definition bop_interface.cc:276

void ClearObjective() override

Definition bop_interface.cc:252

int64_t iterations() const override

Definition bop_interface.cc:254

void ExtractObjective() override

Definition bop_interface.cc:333

BopInterface(MPSolver *solver)

Definition bop_interface.cc:113

void SetLpAlgorithm(int value) override

Definition bop_interface.cc:352

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

Definition bop_interface.cc:235

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

Definition bop_interface.cc:215

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

Definition bop_interface.cc:370

void SetOptimizationDirection(bool maximize) override

Definition bop_interface.cc:211

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

Definition bop_interface.cc:223

~BopInterface() override

Definition bop_interface.cc:122

void SetDualTolerance(double value) override

Definition bop_interface.cc:350

bool IsContinuous() const override

Definition bop_interface.cc:272

void SetPresolveMode(int value) override

Definition bop_interface.cc:355

void ClearConstraint(MPConstraint *constraint) override

Definition bop_interface.cc:241

MPSolver::BasisStatus column_status(int variable_index) const override

Definition bop_interface.cc:268

void ExtractNewConstraints() override

Definition bop_interface.cc:308

void SetObjectiveOffset(double value) override

Definition bop_interface.cc:250

void AddRowConstraint(MPConstraint *ct) override

Definition bop_interface.cc:227

void SetPrimalTolerance(double value) override

Definition bop_interface.cc:349

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

Definition bop_interface.cc:124

double lb() const

Returns the lower bound.

double ub() const

Returns the upper bound.

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)

static constexpr int64_t kUnknownNumberOfIterations

friend class MPConstraint

void set_constraint_as_extracted(int ct_index, bool extracted)

void ResetExtractionInformation()

virtual void SetIntegerParamToUnsupportedValue(MPSolverParameters::IntegerParam param, int value)

int last_constraint_index_

bool variable_is_extracted(int var_index) const

static constexpr int64_t kUnknownNumberOfNodes

double best_objective_bound_

MPSolverInterface(MPSolver *solver)

void SetCommonParameters(const MPSolverParameters &param)

MPSolver::ResultStatus result_status_

SynchronizationStatus sync_status_

@ PRESOLVE_OFF

Presolve is off.

@ PRESOLVE_ON

Presolve is on.

static const int kDefaultIntegerParamValue

@ PRESOLVE

Advanced usage: presolve mode.

@ FEASIBLE

feasible, or stopped by limit.

@ NOT_SOLVED

not been solved yet.

@ INFEASIBLE

proven infeasible.

@ ABNORMAL

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

@ BOP_INTEGER_PROGRAMMING

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

bool integer() const

Returns the integrality requirement of the variable.

double lb() const

Returns the lower bound.

double ub() const

Returns the upper bound.

void set_solution_value(double value)

int index() const

Returns the index of the variable in the MPSolver::variables_.

static std::unique_ptr< TimeLimit > FromParameters(const Parameters &parameters)

void assign(IntType size, const T &v)

@ FEASIBLE_SOLUTION_FOUND

StrictITIVector< ColIndex, Fractional > DenseRow