14 ConstraintCache&& cache,
15 const ConstraintGenerators& constraint_generators,
int verbosity,
16 ConstraintDest destination,
const ConstraintAddConf& add_conf, GRBCallbackPublic* cb, GRBModel* model
18 const bool in_callback_mode = destination == ConstraintDest::ViaCallback;
20 MaybeGRBCallback maybe_callback(in_callback_mode ? cb :
nullptr);
22 if (in_callback_mode) {
26 throw std::logic_error(
"need a model for non-lazy mode");
33 int re_add_avoids = 0;
35 bool isInteresting()
const {
return re_adds + novel + re_add_avoids != 0; }
36 int totalAdded()
const {
return re_adds + novel; }
39 std::map<TypeErasedConstraintEnum, Counts> constraint_numbers;
41 for (
auto& group_and_gen : constraint_generators) {
42 const auto& group = group_and_gen.first;
45 if (add_conf.add_modes.find(group) ==
end(add_conf.add_modes)) {
49 const auto& add_mode = add_conf.add_modes.at(group);
50 const bool is_callback_mode_group = add_mode == ConstraintAddMode::LazyViaCallback;
51 const bool add_via_lazy_callback = in_callback_mode && is_callback_mode_group;
52 const bool add_to_model_non_lazy = not in_callback_mode && ( add_mode == ConstraintAddMode::NotLazyToModel );
53 const bool add_to_model_lazy = not in_callback_mode && ( add_mode == ConstraintAddMode::LazyToModel1 || add_mode == ConstraintAddMode::LazyToModel2 || add_mode == ConstraintAddMode::LazyToModel3 );
54 const bool adding_lazy = add_via_lazy_callback || add_to_model_lazy;
58 not add_via_lazy_callback
59 && not add_to_model_non_lazy
60 && not add_to_model_lazy
65 auto& added_constraints = cache.added_constraints[group];
66 auto& constr_nums = constraint_numbers[group];
67 for (
auto& id_and_constraint : added_constraints) {
68 if (add_via_lazy_callback) {
69 cb->addLazy(id_and_constraint.second);
70 ++constr_nums.re_adds;
74 auto gen_result = group_and_gen.second.gen_func(ConstraintGeneratorParams{ maybe_callback });
75 for (
auto& id_and_constraint : gen_result.constraints) {
76 auto& c = id_and_constraint.second;
77 if (add_via_lazy_callback) {
78 if (std::find_if(
begin(added_constraints),
end(added_constraints), [&](
auto&& test) {
return *test.first == *id_and_constraint.first; }) ==
end(added_constraints)) {
80 added_constraints.emplace_back(std::move(id_and_constraint));
83 ++constr_nums.re_add_avoids;
86 if (add_to_model_non_lazy || add_to_model_lazy) {
87 std::stringstream name_ss;
88 name_ss << group <<
'_' << constr_nums.novel;
89 const auto& the_constraint = model->addConstr(c, name_ss.str());
92 if (add_to_model_lazy) {
93 const auto the_value = gurobiLazyIntValueOf(add_mode);
94 model->set(GRB_IntAttr_Lazy, &the_constraint, &the_value, 1);
101 const char* destination_string = in_callback_mode ?
"via callback" :
"to model";
102 const char* lazy_string = adding_lazy ?
" lazy" :
"";
103 const auto lazy_int_string = add_to_model_lazy ? (
" L=" +
std::to_string(gurobiLazyIntValueOf(add_mode))) : std::string();
105 if (constr_nums.isInteresting() && verbosity > 0) {
107 <<
"added this many" << lazy_string << lazy_int_string <<
" constraints " << destination_string <<
" for " << group
108 <<
": (re-add, novel, re-add-avoid) : " << constr_nums.re_adds <<
", " << constr_nums.novel <<
", " << constr_nums.re_add_avoids <<
'\n';
113 std::cout <<
"tried adding constraints from " << constraint_numbers.size() <<
" groups, with no constraints from";
114 std::vector<TypeErasedConstraintEnum> groups_with_no_constraints_added;
115 for (
const auto& group_and_nums : constraint_numbers) {
116 if (group_and_nums.second.totalAdded() != 0) {
continue; }
117 groups_with_no_constraints_added.push_back(group_and_nums.first);
120 std::cout << std::endl;
123 return std::move(cache);
130 using S = std::remove_cv_t<std::remove_reference_t<decltype(s)>>;
131 const auto hash = std::hash<S>{}(s);
132 using H = std::remove_cv_t<std::remove_reference_t<decltype(hash)>>;
133 using HNL = std::numeric_limits<H>;
134 const int print_radix = 16;
135 const int HASH_CHARS = HNL::digits / print_radix * HNL::radix;
137 const char hex_digits[] =
"0123456789abcdef";
138 for (
auto i = 0; i < HASH_CHARS; ++i) {
143 for (
auto& c : s)
if (c ==
':') c =
'-';