Generated on Sat Oct 20 2018 12:43:45 for Gecode by doxygen 1.8.13
brancher.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christopher Mears <chris.mears@monash.edu>
5  *
6  * Copyright:
7  * Christopher Mears, 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 #include <deque>
35 #include <set>
36 
37 namespace Gecode { namespace Int { namespace LDSB {
38 
41  : _variable(-1), _value(-1) {}
42 
44  Literal::Literal(int idx, int val)
45  : _variable(idx), _value(val) {}
46 
48  bool
49  Literal::operator <(const Literal &rhs) const {
50  int d = rhs._variable - _variable;
51  if (d > 0) return true;
52  else if (d == 0) return rhs._value > _value;
53  else return false;
54  }
55 
56 
57  template<class Val>
58  inline
59  LDSBChoice<Val>::LDSBChoice(const Brancher& b, unsigned int a, const Pos& p,
60  const Val& n, const Literal* literals,
61  int nliterals)
62  : PosValChoice<Val>(b,a,p,n), _literals(literals), _nliterals(nliterals)
63  {}
64 
65  template<class Val>
67  delete [] _literals;
68  }
69 
70  template<class Val>
71  forceinline const Literal*
72  LDSBChoice<Val>::literals(void) const { return _literals; }
73 
74  template<class Val>
75  forceinline int
76  LDSBChoice<Val>::nliterals(void) const { return _nliterals; }
77 
78  template<class Val>
79  void
82  e << _nliterals;
83  for (int i = 0 ; i < _nliterals ; i++) {
84  e << _literals[i]._variable;
85  e << _literals[i]._value;
86  }
87  }
88 
89 
90 
91  template<class View, int n, class Val, unsigned int a,
92  class Filter, class Print>
95  ViewSel<View>* vs[n],
97  SymmetryImp<View>** syms, int nsyms,
100  : ViewValBrancher<View,n,Val,a,Filter,Print>(home, x, vs, vsc, bf, vvp),
101  _syms(syms),
102  _nsyms(nsyms),
103  _prevPos(-1)
104  {
105  home.notice(*this, AP_DISPOSE, true);
106  }
107 
108  template<class View, int n, class Val, unsigned int a,
109  class Filter, class Print>
110  forceinline void
114  SymmetryImp<View>** syms, int nsyms,
116  VarValPrint<Var,Val> vvp) {
118  (home,x,vs,vsc,syms,nsyms,bf,vvp);
119  }
120 
121  template<class View, int n, class Val, unsigned int a,
122  class Filter, class Print>
127  : ViewValBrancher<View,n,Val,a,Filter,Print>(home,b),
128  _nsyms(b._nsyms),
129  _prevPos(b._prevPos) {
131  for (int i = 0 ; i < _nsyms ; i++)
132  _syms[i] = b._syms[i]->copy(home);
133  }
134 
135  template<class View, int n, class Val, unsigned int a,
136  class Filter, class Print>
137  Actor*
140  (home,*this);
141  }
142 
143 
144  // Compute choice
145  template<class View, int n, class Val, unsigned int a,
146  class Filter, class Print>
147  const Choice*
149  // Making the PVC here is not so nice, I think.
151  const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
152 
153  // Compute symmetries.
154 
155  int choicePos = pvc->pos().pos;
156  int choiceVal = pvc->val();
157  delete c;
158 
159  _prevPos = choicePos;
160 
161  // TODO: It should be possible to use simpler containers than the
162  // STL ones here.
163  std::deque<Literal> queue;
164  std::set<Literal> seen;
165 
166  seen.insert(Literal(choicePos, choiceVal));
167  queue.push_back(Literal(choicePos, choiceVal));
168 
169  do {
170  Literal l = queue[0];
171  queue.pop_front();
172 
173  for (int i = 0 ; i < _nsyms ; i++) {
174  ArgArray<Literal> toExclude = _syms[i]->symmetric(l, this->x);
175  for (int j = 0 ; j < toExclude.size() ; ++j) {
176  if (seen.find(toExclude[j]) == seen.end())
177  queue.push_back(toExclude[j]);
178  seen.insert(toExclude[j]);
179  }
180  }
181  } while (queue.size() > 0);
182 
183  // Convert "seen" vector into array.
184  int nliterals = static_cast<int>(seen.size());
185  Literal* literals = new Literal[nliterals];
186  std::set<Literal>::iterator it = seen.begin();
187  for (int i = 0 ; i < nliterals ; i++) {
188  literals[i] = *it;
189  ++it;
190  }
191 
192  return new LDSBChoice<Val>(*this,a,choicePos,choiceVal, literals, nliterals);
193  }
194 
195 
196  template<class View, int n, class Val, unsigned int a,
197  class Filter, class Print>
198  const Choice*
200  Archive& e) {
201  (void) home;
202  int p; e >> p;
203  Val v; e >> v;
204  int nliterals; e >> nliterals;
205  Literal* literals = new Literal[nliterals];
206  for (int i = 0 ; i < nliterals ; i++) {
207  e >> literals[i]._variable;
208  e >> literals[i]._value;
209  }
210  return new LDSBChoice<Val>(*this,a,p,v, literals, nliterals);
211  }
212 
213  template <>
214  inline
215  ModEvent
216  prune<Int::IntView>(Space& home, Int::IntView x, int v) {
217  return x.nq(home, v);
218  }
219 
220  template <>
221  inline
222  ModEvent
223  prune<Int::BoolView>(Space& home, Int::BoolView x, int v) {
224  return x.nq(home, v);
225  }
226 
227 
228  template<class View, int n, class Val, unsigned int a,
229  class Filter, class Print>
230  ExecStatus
232  ::commit(Space& home, const Choice& c, unsigned int b) {
233  const LDSBChoice<Val>& pvc
234  = static_cast<const LDSBChoice<Val>&>(c);
235  int choicePos = pvc.pos().pos;
236  int choiceVal = pvc.val();
237 
238  if (b == 0) {
239  // Post the branching constraint.
241  ::commit(home, c, b);
242  GECODE_ES_CHECK(fromBase);
243  for (int i = 0 ; i < this->_nsyms ; i++)
244  this->_syms[i]->update(Literal(choicePos, choiceVal));
245  } else if (b == 1) {
246  // Post the branching constraint.
248  ::commit(home, c, b);
249  GECODE_ES_CHECK(fromBase);
250 
251  // Post prunings.
252  int nliterals = pvc.nliterals();
253  const Literal* literals = pvc.literals();
254  for (int i = 0 ; i < nliterals ; i++) {
255  const Literal& l = literals[i];
256  ModEvent me = prune<View>(home, this->x[l._variable], l._value);
257  GECODE_ME_CHECK(me);
258  }
259  }
260 
261  return ES_OK;
262  }
263 
264  template<class View, int n, class Val, unsigned int a,
265  class Filter, class Print>
266  size_t
268  home.ignore(*this,AP_DISPOSE,true);
271  }
272 
273  template<class View, int n, class Val, unsigned int a>
274  forceinline void
276  ViewArray<View>& x,
277  ViewSel<View>* vs[n],
279  SymmetryImp<View>** syms, int nsyms,
282  if (bf) {
283  if (vvp) {
285  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
286  } else {
288  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
289  }
290  } else {
291  if (vvp) {
293  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
294  } else {
296  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
297  }
298  }
299  }
300 
301 }}}
302 
303 // STATISTICS: int-branch
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition: brancher.hpp:232
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1569
A Literal is a pair of variable index and value.
Definition: ldsb.hh:46
Actor must always be disposed.
Definition: core.hpp:561
virtual Actor * copy(Space &home)
Perform cloning.
Definition: brancher.hpp:138
LDSBChoice(const Brancher &b, unsigned int a, const Pos &p, const Val &n, const Literal *literals, int nliterals)
Initialize choice for brancher b, position p, value n, and set of literals literals (of size nliteral...
Definition: brancher.hpp:59
std::function< void(const Space &home, const Brancher &b, unsigned int a, Var x, int i, const Val &m, std::ostream &o)> VarValPrint
Function type for printing variable and value selection.
Definition: print.hpp:43
void postldsbbrancher(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **syms, int nsyms, BranchFilter< typename View::VarType > bf, VarValPrint< typename View::VarType, Val > vvp)
Post LDSB brancher.
Definition: brancher.hpp:275
int ModEvent
Type for modification events.
Definition: core.hpp:62
virtual void archive(Archive &e) const
Archive into e.
Definition: brancher.hpp:80
ViewArray< View > x
Views to branch on.
Definition: view.hpp:83
int _value
The value of the literal. For int and bool variables, this is the value itself; for set variables...
Definition: ldsb.hh:59
#define forceinline
Definition: config.hpp:185
int _variable
Variable index. The ViewArray that the index is meant for is assumed to be known by context...
Definition: ldsb.hh:55
Computation spaces.
Definition: core.hpp:1701
Class storing a print function.
Definition: print.hpp:47
const Pos & pos(void) const
Return position in array.
Definition: view.hpp:126
Class without print function.
Definition: print.hpp:73
Base-class for both propagators and branchers.
Definition: core.hpp:627
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition: view-val.hpp:292
Gecode::IntSet d(v, 7)
ViewSel< View > * vs[n]
View selection objects.
Definition: view.hpp:87
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2794
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition: brancher.hpp:267
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:91
bool operator<(const Literal &rhs) const
Less than. The ordering is the lexicographical order on the (variable,value) pair.
Definition: brancher.hpp:49
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Choice storing position and value, and symmetric literals to be excluded on the right branch...
Definition: ldsb.hh:301
Base-class for branchers.
Definition: core.hpp:1401
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Argument array for non-primitive types.
Definition: array.hpp:638
Gecode::IntArgs i({1, 2, 3, 4})
Generic brancher by view and value selection.
Definition: view-val.hpp:91
Literal(void)
Constructor for an empty literal.
Definition: brancher.hpp:40
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition: filter.hpp:41
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
Definition: view-val.hpp:99
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
static void post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **syms, int nsyms, BranchFilter< Var > bf, VarValPrint< Var, Val > vvp)
Brancher post function.
Definition: brancher.hpp:112
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:332
const Val & val(void) const
Definition: view-val.hpp:165
LDSBBrancher(Space &home, LDSBBrancher &b)
Constructor for cloning b.
Definition: brancher.hpp:125
virtual const Choice * choice(Space &home)
Return choice.
Definition: view-val.hpp:271
Position information.
Definition: view.hpp:44
View arrays.
Definition: array.hpp:235
~LDSBChoice(void)
Destructor.
Definition: brancher.hpp:66
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:52
Print p
Print function.
Definition: view-val.hpp:101
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3176
const int v[7]
Definition: distinct.cpp:259
int nliterals(void) const
Return number of literals.
Definition: brancher.hpp:76
Integer view for integer variables.
Definition: view.hpp:129
Implementation of a single symmetry.
Definition: ldsb.hh:162
virtual void archive(Archive &e) const
Archive into e.
Definition: view-val.hpp:171
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4001
Choice for performing commit
Definition: core.hpp:1371
Archive representation
Definition: archive.hpp:42
ExecStatus
Definition: core.hpp:471
Post propagator for SetVar x
Definition: set.hh:767
int _nsyms
Number of symmetry implementations.
Definition: ldsb.hh:338
Execution is okay.
Definition: core.hpp:475
SymmetryImp< View > ** _syms
Array of symmetry implementations.
Definition: ldsb.hh:336
const Literal * literals(void) const
Return literals.
Definition: brancher.hpp:72
Gecode toplevel namespace
Home class for posting propagators
Definition: core.hpp:853
Choice storing position and value
Definition: view-val.hpp:46
virtual const Choice * choice(Space &home)
Return choice.
Definition: brancher.hpp:148
void update(IntSet &y, Space &home, IntSet &py)
Definition: rel.hpp:103
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.
Boolean view for Boolean variables.
Definition: view.hpp:1380