00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "constraint_solver/constraint_solver.h"
00018
00019 #include <setjmp.h>
00020 #include <string.h>
00021 #include <iosfwd>
00022
00023 #include "base/callback.h"
00024 #include "base/commandlineflags.h"
00025 #include "base/integral_types.h"
00026 #include "base/logging.h"
00027 #include "base/macros.h"
00028 #include "base/scoped_ptr.h"
00029 #include "base/stringprintf.h"
00030 #include "base/file.h"
00031 #include "base/recordio.h"
00032 #include "base/stringpiece.h"
00033 #include "zlib.h"
00034 #include "base/concise_iterator.h"
00035 #include "base/map-util.h"
00036 #include "base/stl_util.h"
00037 #include "constraint_solver/constraint_solveri.h"
00038 #include "constraint_solver/model.pb.h"
00039 #include "util/const_int_array.h"
00040 #include "util/tuple_set.h"
00041
00042 DEFINE_bool(cp_trace_propagation,
00043 false,
00044 "Trace propagation events (constraint and demon executions,"
00045 " variable modifications).");
00046 DEFINE_bool(cp_trace_search, false, "Trace search events");
00047 DEFINE_bool(cp_show_constraints, false,
00048 "show all constraints added to the solver.");
00049 DEFINE_bool(cp_print_model, false,
00050 "use PrintModelVisitor on model before solving.");
00051 DEFINE_bool(cp_model_stats, false,
00052 "use StatisticsModelVisitor on model before solving.");
00053 DEFINE_string(cp_export_file, "", "Export model to file using CPModelProto.");
00054 DEFINE_bool(cp_no_solve, false, "Force failure at the beginning of a search.");
00055 DEFINE_string(cp_profile_file, "", "Export profiling overview to file.");
00056 DEFINE_bool(cp_verbose_fail, false, "Verbose output when failing.");
00057 DEFINE_bool(cp_name_variables, false, "Force all variables to have names.");
00058
00059 void ConstraintSolverFailsHere() {
00060 VLOG(3) << "Fail";
00061 }
00062
00063 #if defined(_MSC_VER) // WINDOWS
00064 #pragma warning(disable : 4351 4355)
00065 #endif
00066
00067 namespace operations_research {
00068
00069
00070
00071 SolverParameters::SolverParameters()
00072 : compress_trail(kDefaultTrailCompression),
00073 trail_block_size(kDefaultTrailBlockSize),
00074 array_split_size(kDefaultArraySplitSize),
00075 store_names(kDefaultNameStoring),
00076 profile_level(kDefaultProfileLevel),
00077 trace_level(kDefaultTraceLevel),
00078 name_all_variables(kDefaultNameAllVariables) {}
00079
00080
00081 extern DemonProfiler* BuildDemonProfiler(Solver* const solver);
00082 extern void DeleteDemonProfiler(DemonProfiler* const monitor);
00083 extern void InstallDemonProfiler(DemonProfiler* const monitor);
00084
00085
00086
00087
00088
00089 bool Solver::InstrumentsDemons() const {
00090 return IsProfilingEnabled() || InstrumentsVariables();
00091 }
00092
00093 bool Solver::IsProfilingEnabled() const {
00094 return parameters_.profile_level != SolverParameters::NO_PROFILING ||
00095 !FLAGS_cp_profile_file.empty();
00096 }
00097
00098 bool Solver::InstrumentsVariables() const {
00099 return parameters_.trace_level != SolverParameters::NO_TRACE ||
00100 FLAGS_cp_trace_propagation;
00101 }
00102
00103 bool Solver::NameAllVariables() const {
00104 return parameters_.name_all_variables || FLAGS_cp_name_variables;
00105 }
00106
00107
00108
00109 Solver::DemonPriority Demon::priority() const {
00110 return Solver::NORMAL_PRIORITY;
00111 }
00112
00113 string Demon::DebugString() const {
00114 return "Demon";
00115 }
00116
00117 void Demon::inhibit(Solver* const s) {
00118 if (stamp_ < kuint64max) {
00119 s->SaveAndSetValue(&stamp_, kuint64max);
00120 }
00121 }
00122
00123 void Demon::desinhibit(Solver* const s) {
00124 if (stamp_ == kuint64max) {
00125 s->SaveAndSetValue(&stamp_, s->stamp() - 1);
00126 }
00127 }
00128
00129
00130
00131 string Action::DebugString() const {
00132 return "Action";
00133 }
00134
00135
00136
00137 namespace {
00138 class SinglePriorityQueue {
00139 public:
00140 virtual ~SinglePriorityQueue() {}
00141 virtual Demon* NextDemon() = 0;
00142 virtual void Enqueue(Demon* const d) = 0;
00143 virtual void AfterFailure() = 0;
00144 virtual void Init() = 0;
00145 virtual bool Empty() const = 0;
00146 };
00147
00148 class FifoPriorityQueue : public SinglePriorityQueue {
00149 public:
00150 struct Cell {
00151 explicit Cell(Demon* const d) : demon(d), next(NULL) {}
00152 Demon* demon;
00153 Cell* next;
00154 };
00155
00156 FifoPriorityQueue() : first_(NULL), last_(NULL), free_cells_(NULL) {}
00157
00158 virtual ~FifoPriorityQueue() {
00159 while (first_ != NULL) {
00160 Cell* const tmp = first_;
00161 first_ = tmp->next;
00162 delete tmp;
00163 }
00164 while (free_cells_ != NULL) {
00165 Cell* const tmp = free_cells_;
00166 free_cells_ = tmp->next;
00167 delete tmp;
00168 }
00169 }
00170
00171 virtual bool Empty() const {
00172 return first_ == NULL;
00173 }
00174
00175 virtual Demon* NextDemon() {
00176 if (first_ != NULL) {
00177 DCHECK(last_ != NULL);
00178 Cell* const tmp_cell = first_;
00179 Demon* const demon = tmp_cell->demon;
00180 first_ = tmp_cell->next;
00181 if (first_ == NULL) {
00182 last_ = NULL;
00183 }
00184 tmp_cell->next = free_cells_;
00185 free_cells_ = tmp_cell;
00186 return demon;
00187 }
00188 return NULL;
00189 }
00190
00191 virtual void Enqueue(Demon* const d) {
00192 Cell* cell = free_cells_;
00193 if (cell != NULL) {
00194 cell->demon = d;
00195 free_cells_ = cell->next;
00196 cell->next = NULL;
00197 } else {
00198 cell = new Cell(d);
00199 }
00200 if (last_ != NULL) {
00201 last_->next = cell;
00202 last_ = cell;
00203 } else {
00204 first_ = cell;
00205 last_ = cell;
00206 }
00207 }
00208
00209 virtual void AfterFailure() {
00210 if (first_ != NULL) {
00211 last_->next = free_cells_;
00212 free_cells_ = first_;
00213 first_ = NULL;
00214 last_ = NULL;
00215 }
00216 }
00217
00218 virtual void Init() {}
00219
00220 private:
00221 Cell* first_;
00222 Cell* last_;
00223 Cell* free_cells_;
00224 };
00225 }
00226
00227 class Queue {
00228 public:
00229 explicit Queue(Solver* const s)
00230 : solver_(s),
00231 stamp_(1),
00232 freeze_level_(0),
00233 in_process_(false),
00234 clear_action_(NULL),
00235 in_add_(false),
00236 instruments_demons_(s->InstrumentsDemons()) {
00237 for (int i = 0; i < Solver::kNumPriorities; ++i) {
00238 containers_[i] = new FifoPriorityQueue();
00239 containers_[i]->Init();
00240 }
00241 }
00242
00243 ~Queue() {
00244 for (int i = 0; i < Solver::kNumPriorities; ++i) {
00245 delete containers_[i];
00246 containers_[i] = NULL;
00247 }
00248 }
00249
00250 void Freeze() {
00251 freeze_level_++;
00252 stamp_++;
00253 }
00254
00255 void Unfreeze() {
00256 freeze_level_--;
00257 ProcessIfUnfrozen();
00258 }
00259
00260 void ProcessOneDemon(Solver::DemonPriority prio) {
00261 Demon* const demon = containers_[prio]->NextDemon();
00262
00263 if (demon != NULL) {
00264 demon->set_stamp(stamp_ - 1);
00265 DCHECK_EQ(prio, demon->priority());
00266 if (instruments_demons_) {
00267 solver_->GetPropagationMonitor()->BeginDemonRun(demon);
00268 }
00269 solver_->demon_runs_[prio]++;
00270 demon->Run(solver_);
00271 if (instruments_demons_) {
00272 solver_->GetPropagationMonitor()->EndDemonRun(demon);
00273 }
00274 }
00275 }
00276
00277 void ProcessNormalDemons() {
00278 while (!containers_[Solver::NORMAL_PRIORITY]->Empty()) {
00279 ProcessOneDemon(Solver::NORMAL_PRIORITY);
00280 }
00281 }
00282
00283 void Process() {
00284 if (!in_process_) {
00285 in_process_ = true;
00286 while (!containers_[Solver::VAR_PRIORITY]->Empty() ||
00287 !containers_[Solver::NORMAL_PRIORITY]->Empty() ||
00288 !containers_[Solver::DELAYED_PRIORITY]->Empty()) {
00289 while (!containers_[Solver::VAR_PRIORITY]->Empty() ||
00290 !containers_[Solver::NORMAL_PRIORITY]->Empty()) {
00291 while (!containers_[Solver::NORMAL_PRIORITY]->Empty()) {
00292 ProcessOneDemon(Solver::NORMAL_PRIORITY);
00293 }
00294 ProcessOneDemon(Solver::VAR_PRIORITY);
00295 }
00296 ProcessOneDemon(Solver::DELAYED_PRIORITY);
00297 }
00298 in_process_ = false;
00299 }
00300 }
00301
00302 void Enqueue(Demon* const demon) {
00303 if (demon->stamp() < stamp_) {
00304 demon->set_stamp(stamp_);
00305 containers_[demon->priority()]->Enqueue(demon);
00306 ProcessIfUnfrozen();
00307 }
00308 }
00309
00310 void AfterFailure() {
00311 for (int i = 0; i < Solver::kNumPriorities; ++i) {
00312 containers_[i]->AfterFailure();
00313 }
00314 if (clear_action_ != NULL) {
00315 clear_action_->Run(solver_);
00316 clear_action_ = NULL;
00317 }
00318 freeze_level_ = 0;
00319 in_process_ = false;
00320 in_add_ = false;
00321 to_add_.clear();
00322 }
00323
00324 void increase_stamp() {
00325 stamp_++;
00326 }
00327
00328 uint64 stamp() const {
00329 return stamp_;
00330 }
00331
00332 void set_action_on_fail(Action* const a) {
00333 clear_action_ = a;
00334 }
00335
00336 void clear_action_on_fail() {
00337 clear_action_ = NULL;
00338 }
00339
00340 void AddConstraint(Constraint* const c) {
00341 to_add_.push_back(c);
00342 ProcessConstraints();
00343 }
00344
00345 void ProcessConstraints() {
00346 if (!in_add_) {
00347 in_add_ = true;
00348
00349
00350 for (int counter = 0; counter < to_add_.size(); ++counter) {
00351 Constraint* const constraint = to_add_[counter];
00352
00353 constraint->PostAndPropagate();
00354 }
00355 in_add_ = false;
00356 to_add_.clear();
00357 }
00358 }
00359
00360 private:
00361 void ProcessIfUnfrozen() {
00362 if (freeze_level_ == 0) {
00363 Process();
00364 }
00365 }
00366
00367 Solver* const solver_;
00368 SinglePriorityQueue* containers_[Solver::kNumPriorities];
00369 uint64 stamp_;
00370
00371
00372 uint32 freeze_level_;
00373 bool in_process_;
00374 Action* clear_action_;
00375 std::vector<Constraint*> to_add_;
00376 bool in_add_;
00377 const bool instruments_demons_;
00378 };
00379
00380
00381
00382 struct StateInfo {
00383
00384 public:
00385 StateInfo() : ptr_info(NULL), int_info(0), depth(0), left_depth(0) {}
00386 StateInfo(void* pinfo, int iinfo)
00387 : ptr_info(pinfo), int_info(iinfo), depth(0), left_depth(0) {}
00388 StateInfo(void* pinfo, int iinfo, int d, int ld)
00389 : ptr_info(pinfo), int_info(iinfo), depth(d), left_depth(ld) {}
00390 void* ptr_info;
00391 int int_info;
00392 int depth;
00393 int left_depth;
00394 };
00395
00396 struct StateMarker {
00397 public:
00398 StateMarker(Solver::MarkerType t, const StateInfo& info);
00399 friend class Solver;
00400 friend struct Trail;
00401 private:
00402 Solver::MarkerType type_;
00403 int rev_int_index_;
00404 int rev_int64_index_;
00405 int rev_uint64_index_;
00406 int rev_ptr_index_;
00407 int rev_boolvar_list_index_;
00408 int rev_bools_index_;
00409 int rev_int_memory_index_;
00410 int rev_int64_memory_index_;
00411 int rev_object_memory_index_;
00412 int rev_object_array_memory_index_;
00413 int rev_memory_index_;
00414 int rev_memory_array_index_;
00415 StateInfo info_;
00416 };
00417
00418 StateMarker::StateMarker(Solver::MarkerType t, const StateInfo& info)
00419 : type_(t),
00420 rev_int_index_(0),
00421 rev_int64_index_(0),
00422 rev_uint64_index_(0),
00423 rev_ptr_index_(0),
00424 rev_boolvar_list_index_(0),
00425 rev_bools_index_(0),
00426 rev_int_memory_index_(0),
00427 rev_int64_memory_index_(0),
00428 rev_object_memory_index_(0),
00429 rev_object_array_memory_index_(0),
00430 info_(info) {}
00431
00432
00433
00434 namespace {
00435
00436
00437
00438
00439 template <class T> struct addrval {
00440 public:
00441 addrval() : address_(NULL) {}
00442 explicit addrval(T* adr) : address_(adr), old_value_(*adr) {}
00443 void restore() const { (*address_) = old_value_; }
00444 private:
00445 T* address_;
00446 T old_value_;
00447 };
00448
00449
00450
00451
00452
00453
00454 template <class T> class TrailPacker {
00455 public:
00456 explicit TrailPacker(int block_size) : block_size_(block_size) {}
00457 virtual ~TrailPacker() {}
00458 int input_size() const { return block_size_ * sizeof(addrval<T>); }
00459 virtual void Pack(const addrval<T>* block, string* packed_block) = 0;
00460 virtual void Unpack(const string& packed_block, addrval<T>* block) = 0;
00461 private:
00462 const int block_size_;
00463 DISALLOW_COPY_AND_ASSIGN(TrailPacker);
00464 };
00465
00466
00467 template <class T> class NoCompressionTrailPacker : public TrailPacker<T> {
00468 public:
00469 explicit NoCompressionTrailPacker(int block_size)
00470 : TrailPacker<T>(block_size) {}
00471 virtual ~NoCompressionTrailPacker() {}
00472 virtual void Pack(const addrval<T>* block, string* packed_block) {
00473 DCHECK(block != NULL);
00474 DCHECK(packed_block != NULL);
00475 StringPiece block_str;
00476 block_str.set(block, this->input_size());
00477 block_str.CopyToString(packed_block);
00478 }
00479 virtual void Unpack(const string& packed_block, addrval<T>* block) {
00480 DCHECK(block != NULL);
00481 memcpy(block, packed_block.c_str(), packed_block.size());
00482 }
00483 private:
00484 DISALLOW_COPY_AND_ASSIGN(NoCompressionTrailPacker<T>);
00485 };
00486
00487 template <class T> class ZlibTrailPacker : public TrailPacker<T> {
00488 public:
00489 explicit ZlibTrailPacker(int block_size)
00490 : TrailPacker<T>(block_size),
00491 tmp_size_(compressBound(this->input_size())),
00492 tmp_block_(new char[tmp_size_]) {}
00493
00494 virtual ~ZlibTrailPacker() {}
00495
00496 virtual void Pack(const addrval<T>* block, string* packed_block) {
00497 DCHECK(block != NULL);
00498 DCHECK(packed_block != NULL);
00499 uLongf size = tmp_size_;
00500 const int result = compress(reinterpret_cast<Bytef*>(tmp_block_.get()),
00501 &size,
00502 reinterpret_cast<const Bytef*>(block),
00503 this->input_size());
00504 CHECK_EQ(Z_OK, result);
00505 StringPiece block_str;
00506 block_str.set(tmp_block_.get(), size);
00507 block_str.CopyToString(packed_block);
00508 }
00509
00510 virtual void Unpack(const string& packed_block, addrval<T>* block) {
00511 DCHECK(block != NULL);
00512 uLongf size = this->input_size();
00513 const int result =
00514 uncompress(reinterpret_cast<Bytef*>(block),
00515 &size,
00516 reinterpret_cast<const Bytef*>(packed_block.c_str()),
00517 packed_block.size());
00518 CHECK_EQ(Z_OK, result);
00519 }
00520
00521 private:
00522 const uint64 tmp_size_;
00523 scoped_array<char> tmp_block_;
00524 DISALLOW_COPY_AND_ASSIGN(ZlibTrailPacker<T>);
00525 };
00526
00527
00528 template <class T> class CompressedTrail {
00529 public:
00530 CompressedTrail(int block_size,
00531 SolverParameters::TrailCompression compression_level)
00532 : block_size_(block_size),
00533 blocks_(NULL),
00534 free_blocks_(NULL),
00535 data_(new addrval<T>[block_size]),
00536 buffer_(new addrval<T>[block_size]),
00537 buffer_used_(false),
00538 current_(0),
00539 size_(0) {
00540 switch (compression_level) {
00541 case SolverParameters::NO_COMPRESSION: {
00542 packer_.reset(new NoCompressionTrailPacker<T>(block_size));
00543 break;
00544 }
00545 case SolverParameters::COMPRESS_WITH_ZLIB: {
00546 packer_.reset(new ZlibTrailPacker<T>(block_size));
00547 break;
00548 }
00549 }
00550
00551
00552
00553
00554
00555
00556 memset(data_.get(), 0, sizeof(*data_.get()) * block_size);
00557 memset(buffer_.get(), 0, sizeof(*buffer_.get()) * block_size);
00558 }
00559 ~CompressedTrail() {
00560 FreeBlocks(blocks_);
00561 FreeBlocks(free_blocks_);
00562 }
00563 const addrval<T>& Back() const {
00564
00565 DCHECK_GT(current_, 0);
00566 return data_[current_ - 1];
00567 }
00568 void PopBack() {
00569 if (size_ > 0) {
00570 --current_;
00571 if (current_ <= 0) {
00572 if (buffer_used_) {
00573 data_.swap(buffer_);
00574 current_ = block_size_;
00575 buffer_used_ = false;
00576 } else if (blocks_ != NULL) {
00577 packer_->Unpack(blocks_->compressed, data_.get());
00578 FreeTopBlock();
00579 current_ = block_size_;
00580 }
00581 }
00582 --size_;
00583 }
00584 }
00585 void PushBack(const addrval<T>& addr_val) {
00586 if (current_ >= block_size_) {
00587 if (buffer_used_) {
00588 NewTopBlock();
00589 packer_->Pack(buffer_.get(), &blocks_->compressed);
00590
00591 data_.swap(buffer_);
00592 } else {
00593 data_.swap(buffer_);
00594 buffer_used_ = true;
00595 }
00596 current_ = 0;
00597 }
00598 data_[current_] = addr_val;
00599 ++current_;
00600 ++size_;
00601 }
00602 int size() const { return size_; }
00603
00604 private:
00605 struct Block {
00606 string compressed;
00607 Block* next;
00608 };
00609
00610 void FreeTopBlock() {
00611 Block* block = blocks_;
00612 blocks_ = block->next;
00613 block->compressed.clear();
00614 block->next = free_blocks_;
00615 free_blocks_ = block;
00616 }
00617 void NewTopBlock() {
00618 Block* block = NULL;
00619 if (free_blocks_ != NULL) {
00620 block = free_blocks_;
00621 free_blocks_ = block->next;
00622 } else {
00623 block = new Block;
00624 }
00625 block->next = blocks_;
00626 blocks_ = block;
00627 }
00628 void FreeBlocks(Block* blocks) {
00629 while (NULL != blocks) {
00630 Block* next = blocks->next;
00631 delete blocks;
00632 blocks = next;
00633 }
00634 }
00635
00636 scoped_ptr<TrailPacker<T> > packer_;
00637 const int block_size_;
00638 Block* blocks_;
00639 Block* free_blocks_;
00640 scoped_array<addrval<T> > data_;
00641 scoped_array<addrval<T> > buffer_;
00642 bool buffer_used_;
00643 int current_;
00644 int size_;
00645 };
00646 }
00647
00648
00649
00650
00651
00652
00653
00654 extern void RestoreBoolValue(IntVar* const var);
00655
00656 struct Trail {
00657 CompressedTrail<int> rev_ints_;
00658 CompressedTrail<int64> rev_int64s_;
00659 CompressedTrail<uint64> rev_uint64s_;
00660 CompressedTrail<void*> rev_ptrs_;
00661 std::vector<IntVar*> rev_boolvar_list_;
00662 std::vector<bool*> rev_bools_;
00663 std::vector<bool> rev_bool_value_;
00664 std::vector<int*> rev_int_memory_;
00665 std::vector<int64*> rev_int64_memory_;
00666 std::vector<BaseObject*> rev_object_memory_;
00667 std::vector<BaseObject**> rev_object_array_memory_;
00668 std::vector<void*> rev_memory_;
00669 std::vector<void**> rev_memory_array_;
00670
00671 Trail(int block_size, SolverParameters::TrailCompression compression_level)
00672 : rev_ints_(block_size, compression_level),
00673 rev_int64s_(block_size, compression_level),
00674 rev_uint64s_(block_size, compression_level),
00675 rev_ptrs_(block_size, compression_level) {}
00676
00677 void BacktrackTo(StateMarker* m) {
00678 int target = m->rev_int_index_;
00679 for (int curr = rev_ints_.size(); curr > target; --curr) {
00680 const addrval<int>& cell = rev_ints_.Back();
00681 cell.restore();
00682 rev_ints_.PopBack();
00683 }
00684 DCHECK_EQ(rev_ints_.size(), target);
00685
00686 target = m->rev_int64_index_;
00687 for (int curr = rev_int64s_.size(); curr > target; --curr) {
00688 const addrval<int64>& cell = rev_int64s_.Back();
00689 cell.restore();
00690 rev_int64s_.PopBack();
00691 }
00692 DCHECK_EQ(rev_int64s_.size(), target);
00693
00694 target = m->rev_uint64_index_;
00695 for (int curr = rev_uint64s_.size(); curr > target; --curr) {
00696 const addrval<uint64>& cell = rev_uint64s_.Back();
00697 cell.restore();
00698 rev_uint64s_.PopBack();
00699 }
00700 DCHECK_EQ(rev_uint64s_.size(), target);
00701
00702 target = m->rev_ptr_index_;
00703 for (int curr = rev_ptrs_.size(); curr > target; --curr) {
00704 const addrval<void*>& cell = rev_ptrs_.Back();
00705 cell.restore();
00706 rev_ptrs_.PopBack();
00707 }
00708 DCHECK_EQ(rev_ptrs_.size(), target);
00709
00710 target = m->rev_boolvar_list_index_;
00711 for (int curr = rev_boolvar_list_.size() - 1; curr >= target; --curr) {
00712 IntVar* const var = rev_boolvar_list_[curr];
00713 RestoreBoolValue(var);
00714 }
00715 rev_boolvar_list_.resize(target);
00716
00717 DCHECK_EQ(rev_bools_.size(), rev_bool_value_.size());
00718 target = m->rev_bools_index_;
00719 for (int curr = rev_bools_.size() - 1; curr >= target; --curr) {
00720 *(rev_bools_[curr]) = rev_bool_value_[curr];
00721 }
00722 rev_bools_.resize(target);
00723 rev_bool_value_.resize(target);
00724
00725 target = m->rev_int_memory_index_;
00726 for (int curr = rev_int_memory_.size() - 1; curr >= target; --curr) {
00727 delete[] rev_int_memory_[curr];
00728 }
00729 rev_int_memory_.resize(target);
00730
00731 target = m->rev_int64_memory_index_;
00732 for (int curr = rev_int64_memory_.size() - 1; curr >= target; --curr) {
00733 delete[] rev_int64_memory_[curr];
00734 }
00735 rev_int64_memory_.resize(target);
00736
00737 target = m->rev_object_memory_index_;
00738 for (int curr = rev_object_memory_.size() - 1; curr >= target; --curr) {
00739 delete rev_object_memory_[curr];
00740 }
00741 rev_object_memory_.resize(target);
00742
00743 target = m->rev_object_array_memory_index_;
00744 for (int curr = rev_object_array_memory_.size() - 1;
00745 curr >= target; --curr) {
00746 delete[] rev_object_array_memory_[curr];
00747 }
00748 rev_object_array_memory_.resize(target);
00749
00750 target = m->rev_memory_index_;
00751 for (int curr = rev_memory_.size() - 1; curr >= target; --curr) {
00752 delete reinterpret_cast<char*>(rev_memory_[curr]);
00753
00754
00755
00756
00757
00758 }
00759 rev_memory_.resize(target);
00760
00761 target = m->rev_memory_array_index_;
00762 for (int curr = rev_memory_array_.size() - 1; curr >= target; --curr) {
00763 delete [] rev_memory_array_[curr];
00764
00765 }
00766 rev_memory_array_.resize(target);
00767 }
00768 };
00769
00770 void Solver::InternalSaveValue(int* valptr) {
00771 trail_->rev_ints_.PushBack(addrval<int>(valptr));
00772 }
00773
00774 void Solver::InternalSaveValue(int64* valptr) {
00775 trail_->rev_int64s_.PushBack(addrval<int64>(valptr));
00776 }
00777
00778 void Solver::InternalSaveValue(uint64* valptr) {
00779 trail_->rev_uint64s_.PushBack(addrval<uint64>(valptr));
00780 }
00781
00782 void Solver::InternalSaveValue(void** valptr) {
00783 trail_->rev_ptrs_.PushBack(addrval<void*>(valptr));
00784 }
00785
00786
00787
00788
00789 void Solver::InternalSaveValue(bool* valptr) {
00790 trail_->rev_bools_.push_back(valptr);
00791 trail_->rev_bool_value_.push_back(*valptr);
00792 }
00793
00794 BaseObject* Solver::SafeRevAlloc(BaseObject* ptr) {
00795 check_alloc_state();
00796 trail_->rev_object_memory_.push_back(ptr);
00797 return ptr;
00798 }
00799
00800 int* Solver::SafeRevAllocArray(int* ptr) {
00801 check_alloc_state();
00802 trail_->rev_int_memory_.push_back(ptr);
00803 return ptr;
00804 }
00805
00806 int64* Solver::SafeRevAllocArray(int64* ptr) {
00807 check_alloc_state();
00808 trail_->rev_int64_memory_.push_back(ptr);
00809 return ptr;
00810 }
00811
00812 uint64* Solver::SafeRevAllocArray(uint64* ptr) {
00813 check_alloc_state();
00814 trail_->rev_int64_memory_.push_back(reinterpret_cast<int64*>(ptr));
00815 return ptr;
00816 }
00817
00818 BaseObject** Solver::SafeRevAllocArray(BaseObject** ptr) {
00819 check_alloc_state();
00820 trail_->rev_object_array_memory_.push_back(ptr);
00821 return ptr;
00822 }
00823
00824 IntVar** Solver::SafeRevAllocArray(IntVar** ptr) {
00825 BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
00826 return reinterpret_cast<IntVar**>(in);
00827 }
00828
00829 IntExpr** Solver::SafeRevAllocArray(IntExpr** ptr) {
00830 BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
00831 return reinterpret_cast<IntExpr**>(in);
00832 }
00833
00834 Constraint** Solver::SafeRevAllocArray(Constraint** ptr) {
00835 BaseObject** in = SafeRevAllocArray(reinterpret_cast<BaseObject**>(ptr));
00836 return reinterpret_cast<Constraint**>(in);
00837 }
00838
00839 void* Solver::UnsafeRevAllocAux(void* ptr) {
00840 check_alloc_state();
00841 trail_->rev_memory_.push_back(ptr);
00842 return ptr;
00843 }
00844
00845 void** Solver::UnsafeRevAllocArrayAux(void** ptr) {
00846 check_alloc_state();
00847 trail_->rev_memory_array_.push_back(ptr);
00848 return ptr;
00849 }
00850
00851 void InternalSaveBooleanVarValue(Solver* const solver, IntVar* const var) {
00852 solver->trail_->rev_boolvar_list_.push_back(var);
00853 }
00854
00855
00856
00857 class Search {
00858 public:
00859 explicit Search(Solver* const s)
00860 : solver_(s), marker_stack_(), fail_buffer_(), solution_counter_(0),
00861 decision_builder_(NULL), created_by_solve_(false),
00862 selector_(NULL), search_depth_(0), left_search_depth_(0),
00863 should_restart_(false), should_finish_(false),
00864 sentinel_pushed_(0), jmpbuf_filled_(false) {}
00865
00866
00867
00868
00869 Search(Solver* const s, int )
00870 : solver_(s), marker_stack_(), fail_buffer_(), solution_counter_(0),
00871 decision_builder_(NULL), created_by_solve_(false),
00872 selector_(NULL), search_depth_(-1), left_search_depth_(-1),
00873 should_restart_(false), should_finish_(false),
00874 sentinel_pushed_(0), jmpbuf_filled_(false) {}
00875
00876 ~Search() {
00877 STLDeleteElements(&marker_stack_);
00878 }
00879
00880 void EnterSearch();
00881 void RestartSearch();
00882 void ExitSearch();
00883 void BeginNextDecision(DecisionBuilder* const b);
00884 void EndNextDecision(DecisionBuilder* const b, Decision* const d);
00885 void ApplyDecision(Decision* const d);
00886 void AfterDecision(Decision* const d, bool apply);
00887 void RefuteDecision(Decision* const d);
00888 void BeginFail();
00889 void EndFail();
00890 void BeginInitialPropagation();
00891 void EndInitialPropagation();
00892 bool AtSolution();
00893 bool AcceptSolution();
00894 void NoMoreSolutions();
00895 bool LocalOptimum();
00896 bool AcceptDelta(Assignment* delta, Assignment* deltadelta);
00897 void AcceptNeighbor();
00898 void PeriodicCheck();
00899 int ProgressPercent();
00900 void Accept(ModelVisitor* const visitor) const;
00901 void push_monitor(SearchMonitor* const m);
00902 void Clear();
00903 void IncrementSolutionCounter() { ++solution_counter_; }
00904 int64 solution_counter() const { return solution_counter_; }
00905 void set_decision_builder(DecisionBuilder* const db) {
00906 decision_builder_ = db;
00907 }
00908 DecisionBuilder* decision_builder() const { return decision_builder_; }
00909 void set_created_by_solve(bool c) { created_by_solve_ = c; }
00910 bool created_by_solve() const { return created_by_solve_; }
00911 Solver::DecisionModification ModifyDecision();
00912 void SetBranchSelector(
00913 ResultCallback1<Solver::DecisionModification, Solver*>* const s);
00914 void LeftMove() {
00915 search_depth_++;
00916 left_search_depth_++;
00917 }
00918 void RightMove() {
00919 search_depth_++;
00920 }
00921 int search_depth() const { return search_depth_; }
00922 void set_search_depth(int d) { search_depth_ = d; }
00923 int left_search_depth() const { return left_search_depth_; }
00924 void set_search_left_depth(int d) { left_search_depth_ = d; }
00925 void set_should_restart(bool s) { should_restart_ = s; }
00926 bool should_restart() const { return should_restart_; }
00927 void set_should_finish(bool s) { should_finish_ = s; }
00928 bool should_finish() const { return should_finish_; }
00929 void CheckFail() {
00930 if (should_finish_ || should_restart_) {
00931 solver_->Fail();
00932 }
00933 }
00934 friend class Solver;
00935 private:
00936
00937 void JumpBack();
00938 void ClearBuffer() {
00939 CHECK(jmpbuf_filled_) << "Internal error in backtracking";
00940 jmpbuf_filled_ = false;
00941 }
00942
00943 Solver* const solver_;
00944 std::vector<StateMarker*> marker_stack_;
00945 std::vector<SearchMonitor*> monitors_;
00946 jmp_buf fail_buffer_;
00947 int64 solution_counter_;
00948 DecisionBuilder* decision_builder_;
00949 bool created_by_solve_;
00950 scoped_ptr<ResultCallback1<Solver::DecisionModification, Solver*> > selector_;
00951 int search_depth_;
00952 int left_search_depth_;
00953 bool should_restart_;
00954 bool should_finish_;
00955 int sentinel_pushed_;
00956 bool jmpbuf_filled_;
00957 };
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968 #define CP_FAST_BACKTRACK
00969 #if defined(CP_FAST_BACKTRACK)
00970
00971
00972 #define CP_TRY(search) \
00973 CHECK(!search->jmpbuf_filled_) << "Fail() called outside search"; \
00974 search->jmpbuf_filled_ = true; \
00975 if (setjmp(search->fail_buffer_) == 0)
00976 #define CP_ON_FAIL else
00977 #define CP_DO_FAIL(search) longjmp(search->fail_buffer_, 1)
00978 #else // CP_FAST_BACKTRACK
00979 class FailException {};
00980 #define CP_TRY(search) \
00981 CHECK(!search->jmpbuf_filled_) << "Fail() called outside search"; \
00982 search->jmpbuf_filled_ = true; \
00983 try
00984 #define CP_ON_FAIL catch(FailException&)
00985 #define CP_DO_FAIL(search) throw FailException()
00986 #endif // CP_FAST_BACKTRACK
00987
00988 void Search::JumpBack() {
00989 if (jmpbuf_filled_) {
00990 jmpbuf_filled_ = false;
00991 CP_DO_FAIL(this);
00992 } else {
00993 string explanation = "Failure outside of search";
00994 solver_->AddConstraint(solver_->MakeFalseConstraint(explanation));
00995 }
00996 }
00997
00998 Search* Solver::ActiveSearch() const {
00999 return searches_.back();
01000 }
01001
01002 namespace {
01003 class UndoBranchSelector : public Action {
01004 public:
01005 explicit UndoBranchSelector(int depth) : depth_(depth) {}
01006 virtual ~UndoBranchSelector() {}
01007 virtual void Run(Solver* const s) {
01008 if (s->SolveDepth() == depth_) {
01009 s->ActiveSearch()->SetBranchSelector(NULL);
01010 }
01011 }
01012 virtual string DebugString() const {
01013 return StringPrintf("UndoBranchSelector(%i)", depth_);
01014 }
01015 private:
01016 const int depth_;
01017 };
01018
01019 class ApplyBranchSelector : public DecisionBuilder {
01020 public:
01021 explicit ApplyBranchSelector(
01022 ResultCallback1<Solver::DecisionModification, Solver*>* const bs)
01023 : selector_(bs) {}
01024 virtual ~ApplyBranchSelector() {}
01025
01026 virtual Decision* Next(Solver* const s) {
01027 s->SetBranchSelector(selector_);
01028 return NULL;
01029 }
01030
01031 virtual string DebugString() const {
01032 return "Apply(BranchSelector)";
01033 }
01034 private:
01035 ResultCallback1<Solver::DecisionModification, Solver*>* const selector_;
01036 };
01037 }
01038
01039 void Search::SetBranchSelector(
01040 ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
01041 CHECK(bs == selector_ || selector_ == NULL || bs == NULL);
01042 if (selector_ != bs) {
01043 selector_.reset(bs);
01044 }
01045 }
01046
01047 void Solver::SetBranchSelector(
01048 ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
01049 bs->CheckIsRepeatable();
01050
01051
01052
01053 AddBacktrackAction(RevAlloc(new UndoBranchSelector(SolveDepth())),
01054 false);
01055 searches_.back()->SetBranchSelector(bs);
01056 }
01057
01058 DecisionBuilder* Solver::MakeApplyBranchSelector(
01059 ResultCallback1<Solver::DecisionModification, Solver*>* const bs) {
01060 return RevAlloc(new ApplyBranchSelector(bs));
01061 }
01062
01063 int Solver::SolveDepth() const {
01064 return state_ == OUTSIDE_SEARCH ? 0 : searches_.size() - 1;
01065 }
01066
01067 int Solver::SearchDepth() const {
01068 return searches_.back()->search_depth();
01069 }
01070
01071 int Solver::SearchLeftDepth() const {
01072 return searches_.back()->left_search_depth();
01073 }
01074
01075 Solver::DecisionModification Search::ModifyDecision() {
01076 if (selector_ != NULL) {
01077 return selector_->Run(solver_);
01078 }
01079 return Solver::NO_CHANGE;
01080 }
01081
01082 void Search::push_monitor(SearchMonitor* const m) {
01083 if (m) {
01084 monitors_.push_back(m);
01085 }
01086 }
01087
01088 void Search::Clear() {
01089 monitors_.clear();
01090 search_depth_ = 0;
01091 left_search_depth_ = 0;
01092 selector_.reset(NULL);
01093 }
01094
01095 void Search::EnterSearch() {
01096
01097
01098
01099 solution_counter_ = 0;
01100
01101 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01102 it != monitors_.end();
01103 ++it) {
01104 (*it)->EnterSearch();
01105 }
01106 }
01107
01108 void Search::ExitSearch() {
01109 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01110 it != monitors_.end();
01111 ++it) {
01112 (*it)->ExitSearch();
01113 }
01114 }
01115
01116 void Search::RestartSearch() {
01117 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01118 it != monitors_.end();
01119 ++it) {
01120 (*it)->RestartSearch();
01121 }
01122 }
01123
01124 void Search::BeginNextDecision(DecisionBuilder* const db) {
01125 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01126 it != monitors_.end();
01127 ++it) {
01128 (*it)->BeginNextDecision(db);
01129 }
01130 CheckFail();
01131 }
01132
01133 void Search::EndNextDecision(DecisionBuilder* const db, Decision* const d) {
01134 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01135 it != monitors_.end();
01136 ++it) {
01137 (*it)->EndNextDecision(db, d);
01138 }
01139 CheckFail();
01140 }
01141
01142 void Search::ApplyDecision(Decision* const d) {
01143 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01144 it != monitors_.end();
01145 ++it) {
01146 (*it)->ApplyDecision(d);
01147 }
01148 CheckFail();
01149 }
01150
01151 void Search::AfterDecision(Decision* const d, bool apply) {
01152 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01153 it != monitors_.end();
01154 ++it) {
01155 (*it)->AfterDecision(d, apply);
01156 }
01157 CheckFail();
01158 }
01159
01160 void Search::RefuteDecision(Decision* const d) {
01161 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01162 it != monitors_.end();
01163 ++it) {
01164 (*it)->RefuteDecision(d);
01165 }
01166 CheckFail();
01167 }
01168
01169 void Search::BeginFail() {
01170 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01171 it != monitors_.end();
01172 ++it) {
01173 (*it)->BeginFail();
01174 }
01175 }
01176
01177 void Search::EndFail() {
01178 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01179 it != monitors_.end();
01180 ++it) {
01181 (*it)->EndFail();
01182 }
01183 }
01184
01185 void Search::BeginInitialPropagation() {
01186 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01187 it != monitors_.end();
01188 ++it) {
01189 (*it)->BeginInitialPropagation();
01190 }
01191 }
01192
01193 void Search::EndInitialPropagation() {
01194 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01195 it != monitors_.end();
01196 ++it) {
01197 (*it)->EndInitialPropagation();
01198 }
01199 }
01200
01201 bool Search::AcceptSolution() {
01202 bool valid = true;
01203 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01204 it != monitors_.end();
01205 ++it) {
01206 if (!(*it)->AcceptSolution()) {
01207
01208
01209
01210 valid = false;
01211 }
01212 }
01213 return valid;
01214 }
01215
01216 bool Search::AtSolution() {
01217 bool should_continue = false;
01218 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01219 it != monitors_.end();
01220 ++it) {
01221 if ((*it)->AtSolution()) {
01222
01223
01224
01225 should_continue = true;
01226 }
01227 }
01228 return should_continue;
01229 }
01230
01231 void Search::NoMoreSolutions() {
01232 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01233 it != monitors_.end();
01234 ++it) {
01235 (*it)->NoMoreSolutions();
01236 }
01237 }
01238
01239 bool Search::LocalOptimum() {
01240 bool res = false;
01241 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01242 it != monitors_.end();
01243 ++it) {
01244 if ((*it)->LocalOptimum()) {
01245 res = true;
01246 }
01247 }
01248 return res;
01249 }
01250
01251 bool Search::AcceptDelta(Assignment* delta, Assignment* deltadelta) {
01252 bool accept = true;
01253 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01254 it != monitors_.end();
01255 ++it) {
01256 if (!(*it)->AcceptDelta(delta, deltadelta)) {
01257 accept = false;
01258 }
01259 }
01260 return accept;
01261 }
01262
01263 void Search::AcceptNeighbor() {
01264 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01265 it != monitors_.end();
01266 ++it) {
01267 (*it)->AcceptNeighbor();
01268 }
01269 }
01270
01271 void Search::PeriodicCheck() {
01272 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01273 it != monitors_.end();
01274 ++it) {
01275 (*it)->PeriodicCheck();
01276 }
01277 }
01278
01279 int Search::ProgressPercent() {
01280 int progress = SearchMonitor::kNoProgress;
01281 for (std::vector<SearchMonitor*>::iterator it = monitors_.begin();
01282 it != monitors_.end();
01283 ++it) {
01284 progress = std::max(progress, (*it)->ProgressPercent());
01285 }
01286 return progress;
01287 }
01288
01289 void Search::Accept(ModelVisitor* const visitor) const {
01290 for (std::vector<SearchMonitor*>::const_iterator it = monitors_.begin();
01291 it != monitors_.end();
01292 ++it) {
01293 DCHECK((*it) != NULL);
01294 (*it)->Accept(visitor);
01295 }
01296 if (decision_builder_ != NULL) {
01297 decision_builder_->Accept(visitor);
01298 }
01299 }
01300
01301 bool LocalOptimumReached(Search* const search) {
01302 return search->LocalOptimum();
01303 }
01304
01305 bool AcceptDelta(Search* const search,
01306 Assignment* delta,
01307 Assignment* deltadelta) {
01308 return search->AcceptDelta(delta, deltadelta);
01309 }
01310
01311 void AcceptNeighbor(Search* const search) {
01312 search->AcceptNeighbor();
01313 }
01314
01315
01316 namespace {
01317
01318
01319
01320 class FailDecision : public Decision {
01321 public:
01322 virtual void Apply(Solver* const s) {
01323 s->Fail();
01324 }
01325 virtual void Refute(Solver* const s) {
01326 s->Fail();
01327 }
01328 };
01329
01330
01331
01332 class BalancingDecision : public Decision {
01333 public:
01334 virtual ~BalancingDecision() {}
01335 virtual void Apply(Solver* const s) {}
01336 virtual void Refute(Solver* const s) {}
01337 };
01338 }
01339
01340 Decision* Solver::MakeFailDecision() {
01341 return fail_decision_.get();
01342 }
01343
01344
01345
01346
01347
01348
01349 namespace {
01350 enum SentinelMarker {
01351 INITIAL_SEARCH_SENTINEL = 10000000,
01352 ROOT_NODE_SENTINEL = 20000000,
01353 SOLVER_CTOR_SENTINEL = 40000000
01354 };
01355 }
01356
01357 extern Action* NewDomainIntVarCleaner();
01358 extern PropagationMonitor* BuildTrace(Solver* const s);
01359 extern ModelCache* BuildModelCache(Solver* const solver);
01360 extern DependencyGraph* BuildDependencyGraph(Solver* const solver);
01361
01362 string Solver::model_name() const { return name_; }
01363
01364 Solver::Solver(const string& name, const SolverParameters& parameters)
01365 : name_(name),
01366 parameters_(parameters),
01367 queue_(new Queue(this)),
01368 trail_(new Trail(parameters.trail_block_size, parameters.compress_trail)),
01369 state_(OUTSIDE_SEARCH),
01370 branches_(0),
01371 fails_(0),
01372 decisions_(0),
01373 neighbors_(0),
01374 filtered_neighbors_(0),
01375 accepted_neighbors_(0),
01376 variable_cleaner_(NewDomainIntVarCleaner()),
01377 timer_(new ClockTimer),
01378 searches_(1, new Search(this, 0)),
01379 random_(ACMRandom::DeterministicSeed()),
01380 fail_hooks_(NULL),
01381 fail_stamp_(GG_ULONGLONG(1)),
01382 balancing_decision_(new BalancingDecision),
01383 fail_intercept_(NULL),
01384 demon_profiler_(BuildDemonProfiler(this)),
01385 true_constraint_(NULL),
01386 false_constraint_(NULL),
01387 fail_decision_(new FailDecision()),
01388 constraint_index_(0),
01389 additional_constraint_index_(0),
01390 model_cache_(NULL),
01391 dependency_graph_(NULL),
01392 propagation_monitor_(BuildTrace(this)),
01393 print_trace_(NULL),
01394 anonymous_variable_index_(0) {
01395 Init();
01396 }
01397
01398 Solver::Solver(const string& name)
01399 : name_(name),
01400 parameters_(),
01401 queue_(new Queue(this)),
01402 trail_(new Trail(parameters_.trail_block_size,
01403 parameters_.compress_trail)),
01404 state_(OUTSIDE_SEARCH),
01405 branches_(0),
01406 fails_(0),
01407 decisions_(0),
01408 neighbors_(0),
01409 filtered_neighbors_(0),
01410 accepted_neighbors_(0),
01411 variable_cleaner_(NewDomainIntVarCleaner()),
01412 timer_(new ClockTimer),
01413 searches_(1, new Search(this, 0)),
01414 random_(ACMRandom::DeterministicSeed()),
01415 fail_hooks_(NULL),
01416 fail_stamp_(GG_ULONGLONG(1)),
01417 balancing_decision_(new BalancingDecision),
01418 fail_intercept_(NULL),
01419 demon_profiler_(BuildDemonProfiler(this)),
01420 true_constraint_(NULL),
01421 false_constraint_(NULL),
01422 fail_decision_(new FailDecision()),
01423 constraint_index_(0),
01424 additional_constraint_index_(0),
01425 model_cache_(NULL),
01426 dependency_graph_(NULL),
01427 propagation_monitor_(BuildTrace(this)),
01428 print_trace_(NULL),
01429 anonymous_variable_index_(0) {
01430 Init();
01431 }
01432
01433 void Solver::Init() {
01434 for (int i = 0; i < kNumPriorities; ++i) {
01435 demon_runs_[i] = 0;
01436 }
01437 searches_.push_back(new Search(this));
01438 PushSentinel(SOLVER_CTOR_SENTINEL);
01439 InitCachedIntConstants();
01440 InitCachedConstraint();
01441 InitBuilders();
01442 timer_->Restart();
01443 model_cache_.reset(BuildModelCache(this));
01444 dependency_graph_.reset(BuildDependencyGraph(this));
01445 AddPropagationMonitor(reinterpret_cast<PropagationMonitor*>(demon_profiler_));
01446 }
01447
01448 Solver::~Solver() {
01449
01450 CHECK_EQ(2, searches_.size());
01451 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
01452
01453 StateInfo info;
01454 Solver::MarkerType finalType = PopState(&info);
01455
01456 DCHECK_EQ(finalType, SENTINEL);
01457
01458 DCHECK_EQ(info.int_info, SOLVER_CTOR_SENTINEL);
01459 STLDeleteElements(&searches_);
01460 DeleteDemonProfiler(demon_profiler_);
01461 DeleteBuilders();
01462 }
01463
01464 const SolverParameters::TrailCompression
01465 SolverParameters::kDefaultTrailCompression = SolverParameters::NO_COMPRESSION;
01466 const int SolverParameters::kDefaultTrailBlockSize = 8000;
01467 const int SolverParameters::kDefaultArraySplitSize = 16;
01468 const bool SolverParameters::kDefaultNameStoring = true;
01469 const SolverParameters::ProfileLevel SolverParameters::kDefaultProfileLevel =
01470 SolverParameters::NO_PROFILING;
01471 const SolverParameters::TraceLevel SolverParameters::kDefaultTraceLevel =
01472 SolverParameters::NO_TRACE;
01473 const bool SolverParameters::kDefaultNameAllVariables = false;
01474
01475 string Solver::DebugString() const {
01476 string out = "Solver(name = \"" + name_ + "\", state = ";
01477 switch (state_) {
01478 case OUTSIDE_SEARCH:
01479 out += "OUTSIDE_SEARCH";
01480 break;
01481 case IN_ROOT_NODE:
01482 out += "IN_ROOT_NODE";
01483 break;
01484 case IN_SEARCH:
01485 out += "IN_SEARCH";
01486 break;
01487 case AT_SOLUTION:
01488 out += "AT_SOLUTION";
01489 break;
01490 case NO_MORE_SOLUTIONS:
01491 out += "NO_MORE_SOLUTIONS";
01492 break;
01493 case PROBLEM_INFEASIBLE:
01494 out += "PROBLEM_INFEASIBLE";
01495 break;
01496 }
01497 StringAppendF(&out, ", branches = %" GG_LL_FORMAT
01498 "d, fails = %" GG_LL_FORMAT
01499 "d, decisions = %" GG_LL_FORMAT
01500 "d, delayed demon runs = %" GG_LL_FORMAT
01501 "d, var demon runs = %" GG_LL_FORMAT
01502 "d, normal demon runs = %" GG_LL_FORMAT
01503 "d, Run time = %" GG_LL_FORMAT "d ms)",
01504 branches_, fails_, decisions_, demon_runs_[DELAYED_PRIORITY],
01505 demon_runs_[VAR_PRIORITY], demon_runs_[NORMAL_PRIORITY],
01506 wall_time());
01507 return out;
01508 }
01509
01510 int64 Solver::MemoryUsage() {
01511 return GetProcessMemoryUsage();
01512 }
01513
01514
01515 int64 Solver::wall_time() const {
01516 return timer_->GetInMs();
01517 }
01518
01519 int64 Solver::solutions() const {
01520 return TopLevelSearch()->solution_counter();
01521 }
01522
01523 void Solver::TopPeriodicCheck() {
01524 TopLevelSearch()->PeriodicCheck();
01525 }
01526
01527 int Solver::TopProgressPercent() {
01528 return TopLevelSearch()->ProgressPercent();
01529 }
01530
01531 void Solver::PushState() {
01532 StateInfo info;
01533 PushState(SIMPLE_MARKER, info);
01534 }
01535
01536 void Solver::PopState() {
01537 StateInfo info;
01538 Solver::MarkerType t = PopState(&info);
01539 CHECK_EQ(SIMPLE_MARKER, t);
01540 }
01541
01542 void Solver::PushState(Solver::MarkerType t, const StateInfo& info) {
01543 StateMarker* m = new StateMarker(t, info);
01544 if (t != REVERSIBLE_ACTION || info.int_info == 0) {
01545 m->rev_int_index_ = trail_->rev_ints_.size();
01546 m->rev_int64_index_ = trail_->rev_int64s_.size();
01547 m->rev_uint64_index_ = trail_->rev_uint64s_.size();
01548 m->rev_ptr_index_ = trail_->rev_ptrs_.size();
01549 m->rev_boolvar_list_index_ = trail_->rev_boolvar_list_.size();
01550 m->rev_bools_index_ = trail_->rev_bools_.size();
01551 m->rev_int_memory_index_ = trail_->rev_int_memory_.size();
01552 m->rev_int64_memory_index_ = trail_->rev_int64_memory_.size();
01553 m->rev_object_memory_index_ = trail_->rev_object_memory_.size();
01554 m->rev_object_array_memory_index_ = trail_->rev_object_array_memory_.size();
01555 m->rev_memory_index_ = trail_->rev_memory_.size();
01556 m->rev_memory_array_index_ = trail_->rev_memory_array_.size();
01557 }
01558 searches_.back()->marker_stack_.push_back(m);
01559 queue_->increase_stamp();
01560 }
01561
01562 void Solver::AddBacktrackAction(Action* a, bool fast) {
01563 StateInfo info(a, static_cast<int>(fast));
01564 PushState(REVERSIBLE_ACTION, info);
01565 }
01566
01567 Solver::MarkerType Solver::PopState(StateInfo* info) {
01568 CHECK(!searches_.back()->marker_stack_.empty())
01569 << "PopState() on an empty stack";
01570 CHECK(info != NULL);
01571 StateMarker* m = searches_.back()->marker_stack_.back();
01572 if (m->type_ != REVERSIBLE_ACTION || m->info_.int_info == 0) {
01573 trail_->BacktrackTo(m);
01574 }
01575 Solver::MarkerType t = m->type_;
01576 (*info) = m->info_;
01577 searches_.back()->marker_stack_.pop_back();
01578 delete m;
01579 queue_->increase_stamp();
01580 return t;
01581 }
01582
01583 void Solver::check_alloc_state() {
01584 switch (state_) {
01585 case OUTSIDE_SEARCH:
01586 case IN_ROOT_NODE:
01587 case IN_SEARCH:
01588 case NO_MORE_SOLUTIONS:
01589 case PROBLEM_INFEASIBLE:
01590 break;
01591 case AT_SOLUTION:
01592 LOG(FATAL) << "allocating at a leaf node";
01593 default:
01594 LOG(FATAL) << "This switch was supposed to be exhaustive, but it is not!";
01595 }
01596 }
01597
01598 void Solver::AddFailHook(Action* a) {
01599 if (fail_hooks_ == NULL) {
01600 SaveValue(reinterpret_cast<void**>(&fail_hooks_));
01601 fail_hooks_ = UnsafeRevAlloc(new SimpleRevFIFO<Action*>);
01602 }
01603 fail_hooks_->Push(this, a);
01604 }
01605
01606 void Solver::CallFailHooks() {
01607 if (fail_hooks_ != NULL) {
01608 for (SimpleRevFIFO<Action*>::Iterator it(fail_hooks_); it.ok(); ++it) {
01609 (*it)->Run(this);
01610 }
01611 }
01612 }
01613
01614 void Solver::FreezeQueue() {
01615 queue_->Freeze();
01616 }
01617
01618 void Solver::UnfreezeQueue() {
01619 queue_->Unfreeze();
01620 }
01621
01622 void Solver::Enqueue(Demon* d) {
01623 queue_->Enqueue(d);
01624 }
01625
01626 void Solver::ProcessDemonsOnQueue() {
01627 queue_->ProcessNormalDemons();
01628 }
01629
01630 uint64 Solver::stamp() const {
01631 return queue_->stamp();
01632 }
01633
01634 uint64 Solver::fail_stamp() const {
01635 return fail_stamp_;
01636 }
01637
01638 void Solver::set_queue_action_on_fail(Action* a) {
01639 queue_->set_action_on_fail(a);
01640 }
01641
01642 void SetQueueCleanerOnFail(Solver* const solver, IntVar* const var) {
01643 solver->set_queue_cleaner_on_fail(var);
01644 }
01645
01646 void Solver::clear_queue_action_on_fail() {
01647 queue_->clear_action_on_fail();
01648 }
01649
01650 void Solver::AddConstraint(Constraint* const c) {
01651 if (state_ == IN_SEARCH) {
01652 queue_->AddConstraint(c);
01653 } else if (state_ == IN_ROOT_NODE) {
01654 DCHECK_GE(constraint_index_, 0);
01655 DCHECK_LE(constraint_index_, constraints_list_.size());
01656 const int constraint_parent =
01657 constraint_index_ == constraints_list_.size() ?
01658 additional_constraints_parent_list_[additional_constraint_index_] :
01659 constraint_index_;
01660 additional_constraints_list_.push_back(c);
01661 additional_constraints_parent_list_.push_back(constraint_parent);
01662 } else {
01663 if (FLAGS_cp_show_constraints) {
01664 LOG(INFO) << c->DebugString();
01665 }
01666 constraints_list_.push_back(c);
01667 }
01668 }
01669
01670 void Solver::AddCastConstraint(CastConstraint* const constraint,
01671 IntVar* const target_var,
01672 IntExpr* const expr) {
01673 if (constraint != NULL) {
01674 if (state_ != IN_SEARCH) {
01675 cast_constraints_.insert(constraint);
01676 cast_information_[target_var] =
01677 Solver::IntegerCastInfo(target_var, expr, constraint);
01678 }
01679 AddConstraint(constraint);
01680 }
01681 }
01682
01683 void Solver::Accept(ModelVisitor* const visitor) const {
01684 std::vector<SearchMonitor*> monitors;
01685 Accept(visitor, monitors);
01686 }
01687
01688 void Solver::Accept(ModelVisitor* const visitor,
01689 const std::vector<SearchMonitor*>& monitors) const {
01690 visitor->BeginVisitModel(name_);
01691 for (int index = 0; index < constraints_list_.size(); ++index) {
01692 Constraint* const constraint = constraints_list_[index];
01693 constraint->Accept(visitor);
01694 }
01695 if (state_ == IN_ROOT_NODE) {
01696 TopLevelSearch()->Accept(visitor);
01697 } else {
01698 for (int i = 0; i < monitors.size(); ++i) {
01699 monitors[i]->Accept(visitor);
01700 }
01701 }
01702 visitor->EndVisitModel(name_);
01703 }
01704
01705 void Solver::ProcessConstraints() {
01706
01707
01708 if (FLAGS_cp_print_model) {
01709 ModelVisitor* const visitor = MakePrintModelVisitor();
01710 Accept(visitor);
01711 }
01712 if (FLAGS_cp_model_stats) {
01713 ModelVisitor* const visitor = MakeStatisticsModelVisitor();
01714 Accept(visitor);
01715 }
01716 if (!FLAGS_cp_export_file.empty()) {
01717 File::Init();
01718 File* file = File::Open(FLAGS_cp_export_file, "wb");
01719 if (file == NULL) {
01720 LOG(WARNING) << "Cannot open " << FLAGS_cp_export_file;
01721 } else {
01722 CPModelProto export_proto;
01723 ExportModel(&export_proto);
01724 VLOG(1) << export_proto.DebugString();
01725 RecordWriter writer(file);
01726 writer.WriteProtocolMessage(export_proto);
01727 writer.Close();
01728 }
01729 }
01730
01731 if (FLAGS_cp_no_solve) {
01732 LOG(INFO) << "Forcing early failure";
01733 Fail();
01734 }
01735
01736
01737 const int constraints_size = constraints_list_.size();
01738 additional_constraints_list_.clear();
01739 additional_constraints_parent_list_.clear();
01740
01741 for (constraint_index_ = 0;
01742 constraint_index_ < constraints_size;
01743 ++constraint_index_) {
01744 Constraint* const constraint = constraints_list_[constraint_index_];
01745 propagation_monitor_->BeginConstraintInitialPropagation(constraint);
01746 constraint->PostAndPropagate();
01747 propagation_monitor_->EndConstraintInitialPropagation(constraint);
01748 }
01749 CHECK_EQ(constraints_list_.size(), constraints_size);
01750
01751
01752 for (int additional_constraint_index_ = 0;
01753 additional_constraint_index_ < additional_constraints_list_.size();
01754 ++additional_constraint_index_) {
01755 Constraint* const nested =
01756 additional_constraints_list_[additional_constraint_index_];
01757 const int parent_index =
01758 additional_constraints_parent_list_[additional_constraint_index_];
01759 const Constraint* const parent = constraints_list_[parent_index];
01760 propagation_monitor_->BeginNestedConstraintInitialPropagation(parent,
01761 nested);
01762 nested->PostAndPropagate();
01763 propagation_monitor_->EndNestedConstraintInitialPropagation(parent, nested);
01764 }
01765 }
01766
01767 bool Solver::CurrentlyInSolve() const {
01768 DCHECK_GT(SolveDepth(), 0);
01769 DCHECK(searches_.back() != NULL);
01770 return searches_.back()->created_by_solve();
01771 }
01772
01773 bool Solver::Solve(DecisionBuilder* const db,
01774 const std::vector<SearchMonitor*>& monitors) {
01775 return Solve(db, monitors.data(), monitors.size());
01776 }
01777
01778 bool Solver::Solve(DecisionBuilder* const db, SearchMonitor* const m1) {
01779 std::vector<SearchMonitor*> monitors;
01780 monitors.push_back(m1);
01781 return Solve(db, monitors.data(), monitors.size());
01782 }
01783
01784 bool Solver::Solve(DecisionBuilder* const db) {
01785 return Solve(db, NULL, Zero());
01786 }
01787
01788 bool Solver::Solve(DecisionBuilder* const db,
01789 SearchMonitor* const m1,
01790 SearchMonitor* const m2) {
01791 std::vector<SearchMonitor*> monitors;
01792 monitors.push_back(m1);
01793 monitors.push_back(m2);
01794 return Solve(db, monitors.data(), monitors.size());
01795 }
01796
01797 bool Solver::Solve(DecisionBuilder* const db,
01798 SearchMonitor* const m1,
01799 SearchMonitor* const m2,
01800 SearchMonitor* const m3) {
01801 std::vector<SearchMonitor*> monitors;
01802 monitors.push_back(m1);
01803 monitors.push_back(m2);
01804 monitors.push_back(m3);
01805 return Solve(db, monitors.data(), monitors.size());
01806 }
01807
01808 bool Solver::Solve(DecisionBuilder* const db,
01809 SearchMonitor* const m1,
01810 SearchMonitor* const m2,
01811 SearchMonitor* const m3,
01812 SearchMonitor* const m4) {
01813 std::vector<SearchMonitor*> monitors;
01814 monitors.push_back(m1);
01815 monitors.push_back(m2);
01816 monitors.push_back(m3);
01817 monitors.push_back(m4);
01818 return Solve(db, monitors.data(), monitors.size());
01819 }
01820
01821 bool Solver::Solve(DecisionBuilder* const db,
01822 SearchMonitor* const * monitors,
01823 int size) {
01824 NewSearch(db, monitors, size);
01825 searches_.back()->set_created_by_solve(true);
01826 NextSolution();
01827 const bool solution_found = searches_.back()->solution_counter() > 0;
01828 EndSearch();
01829 return solution_found;
01830 }
01831
01832 void Solver::NewSearch(DecisionBuilder* const db,
01833 const std::vector<SearchMonitor*>& monitors) {
01834 return NewSearch(db, monitors.data(), monitors.size());
01835 }
01836
01837 void Solver::NewSearch(DecisionBuilder* const db, SearchMonitor* const m1) {
01838 std::vector<SearchMonitor*> monitors;
01839 monitors.push_back(m1);
01840 return NewSearch(db, monitors.data(), monitors.size());
01841 }
01842
01843 void Solver::NewSearch(DecisionBuilder* const db) {
01844 return NewSearch(db, NULL, Zero());
01845 }
01846
01847 void Solver::NewSearch(DecisionBuilder* const db,
01848 SearchMonitor* const m1,
01849 SearchMonitor* const m2) {
01850 std::vector<SearchMonitor*> monitors;
01851 monitors.push_back(m1);
01852 monitors.push_back(m2);
01853 return NewSearch(db, monitors.data(), monitors.size());
01854 }
01855
01856 void Solver::NewSearch(DecisionBuilder* const db,
01857 SearchMonitor* const m1,
01858 SearchMonitor* const m2,
01859 SearchMonitor* const m3) {
01860 std::vector<SearchMonitor*> monitors;
01861 monitors.push_back(m1);
01862 monitors.push_back(m2);
01863 monitors.push_back(m3);
01864 return NewSearch(db, monitors.data(), monitors.size());
01865 }
01866
01867 void Solver::NewSearch(DecisionBuilder* const db,
01868 SearchMonitor* const m1,
01869 SearchMonitor* const m2,
01870 SearchMonitor* const m3,
01871 SearchMonitor* const m4) {
01872 std::vector<SearchMonitor*> monitors;
01873 monitors.push_back(m1);
01874 monitors.push_back(m2);
01875 monitors.push_back(m3);
01876 monitors.push_back(m4);
01877 return NewSearch(db, monitors.data(), monitors.size());
01878 }
01879
01880 extern PropagationMonitor* BuildPrintTrace(Solver* const s);
01881
01882
01883 void Solver::NewSearch(DecisionBuilder* const db,
01884 SearchMonitor* const * monitors,
01885 int size) {
01886
01887
01888 CHECK_NOTNULL(db);
01889 DCHECK_GE(size, 0);
01890
01891 if (state_ == IN_SEARCH || state_ == IN_ROOT_NODE) {
01892 LOG(FATAL) << "Use NestedSolve() inside search";
01893 }
01894
01895 Search* const search = searches_.back();
01896 search->set_created_by_solve(false);
01897
01898 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
01899 state_ = OUTSIDE_SEARCH;
01900
01901
01902 propagation_monitor_->Install();
01903 if (demon_profiler_ != NULL) {
01904 InstallDemonProfiler(demon_profiler_);
01905 }
01906
01907
01908 for (int i = 0; i < size; ++i) {
01909 if (monitors[i] != NULL) {
01910 monitors[i]->Install();
01911 }
01912 }
01913 std::vector<SearchMonitor*> extras;
01914 db->AppendMonitors(this, &extras);
01915 for (ConstIter<std::vector<SearchMonitor*> > it(extras); !it.at_end(); ++it) {
01916 SearchMonitor* const monitor = *it;
01917 if (monitor != NULL) {
01918 monitor->Install();
01919 }
01920 }
01921
01922
01923 if (FLAGS_cp_trace_propagation) {
01924 print_trace_ = BuildPrintTrace(this);
01925 print_trace_->Install();
01926 } else {
01927
01928
01929 if (FLAGS_cp_trace_search) {
01930 SearchMonitor* const trace = MakeSearchTrace("######## ");
01931 trace->Install();
01932 }
01933 print_trace_ = NULL;
01934 }
01935
01936 search->EnterSearch();
01937
01938
01939 DCHECK_EQ(2, searches_.size());
01940 PushSentinel(INITIAL_SEARCH_SENTINEL);
01941 search->set_decision_builder(db);
01942 }
01943
01944
01945
01946 bool Solver::BacktrackOneLevel(Decision** const fail_decision) {
01947 bool no_more_solutions = false;
01948 bool end_loop = false;
01949 while (!end_loop) {
01950 StateInfo info;
01951 Solver::MarkerType t = PopState(&info);
01952 switch (t) {
01953 case SENTINEL:
01954 CHECK_EQ(info.ptr_info, this) << "Wrong sentinel found";
01955 CHECK((info.int_info == ROOT_NODE_SENTINEL && SolveDepth() == 1) ||
01956 (info.int_info == INITIAL_SEARCH_SENTINEL &&
01957 SolveDepth() > 1));
01958 searches_.back()->sentinel_pushed_--;
01959 no_more_solutions = true;
01960 end_loop = true;
01961 break;
01962 case SIMPLE_MARKER:
01963 LOG(ERROR)
01964 << "Simple markers should not be encountered during search";
01965 break;
01966 case CHOICE_POINT:
01967 if (info.int_info == 0) {
01968 (*fail_decision) = reinterpret_cast<Decision*>(info.ptr_info);
01969 end_loop = true;
01970 searches_.back()->set_search_depth(info.depth);
01971 searches_.back()->set_search_left_depth(info.left_depth);
01972 }
01973 break;
01974 case REVERSIBLE_ACTION: {
01975 Action* d = reinterpret_cast<Action*>(info.ptr_info);
01976 d->Run(this);
01977 break;
01978 }
01979 }
01980 }
01981 Search* const search = searches_.back();
01982 search->EndFail();
01983 CallFailHooks();
01984 fail_stamp_++;
01985 if (no_more_solutions) {
01986 search->NoMoreSolutions();
01987 }
01988 return no_more_solutions;
01989 }
01990
01991 void Solver::PushSentinel(int magic_code) {
01992 StateInfo info(this, magic_code);
01993 PushState(SENTINEL, info);
01994
01995 if (magic_code != SOLVER_CTOR_SENTINEL) {
01996 searches_.back()->sentinel_pushed_++;
01997 }
01998 const int pushed = searches_.back()->sentinel_pushed_;
01999 DCHECK((magic_code == SOLVER_CTOR_SENTINEL) ||
02000 (magic_code == INITIAL_SEARCH_SENTINEL && pushed == 1) ||
02001 (magic_code == ROOT_NODE_SENTINEL && pushed == 2));
02002 }
02003
02004 void Solver::RestartSearch() {
02005 Search* const search = searches_.back();
02006 CHECK_NE(0, search->sentinel_pushed_);
02007 if (SolveDepth() == 1) {
02008 if (search->sentinel_pushed_ > 1) {
02009 BacktrackToSentinel(ROOT_NODE_SENTINEL);
02010 }
02011 CHECK_EQ(1, search->sentinel_pushed_);
02012 PushSentinel(ROOT_NODE_SENTINEL);
02013 state_ = IN_SEARCH;
02014 } else {
02015 CHECK_EQ(IN_SEARCH, state_);
02016 if (search->sentinel_pushed_ > 0) {
02017 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02018 }
02019 CHECK_EQ(0, search->sentinel_pushed_);
02020 PushSentinel(INITIAL_SEARCH_SENTINEL);
02021 }
02022
02023 search->RestartSearch();
02024 }
02025
02026
02027
02028 void Solver::BacktrackToSentinel(int magic_code) {
02029 Search* const search = searches_.back();
02030 bool end_loop = search->sentinel_pushed_ == 0;
02031 while (!end_loop) {
02032 StateInfo info;
02033 Solver::MarkerType t = PopState(&info);
02034 switch (t) {
02035 case SENTINEL: {
02036 CHECK_EQ(info.ptr_info, this) << "Wrong sentinel found";
02037 CHECK_GE(--search->sentinel_pushed_, 0);
02038 search->set_search_depth(0);
02039 search->set_search_left_depth(0);
02040
02041 if (info.int_info == magic_code) {
02042 end_loop = true;
02043 }
02044 break;
02045 }
02046 case SIMPLE_MARKER:
02047 break;
02048 case CHOICE_POINT:
02049 break;
02050 case REVERSIBLE_ACTION: {
02051 Demon* d = reinterpret_cast<Demon*>(info.ptr_info);
02052 d->Run(this);
02053 break;
02054 }
02055 }
02056 }
02057 fail_stamp_++;
02058 }
02059
02060
02061 void Solver::JumpToSentinelWhenNested() {
02062 CHECK_GT(SolveDepth(), 1) << "calling JumpToSentinel from top level";
02063 Search* c = searches_.back();
02064 Search* p = ParentSearch();
02065 bool found = false;
02066 while (!c->marker_stack_.empty()) {
02067 StateMarker* const m = c->marker_stack_.back();
02068 if (m->type_ == REVERSIBLE_ACTION) {
02069 p->marker_stack_.push_back(m);
02070 } else {
02071 if (m->type_ == SENTINEL) {
02072 CHECK_EQ(c->marker_stack_.size(), 1) << "Sentinel found too early";
02073 found = true;
02074 }
02075 delete m;
02076 }
02077 c->marker_stack_.pop_back();
02078 }
02079 c->set_search_depth(0);
02080 c->set_search_left_depth(0);
02081 CHECK_EQ(found, true) << "Sentinel not found";
02082 }
02083
02084 namespace {
02085 class ReverseDecision : public Decision {
02086 public:
02087 explicit ReverseDecision(Decision* const d) : decision_(d) {
02088 CHECK(d != NULL);
02089 }
02090 virtual ~ReverseDecision() {}
02091
02092 virtual void Apply(Solver* const s) {
02093 decision_->Refute(s);
02094 }
02095
02096 virtual void Refute(Solver* const s) {
02097 decision_->Apply(s);
02098 }
02099
02100 virtual void Accept(DecisionVisitor* const visitor) const {
02101 decision_->Accept(visitor);
02102 }
02103
02104 virtual string DebugString() const {
02105 string str = "Reverse(";
02106 str += decision_->DebugString();
02107 str += ")";
02108 return str;
02109 }
02110
02111 private:
02112 Decision* const decision_;
02113 };
02114 }
02115
02116
02117 bool Solver::NextSolution() {
02118 Search* const search = searches_.back();
02119 Decision* fd = NULL;
02120 const int solve_depth = SolveDepth();
02121 const bool top_level = solve_depth <= 1;
02122
02123 if (solve_depth == 0 && !search->decision_builder()) {
02124 LOG(WARNING) << "NextSolution() called without a NewSearch before";
02125 return false;
02126 }
02127
02128 if (top_level) {
02129 switch (state_) {
02130 case PROBLEM_INFEASIBLE:
02131 return false;
02132 case NO_MORE_SOLUTIONS:
02133 return false;
02134 case AT_SOLUTION: {
02135 if (BacktrackOneLevel(&fd)) {
02136 state_ = NO_MORE_SOLUTIONS;
02137 return false;
02138 }
02139 state_ = IN_SEARCH;
02140 break;
02141 }
02142 case OUTSIDE_SEARCH: {
02143 state_ = IN_ROOT_NODE;
02144 search->BeginInitialPropagation();
02145 CP_TRY(search) {
02146 ProcessConstraints();
02147 search->EndInitialPropagation();
02148 PushSentinel(ROOT_NODE_SENTINEL);
02149 state_ = IN_SEARCH;
02150 search->ClearBuffer();
02151 } CP_ON_FAIL {
02152 queue_->AfterFailure();
02153 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02154 state_ = PROBLEM_INFEASIBLE;
02155 return false;
02156 }
02157 break;
02158 }
02159 case IN_SEARCH:
02160 break;
02161 case IN_ROOT_NODE:
02162 LOG(FATAL) << "Should not happen";
02163 break;
02164 }
02165 }
02166
02167 volatile bool finish = false;
02168 volatile bool result = false;
02169 DecisionBuilder* const db = search->decision_builder();
02170
02171 while (!finish) {
02172 CP_TRY(search) {
02173 if (fd != NULL) {
02174 StateInfo i1(fd,
02175 1,
02176 search->search_depth(),
02177 search->left_search_depth());
02178 PushState(CHOICE_POINT, i1);
02179 search->RefuteDecision(fd);
02180 branches_++;
02181 fd->Refute(this);
02182 search->AfterDecision(fd, false);
02183 search->RightMove();
02184 fd = NULL;
02185 }
02186 Decision* d = NULL;
02187 for (;;) {
02188 search->BeginNextDecision(db);
02189 d = db->Next(this);
02190 search->EndNextDecision(db, d);
02191 if (d == fail_decision_) {
02192 Fail();
02193 }
02194 if (d != NULL) {
02195 DecisionModification modification = search->ModifyDecision();
02196 switch (modification) {
02197 case SWITCH_BRANCHES: {
02198 d = RevAlloc(new ReverseDecision(d));
02199 }
02200 case NO_CHANGE: {
02201 decisions_++;
02202 StateInfo i2(d,
02203 0,
02204 search->search_depth(),
02205 search->left_search_depth());
02206 PushState(CHOICE_POINT, i2);
02207 search->ApplyDecision(d);
02208 branches_++;
02209 d->Apply(this);
02210 search->AfterDecision(d, true);
02211 search->LeftMove();
02212 break;
02213 }
02214 case KEEP_LEFT: {
02215 search->ApplyDecision(d);
02216 d->Apply(this);
02217 search->AfterDecision(d, true);
02218 break;
02219 }
02220 case KEEP_RIGHT: {
02221 search->RefuteDecision(d);
02222 d->Refute(this);
02223 search->AfterDecision(d, false);
02224 break;
02225 }
02226 case KILL_BOTH: {
02227 Fail();
02228 }
02229 }
02230 } else {
02231 break;
02232 }
02233 }
02234 if (search->AcceptSolution()) {
02235 search->IncrementSolutionCounter();
02236 if (!search->AtSolution() || !CurrentlyInSolve()) {
02237 result = true;
02238 finish = true;
02239 } else {
02240 Fail();
02241 }
02242 } else {
02243 Fail();
02244 }
02245 } CP_ON_FAIL {
02246 queue_->AfterFailure();
02247 if (search->should_finish()) {
02248 fd = NULL;
02249 BacktrackToSentinel(top_level ?
02250 ROOT_NODE_SENTINEL :
02251 INITIAL_SEARCH_SENTINEL);
02252 result = false;
02253 finish = true;
02254 search->set_should_finish(false);
02255 search->set_should_restart(false);
02256
02257 } else if (search->should_restart()) {
02258 fd = NULL;
02259 BacktrackToSentinel(top_level ?
02260 ROOT_NODE_SENTINEL :
02261 INITIAL_SEARCH_SENTINEL);
02262 search->set_should_finish(false);
02263 search->set_should_restart(false);
02264 PushSentinel(top_level ? ROOT_NODE_SENTINEL : INITIAL_SEARCH_SENTINEL);
02265 search->RestartSearch();
02266 } else {
02267 if (BacktrackOneLevel(&fd)) {
02268 result = false;
02269 finish = true;
02270 }
02271 }
02272 }
02273 }
02274 if (result) {
02275 search->ClearBuffer();
02276 }
02277 if (top_level) {
02278 state_ = (result ? AT_SOLUTION : NO_MORE_SOLUTIONS);
02279 }
02280 return result;
02281 }
02282
02283 void Solver::EndSearch() {
02284 CHECK_EQ(2, searches_.size());
02285 Search* const search = searches_.back();
02286 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02287 search->ExitSearch();
02288 search->Clear();
02289 state_ = OUTSIDE_SEARCH;
02290 if (!FLAGS_cp_profile_file.empty()) {
02291 LOG(INFO) << "Exporting profile to " << FLAGS_cp_profile_file;
02292 ExportProfilingOverview(FLAGS_cp_profile_file);
02293 }
02294 }
02295
02296 bool Solver::CheckAssignment(Assignment* const solution) {
02297 CHECK(solution);
02298 if (state_ == IN_SEARCH || state_ == IN_ROOT_NODE) {
02299 LOG(FATAL) << "Use NestedSolve() inside search";
02300 }
02301
02302 Search* const search = searches_.back();
02303 search->set_created_by_solve(false);
02304
02305 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02306 state_ = OUTSIDE_SEARCH;
02307
02308
02309 search->EnterSearch();
02310
02311
02312 DCHECK_EQ(0, SolveDepth());
02313 DCHECK_EQ(2, searches_.size());
02314 PushSentinel(INITIAL_SEARCH_SENTINEL);
02315 search->BeginInitialPropagation();
02316 CP_TRY(search) {
02317 state_ = IN_ROOT_NODE;
02318 DecisionBuilder * const restore = MakeRestoreAssignment(solution);
02319 restore->Next(this);
02320 ProcessConstraints();
02321 search->EndInitialPropagation();
02322 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02323 search->ClearBuffer();
02324 state_ = OUTSIDE_SEARCH;
02325 return true;
02326 } CP_ON_FAIL {
02327 const int index = constraint_index_ < constraints_list_.size() ?
02328 constraint_index_ :
02329 additional_constraints_parent_list_[additional_constraint_index_];
02330 Constraint* const ct = constraints_list_[index];
02331 if (ct->name().empty()) {
02332 LOG(INFO) << "Failing constraint = " << ct->DebugString();
02333 } else {
02334 LOG(INFO) << "Failing constraint = " << ct->name() << ":"
02335 << ct->DebugString();
02336 }
02337 queue_->AfterFailure();
02338 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02339 state_ = PROBLEM_INFEASIBLE;
02340 return false;
02341 }
02342 }
02343
02344 namespace {
02345 class AddConstraintDecisionBuilder : public DecisionBuilder {
02346 public:
02347 explicit AddConstraintDecisionBuilder(Constraint* const ct)
02348 : constraint_(ct) {
02349 CHECK_NOTNULL(ct);
02350 }
02351
02352 virtual ~AddConstraintDecisionBuilder() {}
02353
02354 virtual Decision* Next(Solver* const solver) {
02355 solver->AddConstraint(constraint_);
02356 return NULL;
02357 }
02358
02359 virtual string DebugString() const {
02360 return StringPrintf("AddConstraintDecisionBuilder(%s)",
02361 constraint_->DebugString().c_str());
02362 }
02363 private:
02364 Constraint* const constraint_;
02365 };
02366 }
02367
02368 DecisionBuilder* Solver::MakeConstraintAdder(Constraint* const ct) {
02369 return RevAlloc(new AddConstraintDecisionBuilder(ct));
02370 }
02371
02372 bool Solver::CheckConstraint(Constraint* const ct) {
02373 return Solve(MakeConstraintAdder(ct));
02374 }
02375
02376 bool Solver::NestedSolve(DecisionBuilder* const db,
02377 bool restore,
02378 const std::vector<SearchMonitor*>& monitors) {
02379 return NestedSolve(db, restore, monitors.data(), monitors.size());
02380 }
02381
02382 bool Solver::NestedSolve(DecisionBuilder* const db,
02383 bool restore,
02384 SearchMonitor* const m1) {
02385 std::vector<SearchMonitor*> monitors;
02386 monitors.push_back(m1);
02387 return NestedSolve(db, restore, monitors.data(), monitors.size());
02388 }
02389
02390 bool Solver::NestedSolve(DecisionBuilder* const db, bool restore) {
02391 return NestedSolve(db, restore, NULL, Zero());
02392 }
02393
02394 bool Solver::NestedSolve(DecisionBuilder* const db,
02395 bool restore,
02396 SearchMonitor* const m1,
02397 SearchMonitor* const m2) {
02398 std::vector<SearchMonitor*> monitors;
02399 monitors.push_back(m1);
02400 monitors.push_back(m2);
02401 return NestedSolve(db, restore, monitors.data(), monitors.size());
02402 }
02403
02404 bool Solver::NestedSolve(DecisionBuilder* const db,
02405 bool restore,
02406 SearchMonitor* const m1,
02407 SearchMonitor* const m2,
02408 SearchMonitor* const m3) {
02409 std::vector<SearchMonitor*> monitors;
02410 monitors.push_back(m1);
02411 monitors.push_back(m2);
02412 monitors.push_back(m3);
02413 return NestedSolve(db, restore, monitors.data(), monitors.size());
02414 }
02415
02416 bool Solver::NestedSolve(DecisionBuilder* const db,
02417 bool restore,
02418 SearchMonitor* const * monitors,
02419 int size) {
02420 Search new_search(this);
02421 searches_.push_back(&new_search);
02422
02423 propagation_monitor_->Install();
02424
02425 if (demon_profiler_ != NULL) {
02426 InstallDemonProfiler(demon_profiler_);
02427 }
02428
02429 for (int i = 0; i < size; ++i) {
02430 if (monitors[i] != NULL) {
02431 monitors[i]->Install();
02432 }
02433 }
02434 std::vector<SearchMonitor*> extras;
02435 db->AppendMonitors(this, &extras);
02436 for (ConstIter<std::vector<SearchMonitor*> > it(extras); !it.at_end(); ++it) {
02437 SearchMonitor* const monitor = *it;
02438 if (monitor != NULL) {
02439 monitor->Install();
02440 }
02441 }
02442
02443 if (print_trace_ != NULL) {
02444 print_trace_->Install();
02445 }
02446
02447 searches_.back()->set_created_by_solve(true);
02448 new_search.EnterSearch();
02449 PushSentinel(INITIAL_SEARCH_SENTINEL);
02450 new_search.set_decision_builder(db);
02451 bool res = NextSolution();
02452 if (res) {
02453 if (restore) {
02454 BacktrackToSentinel(INITIAL_SEARCH_SENTINEL);
02455 } else {
02456 JumpToSentinelWhenNested();
02457 }
02458 }
02459 new_search.ExitSearch();
02460 new_search.Clear();
02461 searches_.pop_back();
02462 return res;
02463 }
02464
02465 void Solver::Fail() {
02466 if (fail_intercept_) {
02467 fail_intercept_->Run();
02468 return;
02469 }
02470 ConstraintSolverFailsHere();
02471 fails_++;
02472 searches_.back()->BeginFail();
02473 searches_.back()->JumpBack();
02474 }
02475
02476
02477
02478 string Solver::GetName(const PropagationBaseObject* object) {
02479 const string* name = FindOrNull(propagation_object_names_, object);
02480 if (name != NULL) {
02481 return *name;
02482 }
02483 const IntegerCastInfo* const cast_info =
02484 FindOrNull(cast_information_, object);
02485 if (cast_info != NULL && cast_info->expression != NULL) {
02486 if (cast_info->expression->HasName()) {
02487 return StringPrintf("Var<%s>",
02488 cast_info->expression->name().c_str());
02489 } else {
02490 return StringPrintf("Var<%s>",
02491 cast_info->expression->DebugString().c_str());
02492 }
02493 }
02494 const string base_name = object->BaseName();
02495 if (FLAGS_cp_name_variables && !base_name.empty()) {
02496 const string new_name =
02497 StringPrintf("%s_%d", base_name.c_str(), anonymous_variable_index_++);
02498 propagation_object_names_[object] = new_name;
02499 return new_name;
02500 }
02501 return empty_name_;
02502 }
02503
02504 void Solver::SetName(const PropagationBaseObject* object, const string& name) {
02505 if (parameters_.store_names
02506 && GetName(object).compare(name) != 0) {
02507 propagation_object_names_[object] = name;
02508 }
02509 }
02510
02511 bool Solver::HasName(const PropagationBaseObject* const object) const {
02512 return ContainsKey(propagation_object_names_, object) ||
02513 (!object->BaseName().empty() && FLAGS_cp_name_variables);
02514 }
02515
02516
02517
02518 std::ostream& operator <<(std::ostream& out, const Solver* const s) {
02519 out << s->DebugString();
02520 return out;
02521 }
02522
02523 std::ostream& operator <<(std::ostream& out, const BaseObject* const o) {
02524 out << o->DebugString();
02525 return out;
02526 }
02527
02528
02529
02530 string PropagationBaseObject::name() const {
02531
02532 return solver_->GetName(this);
02533 }
02534
02535 void PropagationBaseObject::set_name(const string& name) {
02536 solver_->SetName(this, name);
02537 }
02538
02539 bool PropagationBaseObject::HasName() const {
02540 return solver_->HasName(this);
02541 }
02542
02543 string PropagationBaseObject::BaseName() const {
02544 return "";
02545 }
02546
02547
02548
02549 string DecisionBuilder::DebugString() const {
02550 return "DecisionBuilder";
02551 }
02552
02553 void DecisionBuilder::AppendMonitors(Solver* const solver,
02554 std::vector<SearchMonitor*>* const extras) {}
02555
02556 void DecisionBuilder::Accept(ModelVisitor* const visitor) const {}
02557
02558
02559
02560 void Decision::Accept(DecisionVisitor* const visitor) const {
02561 visitor->VisitUnknownDecision();
02562 }
02563
02564 void DecisionVisitor::VisitSetVariableValue(IntVar* const var, int64 value) {}
02565 void DecisionVisitor::VisitSplitVariableDomain(IntVar* const var,
02566 int64 value,
02567 bool lower) {}
02568 void DecisionVisitor::VisitUnknownDecision() {}
02569 void DecisionVisitor::VisitScheduleOrPostpone(IntervalVar* const var,
02570 int64 est) {}
02571 void DecisionVisitor::VisitRankFirstInterval(SequenceVar* const sequence,
02572 int index) {}
02573
02574 void DecisionVisitor::VisitRankLastInterval(SequenceVar* const sequence,
02575 int index) {}
02576
02577
02578
02579
02580
02581 const char ModelVisitor::kAbs[] = "Abs";
02582 const char ModelVisitor::kAllDifferent[] = "AllDifferent";
02583 const char ModelVisitor::kAllowedAssignments[] = "AllowedAssignments";
02584 const char ModelVisitor::kBetween[] = "Between";
02585 const char ModelVisitor::kConvexPiecewise[] = "ConvexPiecewise";
02586 const char ModelVisitor::kCountEqual[] = "CountEqual";
02587 const char ModelVisitor::kCumulative[] = "Cumulative";
02588 const char ModelVisitor::kDeviation[] = "Deviation";
02589 const char ModelVisitor::kDifference[] = "Difference";
02590 const char ModelVisitor::kDisjunctive[] = "Disjunctive";
02591 const char ModelVisitor::kDistribute[] = "Distribute";
02592 const char ModelVisitor::kDivide[] = "Divide";
02593 const char ModelVisitor::kDurationExpr[] = "DurationExpression";
02594 const char ModelVisitor::kElement[] = "Element";
02595 const char ModelVisitor::kElementEqual[] = "ElementEqual";
02596 const char ModelVisitor::kEndExpr[] = "EndExpression";
02597 const char ModelVisitor::kEquality[] = "Equal";
02598 const char ModelVisitor::kFalseConstraint[] = "FalseConstraint";
02599 const char ModelVisitor::kGreater[] = "Greater";
02600 const char ModelVisitor::kGreaterOrEqual[] = "GreaterOrEqual";
02601 const char ModelVisitor::kIntegerVariable[] = "IntegerVariable";
02602 const char ModelVisitor::kIntervalBinaryRelation[] = "IntervalBinaryRelation";
02603 const char ModelVisitor::kIntervalDisjunction[] = "IntervalDisjunction";
02604 const char ModelVisitor::kIntervalUnaryRelation[] = "IntervalUnaryRelation";
02605 const char ModelVisitor::kIntervalVariable[] = "IntervalVariable";
02606 const char ModelVisitor::kIsBetween[] = "IsBetween;";
02607 const char ModelVisitor::kIsDifferent[] = "IsDifferent";
02608 const char ModelVisitor::kIsEqual[] = "IsEqual";
02609 const char ModelVisitor::kIsGreaterOrEqual[] = "IsGreaterOrEqual";
02610 const char ModelVisitor::kIsLessOrEqual[] = "IsLessOrEqual";
02611 const char ModelVisitor::kIsMember[] = "IsMember;";
02612 const char ModelVisitor::kLess[] = "Less";
02613 const char ModelVisitor::kLessOrEqual[] = "LessOrEqual";
02614 const char ModelVisitor::kLinkExprVar[] = "CastExpressionIntoVariable";
02615 const char ModelVisitor::kMapDomain[] = "MapDomain";
02616 const char ModelVisitor::kMax[] = "Max";
02617 const char ModelVisitor::kMaxEqual[] = "MaxEqual";
02618 const char ModelVisitor::kMember[] = "Member";
02619 const char ModelVisitor::kMin[] = "Min";
02620 const char ModelVisitor::kMinEqual[] = "MinEqual";
02621 const char ModelVisitor::kNoCycle[] = "NoCycle";
02622 const char ModelVisitor::kNonEqual[] = "NonEqual";
02623 const char ModelVisitor::kOpposite[] = "Opposite";
02624 const char ModelVisitor::kPack[] = "Pack";
02625 const char ModelVisitor::kPathCumul[] = "PathCumul";
02626 const char ModelVisitor::kPerformedExpr[] = "PerformedExpression";
02627 const char ModelVisitor::kProduct[] = "Product";
02628 const char ModelVisitor::kScalProd[] = "ScalarProduct";
02629 const char ModelVisitor::kScalProdEqual[] = "ScalarProductEqual";
02630 const char ModelVisitor::kScalProdGreaterOrEqual[] =
02631 "ScalarProductGreaterOrEqual";
02632 const char ModelVisitor::kScalProdLessOrEqual[] = "ScalarProductLessOrEqual";
02633 const char ModelVisitor::kSemiContinuous[] = "SemiContinuous";
02634 const char ModelVisitor::kSequenceVariable[] = "SequenceVariable";
02635 const char ModelVisitor::kSquare[] = "Square";
02636 const char ModelVisitor::kStartExpr[]= "StartExpression";
02637 const char ModelVisitor::kSum[] = "Sum";
02638 const char ModelVisitor::kSumEqual[] = "SumEqual";
02639 const char ModelVisitor::kSumGreaterOrEqual[] = "SumGreaterOrEqual";
02640 const char ModelVisitor::kSumLessOrEqual[] = "SumLessOrEqual";
02641 const char ModelVisitor::kTransition[]= "Transition";
02642 const char ModelVisitor::kTrueConstraint[] = "TrueConstraint";
02643
02644 const char ModelVisitor::kCountAssignedItemsExtension[] = "CountAssignedItems";
02645 const char ModelVisitor::kCountUsedBinsExtension[] = "CountUsedBins";
02646 const char ModelVisitor::kInt64ToBoolExtension[] = "Int64ToBoolFunction";
02647 const char ModelVisitor::kInt64ToInt64Extension[] = "Int64ToInt64Function";
02648 const char ModelVisitor::kObjectiveExtension[] = "Objective";
02649 const char ModelVisitor::kSearchLimitExtension[] = "SearchLimit";
02650 const char ModelVisitor::kUsageEqualVariableExtension[] = "UsageEqualVariable";
02651 const char ModelVisitor::kUsageLessConstantExtension[] = "UsageLessConstant";
02652 const char ModelVisitor::kVariableGroupExtension[] = "VariableGroup";
02653 const char ModelVisitor::kVariableUsageLessConstantExtension[] =
02654 "VariableUsageLessConstant";
02655 const char ModelVisitor::kWeightedSumOfAssignedEqualVariableExtension[] =
02656 "WeightedSumOfAssignedEqualVariable";
02657
02658 const char ModelVisitor::kActiveArgument[] = "active";
02659 const char ModelVisitor::kAssumePathsArgument[] = "assume_paths";
02660 const char ModelVisitor::kBranchesLimitArgument[] = "branches_limit";
02661 const char ModelVisitor::kCapacityArgument[] = "capacity";
02662 const char ModelVisitor::kCardsArgument[] = "cardinalities";
02663 const char ModelVisitor::kCoefficientsArgument[] = "coefficients";
02664 const char ModelVisitor::kCountArgument[] = "count";
02665 const char ModelVisitor::kCumulativeArgument[] = "cumulative";
02666 const char ModelVisitor::kCumulsArgument[] = "cumuls";
02667 const char ModelVisitor::kDemandsArgument[] = "demands";
02668 const char ModelVisitor::kDurationMinArgument[] = "duration_min";
02669 const char ModelVisitor::kDurationMaxArgument[] = "duration_max";
02670 const char ModelVisitor::kEarlyCostArgument[] = "early_cost";
02671 const char ModelVisitor::kEarlyDateArgument[] = "early_date";
02672 const char ModelVisitor::kEndMinArgument[] = "end_min";
02673 const char ModelVisitor::kEndMaxArgument[] = "end_max";
02674 const char ModelVisitor::kExpressionArgument[] = "expression";
02675 const char ModelVisitor::kFailuresLimitArgument[] = "failures_limit";
02676 const char ModelVisitor::kFinalStatesArgument[] = "final_states";
02677 const char ModelVisitor::kFixedChargeArgument[] = "fixed_charge";
02678 const char ModelVisitor::kIndex2Argument[] = "index2";
02679 const char ModelVisitor::kIndexArgument[] = "index";
02680 const char ModelVisitor::kInitialState[] = "initial_state";
02681 const char ModelVisitor::kIntervalArgument[] = "interval";
02682 const char ModelVisitor::kIntervalsArgument[] = "intervals";
02683 const char ModelVisitor::kLateCostArgument[] = "late_cost";
02684 const char ModelVisitor::kLateDateArgument[] = "late_date";
02685 const char ModelVisitor::kLeftArgument[] = "left";
02686 const char ModelVisitor::kMaxArgument[] = "max_value";
02687 const char ModelVisitor::kMaximizeArgument[] = "maximize";
02688 const char ModelVisitor::kMinArgument[] = "min_value";
02689 const char ModelVisitor::kNextsArgument[] = "nexts";
02690 const char ModelVisitor::kOptionalArgument[] = "optional";
02691 const char ModelVisitor::kRangeArgument[] = "range";
02692 const char ModelVisitor::kRelationArgument[] = "relation";
02693 const char ModelVisitor::kRightArgument[] = "right";
02694 const char ModelVisitor::kSequenceArgument[] = "sequence";
02695 const char ModelVisitor::kSequencesArgument[] = "sequences";
02696 const char ModelVisitor::kSmartTimeCheckArgument[] = "smart_time_check";
02697 const char ModelVisitor::kSizeArgument[] = "size";
02698 const char ModelVisitor::kSolutionLimitArgument[] = "solutions_limit";
02699 const char ModelVisitor::kStartMinArgument[] = "start_min";
02700 const char ModelVisitor::kStartMaxArgument[] = "start_max";
02701 const char ModelVisitor::kStepArgument[] = "step";
02702 const char ModelVisitor::kTargetArgument[] = "target_variable";
02703 const char ModelVisitor::kTimeLimitArgument[] = "time_limit";
02704 const char ModelVisitor::kTransitsArgument[] = "transits";
02705 const char ModelVisitor::kTuplesArgument[] = "tuples";
02706 const char ModelVisitor::kValueArgument[] = "value";
02707 const char ModelVisitor::kValuesArgument[] = "values";
02708 const char ModelVisitor::kVarsArgument[] = "variables";
02709 const char ModelVisitor::kVariableArgument[] = "variable";
02710
02711 const char ModelVisitor::kMirrorOperation[] = "mirror";
02712 const char ModelVisitor::kRelaxedMaxOperation[] = "relaxed_max";
02713 const char ModelVisitor::kRelaxedMinOperation[] = "relaxed_min";
02714 const char ModelVisitor::kSumOperation[] = "sum";
02715 const char ModelVisitor::kDifferenceOperation[] = "difference";
02716 const char ModelVisitor::kProductOperation[] = "product";
02717
02718
02719
02720
02721 ModelVisitor::~ModelVisitor() {}
02722
02723 void ModelVisitor::BeginVisitModel(const string& type_name) {}
02724 void ModelVisitor::EndVisitModel(const string& type_name) {}
02725
02726 void ModelVisitor::BeginVisitConstraint(const string& type_name,
02727 const Constraint* const constraint) {}
02728 void ModelVisitor::EndVisitConstraint(const string& type_name,
02729 const Constraint* const constraint) {}
02730
02731 void ModelVisitor::BeginVisitExtension(const string& type) {
02732 }
02733 void ModelVisitor::EndVisitExtension(const string& type) {}
02734
02735 void ModelVisitor::BeginVisitIntegerExpression(const string& type_name,
02736 const IntExpr* const expr) {}
02737 void ModelVisitor::EndVisitIntegerExpression(const string& type_name,
02738 const IntExpr* const expr) {}
02739
02740 void ModelVisitor::VisitIntegerVariable(const IntVar* const variable,
02741 const IntExpr* const delegate) {
02742 if (delegate != NULL) {
02743 delegate->Accept(this);
02744 }
02745 }
02746
02747 void ModelVisitor::VisitIntegerVariable(const IntVar* const variable,
02748 const string& operation,
02749 int64 value,
02750 const IntVar* const delegate) {
02751 if (delegate != NULL) {
02752 delegate->Accept(this);
02753 }
02754 }
02755
02756 void ModelVisitor::VisitIntervalVariable(const IntervalVar* const variable,
02757 const string& operation,
02758 const IntervalVar* const delegate) {
02759 if (delegate != NULL) {
02760 delegate->Accept(this);
02761 }
02762 }
02763
02764 void ModelVisitor::VisitIntervalVariable(const IntervalVar* const variable,
02765 const string& operation,
02766 const IntervalVar* const * delegates,
02767 int size) {
02768 for (int i = 0; i < size; ++i) {
02769 delegates[i]->Accept(this);
02770 }
02771 }
02772
02773 void ModelVisitor::VisitSequenceVariable(const SequenceVar* const variable) {
02774 for (int i = 0; i < variable->size(); ++i) {
02775 variable->Interval(i)->Accept(this);
02776 }
02777 }
02778
02779 void ModelVisitor::VisitIntegerArgument(const string& arg_name, int64 value) {}
02780
02781 void ModelVisitor::VisitIntegerArrayArgument(const string& arg_name,
02782 const int64* const values,
02783 int size) {}
02784
02785 void ModelVisitor::VisitIntegerMatrixArgument(const string& arg_name,
02786 const IntTupleSet& tuples) {}
02787
02788 void ModelVisitor::VisitIntegerExpressionArgument(
02789 const string& arg_name,
02790 const IntExpr* const argument) {
02791 argument->Accept(this);
02792 }
02793
02794 void ModelVisitor::VisitIntegerVariableArrayArgument(
02795 const string& arg_name,
02796 const IntVar* const * arguments,
02797 int size) {
02798 for (int i = 0; i < size; ++i) {
02799 arguments[i]->Accept(this);
02800 }
02801 }
02802
02803 void ModelVisitor::VisitIntegerVariableArrayArgument(
02804 const string& arg_name,
02805 const ConstPtrArray<IntVar>& arguments) {
02806 VisitIntegerVariableArrayArgument(arg_name,
02807 arguments.RawData(),
02808 arguments.size());
02809 }
02810
02811 void ModelVisitor::VisitIntervalArgument(
02812 const string& arg_name,
02813 const IntervalVar* const argument) {
02814 argument->Accept(this);
02815 }
02816
02817 void ModelVisitor::VisitIntervalArrayArgument(
02818 const string& arg_name,
02819 const IntervalVar* const * arguments,
02820 int size) {
02821 for (int i = 0; i < size; ++i) {
02822 arguments[i]->Accept(this);
02823 }
02824 }
02825
02826 void ModelVisitor::VisitSequenceArgument(
02827 const string& arg_name,
02828 const SequenceVar* const argument) {
02829 argument->Accept(this);
02830 }
02831
02832 void ModelVisitor::VisitSequenceArrayArgument(
02833 const string& arg_name,
02834 const SequenceVar* const * arguments,
02835 int size) {
02836 for (int i = 0; i < size; ++i) {
02837 arguments[i]->Accept(this);
02838 }
02839 }
02840
02841
02842
02843 void ModelVisitor::VisitConstIntArrayArgument(const string& arg_name,
02844 const ConstIntArray& values) {
02845 VisitIntegerArrayArgument(arg_name, values.RawData(), values.size());
02846 }
02847
02848 void ModelVisitor::VisitInt64ToBoolExtension(
02849 ResultCallback1<bool, int64>* const callback,
02850 int64 index_min,
02851 int64 index_max) {
02852 if (callback == NULL) {
02853 return;
02854 }
02855 std::vector<int64> cached_results;
02856 for (int i = index_min; i <= index_max; ++i) {
02857 cached_results.push_back(callback->Run(i));
02858 }
02859
02860 BeginVisitExtension(kInt64ToBoolExtension);
02861 VisitIntegerArgument(kMinArgument, index_min);
02862 VisitIntegerArgument(kMaxArgument, index_max);
02863 VisitIntegerArrayArgument(kValuesArgument,
02864 cached_results.data(),
02865 cached_results.size());
02866 EndVisitExtension(kInt64ToBoolExtension);
02867 }
02868
02869 void ModelVisitor::VisitInt64ToInt64Extension(
02870 Solver::IndexEvaluator1* const callback,
02871 int64 index_min,
02872 int64 index_max) {
02873 if (callback == NULL) {
02874 return;
02875 }
02876 std::vector<int64> cached_results;
02877 for (int i = index_min; i <= index_max; ++i) {
02878 cached_results.push_back(callback->Run(i));
02879 }
02880 BeginVisitExtension(kInt64ToInt64Extension);
02881 VisitIntegerArgument(kMinArgument, index_min);
02882 VisitIntegerArgument(kMaxArgument, index_max);
02883 VisitIntegerArrayArgument(kValuesArgument,
02884 cached_results.data(),
02885 cached_results.size());
02886 EndVisitExtension(kInt64ToInt64Extension);
02887 }
02888
02889 void ModelVisitor::VisitInt64ToInt64AsArray(
02890 Solver::IndexEvaluator1* const callback,
02891 const string& arg_name,
02892 int64 index_max) {
02893 if (callback == NULL) {
02894 return;
02895 }
02896 std::vector<int64> cached_results;
02897 for (int i = 0; i <= index_max; ++i) {
02898 cached_results.push_back(callback->Run(i));
02899 }
02900 VisitIntegerArrayArgument(arg_name,
02901 cached_results.data(),
02902 cached_results.size());
02903 }
02904
02905
02906
02907 void SearchMonitor::EnterSearch() {}
02908 void SearchMonitor::RestartSearch() {}
02909 void SearchMonitor::ExitSearch() {}
02910 void SearchMonitor::BeginNextDecision(DecisionBuilder* const b) {}
02911 void SearchMonitor::EndNextDecision(DecisionBuilder* const b,
02912 Decision* const d) {}
02913 void SearchMonitor::ApplyDecision(Decision* const d) {}
02914 void SearchMonitor::RefuteDecision(Decision* const d) {}
02915 void SearchMonitor::AfterDecision(Decision* const d, bool apply) {}
02916 void SearchMonitor::BeginFail() {}
02917 void SearchMonitor::EndFail() {}
02918 void SearchMonitor::BeginInitialPropagation() {}
02919 void SearchMonitor::EndInitialPropagation() {}
02920 bool SearchMonitor::AcceptSolution() { return true; }
02921 bool SearchMonitor::AtSolution() { return false; }
02922 void SearchMonitor::NoMoreSolutions() {}
02923 bool SearchMonitor::LocalOptimum() { return false; }
02924 bool SearchMonitor::AcceptDelta(Assignment* delta,
02925 Assignment* deltadelta) { return true; }
02926 void SearchMonitor::AcceptNeighbor() {}
02927 void SearchMonitor::FinishCurrentSearch() {
02928 solver()->searches_.back()->set_should_finish(true);
02929 }
02930 void SearchMonitor::RestartCurrentSearch() {
02931 solver()->searches_.back()->set_should_restart(true);
02932 }
02933 void SearchMonitor::PeriodicCheck() {}
02934 void SearchMonitor::Accept(ModelVisitor* const visitor) const {}
02935
02936 void SearchMonitor::Install() {
02937 solver()->searches_.back()->push_monitor(this);
02938 }
02939
02940
02941 PropagationMonitor::PropagationMonitor(Solver* const s) : SearchMonitor(s) {}
02942
02943 PropagationMonitor::~PropagationMonitor() {}
02944
02945
02946
02947 void PropagationMonitor::Install() {
02948 SearchMonitor::Install();
02949 solver()->AddPropagationMonitor(this);
02950 }
02951
02952
02953
02954 class Trace : public PropagationMonitor {
02955 public:
02956 explicit Trace(Solver* const s) : PropagationMonitor(s) {}
02957
02958 virtual ~Trace() {}
02959
02960 virtual void BeginConstraintInitialPropagation(
02961 const Constraint* const constraint) {
02962 for (int i = 0; i < monitors_.size(); ++i) {
02963 monitors_[i]->BeginConstraintInitialPropagation(constraint);
02964 }
02965 }
02966
02967 virtual void EndConstraintInitialPropagation(
02968 const Constraint* const constraint) {
02969 for (int i = 0; i < monitors_.size(); ++i) {
02970 monitors_[i]->EndConstraintInitialPropagation(constraint);
02971 }
02972 }
02973
02974 virtual void BeginNestedConstraintInitialPropagation(
02975 const Constraint* const parent,
02976 const Constraint* const nested) {
02977 for (int i = 0; i < monitors_.size(); ++i) {
02978 monitors_[i]->BeginNestedConstraintInitialPropagation(parent, nested);
02979 }
02980 }
02981
02982 virtual void EndNestedConstraintInitialPropagation(
02983 const Constraint* const parent,
02984 const Constraint* const nested) {
02985 for (int i = 0; i < monitors_.size(); ++i) {
02986 monitors_[i]->EndNestedConstraintInitialPropagation(parent, nested);
02987 }
02988 }
02989
02990 virtual void RegisterDemon(const Demon* const demon) {
02991 for (int i = 0; i < monitors_.size(); ++i) {
02992 monitors_[i]->RegisterDemon(demon);
02993 }
02994 }
02995
02996 virtual void BeginDemonRun(const Demon* const demon) {
02997 for (int i = 0; i < monitors_.size(); ++i) {
02998 monitors_[i]->BeginDemonRun(demon);
02999 }
03000 }
03001
03002 virtual void EndDemonRun(const Demon* const demon) {
03003 for (int i = 0; i < monitors_.size(); ++i) {
03004 monitors_[i]->EndDemonRun(demon);
03005 }
03006 }
03007
03008 virtual void PushContext(const string& context) {
03009 for (int i = 0; i < monitors_.size(); ++i) {
03010 monitors_[i]->PushContext(context);
03011 }
03012 }
03013
03014 virtual void PopContext() {
03015 for (int i = 0; i < monitors_.size(); ++i) {
03016 monitors_[i]->PopContext();
03017 }
03018 }
03019
03020
03021 virtual void SetMin(IntExpr* const expr, int64 new_min) {
03022 for (int i = 0; i < monitors_.size(); ++i) {
03023 monitors_[i]->SetMin(expr, new_min);
03024 }
03025 }
03026
03027 virtual void SetMax(IntExpr* const expr, int64 new_max) {
03028 for (int i = 0; i < monitors_.size(); ++i) {
03029 monitors_[i]->SetMax(expr, new_max);
03030 }
03031 }
03032
03033 virtual void SetRange(IntExpr* const expr, int64 new_min, int64 new_max) {
03034 for (int i = 0; i < monitors_.size(); ++i) {
03035 monitors_[i]->SetRange(expr, new_min, new_max);
03036 }
03037 }
03038
03039
03040 virtual void SetMin(IntVar* const var, int64 new_min) {
03041 for (int i = 0; i < monitors_.size(); ++i) {
03042 monitors_[i]->SetMin(var, new_min);
03043 }
03044 }
03045
03046 virtual void SetMax(IntVar* const var, int64 new_max) {
03047 for (int i = 0; i < monitors_.size(); ++i) {
03048 monitors_[i]->SetMax(var, new_max);
03049 }
03050 }
03051
03052 virtual void SetRange(IntVar* const var, int64 new_min, int64 new_max) {
03053 for (int i = 0; i < monitors_.size(); ++i) {
03054 monitors_[i]->SetRange(var, new_min, new_max);
03055 }
03056 }
03057
03058 virtual void RemoveValue(IntVar* const var, int64 value) {
03059 for (int i = 0; i < monitors_.size(); ++i) {
03060 monitors_[i]->RemoveValue(var, value);
03061 }
03062 }
03063
03064 virtual void SetValue(IntVar* const var, int64 value) {
03065 for (int i = 0; i < monitors_.size(); ++i) {
03066 monitors_[i]->SetValue(var, value);
03067 }
03068 }
03069
03070 virtual void RemoveInterval(IntVar* const var, int64 imin, int64 imax) {
03071 for (int i = 0; i < monitors_.size(); ++i) {
03072 monitors_[i]->RemoveInterval(var, imin, imax);
03073 }
03074 }
03075
03076 virtual void SetValues(IntVar* const var,
03077 const int64* const values,
03078 int size) {
03079 for (int i = 0; i < monitors_.size(); ++i) {
03080 monitors_[i]->SetValues(var, values, size);
03081 }
03082 }
03083
03084 virtual void RemoveValues(IntVar* const var,
03085 const int64* const values,
03086 int size) {
03087 for (int i = 0; i < monitors_.size(); ++i) {
03088 monitors_[i]->RemoveValues(var, values, size);
03089 }
03090 }
03091
03092
03093 virtual void SetStartMin(IntervalVar* const var, int64 new_min) {
03094 for (int i = 0; i < monitors_.size(); ++i) {
03095 monitors_[i]->SetStartMin(var, new_min);
03096 }
03097 }
03098
03099 virtual void SetStartMax(IntervalVar* const var, int64 new_max) {
03100 for (int i = 0; i < monitors_.size(); ++i) {
03101 monitors_[i]->SetStartMax(var, new_max);
03102 }
03103 }
03104
03105 virtual void SetStartRange(IntervalVar* const var,
03106 int64 new_min,
03107 int64 new_max) {
03108 for (int i = 0; i < monitors_.size(); ++i) {
03109 monitors_[i]->SetStartRange(var, new_min, new_max);
03110 }
03111 }
03112
03113 virtual void SetEndMin(IntervalVar* const var, int64 new_min) {
03114 for (int i = 0; i < monitors_.size(); ++i) {
03115 monitors_[i]->SetEndMin(var, new_min);
03116 }
03117 }
03118
03119 virtual void SetEndMax(IntervalVar* const var, int64 new_max) {
03120 for (int i = 0; i < monitors_.size(); ++i) {
03121 monitors_[i]->SetEndMax(var, new_max);
03122 }
03123 }
03124
03125 virtual void SetEndRange(IntervalVar* const var,
03126 int64 new_min,
03127 int64 new_max) {
03128 for (int i = 0; i < monitors_.size(); ++i) {
03129 monitors_[i]->SetEndRange(var, new_min, new_max);
03130 }
03131 }
03132
03133 virtual void SetDurationMin(IntervalVar* const var, int64 new_min) {
03134 for (int i = 0; i < monitors_.size(); ++i) {
03135 monitors_[i]->SetDurationMin(var, new_min);
03136 }
03137 }
03138
03139 virtual void SetDurationMax(IntervalVar* const var, int64 new_max) {
03140 for (int i = 0; i < monitors_.size(); ++i) {
03141 monitors_[i]->SetDurationMax(var, new_max);
03142 }
03143 }
03144
03145 virtual void SetDurationRange(IntervalVar* const var,
03146 int64 new_min,
03147 int64 new_max) {
03148 for (int i = 0; i < monitors_.size(); ++i) {
03149 monitors_[i]->SetDurationRange(var, new_min, new_max);
03150 }
03151 }
03152
03153 virtual void SetPerformed(IntervalVar* const var, bool value) {
03154 for (int i = 0; i < monitors_.size(); ++i) {
03155 monitors_[i]->SetPerformed(var, value);
03156 }
03157 }
03158
03159 virtual void RankFirst(SequenceVar* const var, int index) {
03160 for (int i = 0; i < monitors_.size(); ++i) {
03161 monitors_[i]->RankFirst(var, index);
03162 }
03163 }
03164
03165 virtual void RankNotFirst(SequenceVar* const var, int index) {
03166 for (int i = 0; i < monitors_.size(); ++i) {
03167 monitors_[i]->RankNotFirst(var, index);
03168 }
03169 }
03170
03171 virtual void RankLast(SequenceVar* const var, int index) {
03172 for (int i = 0; i < monitors_.size(); ++i) {
03173 monitors_[i]->RankLast(var, index);
03174 }
03175 }
03176
03177 virtual void RankNotLast(SequenceVar* const var, int index) {
03178 for (int i = 0; i < monitors_.size(); ++i) {
03179 monitors_[i]->RankNotLast(var, index);
03180 }
03181 }
03182
03183 virtual void RankSequence(SequenceVar* const var,
03184 const std::vector<int>& rank_first,
03185 const std::vector<int>& rank_last,
03186 const std::vector<int>& unperformed) {
03187 for (int i = 0; i < monitors_.size(); ++i) {
03188 monitors_[i]->RankSequence(var, rank_first, rank_last, unperformed);
03189 }
03190 }
03191
03192
03193 void Add(PropagationMonitor* const monitor) {
03194 if (monitor != NULL) {
03195 monitors_.push_back(monitor);
03196 }
03197 }
03198
03199
03200
03201 virtual void Install() {
03202 SearchMonitor::Install();
03203 }
03204
03205 private:
03206 std::vector<PropagationMonitor*> monitors_;
03207 };
03208
03209 PropagationMonitor* BuildTrace(Solver* const s) {
03210 return new Trace(s);
03211 }
03212
03213 void Solver::AddPropagationMonitor(PropagationMonitor* const monitor) {
03214
03215 reinterpret_cast<class Trace*>(propagation_monitor_.get())->Add(monitor);
03216 }
03217
03218 PropagationMonitor* Solver::GetPropagationMonitor() const {
03219 return propagation_monitor_.get();
03220 }
03221
03222
03223
03224 string Constraint::DebugString() const {
03225 return "Constraint";
03226 }
03227
03228 void Constraint::PostAndPropagate() {
03229 FreezeQueue();
03230 Post();
03231 InitialPropagate();
03232 UnfreezeQueue();
03233 }
03234
03235 void Constraint::Accept(ModelVisitor* const visitor) const {
03236 visitor->BeginVisitConstraint("unknown", this);
03237 visitor->EndVisitConstraint("unknown", this);
03238 }
03239
03240 bool Constraint::IsCastConstraint() const {
03241 return ContainsKey(solver()->cast_constraints_, this);
03242 }
03243
03244 IntVar* Constraint::Var() {
03245 return NULL;
03246 }
03247
03248
03249
03250 void IntExpr::Accept(ModelVisitor* const visitor) const {
03251 visitor->BeginVisitIntegerExpression("unknown", this);
03252 visitor->EndVisitIntegerExpression("unknown", this);
03253 }
03254
03255 #undef CP_TRY // We no longer need those.
03256 #undef CP_ON_FAIL
03257 #undef CP_DO_FAIL
03258
03259 }