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;
145 class SimpleRevFIFO {
147 enum { CHUNK_SIZE = 16 };
150 const Chunk*
const next_;
151 explicit Chunk(
const Chunk* next) : next_(next) {}
159 : chunk_(l->chunks_), value_(l->
Last()) {}
160 bool ok()
const {
return (value_ !=
nullptr); }
164 if (value_ == chunk_->data_ + CHUNK_SIZE) {
165 chunk_ = chunk_->next_;
166 value_ = chunk_ ? chunk_->data_ :
nullptr;
178 if (pos_.
Value() == 0) {
179 Chunk*
const chunk = s->UnsafeRevAlloc(
new Chunk(chunks_));
181 reinterpret_cast<void*>(chunk));
186 chunks_->data_[pos_.
Value()] = val;
191 if (chunks_ ==
nullptr ||
LastValue() != val) {
198 return chunks_ ? &chunks_->data_[pos_.
Value()] :
nullptr;
206 return chunks_->data_[pos_.
Value()];
212 chunks_->data_[pos_.
Value()] = v;
223 value = (~value) + (value << 21);
224 value ^= value >> 24;
225 value += (value << 3) + (value << 8);
226 value ^= value >> 14;
227 value += (value << 2) + (value << 4);
228 value ^= value >> 28;
229 value += (value << 31);
235 a = (a + 0x7ed55d16) + (a << 12);
236 a = (a ^ 0xc761c23c) ^ (a >> 19);
237 a = (a + 0x165667b1) + (a << 5);
238 a = (a + 0xd3a2646c) ^ (a << 9);
239 a = (a + 0xfd7046c5) + (a << 3);
240 a = (a ^ 0xb55a4f09) ^ (a >> 16);
244 inline uint64
Hash1(int64 value) {
return Hash1(static_cast<uint64>(value)); }
246 inline uint64
Hash1(
int value) {
return Hash1(static_cast<uint32>(value)); }
248 inline uint64
Hash1(
void*
const ptr) {
249 #if defined(ARCH_K8) || defined(__powerpc64__) || defined(__aarch64__) 250 return Hash1(reinterpret_cast<uint64>(ptr));
252 return Hash1(reinterpret_cast<uint32>(ptr));
257 uint64
Hash1(
const std::vector<T*>& ptrs) {
260 }
else if (ptrs.size() == 1) {
261 return Hash1(ptrs[0]);
263 uint64 hash =
Hash1(ptrs[0]);
264 for (
int i = 1; i < ptrs.size(); ++i) {
265 hash = hash * i +
Hash1(ptrs[i]);
271 inline uint64
Hash1(
const std::vector<int64>& ptrs) {
274 }
else if (ptrs.size() == 1) {
275 return Hash1(ptrs[0]);
277 uint64 hash =
Hash1(ptrs[0]);
278 for (
int i = 1; i < ptrs.size(); ++i) {
279 hash = hash * i +
Hash1(ptrs[i]);
287 template <
class K,
class V>
292 array_(solver->UnsafeRevAllocArray(new Cell*[initial_size])),
295 memset(array_, 0,
sizeof(*array_) * size_.
Value());
305 Cell* tmp = array_[code];
307 if (tmp->key() == key) {
320 Cell* tmp = array_[code];
322 if (tmp->key() == key) {
327 return default_value;
331 void Insert(
const K& key,
const V& value) {
332 const int position =
Hash1(key) % size_.
Value();
334 solver_->UnsafeRevAlloc(
new Cell(key, value, array_[position]));
336 reinterpret_cast<void*>(cell));
337 num_items_.
Incr(solver_);
346 Cell(
const K& key,
const V& value, Cell*
const next)
347 : key_(key), value_(value), next_(next) {}
349 void SetRevNext(Solver*
const solver, Cell*
const next) {
350 solver->SaveAndSetValue(reinterpret_cast<void**>(&next_),
351 reinterpret_cast<void*>(next));
354 Cell* next()
const {
return next_; }
356 const K& key()
const {
return key_; }
358 const V& value()
const {
return value_; }
367 Cell**
const old_cell_array = array_;
368 const int old_size = size_.
Value();
371 reinterpret_cast<void**>(&array_),
372 reinterpret_cast<void*>(
373 solver_->UnsafeRevAllocArray(
new Cell*[size_.
Value()])));
374 memset(array_, 0, size_.
Value() *
sizeof(*array_));
375 for (
int i = 0; i < old_size; ++i) {
376 Cell* tmp = old_cell_array[i];
377 while (tmp !=
nullptr) {
378 Cell*
const to_reinsert = tmp;
380 const uint64 new_position =
Hash1(to_reinsert->key()) % size_.
Value();
381 to_reinsert->SetRevNext(solver_, array_[new_position]);
383 reinterpret_cast<void**>(&array_[new_position]),
384 reinterpret_cast<void*>(to_reinsert));
389 Solver*
const solver_;
391 NumericalRev<int> size_;
392 NumericalRev<int> num_items_;
445 bool IsSet(int64 index)
const;
462 void Save(
Solver*
const solver,
int offset);
480 bool IsSet(int64 row, int64 column)
const {
482 DCHECK_LT(row, rows_);
483 DCHECK_GE(column, 0);
484 DCHECK_LT(column, columns_);
501 const int64 columns_;
514 CallMethod0(T*
const ct,
void (T::*method)(),
const std::string& name)
515 : constraint_(ct), method_(method), name_(name) {}
519 void Run(
Solver*
const s)
override { (constraint_->*method_)(); }
522 return "CallMethod_" + name_ +
"(" + constraint_->DebugString() +
")";
526 T*
const constraint_;
527 void (T::*
const method_)();
528 const std::string name_;
533 const std::string& name) {
539 return absl::StrCat(param);
545 return param->DebugString();
549 template <
class T,
class P>
552 CallMethod1(T*
const ct,
void (T::*method)(P),
const std::string& name,
554 : constraint_(ct), method_(method), name_(name), param1_(param1) {}
558 void Run(
Solver*
const s)
override { (constraint_->*method_)(param1_); }
561 return absl::StrCat(
"CallMethod_", name_,
"(", constraint_->DebugString(),
566 T*
const constraint_;
567 void (T::*
const method_)(P);
568 const std::string name_;
572 template <
class T,
class P>
574 const std::string& name, P param1) {
579 template <
class T,
class P,
class Q>
582 CallMethod2(T*
const ct,
void (T::*method)(P, Q),
const std::string& name,
593 (constraint_->*method_)(param1_, param2_);
597 return absl::StrCat(absl::StrCat(
"CallMethod_", name_),
598 absl::StrCat(
"(", constraint_->DebugString()),
604 T*
const constraint_;
605 void (T::*
const method_)(P, Q);
606 const std::string name_;
611 template <
class T,
class P,
class Q>
613 void (T::*method)(P, Q),
const std::string& name,
614 P param1, Q param2) {
619 template <
class T,
class P,
class Q,
class R>
622 CallMethod3(T*
const ct,
void (T::*method)(P, Q, R),
const std::string& name,
623 P param1, Q param2, R param3)
634 (constraint_->*method_)(param1_, param2_, param3_);
638 return absl::StrCat(absl::StrCat(
"CallMethod_", name_),
639 absl::StrCat(
"(", constraint_->DebugString()),
646 T*
const constraint_;
647 void (T::*
const method_)(P, Q, R);
648 const std::string name_;
654 template <
class T,
class P,
class Q,
class R>
656 void (T::*method)(P, Q, R),
const std::string& name,
657 P param1, Q param2, R param3) {
673 : constraint_(ct), method_(method), name_(name) {}
677 void Run(
Solver*
const s)
override { (constraint_->*method_)(); }
684 return "DelayedCallMethod_" + name_ +
"(" + constraint_->DebugString() +
689 T*
const constraint_;
690 void (T::*
const method_)();
691 const std::string name_;
697 const std::string& name) {
702 template <
class T,
class P>
707 : constraint_(ct), method_(method), name_(name), param1_(param1) {}
711 void Run(
Solver*
const s)
override { (constraint_->*method_)(param1_); }
718 return absl::StrCat(
"DelayedCallMethod_", name_,
"(",
719 constraint_->DebugString(),
", ",
724 T*
const constraint_;
725 void (T::*
const method_)(P);
726 const std::string name_;
730 template <
class T,
class P>
732 void (T::*method)(P),
733 const std::string& name, P param1) {
738 template <
class T,
class P,
class Q>
742 const std::string& name, P param1, Q param2)
752 (constraint_->*method_)(param1_, param2_);
760 return absl::StrCat(absl::StrCat(
"DelayedCallMethod_", name_),
761 absl::StrCat(
"(", constraint_->DebugString()),
767 T*
const constraint_;
768 void (T::*
const method_)(P, Q);
769 const std::string name_;
774 template <
class T,
class P,
class Q>
776 void (T::*method)(P, Q),
777 const std::string& name, P param1,
784 #endif // !defined(SWIG) 820 template <
class V,
class Val,
class Handler>
834 const int size =
Size();
835 CHECK_LE(size, assignment->
Size())
836 <<
"Assignment contains fewer variables than operator";
837 for (
int i = 0; i < size; ++i) {
850 const Val&
Value(int64 index)
const {
851 DCHECK_LT(index,
vars_.size());
873 for (
const int64 index :
delta_changes_.PositionsSetAtLeastOnce()) {
875 const Val& value =
Value(index);
877 var_handler_.AddToAssignment(var, value, activated,
nullptr, index,
884 for (
const int64 index :
changes_.PositionsSetAtLeastOnce()) {
885 const Val& value =
Value(index);
900 for (
const int64 index :
changes_.PositionsSetAtLeastOnce()) {
910 vars_.insert(
vars_.end(), vars.begin(), vars.end());
911 const int64 size =
Size();
954 std::vector<int>* assignment_indices, int64 index,
959 if (assignment_indices !=
nullptr) {
960 if ((*assignment_indices)[index] == -1) {
961 (*assignment_indices)[index] = container->
Size();
962 element = assignment->
FastAdd(var);
964 element = container->
MutableElement((*assignment_indices)[index]);
967 element = assignment->
FastAdd(var);
977 int64 index, int64* value) {
980 if (element->
Var() != var) {
982 <<
"Assignment does not contain operator variable " << var;
983 element = &(container.
Element(var));
985 *value = element->
Value();
1006 #if defined(SWIGPYTHON) 1007 %unignore VarLocalSearchOperator<IntVar, int64,
1008 IntVarLocalSearchHandler>::Size;
1009 %unignore VarLocalSearchOperator<IntVar, int64,
1010 IntVarLocalSearchHandler>::Value;
1011 %unignore VarLocalSearchOperator<IntVar, int64,
1012 IntVarLocalSearchHandler>::OldValue;
1013 %unignore VarLocalSearchOperator<IntVar, int64,
1014 IntVarLocalSearchHandler>::SetValue;
1015 %feature(
"director") VarLocalSearchOperator<IntVar, int64,
1016 IntVarLocalSearchHandler>::IsIncremental;
1017 %feature("director") VarLocalSearchOperator<IntVar, int64,
1018 IntVarLocalSearchHandler>::OnStart;
1019 %unignore VarLocalSearchOperator<IntVar, int64,
1020 IntVarLocalSearchHandler>::IsIncremental;
1021 %unignore VarLocalSearchOperator<IntVar, int64,
1022 IntVarLocalSearchHandler>::OnStart;
1023 #endif // SWIGPYTHON 1026 %rename(IntVarLocalSearchOperatorTemplate)
1027 VarLocalSearchOperator<IntVar, int64, IntVarLocalSearchHandler>;
1028 %
template(IntVarLocalSearchOperatorTemplate)
1029 VarLocalSearchOperator<IntVar, int64, IntVarLocalSearchHandler>;
1056 virtual bool MakeOneNeighbor();
1060 class SequenceVarLocalSearchOperator;
1069 void AddToAssignment(
SequenceVar* var,
const std::vector<int>& value,
1070 bool active, std::vector<int>* assignment_indices,
1073 int64 index, std::vector<int>* value);
1074 void OnRevertChanges(int64 index);
1089 SequenceVar, std::vector<int>, SequenceVarLocalSearchHandler>;
1091 SequenceVar, std::vector<int>, SequenceVarLocalSearchHandler>;
1095 typedef VarLocalSearchOperator<SequenceVar, std::vector<int>,
1096 SequenceVarLocalSearchHandler>
1130 SequenceVar* var,
const std::vector<int>& value,
bool active,
1131 std::vector<int>* assignment_indices, int64 index,
1136 if (assignment_indices !=
nullptr) {
1137 if ((*assignment_indices)[index] == -1) {
1138 (*assignment_indices)[index] = container->
Size();
1139 element = assignment->
FastAdd(var);
1141 element = container->
MutableElement((*assignment_indices)[index]);
1144 element = assignment->
FastAdd(var);
1157 std::vector<int>* value) {
1161 if (element->
Var() != var) {
1163 <<
"Assignment does not contain operator variable " << var;
1164 element = &(container.
Element(var));
1167 CHECK_GE(var->
size(), element_value.size());
1169 *value = element_value;
1210 explicit BaseLns(
const std::vector<IntVar*>& vars);
1224 void OnStart()
override;
1225 std::vector<int> fragment_;
1234 explicit ChangeValue(
const std::vector<IntVar*>& vars);
1236 virtual int64
ModifyValue(int64 index, int64 value) = 0;
1243 void OnStart()
override;
1277 const std::vector<IntVar*>& path_vars,
int number_of_base_nodes,
1278 bool skip_locally_optimal_paths,
1279 std::function<
int(int64)> start_empty_path_class);
1282 void Reset()
override;
1289 int64
Next(int64 node_index)
const {
1291 return Value(node_index);
1296 int64
Path(int64 node_index)
const {
1311 int64
StartNode(
int i)
const {
return path_starts_[base_paths_[i]]; }
1313 const std::vector<int64>&
path_starts()
const {
return path_starts_; }
1316 return start_empty_path_class_ !=
nullptr 1360 bool MoveChain(int64 before_chain, int64 chain_end, int64 destination);
1364 bool ReverseChain(int64 before_chain, int64 after_chain, int64* chain_last);
1366 bool MakeActive(int64 node, int64 destination);
1401 void OnStart()
override;
1405 virtual void OnNodeInitialization() {}
1407 bool OnSamePath(int64 node1, int64 node2)
const;
1409 bool CheckEnds()
const {
1410 const int base_node_size = base_nodes_.size();
1411 for (
int i = base_node_size - 1; i >= 0; --i) {
1412 if (base_nodes_[i] != end_nodes_[i]) {
1418 bool IncrementPosition();
1419 void InitializePathStarts();
1420 void InitializeInactives();
1421 void InitializeBaseNodes();
1422 bool CheckChainValidity(int64 before_chain, int64 chain_end,
1423 int64 exclude)
const;
1426 std::vector<int> base_nodes_;
1427 std::vector<int> end_nodes_;
1428 std::vector<int> base_paths_;
1429 std::vector<int64> path_starts_;
1430 std::vector<bool> inactives_;
1433 std::function<int(int64)> start_empty_path_class_;
1434 bool skip_locally_optimal_paths_;
1435 bool optimal_paths_enabled_;
1436 std::vector<int> path_basis_;
1437 std::vector<bool> optimal_paths_;
1445 const std::vector<IntVar*>& vars,
1446 const std::vector<IntVar*>& secondary_vars,
int number_of_base_nodes,
1447 std::function<
int(int64)> start_empty_path_class);
1450 bool IsPathStart(int64 node_index)
const {
return prevs_[node_index] == -1; }
1452 int64
Prev(int64 node_index)
const {
1454 return prevs_[node_index];
1458 return "PathWithPreviousNodesOperator";
1465 std::vector<int64> prevs_;
1471 Solver* solver,
const std::vector<IntVar*>& vars,
1472 const std::vector<IntVar*>& secondary_vars,
1473 std::function<
int(int64)> start_empty_path_class);
1481 class MakeActiveOperator;
1482 class MakeInactiveOperator;
1483 class MakeChainInactiveOperator;
1484 class SwapActiveOperator;
1485 class ExtendedSwapActiveOperator;
1486 class MakeActiveAndRelocate;
1487 class RelocateAndMakeActiveOperator;
1488 class RelocateAndMakeInactiveOperator;
1519 class LocalSearchFilterManager :
public LocalSearchFilter {
1525 const std::vector<LocalSearchFilter*>& filters,
1528 return "LocalSearchFilterManager";
1544 return synchronized_value_;
1550 std::vector<LocalSearchFilter*> filters_;
1551 IntVar*
const objective_;
1552 bool is_incremental_;
1553 int64 synchronized_value_;
1554 int64 accepted_value_;
1570 DCHECK(index !=
nullptr);
1571 const int var_index = var->
index();
1572 *index = (var_index < var_index_to_index_.size())
1573 ? var_index_to_index_[var_index]
1575 return *index != kUnassigned;
1583 void AddVars(
const std::vector<IntVar*>& vars);
1584 int Size()
const {
return vars_.size(); }
1588 return values_[index];
1597 return objective_callback_ !=
nullptr;
1600 if (objective_callback_ !=
nullptr) {
1601 objective_callback_(objective_value);
1607 std::vector<IntVar*> vars_;
1608 std::vector<int64> values_;
1609 std::vector<bool> var_synced_;
1610 std::vector<int> var_index_to_index_;
1611 static const int kUnassigned;
1619 std::string
DebugString()
const override {
return "PropagationMonitor"; }
1635 virtual void PushContext(
const std::string& context) = 0;
1638 virtual void SetMin(
IntExpr*
const expr, int64 new_min) = 0;
1639 virtual void SetMax(
IntExpr*
const expr, int64 new_max) = 0;
1640 virtual void SetRange(
IntExpr*
const expr, int64 new_min, int64 new_max) = 0;
1642 virtual void SetMin(
IntVar*
const var, int64 new_min) = 0;
1643 virtual void SetMax(
IntVar*
const var, int64 new_max) = 0;
1644 virtual void SetRange(
IntVar*
const var, int64 new_min, int64 new_max) = 0;
1649 const std::vector<int64>& values) = 0;
1651 const std::vector<int64>& values) = 0;
1672 const std::vector<int>& rank_first,
1673 const std::vector<int>& rank_last,
1674 const std::vector<int>& unperformed) = 0;
1684 std::string
DebugString()
const override {
return "LocalSearchMonitor"; }
1691 bool neighbor_found,
const Assignment* delta,
1695 bool neighbor_found) = 0;
1698 bool neighbor_found) = 0;
1716 void SetMin(int64 m)
override;
1718 void SetMax(int64 m)
override;
1719 void SetRange(int64 mi, int64 ma)
override;
1730 uint64
Size()
const override;
1731 bool Contains(int64 v)
const override;
1743 std::string
BaseName()
const override {
return "BooleanVar"; }
1753 class SymmetryManager;
1761 : symmetry_manager_(nullptr), index_in_symmetry_manager_(-1) {}
1771 void set_symmetry_manager_and_index(
SymmetryManager* manager,
int index) {
1772 CHECK(symmetry_manager_ ==
nullptr);
1773 CHECK_EQ(-1, index_in_symmetry_manager_);
1774 symmetry_manager_ = manager;
1775 index_in_symmetry_manager_ = index;
1777 SymmetryManager* symmetry_manager()
const {
return symmetry_manager_; }
1778 int index_in_symmetry_manager()
const {
return index_in_symmetry_manager_; }
1782 int index_in_symmetry_manager_;
1790 double scaling_factor,
1791 std::function<std::string()> display_callback,
int period);
1809 virtual void OutputLine(
const std::string& line);
1812 static std::string MemoryUsage();
1815 std::unique_ptr<WallTimer> timer_;
1818 const double scaling_factor_;
1819 std::function<std::string()> display_callback_;
1822 int64 objective_min_;
1823 int64 objective_max_;
1824 int min_right_depth_;
1826 int sliding_min_depth_;
1827 int sliding_max_depth_;
1934 virtual void Clear() = 0;
1948 IntVar*
const var, int64 value,
1954 IntVar*
const var, int64 value1, int64 value2,
1984 IntExpr*
const expr, int64 value,
2015 IntVar*
const var, int64 value1, int64 value2,
2019 IntExpr*
const expression,
IntVar*
const var, int64 value1, int64 value2,
2025 IntVar*
const var,
const std::vector<int64>& values,
2030 const std::vector<int64>& values,
2039 const std::vector<IntVar*>& vars,
2045 const std::vector<IntVar*>& vars,
const std::vector<int64>& values,
2049 IntExpr*
const expression,
const std::vector<IntVar*>& var,
2050 const std::vector<int64>& values,
2056 const std::vector<IntVar*>& vars, int64 value,
2060 IntExpr*
const expression,
const std::vector<IntVar*>& var, int64 value,
2074 const std::string&
TypeName()
const;
2080 const std::vector<int64>& values);
2082 const IntTupleSet& values);
2086 const std::vector<IntVar*>& vars);
2089 const std::vector<IntervalVar*>& vars);
2092 const std::vector<SequenceVar*>& vars);
2103 const std::string& arg_name)
const;
2105 const std::string& arg_name)
const;
2108 const std::string& arg_name)
const;
2110 const std::string& arg_name)
const;
2113 std::string type_name_;
2114 absl::flat_hash_map<std::string, int64> integer_argument_;
2115 absl::flat_hash_map<std::string, std::vector<int64> > integer_array_argument_;
2116 absl::flat_hash_map<std::string, IntTupleSet> matrix_argument_;
2117 absl::flat_hash_map<std::string, IntExpr*> integer_expression_argument_;
2118 absl::flat_hash_map<std::string, IntervalVar*> interval_argument_;
2119 absl::flat_hash_map<std::string, SequenceVar*> sequence_argument_;
2120 absl::flat_hash_map<std::string, std::vector<IntVar*> >
2121 integer_variable_array_argument_;
2122 absl::flat_hash_map<std::string, std::vector<IntervalVar*> >
2123 interval_array_argument_;
2124 absl::flat_hash_map<std::string, std::vector<SequenceVar*> >
2125 sequence_array_argument_;
2137 void EndVisitModel(
const std::string& solver_name)
override;
2139 const Constraint*
const constraint)
override;
2141 const Constraint*
const constraint)
override;
2143 const IntExpr*
const expr)
override;
2145 const IntExpr*
const expr)
override;
2147 IntExpr*
const delegate)
override;
2149 const std::string& operation, int64 value,
2150 IntVar*
const delegate)
override;
2152 const std::string& operation, int64 value,
2158 const std::vector<int64>& values)
override;
2160 const IntTupleSet& values)
override;
2163 IntExpr*
const argument)
override;
2165 const std::string& arg_name,
2166 const std::vector<IntVar*>& arguments)
override;
2171 const std::string& arg_name,
2172 const std::vector<IntervalVar*>& arguments)
override;
2177 const std::string& arg_name,
2178 const std::vector<SequenceVar*>& arguments)
override;
2186 std::vector<ArgumentHolder*> holders_;
2193 : index_min_(index_min),
2194 index_max_(index_max),
2195 values_(new T[index_max - index_min + 1]) {
2196 DCHECK_LE(index_min, index_max);
2202 DCHECK_GE(index, index_min_);
2203 DCHECK_LE(index, index_max_);
2204 return values_[index - index_min_];
2208 DCHECK_GE(index, index_min_);
2209 DCHECK_LE(index, index_max_);
2210 values_[index - index_min_] = value;
2213 std::string
DebugString()
const override {
return "ArrayWithOffset"; }
2216 const int64 index_min_;
2217 const int64 index_max_;
2218 std::unique_ptr<T[]> values_;
2226 template <
class T,
class C>
2230 : block_size_(block_size), block_offset_(0) {
2231 CHECK_GT(block_size, 0);
2235 for (
int i = 0; i < elements_.size(); ++i) {
2236 delete[] elements_[i];
2240 T
At(int64 index)
const {
2241 const int64 block_index = ComputeBlockIndex(index);
2242 const int64 relative_index = block_index - block_offset_;
2243 if (relative_index < 0 || relative_index >= elements_.size()) {
2246 const T* block = elements_[relative_index];
2247 return block !=
nullptr ? block[index - block_index * block_size_] : T();
2251 const int64 block_index = ComputeBlockIndex(index);
2252 T*
const block = GetOrCreateBlock(block_index);
2253 const int64 residual = index - block_index * block_size_;
2255 reinterpret_cast<C>(value));
2259 T* NewBlock()
const {
2260 T*
const result =
new T[block_size_];
2261 for (
int i = 0; i < block_size_; ++i) {
2267 T* GetOrCreateBlock(
int block_index) {
2268 if (elements_.size() == 0) {
2269 block_offset_ = block_index;
2270 GrowUp(block_index);
2271 }
else if (block_index < block_offset_) {
2272 GrowDown(block_index);
2273 }
else if (block_index - block_offset_ >= elements_.size()) {
2274 GrowUp(block_index);
2276 T* block = elements_[block_index - block_offset_];
2277 if (block ==
nullptr) {
2279 elements_[block_index - block_offset_] = block;
2284 int64 ComputeBlockIndex(int64 value)
const {
2285 return value >= 0 ? value / block_size_
2286 : (value - block_size_ + 1) / block_size_;
2289 void GrowUp(int64 block_index) {
2290 elements_.resize(block_index - block_offset_ + 1);
2293 void GrowDown(int64 block_index) {
2294 const int64 delta = block_offset_ - block_index;
2295 block_offset_ = block_index;
2296 DCHECK_GT(delta, 0);
2297 elements_.insert(elements_.begin(), delta,
nullptr);
2300 const int64 block_size_;
2301 std::vector<T*> elements_;
2316 : elements_(new T[capacity]),
2318 capacity_(capacity),
2319 position_(new int[capacity]),
2320 delete_position_(true) {
2321 for (
int i = 0; i < capacity; ++i) {
2327 RevIntSet(
int capacity,
int* shared_positions,
int shared_positions_size)
2328 : elements_(new T[capacity]),
2330 capacity_(capacity),
2331 position_(shared_positions),
2332 delete_position_(false) {
2333 for (
int i = 0; i < shared_positions_size; ++i) {
2339 if (delete_position_) {
2350 DCHECK_LT(i, num_elements_.
Value());
2351 return elements_[i];
2356 DCHECK_LT(i + num_elements_.
Value(), capacity_);
2357 return elements_[i + num_elements_.
Value()];
2361 const int position = num_elements_.
Value();
2362 DCHECK_LT(position, capacity_);
2363 DCHECK(NotAlreadyInserted(elt));
2364 elements_[position] = elt;
2365 position_[elt] = position;
2366 num_elements_.
Incr(solver);
2370 num_elements_.
Decr(solver);
2371 SwapTo(value_index, num_elements_.
Value());
2375 SwapTo(value_index, num_elements_.
Value());
2376 num_elements_.
Incr(solver);
2388 bool NotAlreadyInserted(
const T& elt) {
2389 for (
int i = 0; i < num_elements_.
Value(); ++i) {
2390 if (elt == elements_[i]) {
2397 void SwapTo(T value_index,
int next_position) {
2398 const int current_position = position_[value_index];
2399 if (current_position != next_position) {
2400 const T next_value_index = elements_[next_position];
2401 elements_[current_position] = next_value_index;
2402 elements_[next_position] = value_index;
2403 position_[value_index] = next_position;
2404 position_[next_value_index] = current_position;
2409 std::unique_ptr<T[]> elements_;
2411 NumericalRev<int> num_elements_;
2413 const int capacity_;
2417 const bool delete_position_;
2427 last_ranked_(items.size() - 1),
2428 size_(items.size()),
2429 position_(new int[size_]) {
2430 for (
int i = 0; i < size_; ++i) {
2431 elements_[i] = items[i];
2439 last_ranked_(size - 1),
2441 position_(new int[size_]) {
2442 for (
int i = 0; i < size_; ++i) {
2458 DCHECK_GE(index, 0);
2459 DCHECK_LT(index, size_);
2460 return elements_[index];
2465 DCHECK_LE(first_ranked_.
Value(), last_ranked_.
Value());
2466 SwapTo(elt, first_ranked_.
Value());
2467 first_ranked_.
Incr(solver);
2471 DCHECK_LE(first_ranked_.
Value(), last_ranked_.
Value());
2472 SwapTo(elt, last_ranked_.
Value());
2473 last_ranked_.
Decr(solver);
2477 const int position = position_[elt];
2478 return (position < first_ranked_.
Value() ||
2479 position > last_ranked_.
Value());
2483 std::string result =
"[";
2484 for (
int i = 0; i < first_ranked_.
Value(); ++i) {
2485 absl::StrAppend(&result, elements_[i]);
2486 if (i != first_ranked_.
Value() - 1) {
2491 for (
int i = first_ranked_.
Value(); i <= last_ranked_.
Value(); ++i) {
2492 absl::StrAppend(&result, elements_[i]);
2493 if (i != last_ranked_.
Value()) {
2498 for (
int i = last_ranked_.
Value() + 1; i < size_; ++i) {
2499 absl::StrAppend(&result, elements_[i]);
2500 if (i != size_ - 1) {
2509 void SwapTo(
int elt,
int next_position) {
2510 const int current_position = position_[elt];
2511 if (current_position != next_position) {
2512 const int next_elt = elements_[next_position];
2513 elements_[current_position] = next_elt;
2514 elements_[next_position] = elt;
2515 position_[elt] = next_position;
2516 position_[next_elt] = current_position;
2521 std::vector<int> elements_;
2523 NumericalRev<int> first_ranked_;
2525 NumericalRev<int> last_ranked_;
2529 std::unique_ptr<int[]> position_;
2545 void Init(
Solver*
const solver,
const std::vector<uint64>& mask);
2553 bool RevAnd(
Solver*
const solver,
const std::vector<uint64>& mask);
2569 bool Intersects(
const std::vector<uint64>& mask,
int* support_index);
2579 void CleanUpActives(
Solver*
const solver);
2581 const int64 bit_size_;
2582 const int64 word_size_;
2585 std::vector<int> to_remove_;
2590 for (
int i = 0; i < values.size(); ++i) {
2591 if (values[i] != value) {
2600 for (
int i = 0; i < values.size(); ++i) {
2601 if (values[i] != 0 && values[i] != 1) {
2620 for (
const T& current_value : values) {
2621 if (current_value < value) {
2630 for (
const T& current_value : values) {
2631 if (current_value > value) {
2660 for (
int i = 0; i < values.size() - 1; ++i) {
2661 if (values[i + 1] != values[i] + 1) {
2670 for (
int i = 0; i < values.size() - 1; ++i) {
2671 if (values[i + 1] < values[i]) {
2681 for (
int i = 0; i < vars.size(); ++i) {
2682 if (vars[i]->Min() < range_min || vars[i]->Max() > range_max) {
2690 for (
int i = 0; i < vars.size(); ++i) {
2691 if (!vars[i]->Bound()) {
2706 const std::vector<T>& values) {
2707 for (
int i = 0; i < vars.size(); ++i) {
2708 if (values[i] != 0 && !vars[i]->Bound()) {
2717 for (
int i = 0; i < vars.size(); ++i) {
2718 if (!vars[i]->Bound() || vars[i]->Min() != value) {
2726 DCHECK(!vars.empty());
2727 int64 result = kint64min;
2728 for (
int i = 0; i < vars.size(); ++i) {
2730 result = std::max<int64>(result, vars[i]->Max());
2736 DCHECK(!vars.empty());
2737 int64 result = kint64max;
2738 for (
int i = 0; i < vars.size(); ++i) {
2740 result = std::min<int64>(result, vars[i]->Min());
2746 std::vector<int64>*
const values) {
2748 values->resize(vars.size());
2749 for (
int i = 0; i < vars.size(); ++i) {
2750 (*values)[i] = vars[i]->Value();
2757 return e % v == 0 ? e / v : e / v + 1;
2768 return e % v == 0 ? e / v : e / v - 1;
2772 std::vector<int64>
ToInt64Vector(
const std::vector<int>& input);
2775 #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
This class represent a reversible FIFO structure.
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
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)
virtual bool NextFragment()=0
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
A sequence variable is a variable whose domain is a set of possible orderings of the interval variabl...
virtual void SetStartMin(IntervalVar *const var, int64 new_min)=0
IntervalVar modifiers.
bool HasFragments() const override
This class encapsulates an objective.
~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
The base class of all search logs that periodically outputs information when the search is running.
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)
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)
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)
Hash functions.
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)
Base class of the local search operators dedicated to path modifications (a path is a set of nodes li...
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'.
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 ...
This is a special class to represent a 'residual' set of T.
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 are used for fast neighbor pruning.
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)
std::vector< int64 > ToInt64Vector(const std::vector< int > &input)
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)
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
Defines operators which change the value of variables; each neighbor corresponds to one modified vari...
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
A BaseObject is the root of all reversibly allocated 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
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
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.
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 variables are often used in scheduling.
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.
This is the base class for building an Lns operator.
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.
The SequenceVarElement stores a partial representation of ranked interval variables in the underlying...
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.
An Assignment is a variable -> domains mapping, used to report solutions to the user.
void SetIntegerArrayArgument(const std::string &arg_name, const std::vector< int64 > &values)
void Remove(Solver *const solver, const T &value_index)
The base class for all local search operators.
void AddIntegerVariableLessOrEqualValueClause(IntVar *const var, int64 value)
virtual void InjectObjectiveValue(int64 objective_value)
Reversible Immutable MultiMap class.
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...