Generated on Sat Feb 7 2015 02:01:27 for Gecode by doxygen 1.8.9.1
brancher-view-val.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2012
8  *
9  * Last modified:
10  * $Date: 2013-07-08 17:25:13 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13825 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode {
39 
48  template<class Val>
51  private:
53  const Val _val;
54  public:
56  PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n);
58  const Val& val(void) const;
60  virtual size_t size(void) const;
62  virtual void archive(Archive& e) const;
63  };
64 
65 
67  template<class View, class Val, PropCond pc>
68  class ViewValNGL : public NGL {
69  protected:
71  View x;
73  Val n;
74  public:
76  ViewValNGL(Space& home, View x, Val n);
78  ViewValNGL(Space& home, bool share, ViewValNGL& ngl);
80  virtual void subscribe(Space& home, Propagator& p);
82  virtual void cancel(Space& home, Propagator& p);
84  virtual size_t dispose(Space& home);
85  };
86 
94  template<class View, int n, class Val, unsigned int a>
95  class ViewValBrancher : public ViewBrancher<View,n> {
96  typedef typename ViewBrancher<View,n>::BranchFilter BranchFilter;
97  protected:
103  typedef void (*VarValPrint)(const Space& home, const BrancherHandle& bh,
104  unsigned int b,
105  typename View::VarType x, int i,
106  const Val& m,
107  std::ostream& o);
111  ViewValBrancher(Space& home, bool share, ViewValBrancher& b);
113  ViewValBrancher(Home home,
114  ViewArray<View>& x,
115  ViewSel<View>* vs[n],
117  BranchFilter bf,
118  VarValPrint vvp);
119  public:
121  virtual const Choice* choice(Space& home);
123  virtual const Choice* choice(const Space& home, Archive& e);
125  virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
127  virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
135  virtual void print(const Space& home, const Choice& c, unsigned int b,
136  std::ostream& o) const;
138  virtual Actor* copy(Space& home, bool share);
140  virtual size_t dispose(Space& home);
142  static BrancherHandle post(Home home,
143  ViewArray<View>& x,
144  ViewSel<View>* vs[n],
146  BranchFilter bf,
147  VarValPrint vvp);
148  };
150 
151  /*
152  * %Choice with position and value
153  *
154  */
155  template<class Val>
158  const Pos& p, const Val& n)
159  : PosChoice(b,a,p), _val(n) {}
160 
161  template<class Val>
162  forceinline const Val&
164  return _val;
165  }
166 
167  template<class Val>
168  forceinline size_t
170  return sizeof(PosValChoice<Val>);
171  }
172 
173  template<class Val>
174  forceinline void
177  e << _val;
178  }
179 
180 
181  /*
182  * View-value no-good literal
183  *
184  */
185  template<class View, class Val, PropCond pc>
188  : NGL(home), x(x0), n(n0) {}
189 
190  template<class View, class Val, PropCond pc>
193  : NGL(home,share,ngl), n(ngl.n) {
194  x.update(home,share,ngl.x);
195  }
196 
197  template<class View, class Val, PropCond pc>
198  void
200  x.subscribe(home,p,pc);
201  }
202 
203  template<class View, class Val, PropCond pc>
204  void
206  x.cancel(home,p,pc);
207  }
208 
209  template<class View, class Val, PropCond pc>
210  size_t
212  (void) NGL::dispose(home);
213  return sizeof(*this);
214  }
215 
216 
217 
218  /*
219  * Generic brancher based on variable/value selection
220  *
221  */
222  template<class View, int n, class Val, unsigned int a>
227  ViewSel<View>* vs[n],
229  BranchFilter bf,
230  VarValPrint vvp0)
231  : ViewBrancher<View,n>(home,x,vs,bf), vsc(vsc0), vvp(vvp0) {
232  if (vsc->notice())
233  home.notice(*this,AP_DISPOSE,true);
234  }
235 
236  template<class View, int n, class Val, unsigned int a>
241  BranchFilter bf,
242  VarValPrint vvp) {
243  return *new (home) ViewValBrancher<View,n,Val,a>(home,x,vs,vsc,bf,vvp);
244  }
245 
246  template<class View, int n, class Val, unsigned int a>
250  : ViewBrancher<View,n>(home,shared,b),
251  vsc(b.vsc->copy(home,shared)), vvp(b.vvp) {}
252 
253  template<class View, int n, class Val, unsigned int a>
254  Actor*
256  return new (home) ViewValBrancher<View,n,Val,a>(home,shared,*this);
257  }
258 
259  template<class View, int n, class Val, unsigned int a>
260  const Choice*
263  View v = ViewBrancher<View,n>::view(p);
264  return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
265  }
266 
267  template<class View, int n, class Val, unsigned int a>
268  const Choice*
270  (void) home;
271  int p; e >> p;
272  Val v; e >> v;
273  return new PosValChoice<Val>(*this,a,p,v);
274  }
275 
276  template<class View, int n, class Val, unsigned int a>
277  ExecStatus
279  ::commit(Space& home, const Choice& c, unsigned int b) {
280  const PosValChoice<Val>& pvc
281  = static_cast<const PosValChoice<Val>&>(c);
282  return me_failed(vsc->commit(home,b,
284  pvc.pos().pos,
285  pvc.val()))
286  ? ES_FAILED : ES_OK;
287  }
288 
289  template<class View, int n, class Val, unsigned int a>
290  NGL*
292  ::ngl(Space& home, const Choice& c, unsigned int b) const {
293  const PosValChoice<Val>& pvc
294  = static_cast<const PosValChoice<Val>&>(c);
295  return vsc->ngl(home,b,
296  ViewBrancher<View,n>::view(pvc.pos()),pvc.val());
297  }
298 
299  template<class View, int n, class Val, unsigned int a>
300  void
302  ::print(const Space& home, const Choice& c, unsigned int b,
303  std::ostream& o) const {
304  const PosValChoice<Val>& pvc
305  = static_cast<const PosValChoice<Val>&>(c);
306  View xi = ViewBrancher<View,n>::view(pvc.pos());
307  typename View::VarType y(ViewBrancher<View,n>::view(pvc.pos()).varimp());
308  if (vvp != NULL)
309  vvp(home,*this,b,y,pvc.pos().pos,pvc.val(),o);
310  else
311  vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
312  }
313 
314  template<class View, int n, class Val, unsigned int a>
315  forceinline size_t
317  if (vsc->notice())
318  home.ignore(*this,AP_DISPOSE,true);
319  vsc->dispose(home);
320  (void) ViewBrancher<View,n>::dispose(home);
321  return sizeof(ViewValBrancher<View,n,Val,a>);
322  }
323 
324 }
325 
326 // STATISTICS: kernel-branch
virtual size_t dispose(Space &home)
Dispose.
const Pos & pos(void) const
Return position in array.
virtual void cancel(Space &home, Propagator &p)
Cancel subscription for no-good literal.
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
const Val & val(void) const
Return value to branch with.
BranchFilter bf
Branch filter function.
virtual void print(const Space &home, const Choice &c, unsigned int b, std::ostream &o) const
Print branch for choice c and alternative b.
Actor must always be disposed.
Definition: core.hpp:610
virtual NGL * ngl(Space &home, const Choice &c, unsigned int b) const
Create no-good literal for choice c and alternative b.
Val n
The stored value.
Handle for brancher.
Definition: core.hpp:1157
BranchTraits< typename View::VarType >::Filter BranchFilter
The branch filter that corresponds to the var type.
Generic brancher by view selection.
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
View x
The stored view.
Base-class for propagators.
Definition: core.hpp:755
static BrancherHandle post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter bf, VarValPrint vvp)
Brancher post function.
Computation spaces.
Definition: core.hpp:1362
Base-class for both propagators and branchers.
Definition: core.hpp:666
virtual Actor * copy(Space &home, bool share)
Perform cloning.
Gecode::FloatVal c(-8, 8)
Pos pos(Space &home)
Return position information.
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Base-class for branchers.
Definition: core.hpp:1071
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Execution has resulted in failure.
Definition: core.hpp:525
Generic brancher by view and value selection.
ViewSel< View > * vs[n]
View selection objects.
View view(const Pos &p) const
Return view according to position information p.
unsigned int size(I &i)
Size of all ranges of range iterator i.
virtual void subscribe(Space &home, Propagator &p)
Create subscription for no-good literal.
Choices storing position
VarValPrint vvp
Print function.
ViewValBrancher(Space &home, bool share, ViewValBrancher &b)
Constructor for cloning b.
Position information.
View arrays.
Definition: array.hpp:234
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:2849
virtual void archive(Archive &e) const
Archive into e.
const int v[7]
Definition: distinct.cpp:207
BrancherHandle bh
virtual const Choice * choice(Space &home)
Return choice.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.cpp:169
Choice for performing commit
Definition: core.hpp:1036
Archive representation
Definition: archive.hpp:45
ExecStatus
Definition: core.hpp:523
#define forceinline
Definition: config.hpp:132
Execution is okay.
Definition: core.hpp:527
ViewValNGL(Space &home, View x, Val n)
Initialize for propagator p with view x and value n.
View-value no-good literal.
virtual size_t dispose(Space &home)
Dispose.
Definition: core.hpp:3224
void(* VarValPrint)(const Space &home, const BrancherHandle &bh, unsigned int b, typename View::VarType x, int i, const Val &m, std::ostream &o)
Function type for printing variable and value selection.
virtual bool notice(void) const =0
Whether dispose must always be called (that is, notice is needed)
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
virtual size_t dispose(Space &home)
Delete brancher and return its size.
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
virtual size_t size(void) const
Report size occupied.
#define GECODE_VTABLE_EXPORT
Definition: support.hh:76
const int pos
Position of view.
virtual void archive(Archive &e) const
Archive into e.
ViewArray< View > x
Views to branch on.
Home class for posting propagators
Definition: core.hpp:717
Choice storing position and value
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
PosValChoice(const Brancher &b, unsigned int a, const Pos &p, const Val &n)
Initialize choice for brancher b, number of alternatives a, position p, and value n...
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:58
No-good literal recorded during search.
Definition: core.hpp:971