22#include "absl/container/flat_hash_map.h"
23#include "absl/status/status.h"
24#include "absl/strings/str_format.h"
25#include "absl/time/clock.h"
26#include "absl/time/time.h"
35#include "ortools/constraint_solver/demon_profiler.pb.h"
40 Container(
const Constraint* ct_, int64_t value_) :
ct(ct_),
value(value_) {}
41 bool operator<(
const Container& c)
const {
return value > c.value; }
55 active_constraint_(nullptr),
56 active_demon_(nullptr),
57 start_time_ns_(
absl::GetCurrentTimeNanos()) {}
61 constraint_map_.end());
67 return (absl::GetCurrentTimeNanos() - start_time_ns_) / 1000;
76 CHECK(active_constraint_ ==
nullptr);
77 CHECK(active_demon_ ==
nullptr);
78 CHECK(constraint !=
nullptr);
79 ConstraintRuns*
const ct_run =
new ConstraintRuns;
80 ct_run->set_constraint_id(constraint->
DebugString());
81 ct_run->add_initial_propagation_start_time(
CurrentTime());
82 active_constraint_ = constraint;
83 constraint_map_[constraint] = ct_run;
87 CHECK(active_constraint_ !=
nullptr);
88 CHECK(active_demon_ ==
nullptr);
89 CHECK(constraint !=
nullptr);
90 CHECK_EQ(constraint, active_constraint_);
91 ConstraintRuns*
const ct_run = constraint_map_[constraint];
92 if (ct_run !=
nullptr) {
93 ct_run->add_initial_propagation_end_time(
CurrentTime());
94 ct_run->set_failures(0);
96 active_constraint_ =
nullptr;
105 CHECK(active_constraint_ ==
nullptr);
106 CHECK(active_demon_ ==
nullptr);
107 CHECK(constraint !=
nullptr);
108 CHECK(delayed !=
nullptr);
109 ConstraintRuns*
const ct_run = constraint_map_[constraint];
110 ct_run->add_initial_propagation_start_time(
CurrentTime());
111 active_constraint_ = constraint;
116 CHECK(active_constraint_ !=
nullptr);
117 CHECK(active_demon_ ==
nullptr);
118 CHECK(constraint !=
nullptr);
119 CHECK(delayed !=
nullptr);
120 CHECK_EQ(constraint, active_constraint_);
121 ConstraintRuns*
const ct_run = constraint_map_[constraint];
122 if (ct_run !=
nullptr) {
123 ct_run->add_initial_propagation_end_time(
CurrentTime());
124 ct_run->set_failures(0);
126 active_constraint_ =
nullptr;
134 if (demon_map_.find(demon) == demon_map_.end()) {
135 CHECK(active_constraint_ !=
nullptr);
136 CHECK(active_demon_ ==
nullptr);
137 CHECK(demon !=
nullptr);
138 ConstraintRuns*
const ct_run = constraint_map_[active_constraint_];
139 DemonRuns*
const demon_run = ct_run->add_demons();
141 demon_run->set_failures(0);
142 demon_map_[demon] = demon_run;
143 demons_per_constraint_[active_constraint_].push_back(demon_run);
148 CHECK(demon !=
nullptr);
152 CHECK(active_demon_ ==
nullptr);
153 active_demon_ = demon;
154 DemonRuns*
const demon_run = demon_map_[active_demon_];
155 if (demon_run !=
nullptr) {
161 CHECK(demon !=
nullptr);
166 DemonRuns*
const demon_run = demon_map_[active_demon_];
167 if (demon_run !=
nullptr) {
170 active_demon_ =
nullptr;
179 if (active_demon_ !=
nullptr) {
180 DemonRuns*
const demon_run = demon_map_[active_demon_];
181 if (demon_run !=
nullptr) {
183 demon_run->set_failures(demon_run->failures() + 1);
185 active_demon_ =
nullptr;
187 active_constraint_ =
nullptr;
188 }
else if (active_constraint_ !=
nullptr) {
189 ConstraintRuns*
const ct_run = constraint_map_[active_constraint_];
190 if (ct_run !=
nullptr) {
191 ct_run->add_initial_propagation_end_time(
CurrentTime());
192 ct_run->set_failures(1);
194 active_constraint_ =
nullptr;
201 constraint_map_.end());
202 constraint_map_.clear();
204 demons_per_constraint_.clear();
211 int64_t new_max)
override {}
220 const std::vector<int64_t>& values)
override {}
222 const std::vector<int64_t>& values)
override {}
227 int64_t new_max)
override {}
231 int64_t new_max)
override {}
235 int64_t new_max)
override {}
242 const std::vector<int>& rank_last,
243 const std::vector<int>& unperformed)
override {}
248 CHECK(demon !=
nullptr);
249 DemonRuns*
const demon_run = demon_map_[demon];
250 CHECK(demon_run !=
nullptr);
251 demon_run->add_start_time(start_time);
252 demon_run->add_end_time(end_time);
254 demon_run->set_failures(demon_run->failures() + 1);
260 const char*
const kConstraintFormat =
261 " - Constraint: %s\n failures=%d, initial propagation "
262 "runtime=%d us, demons=%d, demon invocations=%d, total demon "
264 const char*
const kDemonFormat =
265 " --- Demon: %s\n invocations=%d, failures=%d, total "
266 "runtime=%d us, [average=%.2lf, median=%.2lf, stddev=%.2lf]\n";
268 const std::string
model =
272 std::vector<Container> to_sort;
274 ConstraintRuns*>::const_iterator it =
275 constraint_map_.begin();
276 it != constraint_map_.end(); ++it) {
279 int64_t demon_invocations = 0;
280 int64_t initial_propagation_runtime = 0;
281 int64_t total_demon_runtime = 0;
284 &demon_invocations, &total_demon_runtime,
287 Container(
ct, total_demon_runtime + initial_propagation_runtime));
289 std::sort(to_sort.begin(), to_sort.end());
291 for (
int i = 0; i < to_sort.size(); ++i) {
294 int64_t demon_invocations = 0;
295 int64_t initial_propagation_runtime = 0;
296 int64_t total_demon_runtime = 0;
299 &demon_invocations, &total_demon_runtime,
301 const std::string constraint_message =
302 absl::StrFormat(kConstraintFormat,
ct->DebugString(), fails,
303 initial_propagation_runtime, demon_count,
304 demon_invocations, total_demon_runtime);
307 const std::vector<DemonRuns*>& demons = demons_per_constraint_[
ct];
308 const int demon_size = demons.size();
309 for (
int demon_index = 0; demon_index < demon_size; ++demon_index) {
310 DemonRuns*
const demon_runs = demons[demon_index];
311 int64_t invocations = 0;
314 double mean_runtime = 0;
315 double median_runtime = 0;
316 double standard_deviation = 0.0;
318 &mean_runtime, &median_runtime,
319 &standard_deviation);
320 const std::string
runs = absl::StrFormat(
321 kDemonFormat, demon_runs->demon_id(), invocations, fails, runtime,
322 mean_runtime, median_runtime, standard_deviation);
332 int64_t*
const fails,
333 int64_t*
const initial_propagation_runtime,
334 int64_t*
const demon_invocations,
335 int64_t*
const total_demon_runtime,
int* demons) {
336 CHECK(constraint !=
nullptr);
337 ConstraintRuns*
const ct_run = constraint_map_[constraint];
338 CHECK(ct_run !=
nullptr);
339 *demon_invocations = 0;
340 *fails = ct_run->failures();
341 *initial_propagation_runtime = 0;
342 for (
int i = 0; i < ct_run->initial_propagation_start_time_size(); ++i) {
343 *initial_propagation_runtime += ct_run->initial_propagation_end_time(i) -
344 ct_run->initial_propagation_start_time(i);
346 *total_demon_runtime = 0;
349 *demons = ct_run->demons_size();
350 CHECK_EQ(*demons, demons_per_constraint_[constraint].size());
351 for (
int demon_index = 0; demon_index < *demons; ++demon_index) {
352 const DemonRuns& demon_runs = ct_run->demons(demon_index);
353 *fails += demon_runs.failures();
354 CHECK_EQ(demon_runs.start_time_size(), demon_runs.end_time_size());
355 const int runs = demon_runs.start_time_size();
356 *demon_invocations +=
runs;
357 for (
int run_index = 0; run_index <
runs; ++run_index) {
358 const int64_t demon_time =
359 demon_runs.end_time(run_index) - demon_runs.start_time(run_index);
360 *total_demon_runtime += demon_time;
366 int64_t*
const demon_invocations, int64_t*
const fails,
367 int64_t*
const total_demon_runtime,
368 double*
const mean_demon_runtime,
369 double*
const median_demon_runtime,
370 double*
const stddev_demon_runtime) {
371 CHECK(demon_runs !=
nullptr);
372 CHECK_EQ(demon_runs->start_time_size(), demon_runs->end_time_size());
374 const int runs = demon_runs->start_time_size();
375 *demon_invocations =
runs;
376 *fails = demon_runs->failures();
377 *total_demon_runtime = 0;
378 *mean_demon_runtime = 0.0;
379 *median_demon_runtime = 0.0;
380 *stddev_demon_runtime = 0.0;
381 std::vector<double> runtimes;
382 for (
int run_index = 0; run_index <
runs; ++run_index) {
383 const int64_t demon_time =
384 demon_runs->end_time(run_index) - demon_runs->start_time(run_index);
385 *total_demon_runtime += demon_time;
386 runtimes.push_back(demon_time);
389 if (!runtimes.empty()) {
390 *mean_demon_runtime = (1.0L * *total_demon_runtime) / runtimes.size();
393 std::sort(runtimes.begin(), runtimes.end());
394 const int pivot = runtimes.size() / 2;
396 if (runtimes.size() == 1) {
397 *median_demon_runtime = runtimes[0];
399 *median_demon_runtime =
400 runtimes.size() % 2 == 1
402 : (runtimes[pivot - 1] + runtimes[pivot]) / 2.0;
406 double total_deviation = 0.0f;
408 for (
int i = 0; i < runtimes.size(); ++i) {
409 total_deviation += pow(runtimes[i] - *mean_demon_runtime, 2);
412 *stddev_demon_runtime = sqrt(total_deviation / runtimes.size());
421 std::string
DebugString()
const override {
return "DemonProfiler"; }
425 Demon* active_demon_;
426 const int64_t start_time_ns_;
427 absl::flat_hash_map<const Constraint*, ConstraintRuns*> constraint_map_;
428 absl::flat_hash_map<const Demon*, DemonRuns*> demon_map_;
429 absl::flat_hash_map<const Constraint*, std::vector<DemonRuns*> >
430 demons_per_constraint_;
434 if (demon_profiler_ !=
nullptr) {
454 CHECK(demon !=
nullptr);
456 propagation_monitor_->RegisterDemon(demon);
469 int64_t start_time, int64_t end_time,
471 monitor->
AddFakeRun(demon, start_time, end_time, is_fail);
476 int64_t*
const fails,
477 int64_t*
const initial_propagation_runtime,
478 int64_t*
const demon_invocations,
479 int64_t*
const total_demon_runtime,
480 int*
const demon_count) {
482 demon_invocations, total_demon_runtime,
#define CHECK_EQ(val1, val2)
A constraint is the main modeling object.
std::string DebugString() const override
A Demon is the base element of a propagation queue.
virtual Solver::DemonPriority priority() const
This method returns the priority of the demon.
std::string DebugString() const override
void BeginFail() override
Just when the failure occurs.
void SetDurationMax(IntervalVar *const var, int64_t new_max) override
void Install() override
Install itself on the solver.
void SetDurationRange(IntervalVar *const var, int64_t new_min, int64_t new_max) override
void SetStartMax(IntervalVar *const var, int64_t new_max) override
void SetMin(IntVar *const var, int64_t new_min) override
IntVar modifiers.
void RestartSearch() override
Restart the search.
void AddFakeRun(Demon *const demon, int64_t start_time, int64_t end_time, bool is_fail)
void SetValue(IntVar *const var, int64_t value) override
void PopContext() override
void SetEndMax(IntervalVar *const var, int64_t new_max) override
void EndProcessingIntegerVariable(IntVar *const var) override
void SetStartMin(IntervalVar *const var, int64_t new_min) override
IntervalVar modifiers.
void SetEndRange(IntervalVar *const var, int64_t new_min, int64_t new_max) override
void SetMin(IntExpr *const expr, int64_t new_min) override
IntExpr modifiers.
void SetPerformed(IntervalVar *const var, bool value) override
void BeginConstraintInitialPropagation(Constraint *const constraint) override
Propagation events.
void SetRange(IntVar *const var, int64_t new_min, int64_t new_max) override
void EndConstraintInitialPropagation(Constraint *const constraint) override
void SetMax(IntVar *const var, int64_t new_max) override
void StartProcessingIntegerVariable(IntVar *const var) override
void RegisterDemon(Demon *const demon) override
void ExportInformation(const DemonRuns *const demon_runs, int64_t *const demon_invocations, int64_t *const fails, int64_t *const total_demon_runtime, double *const mean_demon_runtime, double *const median_demon_runtime, double *const stddev_demon_runtime)
void EndDemonRun(Demon *const demon) override
void SetStartRange(IntervalVar *const var, int64_t new_min, int64_t new_max) override
void RankSequence(SequenceVar *const var, const std::vector< int > &rank_first, const std::vector< int > &rank_last, const std::vector< int > &unperformed) override
void BeginDemonRun(Demon *const demon) override
void SetDurationMin(IntervalVar *const var, int64_t new_min) override
void ExportInformation(const Constraint *const constraint, int64_t *const fails, int64_t *const initial_propagation_runtime, int64_t *const demon_invocations, int64_t *const total_demon_runtime, int *demons)
void RankLast(SequenceVar *const var, int index) override
void PushContext(const std::string &context) override
void EndNestedConstraintInitialPropagation(Constraint *const constraint, Constraint *const delayed) override
void RankNotLast(SequenceVar *const var, int index) override
void RemoveValues(IntVar *const var, const std::vector< int64_t > &values) override
void SetMax(IntExpr *const expr, int64_t new_max) override
void BeginNestedConstraintInitialPropagation(Constraint *const constraint, Constraint *const delayed) override
int64_t CurrentTime() const
void RemoveValue(IntVar *const var, int64_t value) override
DemonProfiler(Solver *const solver)
~DemonProfiler() override
void SetValues(IntVar *const var, const std::vector< int64_t > &values) override
void RankFirst(SequenceVar *const var, int index) override
SequenceVar modifiers.
void SetEndMin(IntervalVar *const var, int64_t new_min) override
void SetRange(IntExpr *const expr, int64_t new_min, int64_t new_max) override
std::string DebugString() const override
void RankNotFirst(SequenceVar *const var, int index) override
void RemoveInterval(IntVar *const var, int64_t imin, int64_t imax) override
void PrintOverview(Solver *const solver, const std::string &filename)
The class IntExpr is the base of all integer expressions in constraint programming.
The class IntVar is a subset of IntExpr.
Interval variables are often used in scheduling.
virtual void Install()
Registers itself on the solver such that it gets notified of the search and propagation events.
A sequence variable is a variable whose domain is a set of possible orderings of the interval variabl...
@ VAR_PRIORITY
VAR_PRIORITY is between DELAYED_PRIORITY and NORMAL_PRIORITY.
@ IN_SEARCH
Executing the search code.
bool IsProfilingEnabled() const
Returns whether we are profiling the solver.
Demon * RegisterDemon(Demon *const demon)
Adds a new demon and wraps it inside a DemonProfiler if necessary.
std::string model_name() const
Returns the name of the model.
void ExportProfilingOverview(const std::string &filename)
Exports the profiling information in a human readable overview.
bool InstrumentsDemons() const
Returns whether we are instrumenting demons.
GurobiMPCallbackContext * context
absl::Status WriteString(File *file, const absl::string_view &contents, int flags)
absl::Status Open(const absl::string_view &filename, const absl::string_view &mode, File **f, int flags)
void STLDeleteContainerPairSecondPointers(ForwardIterator begin, ForwardIterator end)
Collection of objects used to extend the Constraint Solver library.
void InstallDemonProfiler(DemonProfiler *const monitor)
void DemonProfilerEndInitialPropagation(DemonProfiler *const monitor, Constraint *const constraint)
void DemonProfilerExportInformation(DemonProfiler *const monitor, const Constraint *const constraint, int64_t *const fails, int64_t *const initial_propagation_runtime, int64_t *const demon_invocations, int64_t *const total_demon_runtime, int *const demon_count)
void RegisterDemon(Solver *const solver, Demon *const demon, DemonProfiler *const monitor)
void DemonProfilerBeginInitialPropagation(DemonProfiler *const monitor, Constraint *const constraint)
DemonProfiler * BuildDemonProfiler(Solver *const solver)
void DeleteDemonProfiler(DemonProfiler *const monitor)
void DemonProfilerAddFakeRun(DemonProfiler *const monitor, Demon *const demon, int64_t start_time, int64_t end_time, bool is_fail)