Skip to content

Commit

Permalink
python: Provide incumbent solution in callback
Browse files Browse the repository at this point in the history
  • Loading branch information
few committed Aug 21, 2024
1 parent 53273e7 commit d712b85
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/call_highs_from_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def user_interrupt_callback(
print(f"userCallback(type {callback_type};")
print(f"data {local_callback_data:.4g}): {message}")
print(f"with objective {data_out.objective_function_value}")
print(f"and solution[0] = {data_out.mip_solution[0]}")
print(f"and solution = {data_out.mip_solution}")

# Check and update the objective function value
assert (
Expand Down
5 changes: 2 additions & 3 deletions src/highs_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1195,12 +1195,11 @@ PYBIND11_MODULE(_core, m) {
.def_property(
"mip_solution",
[](const HighsCallbackDataOut& self) -> py::array {
// XXX: This is clearly wrong, most likely we need to have the
// length as an input data parameter
return py::array(3, self.mip_solution);
return py::array(self.mip_solution_size, self.mip_solution);
},
[](HighsCallbackDataOut& self, py::array_t<double> new_mip_solution) {
self.mip_solution = new_mip_solution.mutable_data();
self.mip_solution_size = new_mip_solution.shape(0);
});
py::class_<HighsCallbackDataIn>(callbacks, "HighsCallbackDataIn")
.def(py::init<>())
Expand Down
1 change: 1 addition & 0 deletions src/lp_data/HighsCallbackStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct {
double mip_dual_bound;
double mip_gap;
double* mip_solution;
HighsInt mip_solution_size;
HighsInt cutpool_num_col;
HighsInt cutpool_num_cut;
HighsInt cutpool_num_nz;
Expand Down
5 changes: 5 additions & 0 deletions src/mip/HighsMipSolverData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ void HighsMipSolverData::runSetup() {
assert(!mipsolver.submip);
mipsolver.callback_->clearHighsCallbackDataOut();
mipsolver.callback_->data_out.mip_solution = mipsolver.solution_.data();
mipsolver.callback_->data_out.mip_solution_size =
mipsolver.solution_.size();
const bool interrupt = interruptFromCallbackWithData(
kCallbackMipSolution, mipsolver.solution_objective_,
"Feasible solution");
Expand Down Expand Up @@ -832,6 +834,7 @@ double HighsMipSolverData::transformNewIntegerFeasibleSolution(
mipsolver.callback_->active[kCallbackMipSolution]) {
mipsolver.callback_->clearHighsCallbackDataOut();
mipsolver.callback_->data_out.mip_solution = solution.col_value.data();
mipsolver.callback_->data_out.mip_solution_size = solution.col_value.size();
const bool interrupt = interruptFromCallbackWithData(
kCallbackMipSolution, mipsolver_objective_value, "Feasible solution");
assert(!interrupt);
Expand Down Expand Up @@ -1928,6 +1931,8 @@ void HighsMipSolverData::saveReportMipSolution(const double new_upper_limit) {
if (mipsolver.callback_->active[kCallbackMipImprovingSolution]) {
mipsolver.callback_->clearHighsCallbackDataOut();
mipsolver.callback_->data_out.mip_solution = mipsolver.solution_.data();
mipsolver.callback_->data_out.mip_solution_size =
mipsolver.solution_.size();
const bool interrupt = interruptFromCallbackWithData(
kCallbackMipImprovingSolution, mipsolver.solution_objective_,
"Improving solution");
Expand Down

0 comments on commit d712b85

Please sign in to comment.