Generated on Fri Jan 28 2022 04:43:06 for Gecode by doxygen 1.8.13
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 
34 namespace 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);
57  MeritBase(Space& home, MeritBase& mb);
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:
78  MeritFunction(Space& home, const VarBranch<Var>& vb);
80  MeritFunction(Space& home, MeritFunction& mf);
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> {
94  using typename MeritBase<View,unsigned int>::Var;
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:
137  MeritAction(Space& home, const VarBranch<Var>& vb);
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  : MeritBase<View,double>(home,vb), f(vb.merit()) {
194  if (!f())
195  throw InvalidFunction("MeritFunction::MeritFunction");
196  }
197  template<class View>
200  : MeritBase<View,double>(home,mf), f(mf.f) {
201  }
202  template<class View>
203  forceinline double
205  typename View::VarType y(x.varimp());
207  return f()(home,y,i);
208  }
209  template<class View>
210  forceinline bool
212  return true;
213  }
214  template<class View>
215  forceinline void
218  }
219 
220 
221  // Degree merit
222  template<class View>
225  : MeritBase<View,unsigned int>(home,vb) {}
226  template<class View>
229  : MeritBase<View,unsigned int>(home,md) {}
230  template<class View>
231  forceinline unsigned int
233  return x.degree();
234  }
235 
236  // AFC merit
237  template<class View>
240  : MeritBase<View,double>(home,vb), afc(vb.afc()) {}
241  template<class View>
244  : MeritBase<View,double>(home,ma), afc(ma.afc) {}
245  template<class View>
246  forceinline double
248  return x.afc();
249  }
250  template<class View>
251  forceinline bool
253  // Given that AFC is just a fake, this not really necessary
254  return false;
255  }
256  template<class View>
257  forceinline void
259  // Given that AFC is just a fake, this not really necessary
260  afc.~AFC();
261  }
262 
263 
264  // Action merit
265  template<class View>
268  : MeritBase<View,double>(home,vb), action(vb.action()) {}
269  template<class View>
272  : MeritBase<View,double>(home,ma), action(ma.action) {}
273  template<class View>
274  forceinline double
276  return action[i];
277  }
278  template<class View>
279  forceinline bool
281  return true;
282  }
283  template<class View>
284  forceinline void
286  action.~Action();
287  }
288 
289  // CHB merit
290  template<class View>
293  : MeritBase<View,double>(home,vb), chb(vb.chb()) {}
294  template<class View>
297  : MeritBase<View,double>(home,ma), chb(ma.chb) {}
298  template<class View>
299  forceinline double
301  return chb[i];
302  }
303  template<class View>
304  forceinline bool
306  return true;
307  }
308  template<class View>
309  forceinline void
311  chb.~CHB();
312  }
313 
314 }
315 
316 // STATISTICS: kernel-branch
void dispose(Space &home)
Dispose view selection.
Definition: merit.hpp:310
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition: merit.hpp:280
MeritDegree(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:224
BranchTraits< Var >::Merit Function
Corresponding merit function type.
Definition: merit.hpp:72
double operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition: merit.hpp:204
CHB chb
CHB information.
Definition: merit.hpp:157
~Action(void)
Destructor.
Definition: action.cpp:55
Merit class for degree.
Definition: merit.hpp:93
AFC afc
Definition: afc.cpp:135
View::VarType Var
Corresponding variable type.
Definition: merit.hpp:51
MeritAction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:267
Base-class for merit class.
Definition: merit.hpp:46
~AFC(void)
Destructor.
Definition: afc.hpp:157
unsigned int operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition: merit.hpp:232
#define forceinline
Definition: config.hpp:185
MeritCHB(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:292
Computation spaces.
Definition: core.hpp:1701
Action action
Action information.
Definition: merit.hpp:134
Class for AFC (accumulated failure count) management.
Definition: afc.hpp:40
Gecode::IntArgs i({1, 2, 3, 4})
~SharedData(void)
Destructors.
MeritFunction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:192
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition: merit.hpp:300
SharedData< Function > f
The user-defined merit function.
Definition: merit.hpp:75
Merit class for user-defined merit function.
Definition: merit.hpp:68
Class for CHB management.
Definition: chb.hpp:46
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition: merit.hpp:305
~CHB(void)
Destructor.
Definition: chb.cpp:55
Exception: invalid function
Definition: exception.hpp:114
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition: merit.hpp:275
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition: merit.hpp:182
Merit class for AFC.
Definition: merit.hpp:108
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition: macros.hpp:94
Post propagator for f(x \diamond_{\mathit{op}} y) \sim_r z \f$ void rel(Home home
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
Variable branching information.
Definition: var.hpp:55
Set variables
Definition: set.hh:127
void dispose(Space &home)
Dispose view selection.
Definition: merit.hpp:285
void dispose(Space &home)
Dispose view selection.
Definition: merit.hpp:258
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition: merit.hpp:211
AFC afc
AFC information.
Definition: merit.hpp:112
MeritBase(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:176
Post propagator for SetVar x
Definition: set.hh:767
MeritAFC(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition: merit.hpp:239
_View View
View type.
Definition: merit.hpp:49
void dispose(Space &home)
Delete view merit class.
Definition: merit.hpp:216
Gecode toplevel namespace
Class for action management.
Definition: action.hpp:42
void dispose(Space &home)
Delete view merit class.
Definition: merit.hpp:187
_Val Val
Type of merit.
Definition: merit.hpp:53
bool notice(void) const
Whether dispose must always be called (that is, notice is needed)
Definition: merit.hpp:252
double operator()(const Space &home, View x, int i)
Return AFC as merit for view x at position i.
Definition: merit.hpp:247
Traits for branching.
Definition: traits.hpp:55
Merit class for action.
Definition: merit.hpp:130
Merit class for CHB.
Definition: merit.hpp:153