kangaroo  1.0
the cbls library
singleVarTabu.hpp
Go to the documentation of this file.
1 
12 #ifndef SingleVarTabuHppIncluded
13 #define SingleVarTabuHppIncluded
14 
15 
16 
17 #include "cbls/frames/index.hpp"
18 
19 
20 
22 
23 
24 
30 class SingleVarTabu : public TabuBase
31 {
32  public:
33 
36 
37  Bool TabuState(Variable * const pVariable) const;
38 
40 
43 
44  virtual void resetTabuVars() = 0;
45  virtual void tabuExecVars() = 0;
46  virtual void tabuVar(Variable * const pVariable) = 0;
47  virtual void tabuVars(Variable * const pVariable0, Variable * const pVariable1) = 0;
48  virtual void tabuVars(Dim const VarCount, Variable * const * pVariables) = 0;
49 
51 
52  protected:
53 
59  class onehash
60  {
61  public:
62  static Hash iof(Variable * const OneVar);
63  };
64 
67 
68  ~SingleVarTabu();
69  SingleVarTabu(System * const pSystem);
70 
71  SingleVarTabu(SingleVarTabu && that) noexcept = delete;
72  SingleVarTabu(SingleVarTabu const & that) = delete;
73  SingleVarTabu & operator = (SingleVarTabu && that) noexcept = delete;
74  SingleVarTabu & operator = (SingleVarTabu const & that) = delete;
75 
77 
80 
81  virtual void adjustMemAuto() override;
82 
84 
87 
88  virtual void setupComputation() override final;
89 
91 
94 
95  Bool setTabuState(Variable * const pVariable);
96  Bool unsetTabuState(Variable * const pVariable);
97 
99 
102 
103  #if UseArrayForVarTabuOrLockStateInsteadOfHashMap
104  xb<Dim> mTabuStates;
105  #else
106  xhmr<Variable *, Dim, onehash> mTabuStates;
107  #endif
108 
110 };
111 
112 
114 
115 
116 
120 inline SingleVarTabu::SingleVarTabu(System * const pSystem) : TabuBase(pSystem), mTabuStates()
121 {
122  WatchError
124  CatchError
125 }
126 
127 
128 
133 {
134  WatchError
135  // nothing to be done.
136  CatchError
137 }
138 
139 
140 
144 inline Bool SingleVarTabu::setTabuState(Variable * const pVariable)
145 {
146  WatchError
147  #if UseArrayForVarTabuOrLockStateInsteadOfHashMap
148  return (++mTabuStates[pVariable->VarSerial] == 1);
149  #else
150  Index tIndex = mTabuStates.insertIndexMem(pVariable, 0);
151  return (++mTabuStates.Item(tIndex) == 1);
152  #endif
153  CatchError
154 }
155 
156 
160 inline Bool SingleVarTabu::unsetTabuState(Variable * const pVariable)
161 {
162  WatchError
163  #if UseArrayForVarTabuOrLockStateInsteadOfHashMap
164  Warn(!mTabuStates[pVariable->VarSerial], kTabuInconsistent);
165  return (--mTabuStates[pVariable->VarSerial] == 0);
166  #else
167  Index tIndex = mTabuStates.KeyIndex(pVariable);
168  Warn(!mTabuStates.Item(tIndex), kTabuInconsistent);
169  if (--mTabuStates.Item(tIndex))
170  return false;
171  mTabuStates.removeWithIndex(tIndex);
172  return true;
173  #endif
174  CatchError
175 }
176 
177 
181 inline Bool SingleVarTabu::TabuState(Variable * const pVariable) const
182 {
183  WatchError
184  #if UseArrayForVarTabuOrLockStateInsteadOfHashMap
185  return mTabuStates[pVariable->VarSerial];
186  #else
187  return mTabuStates.KeyExists(pVariable);
188  #endif
189  CatchError
190 }
191 
192 
193 
198 {
199  WatchError
200  mTabuStates.adjustMemAuto();
201  CatchError
202 }
203 
204 
205 
210 {
211  WatchError
212  #if UseArrayForVarTabuOrLockStateInsteadOfHashMap
213  mTabuStates.allocMemSafe(SysMaster->VarCount());
214  for(Serial tVarSerial = 0; tVarSerial < SysMaster->VarCount(); ++tVarSerial)
215  mTabuStates.insert(0);
216  #endif
217  CatchError
218 }
219 
220 
221 
222 
226 inline Hash SingleVarTabu::onehash::iof(Variable * const OneVar)
227 {
228  WatchError
229  return OneVar->ExprHash;
230  CatchError
231 }
232 
233 
234 
236 
237 
238 
239 #endif // SingleVarTabuHppIncluded
xhmr< Variable *, Dim, onehash > mTabuStates
Count/state of the variables.
Definition: singleVarTabu.hpp:106
Dim VarCount() const
Number of variables.
Definition: system1.hpp:100
#define openKangarooSpace
Open the project namespace.
Definition: project.hpp:74
Hash const ExprHash
Expression hash value is used in quick matching of a certain expression.
Definition: expression0.hpp:216
virtual void adjustMemAuto() override
Adjust memory automatically.
Definition: singleVarTabu.hpp:197
SingleVarTabu SvTabu
Shortcut for SingleVarTabu.
Definition: singleVarTabu.hpp:113
virtual void tabuExecVars()=0
Tabu the execution variables.
static Hash iof(Variable *const OneVar)
Calculate hash value.
Definition: singleVarTabu.hpp:226
Serial const VarSerial
Unique over all variables.
Definition: variable0.hpp:158
Tabu heuristic base class.
Definition: tabubase0.hpp:40
Abstract variable class.
Definition: variable0.hpp:86
Bool TabuState(Variable *const pVariable) const
Get the tabu state.
Definition: singleVarTabu.hpp:181
~SingleVarTabu()
Destructor.
Definition: singleVarTabu.hpp:132
Index for Library Frames.
virtual void setupComputation() overridefinal
Setup for computation.
Definition: singleVarTabu.hpp:209
Unique identifier of a class.
constexpr char const * kTabuInconsistent
Inconsistent tabu found.
Definition: messages.hpp:224
void setTabuFamily(Identifier const pFamily)
Set tabu family.
Definition: tabubase1.hpp:53
Bool unsetTabuState(Variable *const pVariable)
Unset tabu state.
Definition: singleVarTabu.hpp:160
Bool setTabuState(Variable *const pVariable)
Set tabu state.
Definition: singleVarTabu.hpp:144
virtual void tabuVars(Variable *const pVariable0, Variable *const pVariable1)=0
Tabu the given two variables.
Constraint-based local search systems.
Definition: system0.hpp:45
#define closeKangarooSpace
Close the project namespace.
Definition: project.hpp:75
virtual void resetTabuVars()=0
Reset the tabu states.
Hash function for single variables.
Definition: singleVarTabu.hpp:59
SingleVarTabu & operator=(SingleVarTabu &&that) noexcept=delete
Move assignment.
SingleVarTabu(System *const pSystem)
Param constructor.
Definition: singleVarTabu.hpp:120
virtual void tabuVar(Variable *const pVariable)=0
Tabu the given variable.
Single variable tabu family.
Definition: singleVarTabu.hpp:30
System * SysMaster
The system.
Definition: tabubase0.hpp:169