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