22#include "absl/container/flat_hash_map.h"
23#include "absl/status/status.h"
24#include "absl/strings/str_format.h"
25#include "absl/strings/string_view.h"
26#include "absl/time/clock.h"
27#include "absl/time/time.h"
37#include "ortools/constraint_solver/demon_profiler.pb.h"
42 Container(
const Constraint* ct_, int64_t value_) :
ct(ct_),
value(value_) {}
43 bool operator<(
const Container& c)
const {
return value >
c.value; }
57 active_constraint_(nullptr),
58 active_demon_(nullptr),
59 start_time_ns_(
absl::GetCurrentTimeNanos()) {}
63 constraint_map_.end());
69 return (absl::GetCurrentTimeNanos() - start_time_ns_) / 1000;
78 CHECK(active_constraint_ ==
nullptr);
79 CHECK(active_demon_ ==
nullptr);
80 CHECK(constraint !=
nullptr);
81 ConstraintRuns*
const ct_run =
new ConstraintRuns;
82 ct_run->set_constraint_id(constraint->
DebugString());
83 ct_run->add_initial_propagation_start_time(
CurrentTime());
84 active_constraint_ = constraint;
85 constraint_map_[constraint] = ct_run;
89 CHECK(active_constraint_ !=
nullptr);
90 CHECK(active_demon_ ==
nullptr);
91 CHECK(constraint !=
nullptr);
92 CHECK_EQ(constraint, active_constraint_);
93 ConstraintRuns*
const ct_run = constraint_map_[constraint];
94 if (ct_run !=
nullptr) {
95 ct_run->add_initial_propagation_end_time(
CurrentTime());
96 ct_run->set_failures(0);
98 active_constraint_ =
nullptr;
107 CHECK(active_constraint_ ==
nullptr);
108 CHECK(active_demon_ ==
nullptr);
109 CHECK(constraint !=
nullptr);
110 CHECK(delayed !=
nullptr);
111 ConstraintRuns*
const ct_run = constraint_map_[constraint];
112 ct_run->add_initial_propagation_start_time(
CurrentTime());
113 active_constraint_ = constraint;
118 CHECK(active_constraint_ !=
nullptr);
119 CHECK(active_demon_ ==
nullptr);
120 CHECK(constraint !=
nullptr);
121 CHECK(delayed !=
nullptr);
122 CHECK_EQ(constraint, active_constraint_);
123 ConstraintRuns*
const ct_run = constraint_map_[constraint];
124 if (ct_run !=
nullptr) {
125 ct_run->add_initial_propagation_end_time(
CurrentTime());
126 ct_run->set_failures(0);
128 active_constraint_ =
nullptr;
136 if (demon_map_.find(demon) == demon_map_.end()) {
137 CHECK(active_constraint_ !=
nullptr);
138 CHECK(active_demon_ ==
nullptr);
139 CHECK(demon !=
nullptr);
140 ConstraintRuns*
const ct_run = constraint_map_[active_constraint_];
141 DemonRuns*
const demon_run = ct_run->add_demons();
143 demon_run->set_failures(0);
144 demon_map_[demon] = demon_run;
145 demons_per_constraint_[active_constraint_].push_back(demon_run);
150 CHECK(demon !=
nullptr);
154 CHECK(active_demon_ ==
nullptr);
155 active_demon_ = demon;
156 DemonRuns*
const demon_run = demon_map_[active_demon_];
157 if (demon_run !=
nullptr) {
163 CHECK(demon !=
nullptr);
167 CHECK_EQ(active_demon_, demon);
168 DemonRuns*
const demon_run = demon_map_[active_demon_];
169 if (demon_run !=
nullptr) {
172 active_demon_ =
nullptr;
181 if (active_demon_ !=
nullptr) {
182 DemonRuns*
const demon_run = demon_map_[active_demon_];
183 if (demon_run !=
nullptr) {
185 demon_run->set_failures(demon_run->failures() + 1);
187 active_demon_ =
nullptr;
189 active_constraint_ =
nullptr;
190 }
else if (active_constraint_ !=
nullptr) {
191 ConstraintRuns*
const ct_run = constraint_map_[active_constraint_];
192 if (ct_run !=
nullptr) {
193 ct_run->add_initial_propagation_end_time(
CurrentTime());
194 ct_run->set_failures(1);
196 active_constraint_ =
nullptr;
203 constraint_map_.end());
204 constraint_map_.clear();
206 demons_per_constraint_.clear();
213 int64_t new_max)
override {}
222 const std::vector<int64_t>& values)
override {}
224 const std::vector<int64_t>& values)
override {}
229 int64_t new_max)
override {}
233 int64_t new_max)
override {}
237 int64_t new_max)
override {}
244 const std::vector<int>& rank_last,
245 const std::vector<int>& unperformed)
override {}
250 CHECK(demon !=
nullptr);
251 DemonRuns*
const demon_run = demon_map_[demon];
252 CHECK(demon_run !=
nullptr);
253 demon_run->add_start_time(start_time);
254 demon_run->add_end_time(end_time);
256 demon_run->set_failures(demon_run->failures() + 1);
262 const char*
const kConstraintFormat =
263 " - Constraint: %s\n failures=%d, initial propagation "
264 "runtime=%d us, demons=%d, demon invocations=%d, total demon "
266 const char*
const kDemonFormat =
267 " --- Demon: %s\n invocations=%d, failures=%d, total "
268 "runtime=%d us, [average=%.2lf, median=%.2lf, stddev=%.2lf]\n";
270 const std::string
model =
274 std::vector<Container> to_sort;
276 ConstraintRuns*>::const_iterator it =
277 constraint_map_.begin();
278 it != constraint_map_.end(); ++it) {
281 int64_t demon_invocations = 0;
282 int64_t initial_propagation_runtime = 0;
283 int64_t total_demon_runtime = 0;
286 &demon_invocations, &total_demon_runtime,
289 Container(
ct, total_demon_runtime + initial_propagation_runtime));
291 std::sort(to_sort.begin(), to_sort.end());
293 for (
int i = 0; i < to_sort.size(); ++i) {
296 int64_t demon_invocations = 0;
297 int64_t initial_propagation_runtime = 0;
298 int64_t total_demon_runtime = 0;
301 &demon_invocations, &total_demon_runtime,
303 const std::string constraint_message =
304 absl::StrFormat(kConstraintFormat,
ct->DebugString(), fails,
305 initial_propagation_runtime, demon_count,
306 demon_invocations, total_demon_runtime);
309 const std::vector<DemonRuns*>& demons = demons_per_constraint_[
ct];
310 const int demon_size = demons.size();
311 for (
int demon_index = 0; demon_index < demon_size; ++demon_index) {
312 DemonRuns*
const demon_runs = demons[demon_index];
313 int64_t invocations = 0;
316 double mean_runtime = 0;
317 double median_runtime = 0;
318 double standard_deviation = 0.0;
320 &mean_runtime, &median_runtime,
321 &standard_deviation);
322 const std::string
runs = absl::StrFormat(
323 kDemonFormat, demon_runs->demon_id(), invocations, fails, runtime,
324 mean_runtime, median_runtime, standard_deviation);
334 int64_t*
const fails,
335 int64_t*
const initial_propagation_runtime,
336 int64_t*
const demon_invocations,
337 int64_t*
const total_demon_runtime,
int* demons) {
338 CHECK(constraint !=
nullptr);
339 ConstraintRuns*
const ct_run = constraint_map_[constraint];
340 CHECK(ct_run !=
nullptr);
341 *demon_invocations = 0;
342 *fails = ct_run->failures();
343 *initial_propagation_runtime = 0;
344 for (
int i = 0; i < ct_run->initial_propagation_start_time_size(); ++i) {
345 *initial_propagation_runtime += ct_run->initial_propagation_end_time(i) -
346 ct_run->initial_propagation_start_time(i);
348 *total_demon_runtime = 0;
351 *demons = ct_run->demons_size();
352 CHECK_EQ(*demons, demons_per_constraint_[constraint].
size());
353 for (
int demon_index = 0; demon_index < *demons; ++demon_index) {
354 const DemonRuns& demon_runs = ct_run->demons(demon_index);
355 *fails += demon_runs.failures();
356 CHECK_EQ(demon_runs.start_time_size(), demon_runs.end_time_size());
357 const int runs = demon_runs.start_time_size();
358 *demon_invocations +=
runs;
359 for (
int run_index = 0; run_index <
runs; ++run_index) {
360 const int64_t demon_time =
361 demon_runs.end_time(run_index) - demon_runs.start_time(run_index);
362 *total_demon_runtime += demon_time;
368 int64_t*
const demon_invocations, int64_t*
const fails,
369 int64_t*
const total_demon_runtime,
370 double*
const mean_demon_runtime,
371 double*
const median_demon_runtime,
372 double*
const stddev_demon_runtime) {
373 CHECK(demon_runs !=
nullptr);
374 CHECK_EQ(demon_runs->start_time_size(), demon_runs->end_time_size());
376 const int runs = demon_runs->start_time_size();
377 *demon_invocations =
runs;
378 *fails = demon_runs->failures();
379 *total_demon_runtime = 0;
380 *mean_demon_runtime = 0.0;
381 *median_demon_runtime = 0.0;
382 *stddev_demon_runtime = 0.0;
383 std::vector<double> runtimes;
384 for (
int run_index = 0; run_index <
runs; ++run_index) {
385 const int64_t demon_time =
386 demon_runs->end_time(run_index) - demon_runs->start_time(run_index);
387 *total_demon_runtime += demon_time;
388 runtimes.push_back(demon_time);
391 if (!runtimes.empty()) {
392 *mean_demon_runtime = (1.0L * *total_demon_runtime) / runtimes.size();
395 std::sort(runtimes.begin(), runtimes.end());
396 const int pivot = runtimes.size() / 2;
398 if (runtimes.size() == 1) {
399 *median_demon_runtime = runtimes[0];
401 *median_demon_runtime =
402 runtimes.size() % 2 == 1
404 : (runtimes[pivot - 1] + runtimes[pivot]) / 2.0;
408 double total_deviation = 0.0f;
410 for (
int i = 0; i < runtimes.size(); ++i) {
411 total_deviation += pow(runtimes[i] - *mean_demon_runtime, 2);
414 *stddev_demon_runtime = sqrt(total_deviation / runtimes.size());
423 std::string
DebugString()
const override {
return "DemonProfiler"; }
427 Demon* active_demon_;
428 const int64_t start_time_ns_;
429 absl::flat_hash_map<const Constraint*, ConstraintRuns*> constraint_map_;
430 absl::flat_hash_map<const Demon*, DemonRuns*> demon_map_;
431 absl::flat_hash_map<const Constraint*, std::vector<DemonRuns*> >
432 demons_per_constraint_;
436 if (demon_profiler_ !=
nullptr) {
456 CHECK(demon !=
nullptr);
458 propagation_monitor_->RegisterDemon(demon);
471 int64_t start_time, int64_t end_time,
473 monitor->
AddFakeRun(demon, start_time, end_time, is_fail);
478 int64_t*
const fails,
479 int64_t*
const initial_propagation_runtime,
480 int64_t*
const demon_invocations,
481 int64_t*
const total_demon_runtime,
482 int*
const demon_count) {
484 demon_invocations, total_demon_runtime,
std::string DebugString() const override
--------------— Constraint class ----------------—
void RemoveValues(IntVar *const var, const std::vector< int64_t > &values) override
void PrintOverview(Solver *const solver, absl::string_view filename)
Exports collected data as human-readable text.
void SetEndRange(IntervalVar *const var, int64_t new_min, int64_t new_max) override
void SetMax(IntVar *const var, int64_t new_max) override
void AddFakeRun(Demon *const demon, int64_t start_time, int64_t end_time, bool is_fail)
Useful for unit tests.
void EndProcessingIntegerVariable(IntVar *const var) override
void RankNotLast(SequenceVar *const var, int index) override
void PopContext() override
DemonProfiler(Solver *const solver)
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 SetEndMax(IntervalVar *const var, int64_t new_max) override
void SetPerformed(IntervalVar *const var, bool value) 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)
~DemonProfiler() override
void SetDurationMin(IntervalVar *const var, int64_t new_min) override
void EndConstraintInitialPropagation(Constraint *const constraint) override
int64_t CurrentTime() const
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)
Export Information.
void SetValue(IntVar *const var, int64_t value) override
void SetMin(IntVar *const var, int64_t new_min) override
IntVar modifiers.
void RankNotFirst(SequenceVar *const var, int index) override
void SetEndMin(IntervalVar *const var, int64_t new_min) override
void SetDurationRange(IntervalVar *const var, int64_t new_min, int64_t new_max) override
void BeginNestedConstraintInitialPropagation(Constraint *const constraint, Constraint *const delayed) override
void SetMax(IntExpr *const expr, int64_t new_max) override
void SetStartMax(IntervalVar *const var, int64_t new_max) override
void BeginFail() override
Just when the failure occurs.
void BeginConstraintInitialPropagation(Constraint *const constraint) override
Propagation events.
void RankLast(SequenceVar *const var, int index) override
void EndDemonRun(Demon *const demon) override
void SetRange(IntVar *const var, int64_t new_min, int64_t new_max) override
void BeginDemonRun(Demon *const demon) override
void StartProcessingIntegerVariable(IntVar *const var) override
void SetDurationMax(IntervalVar *const var, int64_t new_max) override
void SetMin(IntExpr *const expr, int64_t new_min) override
IntExpr modifiers.
std::string DebugString() const override
void RankFirst(SequenceVar *const var, int index) override
SequenceVar modifiers.
void RegisterDemon(Demon *const demon) override
void SetStartMin(IntervalVar *const var, int64_t new_min) override
IntervalVar modifiers.
void SetRange(IntExpr *const expr, int64_t new_min, int64_t new_max) override
void RestartSearch() override
Restarts a search and clears all previously collected information.
void SetValues(IntVar *const var, const std::vector< int64_t > &values) override
void RemoveInterval(IntVar *const var, int64_t imin, int64_t imax) override
void RemoveValue(IntVar *const var, int64_t value) override
void PushContext(const std::string &context) override
void EndNestedConstraintInitialPropagation(Constraint *const constraint, Constraint *const delayed) override
virtual Solver::DemonPriority priority() const
---------------— Demon class -------------—
std::string DebugString() const override
virtual void Install()
A search monitors adds itself on the active search.
For the time being, Solver is neither MT_SAFE nor MT_HOT.
Demon * RegisterDemon(Demon *demon)
Adds a new demon and wraps it inside a DemonProfiler if necessary.
void ExportProfilingOverview(const std::string &filename)
@ VAR_PRIORITY
VAR_PRIORITY is between DELAYED_PRIORITY and NORMAL_PRIORITY.
@ IN_SEARCH
Executing the search code.
std::string model_name() const
Returns the name of the model.
bool IsProfilingEnabled() const
Returns whether we are profiling the solver.
bool InstrumentsDemons() const
Returns whether we are instrumenting demons.
GurobiMPCallbackContext * context
absl::Status Open(absl::string_view filename, absl::string_view mode, File **f, Options options)
As of 2016-01, these methods can only be used with flags = file::Defaults().
absl::Status WriteString(File *file, absl::string_view contents, Options options)
void STLDeleteContainerPairSecondPointers(ForwardIterator begin, ForwardIterator end)
In SWIG mode, we don't want anything besides these top-level includes.
void DeleteDemonProfiler(DemonProfiler *monitor)
void DemonProfilerEndInitialPropagation(DemonProfiler *const monitor, Constraint *const constraint)
DemonProfiler * BuildDemonProfiler(Solver *solver)
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)
--— Exported Methods for Unit Tests --—
void DemonProfilerBeginInitialPropagation(DemonProfiler *const monitor, Constraint *const constraint)
void DemonProfilerAddFakeRun(DemonProfiler *const monitor, Demon *const demon, int64_t start_time, int64_t end_time, bool is_fail)
void InstallDemonProfiler(DemonProfiler *monitor)
--— Forward Declarations and Profiling Support --—