00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <math.h>
00015 #include <string.h>
00016 #include <algorithm>
00017 #include "base/hash.h"
00018 #include <stack>
00019 #include <string>
00020 #include <utility>
00021
00022 #include "base/commandlineflags.h"
00023 #include "base/integral_types.h"
00024 #include "base/logging.h"
00025 #include "base/scoped_ptr.h"
00026 #include "base/stringprintf.h"
00027 #include "base/map-util.h"
00028 #include "constraint_solver/constraint_solver.h"
00029 #include "constraint_solver/constraint_solveri.h"
00030 #include "util/string_array.h"
00031
00032 namespace operations_research {
00033 namespace {
00034
00035 class TraceIntVar : public IntVar {
00036 public:
00037 TraceIntVar(Solver* const solver, IntVar* const inner)
00038 : IntVar(solver), inner_(inner) {
00039 if (inner->HasName()) {
00040 set_name(inner->name());
00041 }
00042 CHECK_NE(inner->VarType(), TRACE_VAR);
00043 }
00044
00045 virtual ~TraceIntVar() {}
00046
00047 virtual int64 Min() const { return inner_->Min(); }
00048
00049 virtual void SetMin(int64 m) {
00050 if (m > inner_->Min()) {
00051 solver()->GetPropagationMonitor()->SetMin(inner_, m);
00052 inner_->SetMin(m);
00053 }
00054 }
00055
00056 virtual int64 Max() const { return inner_->Max(); }
00057
00058 virtual void SetMax(int64 m) {
00059 if (m < inner_->Max()) {
00060 solver()->GetPropagationMonitor()->SetMax(inner_, m);
00061 inner_->SetMax(m);
00062 }
00063 }
00064
00065 virtual void Range(int64* l, int64* u) {
00066 inner_->Range(l, u);
00067 }
00068
00069 virtual void SetRange(int64 l, int64 u) {
00070 if (l > inner_->Min() || u < inner_->Max()) {
00071 if (l == u) {
00072 solver()->GetPropagationMonitor()->SetValue(inner_, l);
00073 inner_->SetValue(l);
00074 } else {
00075 solver()->GetPropagationMonitor()->SetRange(inner_, l, u);
00076 inner_->SetRange(l, u);
00077 }
00078 }
00079 }
00080
00081 virtual bool Bound() const {
00082 return inner_->Bound();
00083 }
00084
00085 virtual bool IsVar() const { return true; }
00086
00087 virtual IntVar* Var() { return this; }
00088
00089 virtual int64 Value() const {
00090 return inner_->Value();
00091 }
00092
00093 virtual void RemoveValue(int64 v) {
00094 if (inner_->Contains(v)) {
00095 solver()->GetPropagationMonitor()->RemoveValue(inner_, v);
00096 inner_->RemoveValue(v);
00097 }
00098 }
00099
00100 virtual void SetValue(int64 v) {
00101 solver()->GetPropagationMonitor()->SetValue(inner_, v);
00102 inner_->SetValue(v);
00103 }
00104
00105 virtual void RemoveInterval(int64 l, int64 u) {
00106 solver()->GetPropagationMonitor()->RemoveInterval(inner_, l, u);
00107 inner_->RemoveInterval(l, u);
00108 }
00109
00110 virtual void RemoveValues(const int64* const values, int size) {
00111 solver()->GetPropagationMonitor()->RemoveValues(inner_, values, size);
00112 inner_->RemoveValues(values, size);
00113 }
00114
00115 virtual void SetValues(const int64* const values, int size) {
00116 solver()->GetPropagationMonitor()->SetValues(inner_, values, size);
00117 inner_->SetValues(values, size);
00118 }
00119
00120 virtual void WhenRange(Demon* d) {
00121 inner_->WhenRange(d);
00122 }
00123
00124 virtual void WhenBound(Demon* d) {
00125 inner_->WhenBound(d);
00126 }
00127
00128 virtual void WhenDomain(Demon* d) {
00129 inner_->WhenDomain(d);
00130 }
00131
00132 virtual uint64 Size() const {
00133 return inner_->Size();
00134 }
00135
00136 virtual bool Contains(int64 v) const {
00137 return inner_->Contains(v);
00138 }
00139
00140 virtual IntVarIterator* MakeHoleIterator(bool reversible) const {
00141 return inner_->MakeHoleIterator(reversible);
00142 }
00143
00144 virtual IntVarIterator* MakeDomainIterator(bool reversible) const {
00145 return inner_->MakeDomainIterator(reversible);
00146 }
00147
00148 virtual int64 OldMin() const {
00149 return inner_->OldMin();
00150 }
00151
00152 virtual int64 OldMax() const {
00153 return inner_->OldMax();
00154 }
00155
00156 virtual int VarType() const {
00157 return TRACE_VAR;
00158 }
00159
00160 virtual void Accept(ModelVisitor* const visitor) const {
00161 inner_->Accept(visitor);
00162 }
00163
00164 virtual string DebugString() const {
00165 return inner_->DebugString();
00166 }
00167
00168 private:
00169 IntVar* const inner_;
00170 };
00171
00172
00173 class TraceIntExpr : public IntExpr {
00174 public:
00175 TraceIntExpr(Solver* const solver, IntExpr* const inner)
00176 : IntExpr(solver), inner_(inner) {
00177 CHECK(!inner->IsVar());
00178 if (inner->HasName()) {
00179 set_name(inner->name());
00180 }
00181 }
00182
00183 virtual ~TraceIntExpr() {}
00184
00185 virtual int64 Min() const { return inner_->Min(); }
00186
00187 virtual void SetMin(int64 m) {
00188 solver()->GetPropagationMonitor()->SetMin(inner_, m);
00189 inner_->SetMin(m);
00190 }
00191
00192 virtual int64 Max() const { return inner_->Max(); }
00193
00194 virtual void SetMax(int64 m) {
00195 solver()->GetPropagationMonitor()->SetMax(inner_, m);
00196 inner_->SetMax(m);
00197 }
00198
00199 virtual void Range(int64* l, int64* u) {
00200 inner_->Range(l, u);
00201 }
00202
00203 virtual void SetRange(int64 l, int64 u) {
00204 if (l > inner_->Min() || u < inner_->Max()) {
00205 solver()->GetPropagationMonitor()->SetRange(inner_, l, u);
00206 inner_->SetRange(l, u);
00207 }
00208 }
00209
00210 virtual bool Bound() const {
00211 return inner_->Bound();
00212 }
00213
00214 virtual bool IsVar() const {
00215 DCHECK(!inner_->IsVar());
00216 return false;
00217 }
00218
00219 virtual IntVar* Var() {
00220 return solver()->RegisterIntVar(inner_->Var());
00221 }
00222
00223 virtual void WhenRange(Demon* d) {
00224 inner_->WhenRange(d);
00225 }
00226
00227 virtual void Accept(ModelVisitor* const visitor) const {
00228 inner_->Accept(visitor);
00229 }
00230
00231 virtual string DebugString() const {
00232 return inner_->DebugString();
00233 }
00234
00235 private:
00236 IntExpr* const inner_;
00237 };
00238
00239 class TraceIntervalVar : public IntervalVar {
00240 public:
00241 TraceIntervalVar(Solver* const solver, IntervalVar* const inner)
00242 : IntervalVar(solver, ""), inner_(inner) {
00243 if (inner->HasName()) {
00244 set_name(inner->name());
00245 }
00246 }
00247 virtual ~TraceIntervalVar() {}
00248
00249 virtual int64 StartMin() const {
00250 return inner_->StartMin();
00251 }
00252
00253 virtual int64 StartMax() const {
00254 return inner_->StartMax();
00255 }
00256
00257 virtual void SetStartMin(int64 m) {
00258 if (m > inner_->StartMin()) {
00259 solver()->GetPropagationMonitor()->SetStartMin(inner_, m);
00260 inner_->SetStartMin(m);
00261 }
00262 }
00263
00264 virtual void SetStartMax(int64 m) {
00265 if (m < inner_->StartMax()) {
00266 solver()->GetPropagationMonitor()->SetStartMax(inner_, m);
00267 inner_->SetStartMax(m);
00268 }
00269 }
00270
00271 virtual void SetStartRange(int64 mi, int64 ma) {
00272 if (mi > inner_->StartMin() || ma < inner_->StartMax()) {
00273 solver()->GetPropagationMonitor()->SetStartRange(inner_, mi, ma);
00274 inner_->SetStartRange(mi, ma);
00275 }
00276 }
00277
00278 virtual void WhenStartRange(Demon* const d) {
00279 inner_->WhenStartRange(d);
00280 }
00281
00282 virtual void WhenStartBound(Demon* const d) {
00283 inner_->WhenStartBound(d);
00284 }
00285
00286 virtual int64 EndMin() const {
00287 return inner_->EndMin();
00288 }
00289
00290 virtual int64 EndMax() const {
00291 return inner_->EndMax();
00292 }
00293
00294 virtual void SetEndMin(int64 m) {
00295 if (m > inner_->EndMin()) {
00296 solver()->GetPropagationMonitor()->SetEndMin(inner_, m);
00297 inner_->SetEndMin(m);
00298 }
00299 }
00300
00301 virtual void SetEndMax(int64 m) {
00302 if (m < inner_->EndMax()) {
00303 solver()->GetPropagationMonitor()->SetEndMax(inner_, m);
00304 inner_->SetEndMax(m);
00305 }
00306 }
00307
00308 virtual void SetEndRange(int64 mi, int64 ma) {
00309 if (mi > inner_->EndMin() || ma < inner_->EndMax()) {
00310 solver()->GetPropagationMonitor()->SetEndRange(inner_, mi, ma);
00311 inner_->SetEndRange(mi, ma);
00312 }
00313 }
00314
00315 virtual void WhenEndRange(Demon* const d) {
00316 inner_->WhenEndRange(d);
00317 }
00318
00319 virtual void WhenEndBound(Demon* const d) {
00320 inner_->WhenStartBound(d);
00321 }
00322
00323 virtual int64 DurationMin() const {
00324 return inner_->DurationMin();
00325 }
00326
00327 virtual int64 DurationMax() const {
00328 return inner_->DurationMax();
00329 }
00330
00331 virtual void SetDurationMin(int64 m) {
00332 if (m > inner_->DurationMin()) {
00333 solver()->GetPropagationMonitor()->SetDurationMin(inner_, m);
00334 inner_->SetDurationMin(m);
00335 }
00336 }
00337
00338 virtual void SetDurationMax(int64 m) {
00339 if (m < inner_->DurationMax()) {
00340 solver()->GetPropagationMonitor()->SetDurationMax(inner_, m);
00341 inner_->SetDurationMax(m);
00342 }
00343 }
00344
00345 virtual void SetDurationRange(int64 mi, int64 ma) {
00346 if (mi > inner_->DurationMin() || ma < inner_->DurationMax()) {
00347 solver()->GetPropagationMonitor()->SetDurationRange(inner_, mi, ma);
00348 inner_->SetDurationRange(mi, ma);
00349 }
00350 }
00351
00352 virtual void WhenDurationRange(Demon* const d) {
00353 inner_->WhenDurationRange(d);
00354 }
00355
00356 virtual void WhenDurationBound(Demon* const d) {
00357 inner_->WhenDurationBound(d);
00358 }
00359
00360 virtual bool MustBePerformed() const {
00361 return inner_->MustBePerformed();
00362 }
00363
00364 virtual bool MayBePerformed() const {
00365 return inner_->MayBePerformed();
00366 }
00367
00368 virtual void SetPerformed(bool value) {
00369 if ((value && !inner_->MustBePerformed()) ||
00370 (!value && inner_->MayBePerformed())) {
00371 solver()->GetPropagationMonitor()->SetPerformed(inner_, value);
00372 inner_->SetPerformed(value);
00373 }
00374 }
00375
00376 virtual void WhenPerformedBound(Demon* const d) {
00377 inner_->WhenPerformedBound(d);
00378 }
00379
00380 virtual void Accept(ModelVisitor* const visitor) const {
00381 inner_->Accept(visitor);
00382 }
00383
00384 private:
00385 IntervalVar* const inner_;
00386 };
00387
00388
00389
00390 class PrintTrace : public PropagationMonitor {
00391 public:
00392 struct Info {
00393 explicit Info(const string& m) : message(m), displayed(false) {}
00394 string message;
00395 bool displayed;
00396 };
00397
00398 struct Context {
00399 Context()
00400 : initial_indent(0),
00401 indent(0),
00402 in_demon(false),
00403 in_constraint(false),
00404 in_decision_builder(false),
00405 in_decision(false),
00406 in_objective(false) {}
00407
00408 explicit Context(int start_indent)
00409 : initial_indent(start_indent),
00410 indent(start_indent),
00411 in_demon(false),
00412 in_constraint(false),
00413 in_decision_builder(false),
00414 in_decision(false),
00415 in_objective(false) {}
00416
00417 bool TopLevel() const {
00418 return initial_indent == indent;
00419 }
00420
00421 void Clear() {
00422 indent = initial_indent;
00423 in_demon = false;
00424 in_constraint = false;
00425 in_decision_builder = false;
00426 in_decision = false;
00427 in_objective = false;
00428 delayed_info.clear();
00429 }
00430
00431 int initial_indent;
00432 int indent;
00433 bool in_demon;
00434 bool in_constraint;
00435 bool in_decision_builder;
00436 bool in_decision;
00437 bool in_objective;
00438 std::vector<Info> delayed_info;
00439 };
00440
00441 explicit PrintTrace(Solver* const s) : PropagationMonitor(s) {
00442 contexes_.push(Context());
00443 }
00444
00445 virtual ~PrintTrace() {}
00446
00447
00448
00449 virtual void BeginInitialPropagation() {
00450 CheckNoDelayed();
00451 DisplaySearch("Root Node Propagation");
00452 IncreaseIndent();
00453 }
00454 virtual void EndInitialPropagation() {
00455 DecreaseIndent();
00456 DisplaySearch("Starting Tree Search");
00457 }
00458
00459 virtual void BeginNextDecision(DecisionBuilder* const b) {
00460 DisplaySearch(StringPrintf("DecisionBuilder(%s)",
00461 b->DebugString().c_str()));
00462 IncreaseIndent();
00463 contexes_.top().in_decision_builder = true;
00464 }
00465
00466
00467 virtual void EndNextDecision(DecisionBuilder* const b, Decision* const d) {
00468 contexes_.top().in_decision_builder = false;
00469 DecreaseIndent();
00470 }
00471
00472 virtual void BeginFail() {
00473 contexes_.top().Clear();
00474 while (!contexes_.top().TopLevel()) {
00475 DecreaseIndent();
00476 LOG(INFO) << Indent() << "}";
00477 }
00478 DisplaySearch(StringPrintf("Failure at depth %d", solver()->SearchDepth()));
00479 }
00480
00481 virtual bool AtSolution() {
00482 DisplaySearch(StringPrintf("Solution found at depth %d",
00483 solver()->SearchDepth()));
00484 return false;
00485 }
00486
00487 virtual void ApplyDecision(Decision* const decision) {
00488 DisplaySearch(StringPrintf("ApplyDecision(%s)",
00489 decision->DebugString().c_str()));
00490 IncreaseIndent();
00491 contexes_.top().in_decision = true;
00492 }
00493
00494 virtual void RefuteDecision(Decision* const decision) {
00495 if (contexes_.top().in_objective) {
00496 DecreaseIndent();
00497 contexes_.top().in_objective = false;
00498 }
00499 DisplaySearch(StringPrintf("RefuteDecision(%s)",
00500 decision->DebugString().c_str()));
00501 IncreaseIndent();
00502 contexes_.top().in_decision = true;
00503 }
00504
00505 virtual void AfterDecision(Decision* const decision, bool direction) {
00506 DecreaseIndent();
00507 contexes_.top().in_decision = false;
00508 }
00509
00510 virtual void EnterSearch() {
00511 if (solver()->SolveDepth() == 0) {
00512 CHECK_EQ(1, contexes_.size());
00513 contexes_.top().Clear();
00514 } else {
00515 PrintDelayedString();
00516 PushNestedContext();
00517 }
00518 DisplaySearch("Enter Search");
00519 }
00520
00521 virtual void ExitSearch() {
00522 DisplaySearch("Exit Search");
00523 CHECK(contexes_.top().TopLevel());
00524 if (solver()->SolveDepth() > 1) {
00525 contexes_.pop();
00526 }
00527 }
00528
00529 virtual void RestartSearch() {
00530 CHECK(contexes_.top().TopLevel());
00531 }
00532
00533
00534
00535 virtual void BeginConstraintInitialPropagation(
00536 const Constraint* const constraint) {
00537 PushDelayedInfo(StringPrintf("Constraint(%s)",
00538 constraint->DebugString().c_str()));
00539 contexes_.top().in_constraint= true;
00540 }
00541
00542 virtual void EndConstraintInitialPropagation(const Constraint* const) {
00543 PopDelayedInfo();
00544 contexes_.top().in_constraint = false;
00545 }
00546
00547 virtual void BeginNestedConstraintInitialPropagation(
00548 const Constraint* const parent,
00549 const Constraint* const nested) {
00550 PushDelayedInfo(StringPrintf("Constraint(%s)",
00551 nested->DebugString().c_str()));
00552 contexes_.top().in_constraint = true;
00553 }
00554 virtual void EndNestedConstraintInitialPropagation(const Constraint* const,
00555 const Constraint* const) {
00556 PopDelayedInfo();
00557 contexes_.top().in_constraint = false;
00558 }
00559
00560 virtual void RegisterDemon(const Demon* const demon) {}
00561
00562 virtual void BeginDemonRun(const Demon* const demon) {
00563 if (demon->priority() != Solver::VAR_PRIORITY) {
00564 contexes_.top().in_demon = true;
00565 PushDelayedInfo(StringPrintf("Demon(%s)",
00566 demon->DebugString().c_str()));
00567 }
00568 }
00569
00570 virtual void EndDemonRun(const Demon* const demon) {
00571 if (demon->priority() != Solver::VAR_PRIORITY) {
00572 contexes_.top().in_demon = false;
00573 PopDelayedInfo();
00574 }
00575 }
00576
00577 virtual void PushContext(const string& context) {
00578 PushDelayedInfo(context);
00579 }
00580
00581 virtual void PopContext() {
00582 PopDelayedInfo();
00583 }
00584
00585
00586
00587 virtual void SetMin(IntExpr* const expr, int64 new_min) {
00588 DisplayModification(StringPrintf("SetMin(%s, %lld)",
00589 expr->DebugString().c_str(),
00590 new_min));
00591 }
00592
00593 virtual void SetMax(IntExpr* const expr, int64 new_max) {
00594 DisplayModification(StringPrintf("SetMax(%s, %lld)",
00595 expr->DebugString().c_str(),
00596 new_max));
00597 }
00598
00599 virtual void SetRange(IntExpr* const expr, int64 new_min, int64 new_max) {
00600 DisplayModification(StringPrintf("SetRange(%s, [%lld .. %lld])",
00601 expr->DebugString().c_str(),
00602 new_min,
00603 new_max));
00604 }
00605
00606
00607
00608 virtual void SetMin(IntVar* const var, int64 new_min) {
00609 DisplayModification(StringPrintf("SetMin(%s, %lld)",
00610 var->DebugString().c_str(),
00611 new_min));
00612 }
00613
00614 virtual void SetMax(IntVar* const var, int64 new_max) {
00615 DisplayModification(StringPrintf("SetMax(%s, %lld)",
00616 var->DebugString().c_str(),
00617 new_max));
00618 }
00619
00620 virtual void SetRange(IntVar* const var, int64 new_min, int64 new_max) {
00621 DisplayModification(StringPrintf("SetRange(%s, [%lld .. %lld])",
00622 var->DebugString().c_str(),
00623 new_min,
00624 new_max));
00625 }
00626
00627 virtual void RemoveValue(IntVar* const var, int64 value) {
00628 DisplayModification(StringPrintf("RemoveValue(%s, %lld)",
00629 var->DebugString().c_str(),
00630 value));
00631 }
00632
00633 virtual void SetValue(IntVar* const var, int64 value) {
00634 DisplayModification(StringPrintf("SetValue(%s, %lld)",
00635 var->DebugString().c_str(),
00636 value));
00637 }
00638
00639 virtual void RemoveInterval(IntVar* const var, int64 imin, int64 imax) {
00640 DisplayModification(StringPrintf("RemoveInterval(%s, [%lld .. %lld])",
00641 var->DebugString().c_str(),
00642 imin,
00643 imax));
00644 }
00645
00646 virtual void SetValues(IntVar* const var,
00647 const int64* const values,
00648 int size) {
00649 DisplayModification(
00650 StringPrintf("SetValues(%s, %s)",
00651 var->DebugString().c_str(),
00652 Int64ArrayToString(values, size, ", ").c_str()));
00653 }
00654
00655 virtual void RemoveValues(IntVar* const var,
00656 const int64* const values,
00657 int size) {
00658 DisplayModification(
00659 StringPrintf("RemoveValues(%s, %s)",
00660 var->DebugString().c_str(),
00661 Int64ArrayToString(values, size, ", ").c_str()));
00662 }
00663
00664
00665
00666 virtual void SetStartMin(IntervalVar* const var, int64 new_min) {
00667 DisplayModification(StringPrintf("SetStartMin(%s, %lld)",
00668 var->DebugString().c_str(),
00669 new_min));
00670 }
00671
00672 virtual void SetStartMax(IntervalVar* const var, int64 new_max) {
00673 DisplayModification(StringPrintf("SetStartMax(%s, %lld)",
00674 var->DebugString().c_str(),
00675 new_max));
00676 }
00677
00678 virtual void SetStartRange(IntervalVar* const var,
00679 int64 new_min,
00680 int64 new_max) {
00681 DisplayModification(StringPrintf("SetStartRange(%s, [%lld .. %lld])",
00682 var->DebugString().c_str(),
00683 new_min,
00684 new_max));
00685 }
00686
00687 virtual void SetEndMin(IntervalVar* const var, int64 new_min) {
00688 DisplayModification(StringPrintf("SetEndMin(%s, %lld)",
00689 var->DebugString().c_str(),
00690 new_min));
00691 }
00692
00693 virtual void SetEndMax(IntervalVar* const var, int64 new_max) {
00694 DisplayModification(StringPrintf("SetEndMax(%s, %lld)",
00695 var->DebugString().c_str(),
00696 new_max));
00697 }
00698
00699 virtual void SetEndRange(IntervalVar* const var,
00700 int64 new_min,
00701 int64 new_max) {
00702 DisplayModification(StringPrintf("SetEndRange(%s, [%lld .. %lld])",
00703 var->DebugString().c_str(),
00704 new_min,
00705 new_max));
00706 }
00707
00708 virtual void SetDurationMin(IntervalVar* const var, int64 new_min) {
00709 DisplayModification(StringPrintf("SetDurationMin(%s, %lld)",
00710 var->DebugString().c_str(),
00711 new_min));
00712 }
00713
00714 virtual void SetDurationMax(IntervalVar* const var, int64 new_max) {
00715 DisplayModification(StringPrintf("SetDurationMax(%s, %lld)",
00716 var->DebugString().c_str(),
00717 new_max));
00718 }
00719
00720 virtual void SetDurationRange(IntervalVar* const var,
00721 int64 new_min,
00722 int64 new_max) {
00723 DisplayModification(StringPrintf("SetDurationRange(%s, [%lld .. %lld])",
00724 var->DebugString().c_str(),
00725 new_min,
00726 new_max));
00727 }
00728
00729 virtual void SetPerformed(IntervalVar* const var, bool value) {
00730 DisplayModification(StringPrintf("SetPerformed(%s, %d)",
00731 var->DebugString().c_str(),
00732 value));
00733 }
00734
00735 virtual void RankFirst(SequenceVar* const var, int index) {
00736 DisplayModification(StringPrintf("RankFirst(%s, %d)",
00737 var->DebugString().c_str(),
00738 index));
00739 }
00740
00741 virtual void RankNotFirst(SequenceVar* const var, int index) {
00742 DisplayModification(StringPrintf("RankNotFirst(%s, %d)",
00743 var->DebugString().c_str(),
00744 index));
00745 }
00746
00747 virtual void RankLast(SequenceVar* const var, int index) {
00748 DisplayModification(StringPrintf("RankLast(%s, %d)",
00749 var->DebugString().c_str(),
00750 index));
00751 }
00752
00753 virtual void RankNotLast(SequenceVar* const var, int index) {
00754 DisplayModification(StringPrintf("RankNotLast(%s, %d)",
00755 var->DebugString().c_str(),
00756 index));
00757 }
00758
00759 virtual void RankSequence(SequenceVar* const var,
00760 const std::vector<int>& rank_first,
00761 const std::vector<int>& rank_last,
00762 const std::vector<int>& unperformed) {
00763 DisplayModification(
00764 StringPrintf(
00765 "RankSequence(%s, forward [%s], backward[%s], unperformed[%s])",
00766 var->DebugString().c_str(),
00767 IntVectorToString(rank_first, ", ").c_str(),
00768 IntVectorToString(rank_last, ", ").c_str(),
00769 IntVectorToString(unperformed, ", ").c_str()));
00770 }
00771
00772 virtual void Install() {
00773 SearchMonitor::Install();
00774 if (solver()->SolveDepth() <= 1) {
00775 solver()->AddPropagationMonitor(this);
00776 }
00777 }
00778
00779 private:
00780 void PushDelayedInfo(const string& delayed) {
00781 contexes_.top().delayed_info.push_back(Info(delayed));
00782 }
00783
00784 void PopDelayedInfo() {
00785 CHECK(!contexes_.top().delayed_info.empty());
00786 if (contexes_.top().delayed_info.back().displayed &&
00787 !contexes_.top().TopLevel()) {
00788 DecreaseIndent();
00789 LOG(INFO) << Indent() << "}";
00790 } else {
00791 contexes_.top().delayed_info.pop_back();
00792 }
00793 }
00794
00795 void CheckNoDelayed() {
00796 CHECK(contexes_.top().delayed_info.empty());
00797 }
00798
00799 void PrintDelayedString() {
00800 const std::vector<Info>& infos = contexes_.top().delayed_info;
00801 for (int i = 0; i < infos.size(); ++i) {
00802 const Info& info = infos[i];
00803 if (!info.displayed) {
00804 LOG(INFO) << Indent() << info.message << " {";
00805 IncreaseIndent();
00806
00807 contexes_.top().delayed_info[i].displayed = true;
00808 }
00809 }
00810 }
00811
00812 void DisplayModification(const string& to_print) {
00813 PrintDelayedString();
00814 if (contexes_.top().in_demon ||
00815 contexes_.top().in_constraint ||
00816 contexes_.top().in_decision_builder ||
00817 contexes_.top().in_decision ||
00818 contexes_.top().in_objective) {
00819
00820 LOG(INFO) << Indent() << to_print;
00821 } else {
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831 CHECK(contexes_.top().TopLevel());
00832 DisplaySearch(StringPrintf("Objective -> %s", to_print.c_str()));
00833 IncreaseIndent();
00834 contexes_.top().in_objective = true;
00835 }
00836 }
00837
00838 void DisplaySearch(const string& to_print) {
00839 const int solve_depth = solver()->SolveDepth();
00840 if (solve_depth <= 1) {
00841 LOG(INFO) << Indent() << "######## Top Level Search: " << to_print;
00842 } else {
00843 LOG(INFO) << Indent() << "######## Nested Search(" << solve_depth - 1
00844 << "): " << to_print;
00845 }
00846 }
00847
00848 string Indent() {
00849 string output = " @ ";
00850 for (int i = 0; i < contexes_.top().indent; ++i) {
00851 output.append(" ");
00852 }
00853 return output;
00854 }
00855
00856 void IncreaseIndent() {
00857 contexes_.top().indent++;
00858 }
00859
00860 void DecreaseIndent() {
00861 contexes_.top().indent--;
00862 }
00863
00864 void PushNestedContext() {
00865 const int initial_indent = contexes_.top().indent;
00866 contexes_.push(Context(initial_indent));
00867 }
00868
00869 std::stack<Context> contexes_;
00870 };
00871 }
00872
00873 IntExpr* Solver::RegisterIntExpr(IntExpr* const expr) {
00874 if (InstrumentsVariables()) {
00875 if (expr->IsVar()) {
00876 return RegisterIntVar(expr->Var());
00877 } else {
00878 return RevAlloc(new TraceIntExpr(this, expr));
00879 }
00880 } else {
00881 return expr;
00882 }
00883 }
00884
00885 IntVar* Solver::RegisterIntVar(IntVar* const var) {
00886 if (InstrumentsVariables() &&
00887 var->VarType() != TRACE_VAR) {
00888 return RevAlloc(new TraceIntVar(this, var));
00889 } else {
00890 return var;
00891 }
00892 }
00893
00894 IntervalVar* Solver::RegisterIntervalVar(IntervalVar* const var) {
00895 if (InstrumentsVariables()) {
00896 return RevAlloc(new TraceIntervalVar(this, var));
00897 } else {
00898 return var;
00899 }
00900 }
00901
00902 PropagationMonitor* BuildPrintTrace(Solver* const s) {
00903 return s->RevAlloc(new PrintTrace(s));
00904 }
00905 }