00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #ifndef OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_
00052 #define OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_
00053
00054 #include <math.h>
00055 #include <stddef.h>
00056 #include "base/hash.h"
00057 #include <string>
00058 #include <vector>
00059
00060 #include "base/callback-types.h"
00061 #include "base/commandlineflags.h"
00062 #include "base/integral_types.h"
00063 #include "base/logging.h"
00064 #include "base/scoped_ptr.h"
00065 #include "base/sysinfo.h"
00066 #include "base/timer.h"
00067 #include "base/join.h"
00068 #include "base/bitmap.h"
00069 #include "base/map-util.h"
00070 #include "base/hash.h"
00071 #include "constraint_solver/constraint_solver.h"
00072 #include "util/const_int_array.h"
00073 #include "util/const_ptr_array.h"
00074 #include "util/tuple_set.h"
00075 #include "util/vector_map.h"
00076
00077 template <typename T> class ResultCallback;
00078
00079 class WallTimer;
00080
00081 namespace operations_research {
00082 class CPArgumentProto;
00083 class CPConstraintProto;
00084 class CPIntegerExpressionProto;
00085 class CPIntervalVariableProto;
00086 class ConstIntArray;
00087 template <class T> class ConstPtrArray;
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 class BaseIntExpr : public IntExpr {
00112 public:
00113 explicit BaseIntExpr(Solver* const s) : IntExpr(s), var_(NULL) {}
00114 virtual ~BaseIntExpr() {}
00115
00116 virtual IntVar* Var();
00117 virtual IntVar* CastToVar();
00118
00119 private:
00120 IntVar* var_;
00121 };
00122
00123
00124
00125 enum VarTypes {
00126 UNSPECIFIED,
00127 DOMAIN_INT_VAR,
00128 BOOLEAN_VAR,
00129 CONST_VAR,
00130 VAR_ADD_CST,
00131 DOMAIN_INT_VAR_ADD_CST,
00132 VAR_TIMES_POS_CST,
00133 BOOLEAN_VAR_TIMES_POS_CST,
00134 CST_SUB_VAR,
00135 OPP_VAR,
00136 TRACE_VAR
00137 };
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 #ifndef SWIG
00150 template <class T> class SimpleRevFIFO {
00151 private:
00152 enum { CHUNK_SIZE = 16 };
00153 struct Chunk {
00154 T data_[CHUNK_SIZE];
00155 const Chunk* const next_;
00156 explicit Chunk(const Chunk* next) : next_(next) {}
00157 };
00158
00159 public:
00160
00161 class Iterator {
00162 public:
00163 explicit Iterator(const SimpleRevFIFO<T>* l)
00164 : chunk_(l->chunks_), value_(l->Last()) {}
00165 bool ok() const { return (value_ != NULL); }
00166 T operator*() const { return *value_; }
00167 void operator++() {
00168 ++value_;
00169 if (value_ == chunk_->data_ + CHUNK_SIZE) {
00170 chunk_ = chunk_->next_;
00171 value_ = chunk_ ? chunk_->data_ : NULL;
00172 }
00173 }
00174
00175 private:
00176 const Chunk* chunk_;
00177 const T* value_;
00178 };
00179
00180 SimpleRevFIFO() : chunks_(NULL), pos_(0) {}
00181
00182 void Push(Solver* const s, T val) {
00183 if (pos_.Value() == 0) {
00184 Chunk* const chunk = s->UnsafeRevAlloc(new Chunk(chunks_));
00185 s->SaveAndSetValue(reinterpret_cast<void**>(&chunks_),
00186 reinterpret_cast<void*>(chunk));
00187 pos_.SetValue(s, CHUNK_SIZE - 1);
00188 } else {
00189 pos_.Decr(s);
00190 }
00191 chunks_->data_[pos_.Value()] = val;
00192 }
00193
00194
00195 void PushIfNotTop(Solver* const s, T val) {
00196 if (chunks_ == NULL || LastValue() != val) {
00197 Push(s, val);
00198 }
00199 }
00200
00201
00202 const T* Last() const {
00203 return chunks_ ? &chunks_->data_[pos_.Value()] : NULL;
00204 }
00205
00206
00207 const T& LastValue() const {
00208 DCHECK(chunks_);
00209 return chunks_->data_[pos_.Value()];
00210 }
00211
00212
00213 void SetLastValue(const T& v) {
00214 DCHECK(Last());
00215 chunks_->data_[pos_.Value()] = v;
00216 }
00217
00218 private:
00219 Chunk *chunks_;
00220 NumericalRev<int> pos_;
00221 };
00222
00223
00224
00225
00226
00227 inline uint64 Hash1(uint64 value) {
00228 value = (~value) + (value << 21);
00229 value ^= value >> 24;
00230 value += (value << 3) + (value << 8);
00231 value ^= value >> 14;
00232 value += (value << 2) + (value << 4);
00233 value ^= value >> 28;
00234 value += (value << 31);
00235 return value;
00236 }
00237
00238 inline uint64 Hash1(uint32 value) {
00239 uint64 a = value;
00240 a = (a + 0x7ed55d16) + (a << 12);
00241 a = (a ^ 0xc761c23c) ^ (a >> 19);
00242 a = (a + 0x165667b1) + (a << 5);
00243 a = (a + 0xd3a2646c) ^ (a << 9);
00244 a = (a + 0xfd7046c5) + (a << 3);
00245 a = (a ^ 0xb55a4f09) ^ (a >> 16);
00246 return a;
00247 }
00248
00249 inline uint64 Hash1(int64 value) {
00250 return Hash1(static_cast<uint64>(value));
00251 }
00252
00253 inline uint64 Hash1(int value) {
00254 return Hash1(static_cast<uint32>(value));
00255 }
00256
00257 inline uint64 Hash1(void* const ptr) {
00258 #if defined(ARCH_K8)
00259 return Hash1(reinterpret_cast<uint64>(ptr));
00260 #else
00261 return Hash1(reinterpret_cast<uint32>(ptr));
00262 #endif
00263 }
00264
00265 inline uint64 Hash1(ConstIntArray* const values) {
00266 if (values->size() == 0) {
00267 return 0;
00268 } else if (values->size() == 1) {
00269 return Hash1(values->get(0));
00270 } else {
00271 uint64 hash = Hash1(values->get(0));
00272 for (int i = 1; i < values->size(); ++i) {
00273 hash = hash * i + Hash1(values->get(i));
00274 }
00275 return hash;
00276 }
00277 }
00278
00279 template <class T> uint64 Hash1(ConstPtrArray<T>* const ptrs) {
00280 if (ptrs->size() == 0) {
00281 return 0;
00282 } else if (ptrs->size() == 1) {
00283 return Hash1(ptrs->get(0));
00284 } else {
00285 uint64 hash = Hash1(ptrs->get(0));
00286 for (int i = 1; i < ptrs->size(); ++i) {
00287 hash = hash * i + Hash1(ptrs->get(i));
00288 }
00289 return hash;
00290 }
00291 }
00292
00293
00294
00295
00296
00297 template <class K, class V> class RevImmutableMultiMap {
00298 public:
00299 RevImmutableMultiMap(Solver* const solver, int initial_size)
00300 : solver_(solver),
00301 array_(solver->UnsafeRevAllocArray(new Cell*[initial_size])),
00302 size_(initial_size),
00303 num_items_(0) {
00304 memset(array_, 0, sizeof(*array_) * size_.Value());
00305 }
00306
00307 ~RevImmutableMultiMap() {}
00308
00309 int num_items() const { return num_items_.Value(); }
00310
00311
00312 bool ContainsKey(const K& key) const {
00313 uint64 code = Hash1(key) % size_.Value();
00314 Cell* tmp = array_[code];
00315 while (tmp) {
00316 if (tmp->key() == key) {
00317 return true;
00318 }
00319 tmp = tmp->next();
00320 }
00321 return false;
00322 }
00323
00324
00325
00326
00327 const V& FindWithDefault(const K& key, const V& default_value) const {
00328 uint64 code = Hash1(key) % size_.Value();
00329 Cell* tmp = array_[code];
00330 while (tmp) {
00331 if (tmp->key() == key) {
00332 return tmp->value();
00333 }
00334 tmp = tmp->next();
00335 }
00336 return default_value;
00337 }
00338
00339
00340 void Insert(const K& key, const V& value) {
00341 const int position = Hash1(key) % size_.Value();
00342 Cell* const cell =
00343 solver_->UnsafeRevAlloc(new Cell(key, value, array_[position]));
00344 solver_->SaveAndSetValue(reinterpret_cast<void**>(&array_[position]),
00345 reinterpret_cast<void*>(cell));
00346 num_items_.Incr(solver_);
00347 if (num_items_.Value() > 2 * size_.Value()) {
00348 Double();
00349 }
00350 }
00351
00352 private:
00353 class Cell {
00354 public:
00355 Cell(const K& key, const V& value, Cell* const next)
00356 : key_(key), value_(value), next_(next) {}
00357
00358 void SetRevNext(Solver* const solver, Cell* const next) {
00359 solver->SaveAndSetValue(reinterpret_cast<void**>(&next_),
00360 reinterpret_cast<void*>(next));
00361 }
00362
00363 Cell* next() const { return next_; }
00364
00365 const K& key() const { return key_; }
00366
00367 const V& value() const { return value_; }
00368
00369 private:
00370 const K key_;
00371 const V value_;
00372 Cell* next_;
00373 };
00374
00375 void Double() {
00376 Cell** const old_cell_array = array_;
00377 const int old_size = size_.Value();
00378 size_.SetValue(solver_, size_.Value() * 2);
00379 solver_->SaveAndSetValue(
00380 reinterpret_cast<void**>(&array_),
00381 reinterpret_cast<void*>(
00382 solver_->UnsafeRevAllocArray(new Cell*[size_.Value()])));
00383 memset(array_, 0, size_.Value() * sizeof(*array_));
00384 for (int i = 0; i < old_size; ++i) {
00385 Cell* tmp = old_cell_array[i];
00386 while (tmp != NULL) {
00387 Cell* const to_reinsert = tmp;
00388 tmp = tmp->next();
00389 const uint64 new_position = Hash1(to_reinsert->key()) % size_.Value();
00390 to_reinsert->SetRevNext(solver_, array_[new_position]);
00391 solver_->SaveAndSetValue(
00392 reinterpret_cast<void**>(&array_[new_position]),
00393 reinterpret_cast<void*>(to_reinsert));
00394 }
00395 }
00396 }
00397
00398 Solver* const solver_;
00399 Cell** array_;
00400 NumericalRev<int> size_;
00401 NumericalRev<int> num_items_;
00402 };
00403
00404
00405 class RevSwitch {
00406 public:
00407 RevSwitch() : value_(false) {}
00408
00409 bool Switched() const { return value_; }
00410
00411 void Switch(Solver* const solver) {
00412 solver->SaveAndSetValue(&value_, true);
00413 }
00414
00415 private:
00416 bool value_;
00417 };
00418
00419
00420
00421 class SmallRevBitSet {
00422 public:
00423 explicit SmallRevBitSet(int64 size);
00424
00425 void SetToOne(Solver* const solver, int64 pos);
00426
00427 void SetToZero(Solver* const solver, int64 pos);
00428
00429 int64 Cardinality() const;
00430
00431 bool IsCardinalityZero() const { return bits_.Value() == GG_ULONGLONG(0); }
00432
00433 bool IsCardinalityOne() const {
00434 return (bits_.Value() != 0) && !(bits_.Value() & (bits_.Value() - 1));
00435 }
00436
00437
00438 int64 GetFirstOne() const;
00439
00440 private:
00441 Rev<uint64> bits_;
00442 };
00443
00444
00445
00446 class RevBitSet {
00447 public:
00448 explicit RevBitSet(int64 size);
00449 ~RevBitSet();
00450
00451
00452 void SetToOne(Solver* const solver, int64 pos);
00453
00454 void SetToZero(Solver* const solver, int64 pos);
00455
00456 bool IsSet(int64 pos) const;
00457
00458 int64 Cardinality() const;
00459
00460 bool IsCardinalityZero() const;
00461
00462 bool IsCardinalityOne() const;
00463
00464
00465 int64 GetFirstBit(int start) const;
00466
00467 void ClearAll(Solver* const solver);
00468
00469 friend class RevBitMatrix;
00470
00471 private:
00472
00473 void Save(Solver* const solver, int offset);
00474 const int64 size_;
00475 const int64 length_;
00476 uint64* bits_;
00477 uint64* stamps_;
00478 };
00479
00480
00481 class RevBitMatrix : private RevBitSet {
00482 public:
00483 RevBitMatrix(int64 rows, int64 columns);
00484 ~RevBitMatrix();
00485
00486
00487 void SetToOne(Solver* const solver, int64 row, int64 column);
00488
00489 void SetToZero(Solver* const solver, int64 row, int64 column);
00490
00491 bool IsSet(int64 row, int64 column) const {
00492 DCHECK_GE(row, 0);
00493 DCHECK_LT(row, rows_);
00494 DCHECK_GE(column, 0);
00495 DCHECK_LT(column, columns_);
00496 return RevBitSet::IsSet(row * columns_ + column);
00497 }
00498
00499 int64 Cardinality(int row) const;
00500
00501 bool IsCardinalityZero(int row) const;
00502
00503 bool IsCardinalityOne(int row) const;
00504
00505
00506 int64 GetFirstBit(int row, int start) const;
00507
00508 void ClearAll(Solver* const solver);
00509
00510 private:
00511 const int64 rows_;
00512 const int64 columns_;
00513 };
00514
00515
00516
00517
00518
00519
00520
00521
00522 template <class T> class CallMethod0 : public Demon {
00523 public:
00524 CallMethod0(T* const ct, void (T::*method)(), const string& name)
00525 : constraint_(ct), method_(method), name_(name) {}
00526
00527 virtual ~CallMethod0() {}
00528
00529 virtual void Run(Solver* const s) {
00530 (constraint_->*method_)();
00531 }
00532
00533 virtual string DebugString() const {
00534 return "CallMethod_" + name_ + "(" + constraint_->DebugString() + ")";
00535 }
00536
00537 private:
00538 T* const constraint_;
00539 void (T::* const method_)();
00540 const string name_;
00541 };
00542
00543 template <class T> Demon* MakeConstraintDemon0(Solver* const s,
00544 T* const ct,
00545 void (T::*method)(),
00546 const string& name) {
00547 return s->RevAlloc(new CallMethod0<T>(ct, method, name));
00548 }
00549
00550
00551 template <class T, class P> class CallMethod1 : public Demon {
00552 public:
00553 CallMethod1(T* const ct,
00554 void (T::*method)(P),
00555 const string& name,
00556 P param1)
00557 : constraint_(ct),
00558 method_(method),
00559 name_(name),
00560 param1_(param1) {}
00561
00562 virtual ~CallMethod1() {}
00563
00564 virtual void Run(Solver* const s) {
00565 (constraint_->*method_)(param1_);
00566 }
00567
00568 virtual string DebugString() const {
00569 return StrCat(StrCat("CallMethod_", name_),
00570 StrCat("(", constraint_->DebugString(), ", "),
00571 StrCat(param1_, ")"));
00572 }
00573
00574 private:
00575 T* const constraint_;
00576 void (T::* const method_)(P);
00577 const string name_;
00578 P param1_;
00579 };
00580
00581 template <class T, class P>
00582 Demon* MakeConstraintDemon1(Solver* const s,
00583 T* const ct,
00584 void (T::*method)(P),
00585 const string& name,
00586 P param1) {
00587 return s->RevAlloc(new CallMethod1<T, P>(ct, method, name, param1));
00588 }
00589
00590
00591 template <class T, class P, class Q> class CallMethod2 : public Demon {
00592 public:
00593 CallMethod2(T* const ct,
00594 void (T::*method)(P, Q),
00595 const string& name,
00596 P param1,
00597 Q param2)
00598 : constraint_(ct),
00599 method_(method),
00600 name_(name),
00601 param1_(param1),
00602 param2_(param2) {}
00603
00604 virtual ~CallMethod2() {}
00605
00606 virtual void Run(Solver* const s) {
00607 (constraint_->*method_)(param1_, param2_);
00608 }
00609
00610 virtual string DebugString() const {
00611 return StrCat(StrCat("CallMethod_", name_),
00612 StrCat("(", constraint_->DebugString()),
00613 StrCat(", ", param1_),
00614 StrCat(", ", param2_, ")"));
00615 }
00616
00617 private:
00618 T* const constraint_;
00619 void (T::* const method_)(P, Q);
00620 const string name_;
00621 P param1_;
00622 Q param2_;
00623 };
00624
00625 template <class T, class P, class Q>
00626 Demon* MakeConstraintDemon2(Solver* const s,
00627 T* const ct,
00628 void (T::*method)(P, Q),
00629 const string& name,
00630 P param1,
00631 Q param2) {
00632 return s->RevAlloc(new CallMethod2<T, P, Q>(ct,
00633 method,
00634 name,
00635 param1,
00636 param2));
00637 }
00638
00639
00640
00641
00642
00643
00644
00645
00646 template <class T> class DelayedCallMethod0 : public Demon {
00647 public:
00648 DelayedCallMethod0(T* const ct, void (T::*method)(), const string& name)
00649 : constraint_(ct), method_(method), name_(name) {}
00650
00651 virtual ~DelayedCallMethod0() {}
00652
00653 virtual void Run(Solver* const s) {
00654 (constraint_->*method_)();
00655 }
00656
00657 virtual Solver::DemonPriority priority() const {
00658 return Solver::DELAYED_PRIORITY;
00659 }
00660
00661 virtual string DebugString() const {
00662 return "DelayedCallMethod_"
00663 + name_ + "(" + constraint_->DebugString() + ")";
00664 }
00665
00666 private:
00667 T* const constraint_;
00668 void (T::* const method_)();
00669 const string name_;
00670 };
00671
00672 template <class T> Demon* MakeDelayedConstraintDemon0(Solver* const s,
00673 T* const ct,
00674 void (T::*method)(),
00675 const string& name) {
00676 return s->RevAlloc(new DelayedCallMethod0<T>(ct, method, name));
00677 }
00678
00679
00680 template <class T, class P> class DelayedCallMethod1 : public Demon {
00681 public:
00682 DelayedCallMethod1(T* const ct,
00683 void (T::*method)(P),
00684 const string& name,
00685 P param1)
00686 : constraint_(ct),
00687 method_(method),
00688 name_(name),
00689 param1_(param1) {}
00690
00691 virtual ~DelayedCallMethod1() {}
00692
00693 virtual void Run(Solver* const s) {
00694 (constraint_->*method_)(param1_);
00695 }
00696
00697 virtual Solver::DemonPriority priority() const {
00698 return Solver::DELAYED_PRIORITY;
00699 }
00700
00701 virtual string DebugString() const {
00702 return StrCat(StrCat("DelayedCallMethod_", name_),
00703 StrCat("(", constraint_->DebugString(), ", "),
00704 StrCat(param1_, ")"));
00705 }
00706
00707 private:
00708 T* const constraint_;
00709 void (T::* const method_)(P);
00710 const string name_;
00711 P param1_;
00712 };
00713
00714 template <class T, class P>
00715 Demon* MakeDelayedConstraintDemon1(Solver* const s,
00716 T* const ct,
00717 void (T::*method)(P),
00718 const string& name,
00719 P param1) {
00720 return s->RevAlloc(new DelayedCallMethod1<T, P>(ct, method, name, param1));
00721 }
00722
00723
00724 template <class T, class P, class Q> class DelayedCallMethod2 : public Demon {
00725 public:
00726 DelayedCallMethod2(T* const ct,
00727 void (T::*method)(P, Q),
00728 const string& name,
00729 P param1,
00730 Q param2)
00731 : constraint_(ct),
00732 method_(method),
00733 name_(name),
00734 param1_(param1),
00735 param2_(param2) {}
00736
00737 virtual ~DelayedCallMethod2() {}
00738
00739 virtual void Run(Solver* const s) {
00740 (constraint_->*method_)(param1_, param2_);
00741 }
00742
00743 virtual Solver::DemonPriority priority() const {
00744 return Solver::DELAYED_PRIORITY;
00745 }
00746
00747 virtual string DebugString() const {
00748 return StrCat(StrCat("DelayedCallMethod_", name_),
00749 StrCat("(", constraint_->DebugString()),
00750 StrCat(", ", param1_),
00751 StrCat(", ", param2_, ")"));
00752 }
00753
00754 private:
00755 T* const constraint_;
00756 void (T::* const method_)(P, Q);
00757 const string name_;
00758 P param1_;
00759 Q param2_;
00760 };
00761
00762 template <class T, class P, class Q>
00763 Demon* MakeDelayedConstraintDemon2(Solver* const s,
00764 T* const ct,
00765 void (T::*method)(P, Q),
00766 const string& name,
00767 P param1,
00768 Q param2) {
00769 return s->RevAlloc(new DelayedCallMethod2<T, P, Q>(ct,
00770 method,
00771 name,
00772 param1,
00773 param2));
00774 }
00775
00776
00777 #endif // !defined(SWIG)
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796 class LocalSearchOperator : public BaseObject {
00797 public:
00798 LocalSearchOperator() {}
00799 virtual ~LocalSearchOperator() {}
00800 virtual bool MakeNextNeighbor(Assignment* delta, Assignment* deltadelta) = 0;
00801 virtual void Start(const Assignment* assignment) = 0;
00802 };
00803
00804
00805
00806
00807
00808
00809
00810
00811 class IntVarLocalSearchOperator : public LocalSearchOperator {
00812 public:
00813 IntVarLocalSearchOperator();
00814 IntVarLocalSearchOperator(const IntVar* const* vars, int size);
00815 virtual ~IntVarLocalSearchOperator();
00816
00817
00818 virtual void Start(const Assignment* assignment);
00819 virtual bool IsIncremental() const { return false; }
00820 int Size() const { return size_; }
00821
00822 int64 Value(int64 index) const {
00823 DCHECK_LT(index, size_);
00824 return values_[index];
00825 }
00826
00827 IntVar* Var(int64 index) const { return vars_[index]; }
00828 virtual bool SkipUnchanged(int index) const { return false; }
00829
00830
00831
00832
00833
00834
00835
00836 virtual bool MakeNextNeighbor(Assignment* delta, Assignment* deltadelta);
00837
00838 protected:
00839
00840
00841
00842
00843 virtual bool MakeOneNeighbor();
00844
00845 int64 OldValue(int64 index) const { return old_values_[index]; }
00846 void SetValue(int64 index, int64 value);
00847 bool Activated(int64 index) const;
00848 void Activate(int64 index);
00849 void Deactivate(int64 index);
00850 bool ApplyChanges(Assignment* delta, Assignment* deltadelta) const;
00851 void RevertChanges(bool incremental);
00852 void AddVars(const IntVar* const* vars, int size);
00853
00854 private:
00855
00856
00857
00858 virtual void OnStart() {}
00859 void MarkChange(int64 index);
00860
00861 scoped_array<IntVar*> vars_;
00862 int size_;
00863 scoped_array<int64> values_;
00864 scoped_array<int64> old_values_;
00865 Bitmap activated_;
00866 Bitmap was_activated_;
00867 std::vector<int64> changes_;
00868 Bitmap has_changed_;
00869 Bitmap has_delta_changed_;
00870 bool cleared_;
00871 };
00872
00873
00874
00875
00876 class SequenceVarLocalSearchOperator : public LocalSearchOperator {
00877 public:
00878 SequenceVarLocalSearchOperator();
00879 SequenceVarLocalSearchOperator(const SequenceVar* const* vars, int size);
00880 virtual ~SequenceVarLocalSearchOperator();
00881
00882
00883 virtual void Start(const Assignment* assignment);
00884 virtual bool IsIncremental() const { return false; }
00885 int Size() const { return size_; }
00886
00887 #if !defined(SWIG)
00888 const std::vector<int>& Sequence(int64 index) const {
00889 DCHECK_LT(index, size_);
00890 return values_[index];
00891 }
00892 #endif
00893
00894 SequenceVar* Var(int64 index) const { return vars_[index]; }
00895 virtual bool SkipUnchanged(int index) const { return false; }
00896
00897 protected:
00898 const std::vector<int>& OldSequence(int64 index) const {
00899 return old_values_[index];
00900 }
00901 void SetForwardSequence(int64 index, const std::vector<int>& value);
00902 void SetBackwardSequence(int64 index, const std::vector<int>& value);
00903 bool Activated(int64 index) const;
00904 void Activate(int64 index);
00905 void Deactivate(int64 index);
00906 bool ApplyChanges(Assignment* delta, Assignment* deltadelta) const;
00907 void RevertChanges(bool incremental);
00908 void AddVars(const SequenceVar* const* vars, int size);
00909
00910 private:
00911
00912
00913
00914 virtual void OnStart() {}
00915 void MarkChange(int64 index);
00916
00917 scoped_array<SequenceVar*> vars_;
00918 int size_;
00919 scoped_array<std::vector<int> > values_;
00920 scoped_array<std::vector<int> > backward_values_;
00921 scoped_array<std::vector<int> > old_values_;
00922 Bitmap activated_;
00923 Bitmap was_activated_;
00924 std::vector<int64> changes_;
00925 Bitmap has_changed_;
00926 Bitmap has_delta_changed_;
00927 bool cleared_;
00928 };
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960 class BaseLNS : public IntVarLocalSearchOperator {
00961 public:
00962 BaseLNS(const IntVar* const* vars, int size);
00963 virtual ~BaseLNS();
00964 virtual void InitFragments();
00965 virtual bool NextFragment(std::vector<int>* fragment) = 0;
00966
00967 protected:
00968
00969 virtual bool MakeOneNeighbor();
00970
00971 private:
00972
00973 virtual void OnStart();
00974 };
00975
00976
00977
00978
00979
00980
00981
00982 class ChangeValue : public IntVarLocalSearchOperator {
00983 public:
00984 ChangeValue(const IntVar* const* vars, int size);
00985 virtual ~ChangeValue();
00986 virtual int64 ModifyValue(int64 index, int64 value) = 0;
00987
00988 protected:
00989
00990 virtual bool MakeOneNeighbor();
00991
00992 private:
00993 virtual void OnStart();
00994
00995 int index_;
00996 };
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013 class PathOperator : public IntVarLocalSearchOperator {
01014 public:
01015 PathOperator(const IntVar* const* next_vars,
01016 const IntVar* const* path_vars,
01017 int size,
01018 int number_of_base_nodes);
01019 virtual ~PathOperator() {}
01020 virtual bool MakeNeighbor() = 0;
01021
01022
01023 virtual bool SkipUnchanged(int index) const;
01024
01025
01026
01027 int64 Next(int64 node_index) const {
01028 DCHECK(!IsPathEnd(node_index));
01029 return Value(node_index);
01030 }
01031
01032
01033
01034 int64 Path(int64 node_index) const {
01035 return ignore_path_vars_ ? 0LL : Value(node_index + number_of_nexts_);
01036 }
01037
01038
01039 int number_of_nexts() const { return number_of_nexts_; }
01040
01041 protected:
01042
01043 virtual bool MakeOneNeighbor();
01044
01045
01046 int64 BaseNode(int i) const { return base_nodes_[i]; }
01047
01048
01049 int64 StartNode(int i) const { return path_starts_[base_paths_[i]]; }
01050
01051
01052
01053
01054
01055
01056
01057
01058 virtual bool RestartAtPathStartOnSynchronize() {
01059 return false;
01060 }
01061
01062
01063
01064
01065
01066 virtual bool OnSamePathAsPreviousBase(int64 base_index) {
01067 return false;
01068 }
01069
01070
01071
01072
01073
01074 virtual int64 GetBaseNodeRestartPosition(int base_index) {
01075 return StartNode(base_index);
01076 }
01077
01078 int64 OldNext(int64 node_index) const {
01079 DCHECK(!IsPathEnd(node_index));
01080 return OldValue(node_index);
01081 }
01082
01083 int64 OldPath(int64 node_index) const {
01084 return ignore_path_vars_ ? 0LL : OldValue(node_index + number_of_nexts_);
01085 }
01086
01087
01088
01089 bool MoveChain(int64 before_chain, int64 chain_end, int64 destination);
01090
01091
01092
01093 bool ReverseChain(int64 before_chain, int64 after_chain, int64* chain_last);
01094
01095 bool MakeActive(int64 node, int64 destination);
01096 bool MakeChainInactive(int64 before_chain, int64 chain_end);
01097
01098
01099 void SetNext(int64 from, int64 to, int64 path) {
01100 DCHECK_LT(from, number_of_nexts_);
01101 SetValue(from, to);
01102 if (!ignore_path_vars_) {
01103 DCHECK_LT(from + number_of_nexts_, Size());
01104 SetValue(from + number_of_nexts_, path);
01105 }
01106 }
01107
01108
01109
01110 bool IsPathEnd(int64 i) const { return i >= number_of_nexts_; }
01111
01112
01113 bool IsInactive(int64 i) const { return !IsPathEnd(i) && inactives_[i]; }
01114
01115
01116
01117 virtual bool InitPosition() const { return false; }
01118
01119
01120
01121 void ResetPosition() {
01122 just_started_ = true;
01123 }
01124
01125 const int number_of_nexts_;
01126 const bool ignore_path_vars_;
01127
01128 private:
01129 virtual void OnStart();
01130
01131
01132
01133 virtual void OnNodeInitialization() {}
01134
01135 bool OnSamePath(int64 node1, int64 node2) const;
01136
01137 bool CheckEnds() const;
01138 bool IncrementPosition();
01139 void InitializePathStarts();
01140 void InitializeInactives();
01141 void InitializeBaseNodes();
01142 bool CheckChainValidity(int64 chain_start,
01143 int64 chain_end,
01144 int64 exclude) const;
01145 void Synchronize();
01146
01147 std::vector<int> base_nodes_;
01148 std::vector<int> end_nodes_;
01149 std::vector<int> base_paths_;
01150 std::vector<int64> path_starts_;
01151 std::vector<bool> inactives_;
01152 bool just_started_;
01153 bool first_start_;
01154 };
01155
01156
01157
01158
01159 class LocalSearchFilter : public BaseObject {
01160 public:
01161
01162
01163
01164
01165
01166
01167 virtual bool Accept(const Assignment* delta,
01168 const Assignment* deltadelta) = 0;
01169
01170
01171 virtual void Synchronize(const Assignment* assignment) = 0;
01172 virtual bool IsIncremental() const { return false; }
01173 };
01174
01175
01176
01177 class IntVarLocalSearchFilter : public LocalSearchFilter {
01178 public:
01179 IntVarLocalSearchFilter(const IntVar* const* vars, int size);
01180 ~IntVarLocalSearchFilter();
01181
01182
01183 virtual void Synchronize(const Assignment* assignment);
01184
01185 protected:
01186
01187 void AddVars(const IntVar* const* vars, int size);
01188 bool FindIndex(const IntVar* const var, int64* index) const {
01189 DCHECK(index != NULL);
01190 return FindCopy(var_to_index_, var, index);
01191 }
01192 int Size() const { return size_; }
01193 IntVar* Var(int index) const { return vars_[index]; }
01194 int64 Value(int index) const { return values_[index]; }
01195
01196 private:
01197 virtual void OnSynchronize() {}
01198
01199 scoped_array<IntVar*> vars_;
01200 scoped_array<int64> values_;
01201 int size_;
01202 hash_map<const IntVar*, int64> var_to_index_;
01203 };
01204
01205
01206
01207 class PropagationMonitor : public SearchMonitor {
01208 public:
01209 explicit PropagationMonitor(Solver* const solver);
01210 virtual ~PropagationMonitor();
01211
01212
01213 virtual void BeginConstraintInitialPropagation(
01214 const Constraint* const constraint) = 0;
01215 virtual void EndConstraintInitialPropagation(
01216 const Constraint* const constraint) = 0;
01217 virtual void BeginNestedConstraintInitialPropagation(
01218 const Constraint* const parent,
01219 const Constraint* const nested) = 0;
01220 virtual void EndNestedConstraintInitialPropagation(
01221 const Constraint* const parent,
01222 const Constraint* const nested) = 0;
01223 virtual void RegisterDemon(const Demon* const demon) = 0;
01224 virtual void BeginDemonRun(const Demon* const demon) = 0;
01225 virtual void EndDemonRun(const Demon* const demon) = 0;
01226 virtual void PushContext(const string& context) = 0;
01227 virtual void PopContext() = 0;
01228
01229 virtual void SetMin(IntExpr* const expr, int64 new_min) = 0;
01230 virtual void SetMax(IntExpr* const expr, int64 new_max) = 0;
01231 virtual void SetRange(IntExpr* const expr, int64 new_min, int64 new_max) = 0;
01232
01233 virtual void SetMin(IntVar* const var, int64 new_min) = 0;
01234 virtual void SetMax(IntVar* const var, int64 new_max) = 0;
01235 virtual void SetRange(IntVar* const var, int64 new_min, int64 new_max) = 0;
01236 virtual void RemoveValue(IntVar* const var, int64 value) = 0;
01237 virtual void SetValue(IntVar* const var, int64 value) = 0;
01238 virtual void RemoveInterval(IntVar* const var, int64 imin, int64 imax) = 0;
01239 virtual void SetValues(IntVar* const var,
01240 const int64* const values,
01241 int size) = 0;
01242 virtual void RemoveValues(IntVar* const var,
01243 const int64* const values,
01244 int size) = 0;
01245
01246 virtual void SetStartMin(IntervalVar* const var, int64 new_min) = 0;
01247 virtual void SetStartMax(IntervalVar* const var, int64 new_max) = 0;
01248 virtual void SetStartRange(IntervalVar* const var,
01249 int64 new_min,
01250 int64 new_max) = 0;
01251 virtual void SetEndMin(IntervalVar* const var, int64 new_min) = 0;
01252 virtual void SetEndMax(IntervalVar* const var, int64 new_max) = 0;
01253 virtual void SetEndRange(IntervalVar* const var,
01254 int64 new_min,
01255 int64 new_max) = 0;
01256 virtual void SetDurationMin(IntervalVar* const var, int64 new_min) = 0;
01257 virtual void SetDurationMax(IntervalVar* const var, int64 new_max) = 0;
01258 virtual void SetDurationRange(IntervalVar* const var,
01259 int64 new_min,
01260 int64 new_max) = 0;
01261 virtual void SetPerformed(IntervalVar* const var, bool value) = 0;
01262
01263 virtual void RankFirst(SequenceVar* const var, int index) = 0;
01264 virtual void RankNotFirst(SequenceVar* const var, int index) = 0;
01265 virtual void RankLast(SequenceVar* const var, int index) = 0;
01266 virtual void RankNotLast(SequenceVar* const var, int index) = 0;
01267 virtual void RankSequence(SequenceVar* const var,
01268 const std::vector<int>& rank_first,
01269 const std::vector<int>& rank_last,
01270 const std::vector<int>& unperformed) = 0;
01271
01272 virtual void Install();
01273 };
01274
01275
01276
01277 class SymmetryManager;
01278
01279
01280
01281
01282 class SymmetryBreaker : public DecisionVisitor {
01283 public:
01284 SymmetryBreaker() : symmetry_manager_(NULL), index_in_symmetry_manager_(-1) {}
01285 virtual ~SymmetryBreaker() {}
01286
01287 void AddIntegerVariableEqualValueClause(IntVar* const var, int64 value);
01288 void AddIntegerVariableGreaterOrEqualValueClause(IntVar* const var,
01289 int64 value);
01290 void AddIntegerVariableLessOrEqualValueClause(IntVar* const var, int64 value);
01291
01292 private:
01293 friend class SymmetryManager;
01294 void set_symmetry_manager_and_index(SymmetryManager* manager, int index) {
01295 CHECK(symmetry_manager_ == NULL);
01296 CHECK_EQ(-1, index_in_symmetry_manager_);
01297 symmetry_manager_ = manager;
01298 index_in_symmetry_manager_ = index;
01299 }
01300 SymmetryManager* symmetry_manager() const { return symmetry_manager_; }
01301 int index_in_symmetry_manager() const { return index_in_symmetry_manager_; }
01302
01303 SymmetryManager* symmetry_manager_;
01304
01305 int index_in_symmetry_manager_;
01306 };
01307
01308
01309
01310
01311
01312 class SearchLog : public SearchMonitor {
01313 public:
01314 SearchLog(Solver* const s,
01315 OptimizeVar* const obj,
01316 IntVar* const var,
01317 ResultCallback<string>* display_callback,
01318 int period);
01319 virtual ~SearchLog();
01320 virtual void EnterSearch();
01321 virtual void ExitSearch();
01322 virtual bool AtSolution();
01323 virtual void BeginFail();
01324 virtual void NoMoreSolutions();
01325 virtual void ApplyDecision(Decision* const decision);
01326 virtual void RefuteDecision(Decision* const decision);
01327 void OutputDecision();
01328 void Maintain();
01329 virtual void BeginInitialPropagation();
01330 virtual void EndInitialPropagation();
01331
01332 protected:
01333
01334 virtual void OutputLine(const string& line);
01335
01336 private:
01337 static string MemoryUsage();
01338
01339 const int period_;
01340 scoped_ptr<WallTimer> timer_;
01341 IntVar* const var_;
01342 OptimizeVar* const obj_;
01343 scoped_ptr<ResultCallback<string> > display_callback_;
01344 int nsol_;
01345 int64 tick_;
01346 int64 objective_min_;
01347 int64 objective_max_;
01348 int min_right_depth_;
01349 int max_depth_;
01350 int sliding_min_depth_;
01351 int sliding_max_depth_;
01352 };
01353
01354
01355
01356
01357
01358 class ModelCache {
01359 public:
01360 enum VoidConstraintType {
01361 VOID_FALSE_CONSTRAINT = 0,
01362 VOID_TRUE_CONSTRAINT,
01363 VOID_CONSTRAINT_MAX,
01364 };
01365
01366 enum VarConstantConstraintType {
01367 VAR_CONSTANT_EQUALITY = 0,
01368 VAR_CONSTANT_GREATER_OR_EQUAL,
01369 VAR_CONSTANT_LESS_OR_EQUAL,
01370 VAR_CONSTANT_NON_EQUALITY,
01371 VAR_CONSTANT_CONSTRAINT_MAX,
01372 };
01373
01374 enum VarConstantConstantConstraintType {
01375 VAR_CONSTANT_CONSTANT_BETWEEN = 0,
01376 VAR_CONSTANT_CONSTANT_CONSTRAINT_MAX,
01377 };
01378
01379 enum VarVarConstraintType {
01380 VAR_VAR_EQUALITY = 0,
01381 VAR_VAR_GREATER,
01382 VAR_VAR_GREATER_OR_EQUAL,
01383 VAR_VAR_LESS,
01384 VAR_VAR_LESS_OR_EQUAL,
01385 VAR_VAR_NON_EQUALITY,
01386 VAR_VAR_CONSTRAINT_MAX,
01387 };
01388
01389 enum VarExpressionType {
01390 VAR_OPPOSITE = 0,
01391 VAR_ABS,
01392 VAR_SQUARE,
01393 VAR_EXPRESSION_MAX,
01394 };
01395
01396 enum VarConstantExpressionType {
01397 VAR_CONSTANT_DIFFERENCE = 0,
01398 VAR_CONSTANT_DIVIDE,
01399 VAR_CONSTANT_PROD,
01400 VAR_CONSTANT_MAX,
01401 VAR_CONSTANT_MIN,
01402 VAR_CONSTANT_SUM,
01403 VAR_CONSTANT_IS_EQUAL,
01404 VAR_CONSTANT_IS_NOT_EQUAL,
01405 VAR_CONSTANT_IS_GREATER_OR_EQUAL,
01406 VAR_CONSTANT_IS_LESS_OR_EQUAL,
01407 VAR_CONSTANT_EXPRESSION_MAX,
01408 };
01409
01410 enum VarVarExpressionType {
01411 VAR_VAR_DIFFERENCE = 0,
01412 VAR_VAR_PROD,
01413 VAR_VAR_MAX,
01414 VAR_VAR_MIN,
01415 VAR_VAR_SUM,
01416 VAR_VAR_IS_EQUAL,
01417 VAR_VAR_IS_NOT_EQUAL,
01418 VAR_VAR_IS_LESS,
01419 VAR_VAR_IS_LESS_OR_EQUAL,
01420 VAR_VAR_EXPRESSION_MAX,
01421 };
01422
01423 enum VarConstantConstantExpressionType {
01424 VAR_CONSTANT_CONSTANT_SEMI_CONTINUOUS = 0,
01425 VAR_CONSTANT_CONSTANT_EXPRESSION_MAX,
01426 };
01427
01428 enum VarConstantArrayExpressionType {
01429 VAR_CONSTANT_ARRAY_ELEMENT = 0,
01430 VAR_CONSTANT_ARRAY_EXPRESSION_MAX,
01431 };
01432
01433 enum VarArrayExpressionType {
01434 VAR_ARRAY_MAX = 0,
01435 VAR_ARRAY_MIN,
01436 VAR_ARRAY_SUM,
01437 VAR_ARRAY_EXPRESSION_MAX,
01438 };
01439
01440 explicit ModelCache(Solver* const solver);
01441 virtual ~ModelCache();
01442
01443
01444
01445 virtual Constraint* FindVoidConstraint(VoidConstraintType type) const = 0;
01446
01447 virtual void InsertVoidConstraint(Constraint* const ct,
01448 VoidConstraintType type) = 0;
01449
01450
01451 virtual Constraint* FindVarConstantConstraint(
01452 IntVar* const var,
01453 int64 value,
01454 VarConstantConstraintType type) const = 0;
01455
01456 virtual void InsertVarConstantConstraint(
01457 Constraint* const ct,
01458 IntVar* const var,
01459 int64 value,
01460 VarConstantConstraintType type) = 0;
01461
01462
01463
01464 virtual Constraint* FindVarConstantConstantConstraint(
01465 IntVar* const var,
01466 int64 value1,
01467 int64 value2,
01468 VarConstantConstantConstraintType type) const = 0;
01469
01470 virtual void InsertVarConstantConstantConstraint(
01471 Constraint* const ct,
01472 IntVar* const var,
01473 int64 value1,
01474 int64 value2,
01475 VarConstantConstantConstraintType type) = 0;
01476
01477
01478
01479 virtual Constraint* FindVarVarConstraint(
01480 IntVar* const var1,
01481 IntVar* const var2,
01482 VarVarConstraintType type) const = 0;
01483
01484 virtual void InsertVarVarConstraint(Constraint* const ct,
01485 IntVar* const var1,
01486 IntVar* const var2,
01487 VarVarConstraintType type) = 0;
01488
01489
01490
01491 virtual IntExpr* FindVarExpression(
01492 IntVar* const var,
01493 VarExpressionType type) const = 0;
01494
01495 virtual void InsertVarExpression(IntExpr* const expression,
01496 IntVar* const var,
01497 VarExpressionType type) = 0;
01498
01499
01500
01501 virtual IntExpr* FindVarConstantExpression(
01502 IntVar* const var,
01503 int64 value,
01504 VarConstantExpressionType type) const = 0;
01505
01506 virtual void InsertVarConstantExpression(
01507 IntExpr* const expression,
01508 IntVar* const var,
01509 int64 value,
01510 VarConstantExpressionType type) = 0;
01511
01512
01513
01514 virtual IntExpr* FindVarVarExpression(
01515 IntVar* const var1,
01516 IntVar* const var2,
01517 VarVarExpressionType type) const = 0;
01518
01519 virtual void InsertVarVarExpression(
01520 IntExpr* const expression,
01521 IntVar* const var1,
01522 IntVar* const var2,
01523 VarVarExpressionType type) = 0;
01524
01525
01526
01527 virtual IntExpr* FindVarConstantConstantExpression(
01528 IntVar* const var,
01529 int64 value1,
01530 int64 value2,
01531 VarConstantConstantExpressionType type) const = 0;
01532
01533 virtual void InsertVarConstantConstantExpression(
01534 IntExpr* const expression,
01535 IntVar* const var,
01536 int64 value1,
01537 int64 value2,
01538 VarConstantConstantExpressionType type) = 0;
01539
01540
01541
01542 virtual IntExpr* FindVarConstantArrayExpression(
01543 IntVar* const var,
01544 ConstIntArray* const values,
01545 VarConstantArrayExpressionType type) const = 0;
01546
01547 virtual void InsertVarConstantArrayExpression(
01548 IntExpr* const expression,
01549 IntVar* const var,
01550 ConstIntArray* const values,
01551 VarConstantArrayExpressionType type) = 0;
01552
01553
01554
01555 virtual IntExpr* FindVarArrayExpression(
01556 ConstPtrArray<IntVar>* const vars,
01557 VarArrayExpressionType type) const = 0;
01558
01559 virtual void InsertVarArrayExpression(
01560 IntExpr* const expression,
01561 ConstPtrArray<IntVar>* const vars,
01562 VarArrayExpressionType type) = 0;
01563
01564 Solver* solver() const;
01565
01566 private:
01567 Solver* const solver_;
01568 };
01569
01570 #if !defined(SWIG)
01571
01572
01573
01574 class DependencyGraphNode;
01575 class DependencyGraph : public BaseObject {
01576 public:
01577 virtual ~DependencyGraph();
01578
01579
01580 void AddStartsAfterEndWithDelay(IntervalVar* const left,
01581 IntervalVar* const right,
01582 int64 delay);
01583
01584
01585 void AddStartsAtEndWithDelay(IntervalVar* const left,
01586 IntervalVar* const right,
01587 int64 delay);
01588
01589
01590 void AddStartsAfterStartWithDelay(IntervalVar* const left,
01591 IntervalVar* const right,
01592 int64 delay);
01593
01594
01595 void AddStartsAtStartWithDelay(IntervalVar* const left,
01596 IntervalVar* const right,
01597 int64 delay);
01598
01599
01600
01601
01602
01603 DependencyGraphNode* BuildStartNode(IntervalVar* const var);
01604
01605
01606 virtual void AddEquality(DependencyGraphNode* const left,
01607 DependencyGraphNode* const right,
01608 int64 offset) = 0;
01609
01610 virtual void AddInequality(DependencyGraphNode* const left,
01611 DependencyGraphNode* const right,
01612 int64 offset) = 0;
01613
01614
01615
01616
01617 virtual void Enqueue(DependencyGraphNode* const node,
01618 bool applied_to_min_or_max) = 0;
01619
01620 private:
01621 hash_map<IntervalVar*, DependencyGraphNode*> start_node_map_;
01622 std::vector<DependencyGraphNode*> managed_nodes_;
01623 };
01624 #endif
01625
01626
01627 #if !defined(SWIG)
01628 class ArgumentHolder {
01629 public:
01630
01631 const string& TypeName() const;
01632 void SetTypeName(const string& type_name);
01633
01634
01635 void SetIntegerArgument(const string& arg_name, int64 value);
01636 void SetIntegerArrayArgument(const string& arg_name,
01637 const int64* const values,
01638 int size);
01639 void SetIntegerMatrixArgument(const string& arg_name,
01640 const IntTupleSet& values);
01641 void SetIntegerExpressionArgument(const string& arg_name,
01642 const IntExpr* const expr);
01643 void SetIntegerVariableArrayArgument(const string& arg_name,
01644 const IntVar* const * const vars,
01645 int size);
01646 void SetIntervalArgument(const string& arg_name,
01647 const IntervalVar* const var);
01648 void SetIntervalArrayArgument(const string& arg_name,
01649 const IntervalVar* const * const vars,
01650 int size);
01651 void SetSequenceArgument(const string& arg_name,
01652 const SequenceVar* const var);
01653 void SetSequenceArrayArgument(const string& arg_name,
01654 const SequenceVar* const * const vars,
01655 int size);
01656
01657
01658 bool HasIntegerExpressionArgument(const string& arg_name) const;
01659 bool HasIntegerVariableArrayArgument(const string& arg_name) const;
01660
01661
01662 int64 FindIntegerArgumentWithDefault(
01663 const string& arg_name,
01664 int64 def) const;
01665 int64 FindIntegerArgumentOrDie(const string& arg_name) const;
01666 const std::vector<int64>& FindIntegerArrayArgumentOrDie(
01667 const string& arg_name) const;
01668 const IntTupleSet& FindIntegerMatrixArgumentOrDie(
01669 const string& arg_name) const;
01670
01671 const IntExpr* FindIntegerExpressionArgumentOrDie(
01672 const string& arg_name) const;
01673 const std::vector<const IntVar*>& FindIntegerVariableArrayArgumentOrDie(
01674 const string& arg_name) const;
01675
01676 private:
01677 string type_name_;
01678 hash_map<string, int64> integer_argument_;
01679 hash_map<string, std::vector<int64> > integer_array_argument_;
01680 hash_map<string, IntTupleSet> matrix_argument_;
01681 hash_map<string, const IntExpr*> integer_expression_argument_;
01682 hash_map<string, const IntervalVar*> interval_argument_;
01683 hash_map<string, const SequenceVar*> sequence_argument_;
01684 hash_map<string, std::vector<const IntVar*> > integer_variable_array_argument_;
01685 hash_map<string, std::vector<const IntervalVar*> > interval_array_argument_;
01686 hash_map<string, std::vector<const SequenceVar*> > sequence_array_argument_;
01687 };
01688
01689
01690
01691 class ModelParser : public ModelVisitor {
01692 public:
01693 ModelParser();
01694
01695 virtual ~ModelParser();
01696
01697
01698 virtual void BeginVisitModel(const string& solver_name);
01699 virtual void EndVisitModel(const string& solver_name);
01700 virtual void BeginVisitConstraint(const string& type_name,
01701 const Constraint* const constraint);
01702 virtual void EndVisitConstraint(const string& type_name,
01703 const Constraint* const constraint);
01704 virtual void BeginVisitIntegerExpression(const string& type_name,
01705 const IntExpr* const expr);
01706 virtual void EndVisitIntegerExpression(const string& type_name,
01707 const IntExpr* const expr);
01708 virtual void VisitIntegerVariable(const IntVar* const variable,
01709 const IntExpr* const delegate);
01710 virtual void VisitIntegerVariable(const IntVar* const variable,
01711 const string& operation,
01712 int64 value,
01713 const IntVar* const delegate);
01714 virtual void VisitIntervalVariable(const IntervalVar* const variable,
01715 const string& operation,
01716 const IntervalVar* const delegate);
01717 virtual void VisitIntervalVariable(const IntervalVar* const variable,
01718 const string& operation,
01719 const IntervalVar* const * delegates,
01720 int size);
01721 virtual void VisitSequenceVariable(const SequenceVar* const variable);
01722
01723 virtual void VisitIntegerArgument(const string& arg_name, int64 value);
01724 virtual void VisitIntegerArrayArgument(const string& arg_name,
01725 const int64* const values,
01726 int size);
01727 virtual void VisitIntegerMatrixArgument(const string& arg_name,
01728 const IntTupleSet& values);
01729
01730 virtual void VisitIntegerExpressionArgument(
01731 const string& arg_name,
01732 const IntExpr* const argument);
01733 virtual void VisitIntegerVariableArrayArgument(
01734 const string& arg_name,
01735 const IntVar* const * arguments,
01736 int size);
01737
01738 virtual void VisitIntervalArgument(const string& arg_name,
01739 const IntervalVar* const argument);
01740 virtual void VisitIntervalArrayArgument(const string& arg_name,
01741 const IntervalVar* const * arguments,
01742 int size);
01743
01744 virtual void VisitSequenceArgument(const string& arg_name,
01745 const SequenceVar* const argument);
01746 virtual void VisitSequenceArrayArgument(const string& arg_name,
01747 const SequenceVar* const * arguments,
01748 int size);
01749
01750 protected:
01751 void PushArgumentHolder();
01752 void PopArgumentHolder();
01753 ArgumentHolder* Top() const;
01754
01755 private:
01756 std::vector<ArgumentHolder*> holders_;
01757 };
01758 #endif // SWIG
01759 }
01760
01761 #endif // OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_