49 #ifndef OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_ 50 #define OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_ 60 #include "absl/container/flat_hash_map.h" 61 #include "absl/strings/str_cat.h" 62 #include "absl/strings/str_format.h" 63 #include "absl/strings/str_join.h" 64 #include "ortools/base/commandlineflags.h" 65 #include "ortools/base/hash.h" 66 #include "ortools/base/integral_types.h" 67 #include "ortools/base/logging.h" 68 #include "ortools/base/map_util.h" 69 #include "ortools/base/sysinfo.h" 70 #include "ortools/base/timer.h" 72 #include "ortools/util/bitset.h" 73 #include "ortools/util/tuple_set.h" 74 #include "ortools/util/vector_map.h" 79 class CPArgumentProto;
80 class CPConstraintProto;
81 class CPIntegerExpressionProto;
82 class CPIntervalVariableProto;
147 class SimpleRevFIFO {
149 enum { CHUNK_SIZE = 16 };
152 const Chunk*
const next_;
153 explicit Chunk(
const Chunk* next) : next_(next) {}
161 : chunk_(l->chunks_), value_(l->
Last()) {}
162 bool ok()
const {
return (value_ !=
nullptr); }
166 if (value_ == chunk_->data_ + CHUNK_SIZE) {
167 chunk_ = chunk_->next_;
168 value_ = chunk_ ? chunk_->data_ :
nullptr;
180 if (pos_.
Value() == 0) {
181 Chunk*
const chunk = s->UnsafeRevAlloc(
new Chunk(chunks_));
183 reinterpret_cast<void*>(chunk));
188 chunks_->data_[pos_.
Value()] = val;
193 if (chunks_ ==
nullptr ||
LastValue() != val) {
200 return chunks_ ? &chunks_->data_[pos_.
Value()] :
nullptr;
208 return chunks_->data_[pos_.
Value()];
214 chunks_->data_[pos_.
Value()] = v;
227 value = (~value) + (value << 21);
228 value ^= value >> 24;
229 value += (value << 3) + (value << 8);
230 value ^= value >> 14;
231 value += (value << 2) + (value << 4);
232 value ^= value >> 28;
233 value += (value << 31);
239 a = (a + 0x7ed55d16) + (a << 12);
240 a = (a ^ 0xc761c23c) ^ (a >> 19);
241 a = (a + 0x165667b1) + (a << 5);
242 a = (a + 0xd3a2646c) ^ (a << 9);
243 a = (a + 0xfd7046c5) + (a << 3);
244 a = (a ^ 0xb55a4f09) ^ (a >> 16);
248 inline uint64
Hash1(int64 value) {
return Hash1(static_cast<uint64>(value)); }
250 inline uint64
Hash1(
int value) {
return Hash1(static_cast<uint32>(value)); }
252 inline uint64
Hash1(
void*
const ptr) {
253 #if defined(ARCH_K8) || defined(__powerpc64__) || defined(__aarch64__) 254 return Hash1(reinterpret_cast<uint64>(ptr));
256 return Hash1(reinterpret_cast<uint32>(ptr));
261 uint64
Hash1(
const std::vector<T*>& ptrs) {
264 }
else if (ptrs.size() == 1) {
265 return Hash1(ptrs[0]);
267 uint64 hash =
Hash1(ptrs[0]);
268 for (
int i = 1; i < ptrs.size(); ++i) {
269 hash = hash * i +
Hash1(ptrs[i]);
275 inline uint64
Hash1(
const std::vector<int64>& ptrs) {
278 }
else if (ptrs.size() == 1) {
279 return Hash1(ptrs[0]);
281 uint64 hash =
Hash1(ptrs[0]);
282 for (
int i = 1; i < ptrs.size(); ++i) {
283 hash = hash * i +
Hash1(ptrs[i]);
293 template <
class K,
class V>
298 array_(
solver->UnsafeRevAllocArray(new Cell*[initial_size])),
301 memset(array_, 0,
sizeof(*array_) * size_.
Value());
311 Cell* tmp = array_[code];
313 if (tmp->key() == key) {
326 Cell* tmp = array_[code];
328 if (tmp->key() == key) {
333 return default_value;
337 void Insert(
const K& key,
const V& value) {
338 const int position =
Hash1(key) % size_.
Value();
340 solver_->UnsafeRevAlloc(
new Cell(key, value, array_[position]));
342 reinterpret_cast<void*>(cell));
343 num_items_.
Incr(solver_);
352 Cell(
const K& key,
const V& value, Cell*
const next)
353 : key_(key), value_(value), next_(next) {}
355 void SetRevNext(Solver*
const solver, Cell*
const next) {
356 solver->SaveAndSetValue(reinterpret_cast<void**>(&next_),
357 reinterpret_cast<void*>(next));
360 Cell* next()
const {
return next_; }
362 const K& key()
const {
return key_; }
364 const V& value()
const {
return value_; }
373 Cell**
const old_cell_array = array_;
374 const int old_size = size_.
Value();
377 reinterpret_cast<void**>(&array_),
378 reinterpret_cast<void*>(
379 solver_->UnsafeRevAllocArray(
new Cell*[size_.
Value()])));
380 memset(array_, 0, size_.
Value() *
sizeof(*array_));
381 for (
int i = 0; i < old_size; ++i) {
382 Cell* tmp = old_cell_array[i];
383 while (tmp !=
nullptr) {
384 Cell*
const to_reinsert = tmp;
386 const uint64 new_position =
Hash1(to_reinsert->key()) % size_.
Value();
387 to_reinsert->SetRevNext(solver_, array_[new_position]);
389 reinterpret_cast<void**>(&array_[new_position]),
390 reinterpret_cast<void*>(to_reinsert));
395 Solver*
const solver_;
397 NumericalRev<int> size_;
398 NumericalRev<int> num_items_;
451 bool IsSet(int64 index)
const;
486 bool IsSet(int64 row, int64 column)
const {
488 DCHECK_LT(row, rows_);
489 DCHECK_GE(column, 0);
490 DCHECK_LT(column, columns_);
507 const int64 columns_;
520 CallMethod0(T*
const ct,
void (T::*method)(),
const std::string& name)
521 : constraint_(ct), method_(method), name_(name) {}
525 void Run(
Solver*
const s)
override { (constraint_->*method_)(); }
528 return "CallMethod_" + name_ +
"(" + constraint_->DebugString() +
")";
532 T*
const constraint_;
533 void (T::*
const method_)();
534 const std::string name_;
539 const std::string& name) {
545 return absl::StrCat(param);
551 return param->DebugString();
555 template <
class T,
class P>
558 CallMethod1(T*
const ct,
void (T::*method)(P),
const std::string& name,
560 : constraint_(ct), method_(method), name_(name), param1_(param1) {}
564 void Run(
Solver*
const s)
override { (constraint_->*method_)(param1_); }
567 return absl::StrCat(
"CallMethod_", name_,
"(", constraint_->DebugString(),
572 T*
const constraint_;
573 void (T::*
const method_)(P);
574 const std::string name_;
578 template <
class T,
class P>
580 const std::string& name, P param1) {
585 template <
class T,
class P,
class Q>
588 CallMethod2(T*
const ct,
void (T::*method)(P, Q),
const std::string& name,
599 (constraint_->*method_)(param1_, param2_);
603 return absl::StrCat(absl::StrCat(
"CallMethod_", name_),
604 absl::StrCat(
"(", constraint_->DebugString()),
610 T*
const constraint_;
611 void (T::*
const method_)(P, Q);
612 const std::string name_;
617 template <
class T,
class P,
class Q>
619 void (T::*method)(P, Q),
const std::string& name,
620 P param1, Q param2) {
625 template <
class T,
class P,
class Q,
class R>
628 CallMethod3(T*
const ct,
void (T::*method)(P, Q, R),
const std::string& name,
629 P param1, Q param2, R param3)
640 (constraint_->*method_)(param1_, param2_, param3_);
644 return absl::StrCat(absl::StrCat(
"CallMethod_", name_),
645 absl::StrCat(
"(", constraint_->DebugString()),
652 T*
const constraint_;
653 void (T::*
const method_)(P, Q, R);
654 const std::string name_;
660 template <
class T,
class P,
class Q,
class R>
662 void (T::*method)(P, Q, R),
const std::string& name,
663 P param1, Q param2, R param3) {
679 : constraint_(ct), method_(method), name_(name) {}
683 void Run(
Solver*
const s)
override { (constraint_->*method_)(); }
690 return "DelayedCallMethod_" + name_ +
"(" + constraint_->DebugString() +
695 T*
const constraint_;
696 void (T::*
const method_)();
697 const std::string name_;
703 const std::string& name) {
708 template <
class T,
class P>
713 : constraint_(ct), method_(method), name_(name), param1_(param1) {}
717 void Run(
Solver*
const s)
override { (constraint_->*method_)(param1_); }
724 return absl::StrCat(
"DelayedCallMethod_", name_,
"(",
725 constraint_->DebugString(),
", ",
730 T*
const constraint_;
731 void (T::*
const method_)(P);
732 const std::string name_;
736 template <
class T,
class P>
738 void (T::*method)(P),
739 const std::string& name, P param1) {
744 template <
class T,
class P,
class Q>
748 const std::string& name, P param1, Q param2)
758 (constraint_->*method_)(param1_, param2_);
766 return absl::StrCat(absl::StrCat(
"DelayedCallMethod_", name_),
767 absl::StrCat(
"(", constraint_->DebugString()),
773 T*
const constraint_;
774 void (T::*
const method_)(P, Q);
775 const std::string name_;
780 template <
class T,
class P,
class Q>
782 void (T::*method)(P, Q),
783 const std::string& name, P param1,
790 #endif // !defined(SWIG) 829 template <
class V,
class Val,
class Handler>
843 const int size =
Size();
844 CHECK_LE(size, assignment->
Size())
845 <<
"Assignment contains fewer variables than operator";
846 for (
int i = 0; i < size; ++i) {
858 const Val&
Value(int64 index)
const {
859 DCHECK_LT(index,
vars_.size());
881 for (
const int64 index :
delta_changes_.PositionsSetAtLeastOnce()) {
883 const Val& value =
Value(index);
885 var_handler_.AddToAssignment(var, value, activated,
nullptr, index,
892 for (
const int64 index :
changes_.PositionsSetAtLeastOnce()) {
893 const Val& value =
Value(index);
908 for (
const int64 index :
changes_.PositionsSetAtLeastOnce()) {
918 vars_.insert(
vars_.end(), vars.begin(), vars.end());
919 const int64 size =
Size();
963 std::vector<int>* assignment_indices, int64 index,
968 if (assignment_indices !=
nullptr) {
969 if ((*assignment_indices)[index] == -1) {
970 (*assignment_indices)[index] = container->
Size();
971 element = assignment->
FastAdd(var);
973 element = container->
MutableElement((*assignment_indices)[index]);
976 element = assignment->
FastAdd(var);
986 int64 index, int64* value) {
989 if (element->
Var() != var) {
991 <<
"Assignment does not contain operator variable " << var;
992 element = &(container.
Element(var));
994 *value = element->
Value();
1015 #if defined(SWIGPYTHON) 1016 %unignore VarLocalSearchOperator<IntVar, int64,
1017 IntVarLocalSearchHandler>
::Size;
1018 %unignore VarLocalSearchOperator<IntVar, int64,
1019 IntVarLocalSearchHandler>::Value;
1020 %unignore VarLocalSearchOperator<IntVar, int64,
1021 IntVarLocalSearchHandler>::OldValue;
1022 %unignore VarLocalSearchOperator<IntVar, int64,
1023 IntVarLocalSearchHandler>::SetValue;
1024 %feature(
"director") VarLocalSearchOperator<IntVar, int64,
1025 IntVarLocalSearchHandler>::IsIncremental;
1026 %feature("director") VarLocalSearchOperator<IntVar, int64,
1027 IntVarLocalSearchHandler>::OnStart;
1028 %unignore VarLocalSearchOperator<IntVar, int64,
1029 IntVarLocalSearchHandler>::IsIncremental;
1030 %unignore VarLocalSearchOperator<IntVar, int64,
1031 IntVarLocalSearchHandler>::OnStart;
1032 #endif // SWIGPYTHON 1035 %rename(IntVarLocalSearchOperatorTemplate)
1036 VarLocalSearchOperator<IntVar, int64, IntVarLocalSearchHandler>;
1037 %
template(IntVarLocalSearchOperatorTemplate)
1038 VarLocalSearchOperator<IntVar, int64, IntVarLocalSearchHandler>;
1065 virtual bool MakeOneNeighbor();
1070 class SequenceVarLocalSearchOperator;
1080 bool active, std::vector<int>* assignment_indices,
1083 int64 index, std::vector<int>* value);
1084 void OnRevertChanges(int64 index);
1099 SequenceVar, std::vector<int>, SequenceVarLocalSearchHandler>;
1101 SequenceVar, std::vector<int>, SequenceVarLocalSearchHandler>;
1105 typedef VarLocalSearchOperator<SequenceVar, std::vector<int>,
1106 SequenceVarLocalSearchHandler>
1139 SequenceVar* var,
const std::vector<int>& value,
bool active,
1140 std::vector<int>* assignment_indices, int64 index,
1145 if (assignment_indices !=
nullptr) {
1146 if ((*assignment_indices)[index] == -1) {
1147 (*assignment_indices)[index] = container->
Size();
1148 element = assignment->
FastAdd(var);
1150 element = container->
MutableElement((*assignment_indices)[index]);
1153 element = assignment->
FastAdd(var);
1166 std::vector<int>* value) {
1170 if (element->
Var() != var) {
1172 <<
"Assignment does not contain operator variable " << var;
1173 element = &(container.
Element(var));
1176 CHECK_GE(var->
size(), element_value.size());
1178 *value = element_value;
1221 explicit BaseLns(
const std::vector<IntVar*>& vars);
1235 void OnStart()
override;
1236 std::vector<int> fragment_;
1247 explicit ChangeValue(
const std::vector<IntVar*>& vars);
1249 virtual int64
ModifyValue(int64 index, int64 value) = 0;
1256 void OnStart()
override;
1292 const std::vector<IntVar*>& path_vars,
int number_of_base_nodes,
1293 bool skip_locally_optimal_paths,
1294 std::function<
int(int64)> start_empty_path_class);
1297 void Reset()
override;
1304 int64
Next(int64 node_index)
const {
1306 return Value(node_index);
1311 int64
Path(int64 node_index)
const {
1326 int64
StartNode(
int i)
const {
return path_starts_[base_paths_[i]]; }
1328 const std::vector<int64>&
path_starts()
const {
return path_starts_; }
1331 return start_empty_path_class_ !=
nullptr 1375 bool MoveChain(int64 before_chain, int64 chain_end, int64 destination);
1379 bool ReverseChain(int64 before_chain, int64 after_chain, int64* chain_last);
1381 bool MakeActive(int64 node, int64 destination);
1416 void OnStart()
override;
1420 virtual void OnNodeInitialization() {}
1422 bool OnSamePath(int64 node1, int64 node2)
const;
1424 bool CheckEnds()
const {
1425 const int base_node_size = base_nodes_.size();
1426 for (
int i = base_node_size - 1; i >= 0; --i) {
1427 if (base_nodes_[i] != end_nodes_[i]) {
1433 bool IncrementPosition();
1434 void InitializePathStarts();
1435 void InitializeInactives();
1436 void InitializeBaseNodes();
1437 bool CheckChainValidity(int64 before_chain, int64 chain_end,
1438 int64 exclude)
const;
1441 std::vector<int> base_nodes_;
1442 std::vector<int> end_nodes_;
1443 std::vector<int> base_paths_;
1444 std::vector<int64> path_starts_;
1445 std::vector<bool> inactives_;
1448 std::function<int(int64)> start_empty_path_class_;
1449 bool skip_locally_optimal_paths_;
1450 bool optimal_paths_enabled_;
1451 std::vector<int> path_basis_;
1452 std::vector<bool> optimal_paths_;
1460 const std::vector<IntVar*>& vars,
1461 const std::vector<IntVar*>& secondary_vars,
int number_of_base_nodes,
1462 std::function<
int(int64)> start_empty_path_class);
1465 bool IsPathStart(int64 node_index)
const {
return prevs_[node_index] == -1; }
1467 int64
Prev(int64 node_index)
const {
1469 return prevs_[node_index];
1473 return "PathWithPreviousNodesOperator";
1480 std::vector<int64> prevs_;
1487 Solver*
solver,
const std::vector<IntVar*>& vars,
1488 const std::vector<IntVar*>& secondary_vars,
1489 std::function<
int(int64)> start_empty_path_class);
1497 class MakeActiveOperator;
1498 class MakeInactiveOperator;
1499 class MakeChainInactiveOperator;
1500 class SwapActiveOperator;
1501 class ExtendedSwapActiveOperator;
1502 class MakeActiveAndRelocate;
1503 class RelocateAndMakeActiveOperator;
1504 class RelocateAndMakeInactiveOperator;
1543 const std::vector<LocalSearchFilter*>& filters,
1546 return "LocalSearchFilterManager";
1562 return synchronized_value_;
1568 std::vector<LocalSearchFilter*> filters_;
1569 IntVar*
const objective_;
1570 bool is_incremental_;
1571 int64 synchronized_value_;
1572 int64 accepted_value_;
1590 DCHECK(index !=
nullptr);
1591 const int var_index = var->
index();
1592 *index = (var_index < var_index_to_index_.size())
1593 ? var_index_to_index_[var_index]
1595 return *index != kUnassigned;
1603 void AddVars(
const std::vector<IntVar*>& vars);
1604 int Size()
const {
return vars_.size(); }
1608 return values_[index];
1617 return objective_callback_ !=
nullptr;
1620 if (objective_callback_ !=
nullptr) {
1621 objective_callback_(objective_value);
1627 std::vector<IntVar*> vars_;
1628 std::vector<int64> values_;
1629 std::vector<bool> var_synced_;
1630 std::vector<int> var_index_to_index_;
1631 static const int kUnassigned;
1641 std::string
DebugString()
const override {
return "PropagationMonitor"; }
1657 virtual void PushContext(
const std::string& context) = 0;
1660 virtual void SetMin(
IntExpr*
const expr, int64 new_min) = 0;
1661 virtual void SetMax(
IntExpr*
const expr, int64 new_max) = 0;
1662 virtual void SetRange(
IntExpr*
const expr, int64 new_min, int64 new_max) = 0;
1664 virtual void SetMin(
IntVar*
const var, int64 new_min) = 0;
1665 virtual void SetMax(
IntVar*
const var, int64 new_max) = 0;
1666 virtual void SetRange(
IntVar*
const var, int64 new_min, int64 new_max) = 0;
1671 const std::vector<int64>& values) = 0;
1673 const std::vector<int64>& values) = 0;
1694 const std::vector<int>& rank_first,
1695 const std::vector<int>& rank_last,
1696 const std::vector<int>& unperformed) = 0;
1708 std::string
DebugString()
const override {
return "LocalSearchMonitor"; }
1715 bool neighbor_found,
const Assignment* delta,
1719 bool neighbor_found) = 0;
1722 bool neighbor_found) = 0;
1742 void SetMin(int64 m)
override;
1744 void SetMax(int64 m)
override;
1745 void SetRange(int64 mi, int64 ma)
override;
1756 uint64
Size()
const override;
1757 bool Contains(int64 v)
const override;
1769 std::string
BaseName()
const override {
return "BooleanVar"; }
1781 class SymmetryManager;
1789 : symmetry_manager_(nullptr), index_in_symmetry_manager_(-1) {}
1799 void set_symmetry_manager_and_index(
SymmetryManager* manager,
int index) {
1800 CHECK(symmetry_manager_ ==
nullptr);
1801 CHECK_EQ(-1, index_in_symmetry_manager_);
1802 symmetry_manager_ = manager;
1803 index_in_symmetry_manager_ = index;
1805 SymmetryManager* symmetry_manager()
const {
return symmetry_manager_; }
1806 int index_in_symmetry_manager()
const {
return index_in_symmetry_manager_; }
1810 int index_in_symmetry_manager_;
1820 double scaling_factor,
1821 std::function<std::string()> display_callback,
int period);
1839 virtual void OutputLine(
const std::string& line);
1842 static std::string MemoryUsage();
1845 std::unique_ptr<WallTimer> timer_;
1848 const double scaling_factor_;
1849 std::function<std::string()> display_callback_;
1852 int64 objective_min_;
1853 int64 objective_max_;
1854 int min_right_depth_;
1856 int sliding_min_depth_;
1857 int sliding_max_depth_;
1964 virtual void Clear() = 0;
1978 IntVar*
const var, int64 value,
1984 IntVar*
const var, int64 value1, int64 value2,
2014 IntExpr*
const expr, int64 value,
2045 IntVar*
const var, int64 value1, int64 value2,
2049 IntExpr*
const expression,
IntVar*
const var, int64 value1, int64 value2,
2055 IntVar*
const var,
const std::vector<int64>& values,
2060 const std::vector<int64>& values,
2069 const std::vector<IntVar*>& vars,
2075 const std::vector<IntVar*>& vars,
const std::vector<int64>& values,
2079 IntExpr*
const expression,
const std::vector<IntVar*>& var,
2080 const std::vector<int64>& values,
2086 const std::vector<IntVar*>& vars, int64 value,
2090 IntExpr*
const expression,
const std::vector<IntVar*>& var, int64 value,
2104 const std::string&
TypeName()
const;
2110 const std::vector<int64>& values);
2112 const IntTupleSet& values);
2116 const std::vector<IntVar*>& vars);
2119 const std::vector<IntervalVar*>& vars);
2122 const std::vector<SequenceVar*>& vars);
2133 const std::string& arg_name)
const;
2135 const std::string& arg_name)
const;
2138 const std::string& arg_name)
const;
2140 const std::string& arg_name)
const;
2143 std::string type_name_;
2144 absl::flat_hash_map<std::string, int64> integer_argument_;
2145 absl::flat_hash_map<std::string, std::vector<int64> > integer_array_argument_;
2146 absl::flat_hash_map<std::string, IntTupleSet> matrix_argument_;
2147 absl::flat_hash_map<std::string, IntExpr*> integer_expression_argument_;
2148 absl::flat_hash_map<std::string, IntervalVar*> interval_argument_;
2149 absl::flat_hash_map<std::string, SequenceVar*> sequence_argument_;
2150 absl::flat_hash_map<std::string, std::vector<IntVar*> >
2151 integer_variable_array_argument_;
2152 absl::flat_hash_map<std::string, std::vector<IntervalVar*> >
2153 interval_array_argument_;
2154 absl::flat_hash_map<std::string, std::vector<SequenceVar*> >
2155 sequence_array_argument_;
2168 void EndVisitModel(
const std::string& solver_name)
override;
2170 const Constraint*
const constraint)
override;
2172 const Constraint*
const constraint)
override;
2174 const IntExpr*
const expr)
override;
2176 const IntExpr*
const expr)
override;
2178 IntExpr*
const delegate)
override;
2180 const std::string& operation, int64 value,
2181 IntVar*
const delegate)
override;
2183 const std::string& operation, int64 value,
2189 const std::vector<int64>& values)
override;
2191 const IntTupleSet& values)
override;
2194 IntExpr*
const argument)
override;
2196 const std::string& arg_name,
2197 const std::vector<IntVar*>& arguments)
override;
2202 const std::string& arg_name,
2203 const std::vector<IntervalVar*>& arguments)
override;
2208 const std::string& arg_name,
2209 const std::vector<SequenceVar*>& arguments)
override;
2217 std::vector<ArgumentHolder*> holders_;
2226 : index_min_(index_min),
2227 index_max_(index_max),
2228 values_(new T[index_max - index_min + 1]) {
2229 DCHECK_LE(index_min, index_max);
2235 DCHECK_GE(index, index_min_);
2236 DCHECK_LE(index, index_max_);
2237 return values_[index - index_min_];
2241 DCHECK_GE(index, index_min_);
2242 DCHECK_LE(index, index_max_);
2243 values_[index - index_min_] = value;
2246 std::string
DebugString()
const override {
return "ArrayWithOffset"; }
2249 const int64 index_min_;
2250 const int64 index_max_;
2251 std::unique_ptr<T[]> values_;
2259 template <
class T,
class C>
2263 : block_size_(block_size), block_offset_(0) {
2264 CHECK_GT(block_size, 0);
2268 for (
int i = 0; i < elements_.size(); ++i) {
2269 delete[] elements_[i];
2273 T
At(int64 index)
const {
2274 const int64 block_index = ComputeBlockIndex(index);
2275 const int64 relative_index = block_index - block_offset_;
2276 if (relative_index < 0 || relative_index >= elements_.size()) {
2279 const T* block = elements_[relative_index];
2280 return block !=
nullptr ? block[index - block_index * block_size_] : T();
2284 const int64 block_index = ComputeBlockIndex(index);
2285 T*
const block = GetOrCreateBlock(block_index);
2286 const int64 residual = index - block_index * block_size_;
2287 solver->SaveAndSetValue(reinterpret_cast<C*>(&block[residual]),
2288 reinterpret_cast<C>(value));
2292 T* NewBlock()
const {
2293 T*
const result =
new T[block_size_];
2294 for (
int i = 0; i < block_size_; ++i) {
2300 T* GetOrCreateBlock(
int block_index) {
2301 if (elements_.size() == 0) {
2302 block_offset_ = block_index;
2303 GrowUp(block_index);
2304 }
else if (block_index < block_offset_) {
2305 GrowDown(block_index);
2306 }
else if (block_index - block_offset_ >= elements_.size()) {
2307 GrowUp(block_index);
2309 T* block = elements_[block_index - block_offset_];
2310 if (block ==
nullptr) {
2312 elements_[block_index - block_offset_] = block;
2317 int64 ComputeBlockIndex(int64 value)
const {
2318 return value >= 0 ? value / block_size_
2319 : (value - block_size_ + 1) / block_size_;
2322 void GrowUp(int64 block_index) {
2323 elements_.resize(block_index - block_offset_ + 1);
2326 void GrowDown(int64 block_index) {
2327 const int64 delta = block_offset_ - block_index;
2328 block_offset_ = block_index;
2329 DCHECK_GT(delta, 0);
2330 elements_.insert(elements_.begin(), delta,
nullptr);
2333 const int64 block_size_;
2334 std::vector<T*> elements_;
2351 : elements_(new T[capacity]),
2353 capacity_(capacity),
2354 position_(new int[capacity]),
2355 delete_position_(true) {
2356 for (
int i = 0; i < capacity; ++i) {
2362 RevIntSet(
int capacity,
int* shared_positions,
int shared_positions_size)
2363 : elements_(new T[capacity]),
2365 capacity_(capacity),
2366 position_(shared_positions),
2367 delete_position_(false) {
2368 for (
int i = 0; i < shared_positions_size; ++i) {
2374 if (delete_position_) {
2385 DCHECK_LT(i, num_elements_.
Value());
2386 return elements_[i];
2391 DCHECK_LT(i + num_elements_.
Value(), capacity_);
2392 return elements_[i + num_elements_.
Value()];
2396 const int position = num_elements_.
Value();
2397 DCHECK_LT(position, capacity_);
2398 DCHECK(NotAlreadyInserted(elt));
2399 elements_[position] = elt;
2400 position_[elt] = position;
2406 SwapTo(value_index, num_elements_.
Value());
2410 SwapTo(value_index, num_elements_.
Value());
2423 bool NotAlreadyInserted(
const T& elt) {
2424 for (
int i = 0; i < num_elements_.
Value(); ++i) {
2425 if (elt == elements_[i]) {
2432 void SwapTo(T value_index,
int next_position) {
2433 const int current_position = position_[value_index];
2434 if (current_position != next_position) {
2435 const T next_value_index = elements_[next_position];
2436 elements_[current_position] = next_value_index;
2437 elements_[next_position] = value_index;
2438 position_[value_index] = next_position;
2439 position_[next_value_index] = current_position;
2444 std::unique_ptr<T[]> elements_;
2446 NumericalRev<int> num_elements_;
2448 const int capacity_;
2452 const bool delete_position_;
2462 last_ranked_(items.size() - 1),
2463 size_(items.size()),
2464 position_(new int[size_]) {
2465 for (
int i = 0; i < size_; ++i) {
2466 elements_[i] = items[i];
2474 last_ranked_(size - 1),
2476 position_(new int[size_]) {
2477 for (
int i = 0; i < size_; ++i) {
2493 DCHECK_GE(index, 0);
2494 DCHECK_LT(index, size_);
2495 return elements_[index];
2500 DCHECK_LE(first_ranked_.
Value(), last_ranked_.
Value());
2501 SwapTo(elt, first_ranked_.
Value());
2506 DCHECK_LE(first_ranked_.
Value(), last_ranked_.
Value());
2507 SwapTo(elt, last_ranked_.
Value());
2512 const int position = position_[elt];
2513 return (position < first_ranked_.
Value() ||
2514 position > last_ranked_.
Value());
2518 std::string result =
"[";
2519 for (
int i = 0; i < first_ranked_.
Value(); ++i) {
2520 absl::StrAppend(&result, elements_[i]);
2521 if (i != first_ranked_.
Value() - 1) {
2526 for (
int i = first_ranked_.
Value(); i <= last_ranked_.
Value(); ++i) {
2527 absl::StrAppend(&result, elements_[i]);
2528 if (i != last_ranked_.
Value()) {
2533 for (
int i = last_ranked_.
Value() + 1; i < size_; ++i) {
2534 absl::StrAppend(&result, elements_[i]);
2535 if (i != size_ - 1) {
2544 void SwapTo(
int elt,
int next_position) {
2545 const int current_position = position_[elt];
2546 if (current_position != next_position) {
2547 const int next_elt = elements_[next_position];
2548 elements_[current_position] = next_elt;
2549 elements_[next_position] = elt;
2550 position_[elt] = next_position;
2551 position_[next_elt] = current_position;
2556 std::vector<int> elements_;
2558 NumericalRev<int> first_ranked_;
2560 NumericalRev<int> last_ranked_;
2564 std::unique_ptr<int[]> position_;
2604 bool Intersects(
const std::vector<uint64>& mask,
int* support_index);
2616 const int64 bit_size_;
2617 const int64 word_size_;
2620 std::vector<int> to_remove_;
2629 for (
int i = 0; i < values.size(); ++i) {
2630 if (values[i] != value) {
2639 for (
int i = 0; i < values.size(); ++i) {
2640 if (values[i] != 0 && values[i] != 1) {
2659 for (
const T& current_value : values) {
2660 if (current_value < value) {
2669 for (
const T& current_value : values) {
2670 if (current_value > value) {
2699 for (
int i = 0; i < values.size() - 1; ++i) {
2700 if (values[i + 1] != values[i] + 1) {
2709 for (
int i = 0; i < values.size() - 1; ++i) {
2710 if (values[i + 1] < values[i]) {
2722 for (
int i = 0; i < vars.size(); ++i) {
2723 if (vars[i]->Min() < range_min || vars[i]->Max() > range_max) {
2731 for (
int i = 0; i < vars.size(); ++i) {
2732 if (!vars[i]->Bound()) {
2747 const std::vector<T>& values) {
2748 for (
int i = 0; i < vars.size(); ++i) {
2749 if (values[i] != 0 && !vars[i]->Bound()) {
2758 for (
int i = 0; i < vars.size(); ++i) {
2759 if (!vars[i]->Bound() || vars[i]->Min() != value) {
2767 DCHECK(!vars.empty());
2768 int64 result = kint64min;
2769 for (
int i = 0; i < vars.size(); ++i) {
2771 result = std::max<int64>(result, vars[i]->Max());
2777 DCHECK(!vars.empty());
2778 int64 result = kint64max;
2779 for (
int i = 0; i < vars.size(); ++i) {
2781 result = std::min<int64>(result, vars[i]->Min());
2787 std::vector<int64>*
const values) {
2789 values->resize(vars.size());
2790 for (
int i = 0; i < vars.size(); ++i) {
2791 (*values)[i] = vars[i]->Value();
2800 return e % v == 0 ? e / v : e / v + 1;
2811 return e % v == 0 ? e / v : e / v - 1;
2816 std::vector<int64>
ToInt64Vector(
const std::vector<int>& input);
2819 #endif // OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVERI_H_ Implements a complete cache for model elements: expressions and constraints.
std::vector< int64 > start_to_path_
IntVar * IsGreaterOrEqual(int64 constant) override
void SetForwardSequence(int64 index, const std::vector< int > &value)
void SetToZero(Solver *const solver, int64 row, int64 column)
Erases the 'column' bit in the 'row' row.
bool HasIntegerVariableArrayArgument(const std::string &arg_name) const
virtual void SetDurationMin(IntervalVar *const var, int64 new_min)=0
IntVarLocalSearchFilter(const std::vector< IntVar * > &vars, Solver::ObjectiveWatcher objective_callback)
virtual void InsertExprExprConstraint(Constraint *const ct, IntExpr *const expr1, IntExpr *const expr2, ExprExprConstraintType type)=0
Iterator(const SimpleRevFIFO< T > *l)
std::vector< int > assignment_indices_
void AddIntegerVariableEqualValueClause(IntVar *const var, int64 value)
int64 Prev(int64 node_index) const
bool AreAllBoundOrNull(const std::vector< IntVar * > &vars, const std::vector< T > &values)
Returns true if all the variables are assigned to a single value, or if their corresponding value is ...
std::vector< Val > old_values_
const std::vector< int > & ForwardSequence() const
void Switch(Solver *const solver)
virtual void BeginConstraintInitialPropagation(Constraint *const constraint)=0
Propagation events.
CallMethod1(T *const ct, void(T::*method)(P), const std::string &name, P param1)
bool IsRanked(int elt) const
Low-priority demon proxy to a method on the constraint with two arguments.
T RemovedElement(int i) const
virtual void SetPerformed(IntervalVar *const var, bool value)=0
bool SkipUnchanged(int index) const override
virtual void EndMakeNextNeighbor(const LocalSearchOperator *op, bool neighbor_found, const Assignment *delta, const Assignment *deltadelta)=0
VarConstantConstraintType
virtual void SetDurationRange(IntervalVar *const var, int64 new_min, int64 new_max)=0
bool ContainsKey(const K &key) const
Returns true if the multi-map contains at least one instance of 'key'.
void VisitSequenceVariable(const SequenceVar *const variable) override
std::string DebugString() const override
Matrix version of the RevBitSet class.
void VisitIntegerExpressionArgument(const std::string &arg_name, IntExpr *const argument) override
Variables.
bool Bound() const override
Returns true if the min and the max of the expression are equal.
void ClearAll(Solver *const solver)
Cleans all bits.
void BeginVisitModel(const std::string &solver_name) override
Header/footers.
virtual std::string name() const
Object naming.
bool IsArrayBoolean(const std::vector< T > &values)
std::string DebugString() const override
void AcceptUncheckedNeighbor() override
After accepting an unchecked neighbor during local search.
std::string BaseName() const override
Returns a base name for automatic naming.
virtual void RemoveInterval(IntVar *const var, int64 imin, int64 imax)=0
const Val & Value(int64 index) const
Returns the value in the current assignment of the variable of given index.
virtual bool HasFragments() const
virtual int64 ModifyValue(int64 index, int64 value)=0
CallMethod3(T *const ct, void(T::*method)(P, Q, R), const std::string &name, P param1, Q param2, R param3)
int NumLastRanked() const
virtual void RankLast(SequenceVar *const var, int index)=0
void WhenRange(Demon *d) override
Attach a demon that will watch the min or the max of the expression.
int64 Max() const override
bool MakeChainInactive(int64 before_chain, int64 chain_end)
void VisitSequenceArrayArgument(const std::string &arg_name, const std::vector< SequenceVar * > &arguments) override
CallMethod0(T *const ct, void(T::*method)(), const std::string &name)
VarConstantConstantConstraintType
int VarType() const override
virtual void EndProcessingIntegerVariable(IntVar *const var)=0
void VisitIntegerArrayArgument(const std::string &arg_name, const std::vector< int64 > &values) override
void SetIntegerMatrixArgument(const std::string &arg_name, const IntTupleSet &values)
virtual IntExpr * FindVarConstantArrayExpression(IntVar *const var, const std::vector< int64 > &values, VarConstantArrayExpressionType type) const =0
Var Constant Array Expressions.
bool AreAllStrictlyNegative(const std::vector< T > &values)
--— Base operator class for operators manipulating variables --—
std::string DebugString() const
virtual void RestoreValue()=0
RevGrowingArray(int64 block_size)
int64 FindIntegerArgumentWithDefault(const std::string &arg_name, int64 def) const
Getters.
~IntVarLocalSearchFilter() override
virtual IntExpr * FindVarConstantConstantExpression(IntVar *const var, int64 value1, int64 value2, VarConstantConstantExpressionType type) const =0
Var Constant Constant Expressions.
DELAYED_PRIORITY is the lowest priority: Demons will be processed after VAR_PRIORITY and NORMAL_PRIOR...
VarTypes
This enum is used internally to do dynamic typing on subclasses of integer variables.
virtual void SetStartRange(IntervalVar *const var, int64 new_min, int64 new_max)=0
bool Activated(int64 index) const
virtual Constraint * FindExprExprConstraint(IntExpr *const expr1, IntExpr *const expr2, ExprExprConstraintType type) const =0
Expr Expr Constraints.
virtual void InsertExprConstantExpression(IntExpr *const expression, IntExpr *const var, int64 value, ExprConstantExpressionType type)=0
--— IntVarLocalSearchFilter --—
void SetMax(int64 m) override
virtual void RemoveValue(IntVar *const var, int64 value)=0
void SetSequenceArgument(const std::string &arg_name, SequenceVar *const var)
ModelCache(Solver *const solver)
std::string DebugString() const override
~IntVarLocalSearchOperator() override
std::string DebugString() const override
virtual void InsertVarArrayExpression(IntExpr *const expression, const std::vector< IntVar * > &vars, VarArrayExpressionType type)=0
void SetToOne(Solver *const solver, int64 pos)
Sets the 'pos' bit.
virtual int64 GetSynchronizedObjectiveValue() const
DO NOT USE. Objective value from last time Synchronize() was called.
void AddVars(const std::vector< V * > &vars)
virtual void SetEndRange(IntervalVar *const var, int64 new_min, int64 new_max)=0
Demon proxy to a method on the constraint with no arguments.
void SetSequenceArrayArgument(const std::string &arg_name, const std::vector< SequenceVar * > &vars)
void AddToAssignment(SequenceVar *var, const std::vector< int > &value, bool active, std::vector< int > *assignment_indices, int64 index, Assignment *assignment) const
const int & operator[](int index) const
virtual void InsertVarConstantConstantExpression(IntExpr *const expression, IntVar *const var, int64 value1, int64 value2, VarConstantConstantExpressionType type)=0
void AddVars(const std::vector< IntVar * > &vars)
Add variables to "track" to the filter.
bool IsVarSynced(int index) const
RevBitMatrix(int64 rows, int64 columns)
SimpleRevFIFO< Demon * > bound_demons_
~DelayedCallMethod1() override
DemonPriority
This enum represents the three possible priorities for a demon in the Solver queue.
bool MakeActive(int64 node, int64 destination)
bool Intersects(const std::vector< uint64 > &mask, int *support_index)
This method returns true iff the mask and the active bitset have a non null intersection.
LocalSearchFilterManager(Solver *const solver, const std::vector< LocalSearchFilter * > &filters, IntVar *objective)
IntVarIterator * MakeDomainIterator(bool reversible) const override
Creates a domain iterator.
bool FindIndex(IntVar *const var, int64 *index) const
void WhenDomain(Demon *d) override
This method attaches a demon that will watch any domain modification of the domain of the variable.
virtual void Start(const Assignment *assignment)=0
~DelayedCallMethod0() override
void SetLastValue(const T &v)
Sets the last value in the FIFO.
void ClearAll(Solver *const solver)
Cleans all bits.
int64 injected_objective_value_
Demon * MakeDelayedConstraintDemon1(Solver *const s, T *const ct, void(T::*method)(P), const std::string &name, P param1)
-------— PropagationMonitor -------—
virtual bool NextFragment()=0
-------— LocalSearchMonitor -------—
void Synchronize(const Assignment *assignment, const Assignment *delta) override
Synchronizes all filters to assignment.
const T & LastValue() const
Returns the last value in the FIFO.
std::string DebugString() const override
virtual void SetStartMin(IntervalVar *const var, int64 new_min)=0
IntervalVar modifiers.
bool HasFragments() const override
-------— Objective Management -------—
~PropagationMonitor() override
void VisitIntervalArrayArgument(const std::string &arg_name, const std::vector< IntervalVar * > &arguments) override
void Deactivate(int64 index)
bool HoldsDelta() const override
const T * Last() const
Returns the last item of the FIFO.
VarArrayConstantArrayExpressionType
This is the base class for all expressions that are not variables.
void VisitIntegerVariableArrayArgument(const std::string &arg_name, const std::vector< IntVar * > &arguments) override
-------— Search Log ------—
SmallRevBitSet(int64 size)
virtual void InsertVarConstantConstantConstraint(Constraint *const ct, IntVar *const var, int64 value1, int64 value2, VarConstantConstantConstraintType type)=0
~LocalSearchMonitor() override
friend class SymmetryManager
Solver::DemonPriority priority() const override
This method returns the priority of the demon.
virtual const LocalSearchOperator * Self() const
int64 PosIntDivUp(int64 e, int64 v)
--— Arithmetic operations --—
Demon proxy to a method on the constraint with three arguments.
A Demon is the base element of a propagation queue.
virtual void StartProcessingIntegerVariable(IntVar *const var)=0
bool ValueFromAssignent(const Assignment &assignment, IntVar *var, int64 index, int64 *value)
int64 Cardinality() const
Returns the number of bits set to one.
Low-priority demon proxy to a method on the constraint with one argument.
void Run(Solver *const s) override
This is the main callback of the demon.
bool IsArrayInRange(const std::vector< IntVar * > &vars, T range_min, T range_max)
--— On integer variable vector --—
bool IsCardinalityZero() const
Is bitset null?
bool Accept(Assignment *delta, Assignment *deltadelta) override
Returns true iff all filters return true, and the sum of their accepted objectives is smaller or equa...
virtual void BeginDemonRun(Demon *const demon)=0
void Init(Solver *const solver, const std::vector< uint64 > &mask)
This methods overwrites the active bitset with the mask.
void SetToOne(Solver *const solver, int64 row, int64 column)
Sets the 'column' bit in the 'row' row.
const std::vector< int > & OldSequence(int64 index) const
int64 BaseNode(int i) const
Returns the index of the variable corresponding to the ith base node.
std::string DebugString() const override
PathOperator(const std::vector< IntVar * > &next_vars, const std::vector< IntVar * > &path_vars, int number_of_base_nodes, bool skip_locally_optimal_paths, std::function< int(int64)> start_empty_path_class)
Builds an instance of PathOperator from next and path variables.
virtual void InsertVarConstantConstraint(Constraint *const ct, IntVar *const var, int64 value, VarConstantConstraintType type)=0
uint64 Hash1(uint64 value)
-------— Reversible Hash Table -------—
virtual bool IsIncremental() const
int number_of_nexts() const
Number of next variables.
bool Contains(int64 v) const override
This method returns whether the value 'v' is in the domain of the variable.
virtual bool Accept(Assignment *delta, Assignment *deltadelta)=0
Accepts a "delta" given the assignment with which the filter has been synchronized; the delta holds t...
virtual Constraint * FindVoidConstraint(VoidConstraintType type) const =0
Void constraints.
~ArrayWithOffset() override
DelayedCallMethod1(T *const ct, void(T::*method)(P), const std::string &name, P param1)
IntContainer * MutableIntVarContainer()
const int number_of_nexts_
virtual bool MakeNextNeighbor(Assignment *delta, Assignment *deltadelta)=0
int ActiveWordSize() const
This method returns the number of non null 64 bit words in the bitset representation.
IntVar * IsDifferent(int64 constant) override
bool ApplyChanges(Assignment *delta, Assignment *deltadelta) const
void RankFirst(Solver *const solver, int elt)
--— Path-based Operators --—
virtual int64 GetBaseNodeRestartPosition(int base_index)
Returns the index of the node to which the base node of index base_index must be set to when it reach...
bool IsSet(int64 index) const
Returns whether the 'index' bit is set.
~VarLocalSearchOperator() override
void Start(const Assignment *assignment) override
This method should not be overridden.
virtual void InsertVarArrayConstantExpression(IntExpr *const expression, const std::vector< IntVar * > &var, int64 value, VarArrayConstantExpressionType type)=0
void Push(Solver *const s, T val)
std::string DebugString() const override
void ResetPosition()
Reset the position of the operator to its position when Start() was last called; this can be used to ...
const std::vector< int > & Sequence(int64 index) const
Returns the value in the current assignment of the variable of given index.
const std::vector< IntVar * > & FindIntegerVariableArrayArgumentOrDie(const std::string &arg_name) const
IntExpr * FindIntegerExpressionArgumentOrDie(const std::string &arg_name) const
bool IsInactive(int64 i) const
Returns true if node is inactive.
void SetTypeName(const std::string &type_name)
VarConstantArrayExpressionType
bool CanPropagateObjectiveValue() const
virtual IntExpr * FindVarArrayConstantExpression(const std::vector< IntVar * > &vars, int64 value, VarArrayConstantExpressionType type) const =0
Var Array Constant Expressions.
int64 StartNode(int i) const
Returns the index of the variable corresponding to the current path of the ith base node.
const Val & OldValue(int64 index) const
void MarkChange(int64 index)
OnStart() should really be protected, but then SWIG doesn't see it.
const bool ignore_path_vars_
virtual void RankFirst(SequenceVar *const var, int index)=0
SequenceVar modifiers.
void VisitIntervalArgument(const std::string &arg_name, IntervalVar *const argument) override
Visit interval argument.
bool IsCardinalityOne() const
Does it contains only one bit set?
Demon * MakeConstraintDemon0(Solver *const s, T *const ct, void(T::*method)(), const std::string &name)
std::vector< Val > values_
int64 GetFirstBit(int row, int start) const
Returns the first bit in the row 'row' which position is >= 'start'.
--— Utility Class for Callbacks --—
VarConstantConstantExpressionType
virtual void InsertExprExprExpression(IntExpr *const expression, IntExpr *const var1, IntExpr *const var2, ExprExprExpressionType type)=0
This iterator is not stable with respect to deletion.
void AddToAssignment(IntVar *var, int64 value, bool active, std::vector< int > *assignment_indices, int64 index, Assignment *assignment) const
bool AreAllBooleans(const std::vector< IntVar * > &vars)
virtual void BeginAcceptNeighbor(const LocalSearchOperator *op)=0
LocalSearchMonitor(Solver *const solver)
int64 Value(int index) const
void OnNodeInitialization() override
Called by OnStart() after initializing node information.
const_iterator end() const
virtual void SetMax(IntExpr *const expr, int64 new_max)=0
bool IsCardinalityOne() const
Does it contains only one bit set?
const IntTupleSet & FindIntegerMatrixArgumentOrDie(const std::string &arg_name) const
const IntContainer & IntVarContainer() const
int64 bit_size() const
Returns the number of bits given in the constructor of the bitset.
bool IsPathStart(int64 node_index) const
virtual void OnSynchronize(const Assignment *delta)
std::string DebugString() const override
RevPartialSequence(const std::vector< int > &items)
Argument Holder: useful when visiting a model.
virtual void EndConstraintInitialPropagation(Constraint *const constraint)=0
ExprConstantExpressionType
bool AreAllGreaterOrEqual(const std::vector< T > &values, const T &value)
virtual void RankSequence(SequenceVar *const var, const std::vector< int > &rank_first, const std::vector< int > &rank_last, const std::vector< int > &unperformed)=0
void SynchronizeOnAssignment(const Assignment *assignment)
RevPartialSequence(int size)
virtual void SetRange(IntExpr *const expr, int64 new_min, int64 new_max)=0
virtual bool MakeNeighbor()=0
virtual void PushContext(const std::string &context)=0
void SetIntegerExpressionArgument(const std::string &arg_name, IntExpr *const expr)
virtual void SetValues(IntVar *const var, const std::vector< int64 > &values)=0
virtual void InsertVarConstantArrayExpression(IntExpr *const expression, IntVar *const var, const std::vector< int64 > &values, VarConstantArrayExpressionType type)=0
virtual void BeginOperatorStart()=0
Local search operator events.
int64 Path(int64 node_index) const
Returns the index of the path to which the node of index node_index belongs in the current assignment...
void SetValue(Solver *const s, const T &val)
int next_base_to_increment_
virtual void BeginMakeNextNeighbor(const LocalSearchOperator *op)=0
void SetIntervalArgument(const std::string &arg_name, IntervalVar *const var)
BooleanVar(Solver *const s, const std::string &name="")
void RankLast(Solver *const solver, int elt)
~DelayedCallMethod2() override
const std::vector< int64 > & FindIntegerArrayArgumentOrDie(const std::string &arg_name) const
void BeginInitialPropagation() override
Before the initial propagation.
void SetNext(int64 from, int64 to, int64 path)
Sets the to to be the node after from.
void ApplyDecision(Decision *const decision) override
Before applying the decision.
PathWithPreviousNodesOperator(const std::vector< IntVar * > &vars, const std::vector< IntVar * > &secondary_vars, int number_of_base_nodes, std::function< int(int64)> start_empty_path_class)
void SetToOne(Solver *const solver, int64 index)
Sets the 'index' bit.
bool IsSet(int64 row, int64 column) const
Returns whether the 'column' bit in the 'row' row is set.
virtual void SetStartMax(IntervalVar *const var, int64 new_max)=0
void AddIntegerVariableGreaterOrEqualValueClause(IntVar *const var, int64 value)
bool IsIncremental() const override
ArrayWithOffset(int64 index_min, int64 index_max)
virtual Constraint * FindVarConstantConstraint(IntVar *const var, int64 value, VarConstantConstraintType type) const =0
Var Constant Constraints.
virtual void BeginFiltering(const LocalSearchFilter *filter)=0
Specialization of LocalSearchOperator built from an array of IntVars which specifies the scope of the...
SequenceVarLocalSearchHandler()
VarLocalSearchOperator< SequenceVar, std::vector< int >, SequenceVarLocalSearchHandler > SequenceVarLocalSearchOperatorTemplate
const V & FindWithDefault(const K &key, const V &default_value) const
Returns one value attached to 'key', or 'default_value' if 'key' is not in the multi-map.
virtual IntExpr * FindExprExpression(IntExpr *const expr, ExprExpressionType type) const =0
Expr Expressions.
Simple PathOperator wrapper that also stores the current previous nodes, and is thus able to provide ...
~UnsortedNullableRevBitset()
void EndVisitModel(const std::string &solver_name) override
bool AreAllStrictlyPositive(const std::vector< T > &values)
std::string ParameterDebugString(P param)
PropagationMonitor(Solver *const solver)
bool MoveChain(int64 before_chain, int64 chain_end, int64 destination)
Moves the chain starting after the node before_chain and ending at the node chain_end after the node ...
virtual void OnStart()
Called by Start() after synchronizing the operator with the current assignment.
virtual void EndNestedConstraintInitialPropagation(Constraint *const parent, Constraint *const nested)=0
IntVarLocalSearchOperator()
void ExitSearch() override
End of the search.
void VisitSequenceArgument(const std::string &arg_name, SequenceVar *const argument) override
Visit sequence argument.
bool HasIntegerExpressionArgument(const std::string &arg_name) const
Checks if arguments exist.
IntVarLocalSearchOperator(const std::vector< IntVar * > &vars)
bool IsIncreasingContiguous(const std::vector< T > &values)
void Clear(Solver *const solver)
void RemoveInterval(int64 l, int64 u) override
This method removes the interval 'l' .
--— Local Search Filters ---—
void EndVisitIntegerExpression(const std::string &type_name, const IntExpr *const expr) override
IntVar * Var(int index) const
bool ValueFromAssignent(const Assignment &assignment, SequenceVar *var, int64 index, std::vector< int > *value)
virtual IntExpr * FindVarArrayConstantArrayExpression(const std::vector< IntVar * > &vars, const std::vector< int64 > &values, VarArrayConstantArrayExpressionType type) const =0
Var Array Constant Array Expressions.
uint64 Size() const override
This method returns the number of values in the domain of the variable.
IntVar * IsEqual(int64 constant) override
IsEqual.
int64 MinVarArray(const std::vector< IntVar * > &vars)
static const int kUnboundBooleanVarValue
A constraint is the main modeling object.
E * MutableElement(const V *const var)
SequenceVarLocalSearchOperator()
virtual void EndFilterNeighbor(const LocalSearchOperator *op, bool neighbor_found)=0
SequenceVarLocalSearchHandler(SequenceVarLocalSearchOperator *op)
void Run(Solver *const s) override
This is the main callback of the demon.
virtual IntVar * CastToVar()
bool IsArrayConstant(const std::vector< T > &values, const T &value)
-------— Helpers -------—
std::vector< int64 > ToInt64Vector(const std::vector< int > &input)
--— Vector of integer manipulations --—
void Run(Solver *const s) override
This is the main callback of the demon.
bool AreAllOnes(const std::vector< T > &values)
const E & Element(const V *const var) const
virtual void EndAcceptNeighbor(const LocalSearchOperator *op, bool neighbor_found)=0
This class represents a small reversible bitset (size <= 64).
virtual void InsertVarArrayConstantArrayExpression(IntExpr *const expression, const std::vector< IntVar * > &var, const std::vector< int64 > &values, VarArrayConstantArrayExpressionType type)=0
void Run(Solver *const s) override
This is the main callback of the demon.
~PathWithPreviousNodesOperator() override
SequenceContainer * MutableSequenceVarContainer()
const SequenceContainer & SequenceVarContainer() const
DelayedCallMethod0(T *const ct, void(T::*method)(), const std::string &name)
bool IsPathEnd(int64 i) const
Returns true if i is the last node on the path; defined by the fact that i outside the range of the v...
Demon proxy to a method on the constraint with two arguments.
void SetValue(int64 index, T value)
bool AreAllNegative(const std::vector< T > &values)
void PropagateObjectiveValue(int64 objective_value)
Filter manager: when a move is made, filters are executed to decide whether the solution is feasible ...
Low-priority demon proxy to a method on the constraint with no arguments.
void SetBackwardSequence(int64 index, const std::vector< int > &value)
A DecisionVisitor is used to inspect a decision.
bool IsCardinalityZero() const
Is bitset null?
IntVarElement * FastAdd(IntVar *const var)
Adds without checking if variable has been previously added.
void SetIntegerVariableArrayArgument(const std::string &arg_name, const std::vector< IntVar * > &vars)
VarLocalSearchOperator(std::vector< V * > vars, Handler var_handler)
SearchLog(Solver *const s, OptimizeVar *const obj, IntVar *const var, double scaling_factor, std::function< std::string()> display_callback, int period)
IntVar * Var() override
Creates a variable from the expression.
int64 GetFirstOne() const
Gets the index of the first bit set starting from 0.
virtual void EndDemonRun(Demon *const demon)=0
virtual bool SkipUnchanged(int index) const
void PushArgumentHolder()
void BeginFail() override
Just when the failure occurs.
SequenceVarLocalSearchOperator(const std::vector< SequenceVar * > &vars)
int64 OldPath(int64 node_index) const
VarArrayConstantExpressionType
bool AtSolution() override
This method is called when a valid solution is found.
void VisitIntegerArgument(const std::string &arg_name, int64 value) override
Integer arguments.
virtual void SetNextBaseToIncrement(int64 base_index)
Set the next base to increment on next iteration.
void Run(Solver *const s) override
This is the main callback of the demon.
virtual void EndOperatorStart()=0
SparseBitset delta_changes_
void Install() override
Install itself on the solver.
std::string DebugString() const override
This class is a reversible growing array.
void RemoveValue(int64 v) override
This method removes the value 'v' from the domain of the variable.
virtual void BeginNestedConstraintInitialPropagation(Constraint *const parent, Constraint *const nested)=0
bool AreAllBound(const std::vector< IntVar * > &vars)
void RevertChanges(bool incremental)
void BeginVisitIntegerExpression(const std::string &type_name, const IntExpr *const expr) override
BaseLns(const std::vector< IntVar * > &vars)
SequenceVar * Var() const
~LocalSearchOperator() override
void SetValue(int64 index, const Val &value)
std::vector< Val > prev_values_
ChangeValue(const std::vector< IntVar * > &vars)
virtual void OutputLine(const std::string &line)
std::string DebugString() const override
int64 OldNext(int64 node_index) const
V * Var(int64 index) const
Returns the variable of given index.
int64 FindIntegerArgumentOrDie(const std::string &arg_name) const
--— ChangeValue Operators --—
void Run(Solver *const s) override
This is the main callback of the demon.
int index() const
Returns the index of the variable.
void SetRange(int64 mi, int64 ma) override
This method sets both the min and the max of the expression.
IntVarIterator * MakeHoleIterator(bool reversible) const override
Creates a hole iterator.
virtual bool RestartAtPathStartOnSynchronize()
When the operator is being synchronized with a new solution (when Start() is called),...
virtual void EndFiltering(const LocalSearchFilter *filter, bool reject)=0
void RevInsert(Solver *const solver, int64 index, T value)
int64 Value() const override
This method returns the value of the variable.
SequenceVarLocalSearchHandler(const SequenceVarLocalSearchHandler &other)
const T * const_iterator
Iterators on the indices.
const RevIntSet< int > & active_words() const
Returns the set of active word indices.
virtual void BeginFilterNeighbor(const LocalSearchOperator *op)=0
virtual void InsertExprExpression(IntExpr *const expression, IntExpr *const expr, ExprExpressionType type)=0
RevIntSet(int capacity)
Capacity is the fixed size of the set (it cannot grow).
int64 Cardinality() const
Returns the number of bits set to one.
bool RevAnd(Solver *const solver, const std::vector< uint64 > &mask)
This method ANDs the mask with the active bitset.
~SymmetryBreaker() override
Useful Search and Modeling Objects.
static const int kNoInserted
T * RevAlloc(T *object)
Registers the given object as being reversible.
The class IntExpr is the base of all integer expressions in constraint programming.
bool Empty() const
This method returns true if the active bitset is null.
ArgumentHolder * Top() const
A reversible switch that can switch once from false to true.
A search monitor is a simple set of callbacks to monitor all search events.
RevImmutableMultiMap(Solver *const solver, int initial_size)
int64 size() const
Returns the number of interval vars in the sequence.
virtual T Evaluate(int64 index) const
virtual IntExpr * FindExprConstantExpression(IntExpr *const expr, int64 value, ExprConstantExpressionType type) const =0
Expr Constant Expressions.
RevIntSet(int capacity, int *shared_positions, int shared_positions_size)
Capacity is the fixed size of the set (it cannot grow).
int64 word_size() const
Returns the number of 64 bit words used to store the bitset.
std::function< void(int64)> ObjectiveWatcher
The class IntVar is a subset of IntExpr.
virtual bool IsIncremental() const
--— RevPartialSequence --—
A symmetry breaker is an object that will visit a decision and create the 'symmetrical' decision in r...
void WhenBound(Demon *d) override
This method attaches a demon that will be awakened when the variable is bound.
virtual void RemoveValues(IntVar *const var, const std::vector< int64 > &values)=0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in c...
void Insert(Solver *const solver, const T &elt)
The class Iterator has two direct subclasses.
virtual void RankNotFirst(SequenceVar *const var, int index)=0
virtual void SetDurationMax(IntervalVar *const var, int64 new_max)=0
Solver::DemonPriority priority() const override
This method returns the priority of the demon.
bool MakeOneNeighbor() override
This method should not be overridden. Override ModifyValue() instead.
~SequenceVarLocalSearchOperator() override
void BeginVisitConstraint(const std::string &type_name, const Constraint *const constraint) override
std::string DebugString() const override
void SetBackwardSequence(const std::vector< int > &backward_sequence)
bool AreAllNull(const std::vector< T > &values)
void SetMin(int64 m) override
virtual IntExpr * FindExprExprConstantExpression(IntExpr *const var1, IntExpr *const var2, int64 constant, ExprExprConstantExpressionType type) const =0
Expr Expr Constant Expressions.
void EndInitialPropagation() override
After the initial propagation.
Solver * solver() const
Returns the underlying constraint solver.
void VisitIntegerVariable(const IntVar *const variable, IntExpr *const delegate) override
virtual bool InitPosition() const
Returns true if operator needs to restart its initial position at each call to Start()
void SetToZero(Solver *const solver, int64 pos)
Erases the 'pos' bit.
int64 GetAcceptedObjectiveValue() const override
DO NOT USE.
void Incr(Solver *const s)
void SetForwardSequence(const std::vector< int > &forward_sequence)
std::string DebugString() const override
Demon proxy to a method on the constraint with one argument.
bool AreAllPositive(const std::vector< T > &values)
int64 MaxVarArray(const std::vector< IntVar * > &vars)
virtual Constraint * FindVarConstantConstantConstraint(IntVar *const var, int64 value1, int64 value2, VarConstantConstantConstraintType type) const =0
Var Constant Constant Constraints.
void OnRevertChanges(int64 index)
void RefuteDecision(Decision *const decision) override
Before refuting the decision.
void EndVisitConstraint(const std::string &type_name, const Constraint *const constraint) override
Demon * MakeDelayedConstraintDemon0(Solver *const s, T *const ct, void(T::*method)(), const std::string &name)
std::string DebugString() const override
int NumFirstRanked() const
void SetToZero(Solver *const solver, int64 index)
Erases the 'index' bit.
CallMethod2(T *const ct, void(T::*method)(P, Q), const std::string &name, P param1, Q param2)
virtual void RankNotLast(SequenceVar *const var, int index)=0
UnsortedNullableRevBitset(int bit_size)
Size is the number of bits to store in the bitset.
virtual int64 GetAcceptedObjectiveValue() const
DO NOT USE.
virtual void SetMin(IntExpr *const expr, int64 new_min)=0
IntExpr modifiers.
This class represents a reversible bitset.
void NoMoreSolutions() override
When the search tree is finished.
virtual void SetValue(IntVar *const var, int64 value)=0
void Install() override
Install itself on the solver.
A Decision represents a choice point in the search tree.
DelayedCallMethod2(T *const ct, void(T::*method)(P, Q), const std::string &name, P param1, Q param2)
Demon * MakeDelayedConstraintDemon2(Solver *const s, T *const ct, void(T::*method)(P, Q), const std::string &name, P param1, Q param2)
bool Contains(const V *const var) const
--— Base operator class for operators manipulating IntVars --—
-------— Interval Var -------—
const std::vector< int64 > & path_starts() const
Returns the vector of path start nodes.
void Insert(const K &key, const V &value)
Inserts (key, value) in the multi-map.
ExprExprConstantExpressionType
Demon * MakeConstraintDemon2(Solver *const s, T *const ct, void(T::*method)(P, Q), const std::string &name, P param1, Q param2)
int PathClass(int i) const
Returns the class of the current path of the ith base node.
SimpleRevFIFO< Demon * > delayed_bound_demons_
std::vector< std::vector< int > > backward_values_
bool AreAllBoundTo(const std::vector< IntVar * > &vars, int64 value)
Returns true if all variables are assigned to 'value'.
std::string DebugString() const override
int64 PosIntDivDown(int64 e, int64 v)
void Decr(Solver *const s)
bool MakeOneNeighbor() override
This method should not be overridden. Override MakeNeighbor() instead.
void Activate(int64 index)
virtual void SetEndMin(IntervalVar *const var, int64 new_min)=0
void SaveAndSetValue(T *adr, T val)
All-in-one SaveAndSetValue.
int64 GetSynchronizedObjectiveValue() const override
DO NOT USE. Objective value from last time Synchronize() was called.
virtual void InsertExprExprConstantExpression(IntExpr *const expression, IntExpr *const var1, IntExpr *const var2, int64 constant, ExprExprConstantExpressionType type)=0
Solver::DemonPriority priority() const override
This method returns the priority of the demon.
This class represents a reversible bitset.
--— Base Large Neighborhood Search operator class -—
virtual bool OnSamePathAsPreviousBase(int64 base_index)
Returns true if a base node has to be on the same path as the "previous" base node (base node of inde...
virtual void RegisterDemon(Demon *const demon)=0
int64 Min() const override
virtual void PopContext()=0
virtual void InsertVoidConstraint(Constraint *const ct, VoidConstraintType type)=0
virtual IntExpr * FindExprExprExpression(IntExpr *const var1, IntExpr *const var2, ExprExprExpressionType type) const =0
Expr Expr Expressions.
virtual void InitFragments()
void Synchronize(const Assignment *assignment, const Assignment *delta) override
This method should not be overridden.
bool ReverseChain(int64 before_chain, int64 after_chain, int64 *chain_last)
Reverses the chain starting after before_chain and ending before after_chain.
void EnterSearch() override
Beginning of the search.
BaseIntExpr(Solver *const s)
bool RevSubtract(Solver *const solver, const std::vector< uint64 > &mask)
This method subtracts the mask from the active bitset.
void AppendToFragment(int index)
virtual IntExpr * FindVarArrayExpression(const std::vector< IntVar * > &vars, VarArrayExpressionType type) const =0
Var Array Expressions.
void VisitIntervalVariable(const IntervalVar *const variable, const std::string &operation, int64 value, IntervalVar *const delegate) override
void VisitIntegerMatrixArgument(const std::string &arg_name, const IntTupleSet &values) override
int64 Next(int64 node_index) const
Returns the index of the node after the node of index node_index in the current assignment.
--— SequenceVarElement --—
bool AreAllLessOrEqual(const std::vector< T > &values, const T &value)
Demon * MakeConstraintDemon1(Solver *const s, T *const ct, void(T::*method)(P), const std::string &name, P param1)
void SetIntegerArgument(const std::string &arg_name, int64 value)
Setters.
void PushIfNotTop(Solver *const s, T val)
Pushes the var on top if is not a duplicate of the current top object.
void SetIntegerArrayArgument(const std::string &arg_name, const std::vector< int64 > &values)
void Remove(Solver *const solver, const T &value_index)
int64 Size() const
Returns the number of next variables in the model.
-------— Local search operators -------—
void AddToAssignment(IntVar *const var)
Adds an extra variable to the vehicle routing assignment.
void AddIntegerVariableLessOrEqualValueClause(IntVar *const var, int64 value)
virtual void InjectObjectiveValue(int64 objective_value)
--— Immutable Multi Map --—
bool MakeOneNeighbor() override
This method should not be overridden. Override NextFragment() instead.
const_iterator begin() const
void SetIntervalArrayArgument(const std::string &arg_name, const std::vector< IntervalVar * > &vars)
int64 GetFirstBit(int start) const
Gets the index of the first bit set starting from start.
Demon * MakeConstraintDemon3(Solver *const s, T *const ct, void(T::*method)(P, Q, R), const std::string &name, P param1, Q param2, R param3)
IntVar * IsLessOrEqual(int64 constant) override
void Run(Solver *const s) override
This is the main callback of the demon.
virtual void SetEndMax(IntervalVar *const var, int64 new_max)=0
void OnRevertChanges(int64 index)
void Restore(Solver *const solver, const T &value_index)
LocalSearchOperator * MakeLocalSearchOperator(Solver *solver, const std::vector< IntVar * > &vars, const std::vector< IntVar * > &secondary_vars, std::function< int(int64)> start_empty_path_class)
--— Operator Factories ---—
void FillValues(const std::vector< IntVar * > &vars, std::vector< int64 > *const values)
virtual bool HoldsDelta() const
bool IsIncreasing(const std::vector< T > &values)
const std::string & TypeName() const
Type of the argument.
virtual void Synchronize(const Assignment *assignment, const Assignment *delta)=0
Synchronizes the filter with the current solution, delta being the difference with the solution passe...