Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
merit.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main author:
4 * Christian Schulte <schulte@gecode.org>
5 *
6 * Copyright:
7 * Christian Schulte, 2012
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode {
35
45 template<class _View, class _Val>
46 class MeritBase {
47 public:
49 typedef _View View;
51 typedef typename View::VarType Var;
53 typedef _Val Val;
55 MeritBase(Space& home, const VarBranch<Var>& vb);
59 bool notice(void) const;
61 void dispose(Space& home);
62 };
63
67 template<class View>
68 class MeritFunction : public MeritBase<View,double> {
69 using typename MeritBase<View,double>::Var;
70 public:
73 protected:
76 public:
82 double operator ()(const Space& home, View x, int i);
84 bool notice(void) const;
86 void dispose(Space& home);
87 };
88
92 template<class View>
93 class MeritDegree : public MeritBase<View,unsigned int> {
95 public:
97 MeritDegree(Space& home, const VarBranch<Var>& vb);
99 MeritDegree(Space& home, MeritDegree& md);
101 unsigned int operator ()(const Space& home, View x, int i);
102 };
103
107 template<class View>
108 class MeritAFC : public MeritBase<View,double> {
109 using typename MeritBase<View,double>::Var;
110 protected:
113 public:
115 MeritAFC(Space& home, const VarBranch<Var>& vb);
117 MeritAFC(Space& home, MeritAFC& ma);
119 double operator ()(const Space& home, View x, int i);
121 bool notice(void) const;
123 void dispose(Space& home);
124 };
125
129 template<class View>
130 class MeritAction : public MeritBase<View,double> {
131 using typename MeritBase<View,double>::Var;
132 protected:
135 public:
139 MeritAction(Space& home, MeritAction& ma);
141 double operator ()(const Space& home, View x, int i);
143 bool notice(void) const;
145 void dispose(Space& home);
146 };
148
152 template<class View>
153 class MeritCHB : public MeritBase<View,double> {
154 using typename MeritBase<View,double>::Var;
155 protected:
158 public:
160 MeritCHB(Space& home, const VarBranch<Var>& vb);
162 MeritCHB(Space& home, MeritCHB& ma);
164 double operator ()(const Space& home, View x, int i);
166 bool notice(void) const;
168 void dispose(Space& home);
169 };
171
172
173 // Merit base class
174 template<class View, class Val>
177 template<class View, class Val>
180 template<class View, class Val>
181 forceinline bool
183 return false;
184 }
185 template<class View, class Val>
186 forceinline void
188
189 // User-defined function merit
190 template<class View>
193 (Space& home, const VarBranch<MeritFunction<View>::Var>& vb)
194 : MeritBase<View,double>(home,vb), f(vb.merit()) {
195 if (!f())
196 throw InvalidFunction("MeritFunction::MeritFunction");
197 }
198 template<class View>
201 : MeritBase<View,double>(home,mf), f(mf.f) {
202 }
203 template<class View>
204 forceinline double
205 MeritFunction<View>::operator ()(const Space& home, View x, int i) {
206 typename View::VarType y(x.varimp());
208 return f()(home,y,i);
209 }
210 template<class View>
211 forceinline bool
213 return true;
214 }
215 template<class View>
216 forceinline void
218 f.~SharedData<Function>();
219 }
220
221
222 // Degree merit
223 template<class View>
226 (Space& home, const VarBranch<MeritDegree<View>::Var>& vb)
227 : MeritBase<View,unsigned int>(home,vb) {}
228 template<class View>
231 : MeritBase<View,unsigned int>(home,md) {}
232 template<class View>
233 forceinline unsigned int
235 return x.degree();
236 }
237
238 // AFC merit
239 template<class View>
242 (Space& home, const VarBranch<MeritAFC<View>::Var>& vb)
243 : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
244 template<class View>
247 : MeritBase<View,double>(home,ma), afc(ma.afc) {}
248 template<class View>
249 forceinline double
251 return x.afc();
252 }
253 template<class View>
254 forceinline bool
256 // Given that AFC is just a fake, this not really necessary
257 return false;
258 }
259 template<class View>
260 forceinline void
262 // Given that AFC is just a fake, this not really necessary
263 afc.~AFC();
264 }
265
266
267 // Action merit
268 template<class View>
271 (Space& home, const VarBranch<MeritAction<View>::Var>& vb)
272 : MeritBase<View,double>(home,vb), action(vb.action()) {}
273 template<class View>
276 : MeritBase<View,double>(home,ma), action(ma.action) {}
277 template<class View>
278 forceinline double
280 return action[i];
281 }
282 template<class View>
283 forceinline bool
285 return true;
286 }
287 template<class View>
288 forceinline void
290 action.~Action();
291 }
292
293 // CHB merit
294 template<class View>
297 (Space& home, const VarBranch<MeritCHB<View>::Var>& vb)
298 : MeritBase<View,double>(home,vb), chb(vb.chb()) {}
299 template<class View>
302 : MeritBase<View,double>(home,ma), chb(ma.chb) {}
303 template<class View>
304 forceinline double
305 MeritCHB<View>::operator ()(const Space&, View, int i) {
306 return chb[i];
307 }
308 template<class View>
309 forceinline bool
311 return true;
312 }
313 template<class View>
314 forceinline void
316 chb.~CHB();
317 }
318
319}
320
321// STATISTICS: kernel-branch
Class for AFC (accumulated failure count) management.
Definition afc.hpp:40
~AFC(void)
Destructor.
Definition afc.hpp:157
Class for action management.
Definition action.hpp:42
Traits for branching.
Definition traits.hpp:55
Class for CHB management.
Definition chb.hpp:46
Exception: invalid function
Merit class for AFC.
Definition merit.hpp:108
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:261
MeritAFC(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
AFC afc
AFC information.
Definition merit.hpp:112
double operator()(const Space &home, View x, int i)
Return AFC as merit for view x at position i.
Definition merit.hpp:250
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition merit.hpp:255
Merit class for action.
Definition merit.hpp:130
MeritAction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:289
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition merit.hpp:279
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition merit.hpp:284
Action action
Action information.
Definition merit.hpp:134
Base-class for merit class.
Definition merit.hpp:46
void dispose(Space &home)
Delete view merit class.
Definition merit.hpp:187
MeritBase(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition merit.hpp:176
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition merit.hpp:182
_View View
View type.
Definition merit.hpp:49
_Val Val
Type of merit.
Definition merit.hpp:53
MeritBase(Space &home, MeritBase &mb)
Constructor for cloning.
Definition merit.hpp:179
View::VarType Var
Corresponding variable type.
Definition merit.hpp:51
Merit class for CHB.
Definition merit.hpp:153
MeritCHB(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
CHB chb
CHB information.
Definition merit.hpp:157
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:315
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition merit.hpp:310
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition merit.hpp:305
Merit class for degree.
Definition merit.hpp:93
MeritDegree(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
unsigned int operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition merit.hpp:234
Merit class for user-defined merit function.
Definition merit.hpp:68
void dispose(Space &home)
Delete view merit class.
Definition merit.hpp:217
double operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition merit.hpp:205
BranchTraits< Var >::Merit Function
Corresponding merit function type.
Definition merit.hpp:72
SharedData< Function > f
The user-defined merit function.
Definition merit.hpp:75
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition merit.hpp:212
MeritFunction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Class for sharing data between spaces.
Computation spaces.
Definition core.hpp:1742
Variable branching information.
Definition var.hpp:55
unsigned int degree(void) const
Return degree (number of subscribed propagators and advisors)
Definition var.hpp:101
VarImp * varimp(void) const
Return variable implementation of variable.
Definition var.hpp:96
double afc(void) const
Return accumulated failure count.
Definition var.hpp:106
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:767
Post propagator for SetVar x
Definition set.hh:767
#define forceinline
Definition config.hpp:187
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition macros.hpp:94