Skip to content

Commit cd56b71

Browse files
committed
Fix //examples/cpp:integer_programming_test by implementing linear_solver::nodes() for HiGHS.
1 parent f0d89b3 commit cd56b71

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

ortools/linear_solver/highs_interface.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include <atomic>
1717
#include <cstdint>
18+
#include <optional>
1819
#include <string>
1920
#include <utility>
2021
#include <vector>
@@ -102,6 +103,7 @@ class HighsInterface : public MPSolverInterface {
102103
void NonIncrementalChange();
103104

104105
const bool solve_as_a_mip_;
106+
std::optional<HighsSolveInfo> solve_info_;
105107
};
106108

107109
HighsInterface::HighsInterface(MPSolver* const solver, bool solve_as_a_mip)
@@ -140,8 +142,9 @@ MPSolver::ResultStatus HighsInterface::Solve(const MPSolverParameters& param) {
140142
}
141143

142144
// Set parameters.
145+
solve_info_ = HighsSolveInfo();
143146
absl::StatusOr<MPSolutionResponse> response =
144-
HighsSolveProto(std::move(request));
147+
HighsSolveProto(std::move(request), &*solve_info_);
145148

146149
if (!response.ok()) {
147150
LOG(ERROR) << "Unexpected error solving with Highs: " << response.status();
@@ -163,7 +166,10 @@ MPSolver::ResultStatus HighsInterface::Solve(const MPSolverParameters& param) {
163166
return result_status_;
164167
}
165168

166-
void HighsInterface::Reset() { ResetExtractionInformation(); }
169+
void HighsInterface::Reset() {
170+
ResetExtractionInformation();
171+
solve_info_.reset();
172+
}
167173

168174
void HighsInterface::SetOptimizationDirection(bool maximize) {
169175
NonIncrementalChange();
@@ -215,8 +221,9 @@ int64_t HighsInterface::iterations() const {
215221
}
216222

217223
int64_t HighsInterface::nodes() const {
218-
LOG(DFATAL) << "Number of nodes only available for discrete problems";
219-
return MPSolverInterface::kUnknownNumberOfNodes;
224+
QCHECK(solve_info_.has_value())
225+
<< "Number of nodes only available after solve";
226+
return solve_info_->mip_node_count;
220227
}
221228

222229
MPSolver::BasisStatus HighsInterface::row_status(int constraint_index) const {

ortools/linear_solver/proto_solver/highs_proto_solver.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ absl::Status SetSolverSpecificParameters(const std::string& parameters,
4646
Highs& highs);
4747

4848
absl::StatusOr<MPSolutionResponse> HighsSolveProto(
49-
LazyMutableCopy<MPModelRequest> request) {
49+
LazyMutableCopy<MPModelRequest> request, HighsSolveInfo* solve_info) {
5050
MPSolutionResponse response;
5151
const std::optional<LazyMutableCopy<MPModelProto>> optional_model =
5252
GetMPModelOrPopulateResponse(request, &response);
@@ -262,6 +262,10 @@ absl::StatusOr<MPSolutionResponse> HighsSolveProto(
262262
}
263263
}
264264

265+
if (solve_info != nullptr) {
266+
solve_info->mip_node_count = highs.getInfo().mip_node_count;
267+
}
268+
265269
const absl::Duration solving_duration = absl::Now() - time_before;
266270
user_timer.Stop();
267271
response.mutable_solve_info()->set_solve_wall_time_seconds(

ortools/linear_solver/proto_solver/highs_proto_solver.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414
#ifndef OR_TOOLS_LINEAR_SOLVER_PROTO_SOLVER_HIGHS_PROTO_SOLVER_H_
1515
#define OR_TOOLS_LINEAR_SOLVER_PROTO_SOLVER_HIGHS_PROTO_SOLVER_H_
1616

17-
#include <functional>
18-
#include <string>
17+
#include <cstdint>
1918

2019
#include "absl/status/statusor.h"
2120
#include "ortools/linear_solver/linear_solver.pb.h"
2221
#include "ortools/util/lazy_mutable_copy.h"
2322

2423
namespace operations_research {
2524

26-
// Solve the input MIP model with the HIGHS solver.
25+
// Information about the Highs solve.
26+
struct HighsSolveInfo {
27+
int64_t mip_node_count; // The number of nodes generated by the MIP solver.
28+
};
29+
30+
// Solve the input MIP model with the HIGHS solver and fills `solve_info` is
31+
// provided.
2732
absl::StatusOr<MPSolutionResponse> HighsSolveProto(
28-
LazyMutableCopy<MPModelRequest> request);
33+
LazyMutableCopy<MPModelRequest> request,
34+
HighsSolveInfo* solve_info = nullptr);
2935

3036
} // namespace operations_research
3137

0 commit comments

Comments
 (0)