Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Provide incumbent solution in callback #1882

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading