1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
#include <cstdio>
#include <cstring>
#include "HCheckConfig.h"
#include "Highs.h"
#include "catch.hpp"
#include "lp_data/HighsCallback.h"
const bool dev_run = false;
const double egout_optimal_objective = 568.1007;
const double egout_objective_target = 610;
const HighsInt adlittle_ipm_iteration_limit = 5;
const HighsInt adlittle_simplex_iteration_limit = 30;
const HighsInt kLogBufferSize = kIoBufferSize;
const HighsInt kUserCallbackNoData = -1;
const HighsInt kUserCallbackData = 99;
char printed_log[kLogBufferSize];
using std::memset;
using std::strcmp;
using std::strcpy;
using std::strlen;
using std::strncmp;
using std::strstr;
struct MipData {
HighsInt num_col;
HighsVarType* integrality;
};
// Callback that saves message for comparison
HighsCallbackFunctionType myLogCallback =
[](int callback_type, const std::string& message,
const HighsCallbackDataOut* data_out, HighsCallbackDataIn* data_in,
void* user_callback_data) { strcpy(printed_log, message.c_str()); };
HighsCallbackFunctionType userMipSolutionCallback =
[](int callback_type, const std::string& message,
const HighsCallbackDataOut* data_out, HighsCallbackDataIn* data_in,
void* user_callback_data) {
if (dev_run) {
printf(
"MipSolutionCallback with objective = %15.8g and bounds [%15.8g, "
"%15.8g]",
data_out->objective_function_value, data_out->mip_dual_bound,
data_out->mip_primal_bound);
MipData callback_data = *(static_cast<MipData*>(user_callback_data));
HighsInt num_col = callback_data.num_col;
HighsVarType* integrality = callback_data.integrality;
HighsInt num_integer = 0;
for (HighsInt iCol = 0; iCol < num_col; iCol++)
if (integrality[iCol] == HighsVarType::kInteger) num_integer++;
if (num_integer < 50) {
printf(" and solution [");
for (HighsInt iCol = 0; iCol < num_col; iCol++) {
if (integrality[iCol] != HighsVarType::kInteger) continue;
double value = data_out->mip_solution[iCol];
if (std::abs(value) < 1e-5) {
printf("0");
} else if (std::abs(value - 1) < 1e-5) {
printf("1");
} else {
bool printed = false;
for (HighsInt k = 2; k < 10; k++) {
if (std::abs(value - k) < 1e-5) {
printf("%1d", int(k));
printed = true;
}
}
if (printed) continue;
for (HighsInt k = 10; k < 999; k++) {
if (std::abs(value - k) < 1e-5) {
printf(" %d ", int(k));
printed = true;
}
}
if (printed) continue;
printf("*");
}
}
printf("]\n");
} else {
printf("\n");
}
fflush(stdout);
}
};
HighsCallbackFunctionType userInterruptCallback =
[](int callback_type, const std::string& message,
const HighsCallbackDataOut* data_out, HighsCallbackDataIn* data_in,
void* user_callback_data) {
// Extract local_callback_data from user_callback_data unless it
// is nullptr
if (callback_type == kCallbackMipImprovingSolution) {
// Use local_callback_data to maintain the objective value from
// the previous callback
assert(user_callback_data);
// Extract the double value pointed to from void* user_callback_data
const double local_callback_data = *(double*)user_callback_data;
if (dev_run)
printf(
"userCallback(type %2d; data %11.4g): %s with objective %g and "
"solution[0] = %g\n",
callback_type, local_callback_data, message.c_str(),
data_out->objective_function_value, data_out->mip_solution[0]);
REQUIRE(local_callback_data >= data_out->objective_function_value);
// Update the double value pointed to from void* user_callback_data
*(double*)user_callback_data = data_out->objective_function_value;
} else {
const int local_callback_data =
user_callback_data ? static_cast<int>(reinterpret_cast<intptr_t>(
user_callback_data))
: kUserCallbackNoData;
if (user_callback_data) {
REQUIRE(local_callback_data == kUserCallbackData);
} else {
REQUIRE(local_callback_data == kUserCallbackNoData);
}
if (callback_type == kCallbackLogging) {
if (dev_run) printf("Callback: %s", message.c_str());
// printf("userInterruptCallback(type %2d; data %2d): %s",
// callback_type, local_callback_data,
// message.c_str());
} else if (callback_type == kCallbackSimplexInterrupt) {
if (dev_run)
printf(
"userInterruptCallback(type %2d; data %2d): %s with iteration "
"count = "
"%d\n",
callback_type, local_callback_data, message.c_str(),
int(data_out->simplex_iteration_count));
data_in->user_interrupt = data_out->simplex_iteration_count >
adlittle_simplex_iteration_limit;
} else if (callback_type == kCallbackIpmInterrupt) {
if (dev_run)
printf(
"userInterruptCallback(type %2d; data %2d): %s with iteration "
"count = "
"%d\n",
callback_type, local_callback_data, message.c_str(),
int(data_out->ipm_iteration_count));
data_in->user_interrupt =
data_out->ipm_iteration_count > adlittle_ipm_iteration_limit;
} else if (callback_type == kCallbackMipInterrupt) {
if (dev_run)
printf(
"userInterruptCallback(type %2d; data %2d): %s with Bounds "
"(%11.4g, %11.4g); Gap = %11.4g; Objective = "
"%g\n",
callback_type, local_callback_data, message.c_str(),
data_out->mip_dual_bound, data_out->mip_primal_bound,
data_out->mip_gap, data_out->objective_function_value);
data_in->user_interrupt =
data_out->objective_function_value < egout_objective_target;
}
}
};
HighsCallbackFunctionType userMipCutPoolCallback =
[](int callback_type, const std::string& message,
const HighsCallbackDataOut* data_out, HighsCallbackDataIn* data_in,
void* user_callback_data) {
if (dev_run) {
printf("userMipCutPoolCallback: dim(%2d, %2d, %2d)\n",
int(data_out->cutpool_num_col), int(data_out->cutpool_num_cut),
int(data_out->cutpool_num_nz));
for (HighsInt iCut = 0; iCut < data_out->cutpool_num_cut; iCut++) {
printf("Cut %d\n", int(iCut));
for (HighsInt iEl = data_out->cutpool_start[iCut];
iEl < data_out->cutpool_start[iCut + 1]; iEl++) {
printf(" %2d %11.5g\n", int(data_out->cutpool_index[iEl]),
data_out->cutpool_value[iEl]);
}
}
}
};
std::function<void(int, const std::string&, const HighsCallbackDataOut*,
HighsCallbackDataIn*, void*)>
userDataCallback = [](int callback_type, const std::string& message,
const HighsCallbackDataOut* data_out,
HighsCallbackDataIn* data_in,
void* user_callback_data) {
assert(callback_type == kCallbackMipInterrupt ||
callback_type == kCallbackMipLogging ||
callback_type == kCallbackMipImprovingSolution);
if (dev_run)
printf(
"userDataCallback: Node count = %" PRId64
"; LP total iterations = %" PRId64
"; Time = %6.2f; "
"Bounds (%11.4g, %11.4g); Gap = %11.4g; Objective = %11.4g: %s\n",
data_out->mip_node_count, data_out->mip_total_lp_iterations,
data_out->running_time, data_out->mip_dual_bound,
data_out->mip_primal_bound, data_out->mip_gap,
data_out->objective_function_value, message.c_str());
};
TEST_CASE("my-callback-logging", "[highs-callback]") {
bool output_flag = true; // Still runs quietly
bool log_to_console = false;
HighsInt log_dev_level = kHighsLogDevLevelInfo;
HighsLogOptions log_options;
log_options.clear();
log_options.log_stream = stdout;
log_options.output_flag = &output_flag;
log_options.log_to_console = &log_to_console;
log_options.log_dev_level = &log_dev_level;
log_options.user_callback = myLogCallback;
log_options.user_callback_active = true;
highsLogDev(log_options, HighsLogType::kInfo, "Hi %s!", "HiGHS");
if (dev_run) printf("Log callback yields \"%s\"\n", printed_log);
REQUIRE(strcmp(printed_log, "Hi HiGHS!") == 0);
// Check that nothing is printed if the type is VERBOSE when
// log_dev_level is kHighsLogDevLevelInfo;
*printed_log = '\0';
highsLogDev(log_options, HighsLogType::kVerbose, "Hi %s!", "HiGHS");
REQUIRE(*printed_log == '\0');
{
char long_message[sizeof(printed_log)];
memset(long_message, 'H', sizeof(long_message));
long_message[sizeof(long_message) - 2] = '\0';
long_message[sizeof(long_message) - 1] = '\n';
highsLogDev(log_options, HighsLogType::kInfo, long_message);
if (dev_run) printf("Log callback yields \"%s\"\n", printed_log);
REQUIRE(strncmp(printed_log, "HHHH", 4) == 0);
REQUIRE(strlen(printed_log) <= sizeof(printed_log));
}
highsLogUser(log_options, HighsLogType::kInfo, "Hello %s!\n", "HiGHS");
REQUIRE(strlen(printed_log) > 9);
REQUIRE(strcmp(printed_log, "Hello HiGHS!\n") == 0);
{
char long_message[sizeof(printed_log)];
memset(long_message, 'H', sizeof(long_message));
long_message[sizeof(long_message) - 2] = '\0';
long_message[sizeof(long_message) - 1] = '\n';
highsLogUser(log_options, HighsLogType::kWarning, long_message);
if (dev_run) printf("Log callback yields \"%s\"\n", printed_log);
REQUIRE(strstr(printed_log, "HHHH") != nullptr);
REQUIRE(strlen(printed_log) <= sizeof(printed_log));
}
}
TEST_CASE("highs-callback-logging", "[highs-callback]") {
// Uses userInterruptCallback to start logging lines with
// "userInterruptCallback(kUserCallbackData): " since
// Highs::setCallback has second argument p_user_callback_data
std::string filename = std::string(HIGHS_DIR) + "/check/instances/avgas.mps";
int user_callback_data = kUserCallbackData;
void* p_user_callback_data =
reinterpret_cast<void*>(static_cast<intptr_t>(user_callback_data));
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setCallback(userInterruptCallback, p_user_callback_data);
highs.startCallback(kCallbackLogging);
highs.readModel(filename);
highs.run();
}
TEST_CASE("highs-callback-solution-basis-logging", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/avgas.mps";
int user_callback_data = kUserCallbackData;
void* p_user_callback_data =
reinterpret_cast<void*>(static_cast<intptr_t>(user_callback_data));
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.readModel(filename);
highs.run();
highs.setCallback(userInterruptCallback, p_user_callback_data);
highs.startCallback(kCallbackLogging);
if (dev_run) highs.writeSolution("", kSolutionStylePretty);
if (dev_run) highs.writeBasis("");
}
TEST_CASE("highs-callback-simplex-interrupt", "[highs-callback]") {
std::string filename =
std::string(HIGHS_DIR) + "/check/instances/adlittle.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setCallback(userInterruptCallback);
highs.startCallback(kCallbackSimplexInterrupt);
highs.readModel(filename);
HighsStatus status = highs.run();
REQUIRE(status == HighsStatus::kWarning);
REQUIRE(highs.getModelStatus() == HighsModelStatus::kInterrupt);
REQUIRE(highs.getInfo().simplex_iteration_count >
adlittle_simplex_iteration_limit);
}
TEST_CASE("highs-callback-ipm-interrupt", "[highs-callback]") {
std::string filename =
std::string(HIGHS_DIR) + "/check/instances/adlittle.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setCallback(userInterruptCallback);
highs.startCallback(kCallbackIpmInterrupt);
highs.readModel(filename);
highs.setOptionValue("solver", kIpmString);
HighsStatus status = highs.run();
REQUIRE(status == HighsStatus::kWarning);
REQUIRE(highs.getModelStatus() == HighsModelStatus::kInterrupt);
REQUIRE(highs.getInfo().ipm_iteration_count > adlittle_ipm_iteration_limit);
}
TEST_CASE("highs-callback-mip-interrupt", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/egout.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setOptionValue("presolve", kHighsOffString);
highs.setCallback(userInterruptCallback);
highs.startCallback(kCallbackMipInterrupt);
highs.readModel(filename);
HighsStatus status = highs.run();
REQUIRE(status == HighsStatus::kWarning);
REQUIRE(highs.getModelStatus() == HighsModelStatus::kInterrupt);
REQUIRE(highs.getInfo().objective_function_value > egout_optimal_objective);
}
TEST_CASE("highs-callback-mip-improving", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/egout.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setOptionValue("presolve", kHighsOffString);
double user_callback_data = kHighsInf;
void* p_user_callback_data = (void*)(&user_callback_data);
highs.setCallback(userInterruptCallback, p_user_callback_data);
highs.startCallback(kCallbackMipImprovingSolution);
highs.readModel(filename);
highs.run();
}
TEST_CASE("highs-callback-mip-data", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/egout.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setOptionValue("presolve", kHighsOffString);
highs.setCallback(userDataCallback);
highs.startCallback(kCallbackMipImprovingSolution);
highs.startCallback(kCallbackMipLogging);
highs.readModel(filename);
highs.run();
}
TEST_CASE("highs-callback-mip-solution", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/egout.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.setOptionValue("presolve", kHighsOffString);
highs.readModel(filename);
// To print the values of the integer variables in the callback,
// need the number of columns and the integrality. Set this up in a
// struct to be passed via user_callback_data
HighsLp lp = highs.getLp();
MipData user_callback_data;
user_callback_data.num_col = int(lp.num_col_);
user_callback_data.integrality = lp.integrality_.data();
void* p_user_callback_data = &user_callback_data;
highs.setCallback(userMipSolutionCallback, p_user_callback_data);
highs.startCallback(kCallbackMipSolution);
highs.run();
}
TEST_CASE("highs-callback-mip-cut-pool", "[highs-callback]") {
std::string filename = std::string(HIGHS_DIR) + "/check/instances/flugpl.mps";
Highs highs;
highs.setOptionValue("output_flag", dev_run);
highs.readModel(filename);
// MipData user_callback_data;
highs.setCallback(userMipCutPoolCallback); //, p_user_callback_data);
highs.startCallback(kCallbackMipGetCutPool);
highs.run();
}
|