Skip to content

Commit

Permalink
fix weighted_tardiness_sat crash
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Jun 8, 2018
1 parent d3ac9b6 commit 1ec337d
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions examples/cpp/weighted_tardiness_sat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,36 +212,54 @@ void Solve(const std::vector<int>& durations, const std::vector<int>& due_dates,
Model model;
model.Add(NewSatParameters(FLAGS_params));
model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& r) {
// Note that we conpute the "real" cost here and do not use the tardiness
// Note that we compute the "real" cost here and do not use the tardiness
// variables. This is because in the core based approach, the tardiness
// variable might be fixed before the end date, and we just have a >=
// relation.
auto min_value = [&r](int v) {
if (r.solution_size() > 0) {
return r.solution(v);
} else {
return r.solution_lower_bounds(v);
}
};
auto max_value = [&r](int v) {
if (r.solution_size() > 0) {
return r.solution(v);
} else {
return r.solution_upper_bounds(v);
}
};

int64 objective = 0;
for (int i = 0; i < num_tasks; ++i) {
objective += weights[i] * std::max<int64>(0ll, r.solution(tasks_end[i]) -
due_dates[i]);
CHECK_EQ(min_value(tasks_end[i]), max_value(tasks_end[i]));
const int64 end = min_value(tasks_end[i]);
objective += weights[i] * std::max<int64>(0ll, end - due_dates[i]);
}
LOG(INFO) << "Cost " << objective;

// Print the current solution.
std::vector<int> sorted_tasks(num_tasks);
std::iota(sorted_tasks.begin(), sorted_tasks.end(), 0);
std::sort(sorted_tasks.begin(), sorted_tasks.end(), [&](int v1, int v2) {
return r.solution(tasks_start[v1]) < r.solution(tasks_start[v2]);
CHECK_EQ(min_value(tasks_start[v1]), max_value(tasks_start[v1]));
CHECK_EQ(min_value(tasks_start[v2]), max_value(tasks_start[v2]));
return min_value(tasks_start[v1]) < min_value(tasks_start[v2]);
});
std::string solution = "0";
int end = 0;
for (const int i : sorted_tasks) {
const int64 cost = weights[i] * r.solution(tardiness_vars[i]);
const int64 cost = weights[i] * min_value(tardiness_vars[i]);
absl::StrAppend(&solution, "| #", i, " ");
if (cost > 0) {
// Display the cost in red.
absl::StrAppend(&solution, "\033[1;31m(+", cost, ") \033[0m");
}
absl::StrAppend(&solution, "|", r.solution(tasks_end[i]));
CHECK_EQ(end, r.solution(tasks_start[i]));
absl::StrAppend(&solution, "|", min_value(tasks_end[i]));
CHECK_EQ(end, min_value(tasks_start[i]));
end += durations[i];
CHECK_EQ(end, r.solution(tasks_end[i]));
CHECK_EQ(end, min_value(tasks_end[i]));
}
LOG(INFO) << "solution: " << solution;
}));
Expand Down

0 comments on commit 1ec337d

Please sign in to comment.