Generated on Thu Mar 16 2017 03:24:22 for Gecode by doxygen 1.8.13
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: 2016-06-29 17:28:17 +0200 (Wed, 29 Jun 2016) $ by $Author: schulte $
11  * $Revision: 15137 $
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 void reschedule(Space& home, Propagator& p);
86  virtual size_t dispose(Space& home);
87  };
88 
96  template<class View, int n, class Val, unsigned int a>
97  class ViewValBrancher : public ViewBrancher<View,n> {
98  typedef typename ViewBrancher<View,n>::BranchFilter BranchFilter;
99  protected:
105  typedef void (*VarValPrint)(const Space& home, const Brancher& b,
106  unsigned int alt,
107  typename View::VarType x, int i,
108  const Val& m,
109  std::ostream& o);
111  VarValPrint vvp;
113  ViewValBrancher(Space& home, bool share, ViewValBrancher& b);
115  ViewValBrancher(Home home,
116  ViewArray<View>& x,
117  ViewSel<View>* vs[n],
119  BranchFilter bf,
120  VarValPrint vvp);
121  public:
123  virtual const Choice* choice(Space& home);
125  virtual const Choice* choice(const Space& home, Archive& e);
127  virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
129  virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
137  virtual void print(const Space& home, const Choice& c, unsigned int b,
138  std::ostream& o) const;
140  virtual Actor* copy(Space& home, bool share);
142  virtual size_t dispose(Space& home);
144  static void post(Home home,
145  ViewArray<View>& x,
146  ViewSel<View>* vs[n],
148  BranchFilter bf,
149  VarValPrint vvp);
150  };
152 
153  /*
154  * %Choice with position and value
155  *
156  */
157  template<class Val>
160  const Pos& p, const Val& n)
161  : PosChoice(b,a,p), _val(n) {}
162 
163  template<class Val>
164  forceinline const Val&
166  return _val;
167  }
168 
169  template<class Val>
170  forceinline size_t
172  return sizeof(PosValChoice<Val>);
173  }
174 
175  template<class Val>
176  forceinline void
179  e << _val;
180  }
181 
182 
183  /*
184  * View-value no-good literal
185  *
186  */
187  template<class View, class Val, PropCond pc>
190  : NGL(home), x(x0), n(n0) {}
191 
192  template<class View, class Val, PropCond pc>
195  : NGL(home,share,ngl), n(ngl.n) {
196  x.update(home,share,ngl.x);
197  }
198 
199  template<class View, class Val, PropCond pc>
200  void
202  x.subscribe(home,p,pc);
203  }
204 
205  template<class View, class Val, PropCond pc>
206  void
208  x.cancel(home,p,pc);
209  }
210 
211  template<class View, class Val, PropCond pc>
212  void
214  x.reschedule(home,p,pc);
215  }
216 
217  template<class View, class Val, PropCond pc>
218  size_t
220  (void) NGL::dispose(home);
221  return sizeof(*this);
222  }
223 
224 
225 
226  /*
227  * Generic brancher based on variable/value selection
228  *
229  */
230  template<class View, int n, class Val, unsigned int a>
235  ViewSel<View>* vs[n],
237  BranchFilter bf,
238  VarValPrint vvp0)
239  : ViewBrancher<View,n>(home,x,vs,bf), vsc(vsc0), vvp(vvp0) {
240  if (vsc->notice())
241  home.notice(*this,AP_DISPOSE,true);
242  }
243 
244  template<class View, int n, class Val, unsigned int a>
245  inline void
249  BranchFilter bf,
250  VarValPrint vvp) {
251  (void) new (home) ViewValBrancher<View,n,Val,a>(home,x,vs,vsc,bf,vvp);
252  }
253 
254  template<class View, int n, class Val, unsigned int a>
258  : ViewBrancher<View,n>(home,shared,b),
259  vsc(b.vsc->copy(home,shared)), vvp(b.vvp) {}
260 
261  template<class View, int n, class Val, unsigned int a>
262  Actor*
264  return new (home) ViewValBrancher<View,n,Val,a>(home,shared,*this);
265  }
266 
267  template<class View, int n, class Val, unsigned int a>
268  const Choice*
271  View v = ViewBrancher<View,n>::view(p);
272  return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
273  }
274 
275  template<class View, int n, class Val, unsigned int a>
276  const Choice*
278  (void) home;
279  int p; e >> p;
280  Val v; e >> v;
281  return new PosValChoice<Val>(*this,a,p,v);
282  }
283 
284  template<class View, int n, class Val, unsigned int a>
285  ExecStatus
287  ::commit(Space& home, const Choice& c, unsigned int b) {
288  const PosValChoice<Val>& pvc
289  = static_cast<const PosValChoice<Val>&>(c);
290  return me_failed(vsc->commit(home,b,
292  pvc.pos().pos,
293  pvc.val()))
294  ? ES_FAILED : ES_OK;
295  }
296 
297  template<class View, int n, class Val, unsigned int a>
298  NGL*
300  ::ngl(Space& home, const Choice& c, unsigned int b) const {
301  const PosValChoice<Val>& pvc
302  = static_cast<const PosValChoice<Val>&>(c);
303  return vsc->ngl(home,b,
304  ViewBrancher<View,n>::view(pvc.pos()),pvc.val());
305  }
306 
307  template<class View, int n, class Val, unsigned int a>
308  void
310  ::print(const Space& home, const Choice& c, unsigned int b,
311  std::ostream& o) const {
312  const PosValChoice<Val>& pvc
313  = static_cast<const PosValChoice<Val>&>(c);
314  View xi = ViewBrancher<View,n>::view(pvc.pos());
315  typename View::VarType y(ViewBrancher<View,n>::view(pvc.pos()).varimp());
316  if (vvp != NULL)
317  vvp(home,*this,b,y,pvc.pos().pos,pvc.val(),o);
318  else
319  vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
320  }
321 
322  template<class View, int n, class Val, unsigned int a>
323  forceinline size_t
325  if (vsc->notice())
326  home.ignore(*this,AP_DISPOSE,true);
327  vsc->dispose(home);
328  (void) ViewBrancher<View,n>::dispose(home);
329  return sizeof(ViewValBrancher<View,n,Val,a>);
330  }
331 
332 }
333 
334 // STATISTICS: kernel-branch
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
BranchFilter bf
Branch filter function.
Actor must always be disposed.
Definition: core.hpp:626
void cancel(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:85
Val n
The stored value.
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:1012
virtual void dispose(Space &home)=0
Delete value selection.
Computation spaces.
Definition: core.hpp:1672
const Pos & pos(void) const
Return position in array.
Base-class for both propagators and branchers.
Definition: core.hpp:682
virtual Actor * copy(Space &home, bool share)
Perform cloning.
Gecode::FloatVal c(-8, 8)
Pos pos(Space &home)
Return position information.
struct Gecode::@554::NNF::@60::@62 a
For atomic nodes.
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:1364
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
virtual void print(const Space &home, unsigned int a, View x, int i, const Val &n, std::ostream &o) const =0
Print on o branch for alternative a, view x at position i, and value n.
Execution has resulted in failure.
Definition: core.hpp:538
Generic brancher by view and value selection.
virtual size_t size(void) const
Report size occupied.
void subscribe(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:75
ViewSel< View > * vs[n]
View selection objects.
unsigned int size(I &i)
Size of all ranges of range iterator i.
static void post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter bf, VarValPrint vvp)
Brancher post function.
View view(const Pos &p) const
Return view according to position information p.
const Val & val(void) const
Return value to branch with.
Choices storing position
VarValPrint vvp
Print function.
ViewValBrancher(Space &home, bool share, ViewValBrancher &b)
Constructor for cloning b.
Position information.
virtual void print(const Space &home, const Choice &c, unsigned int b, std::ostream &o) const
Print branch for choice c and alternative b.
View arrays.
Definition: array.hpp:234
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3253
const int v[7]
Definition: distinct.cpp:263
virtual ModEvent commit(Space &home, unsigned int a, View x, int i, Val n)=0
Commit view x at position i to value n for alternative a.
void print(std::basic_ostream< Char, Traits > &s, bool assigned, IL &lb, IU &ub, unsigned int cardMin, unsigned int cardMax)
Print set view.
Definition: print.hpp:67
virtual void archive(Archive &e) const
Archive into e.
virtual const Choice * choice(Space &home)
Return choice.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
virtual NGL * ngl(Space &home, unsigned int a, View x, Val n) const =0
Create no-good literal for choice c and alternative a.
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.cpp:167
Choice for performing commit
Definition: core.hpp:1332
Archive representation
Definition: archive.hpp:45
ExecStatus
Definition: core.hpp:536
#define forceinline
Definition: config.hpp:173
Execution is okay.
Definition: core.hpp:540
ViewValNGL(Space &home, View x, Val n)
Initialize for propagator p with view x and value n.
View-value no-good literal.
virtual void archive(Archive &e) const
Archive into e.
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:690
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Gecode toplevel namespace
#define GECODE_VTABLE_EXPORT
Definition: support.hh:76
virtual NGL * ngl(Space &home, const Choice &c, unsigned int b) const
Create no-good literal for choice c and alternative b.
void reschedule(Space &home, Propagator &p, IntSet &y)
Definition: rel.hpp:96
struct Gecode::@554::NNF::@60::@61 b
For binary nodes (and, or, eqv)
ViewArray< View > x
Views to branch on.
Home class for posting propagators
Definition: core.hpp:905
Choice storing position and value
virtual Val val(const Space &home, View x, int i)=0
Return value of view x at position i.
void(* VarValPrint)(const Space &home, const Brancher &b, unsigned int alt, typename View::VarType x, int i, const Val &m, std::ostream &o)
Function type for printing variable and value selection.
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
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
No-good literal recorded during search.
Definition: core.hpp:1260