Generated on Sat Jul 28 2018 17:15:18 for Gecode by doxygen 1.8.14
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: 2017-04-01 20:27:10 +0200 (Sat, 01 Apr 2017) $ by $Author: schulte $
11  * $Revision: 15623 $
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  size_t
84  LDSBChoice<Val>::size(void) const {
85  return sizeof(LDSBChoice<Val>);
86  }
87 
88  template<class Val>
89  void
92  e << _nliterals;
93  for (int i = 0 ; i < _nliterals ; i++) {
94  e << _literals[i]._variable;
95  e << _literals[i]._value;
96  }
97  }
98 
99 
100 
101  template<class View, int n, class Val, unsigned int a,
102  class Filter, class Print>
105  ViewSel<View>* vs[n],
107  SymmetryImp<View>** syms, int nsyms,
110  : ViewValBrancher<View,n,Val,a,Filter,Print>(home, x, vs, vsc, bf, vvp),
111  _syms(syms),
112  _nsyms(nsyms),
113  _prevPos(-1)
114  {
115  home.notice(*this, AP_DISPOSE, true);
116  }
117 
118  template<class View, int n, class Val, unsigned int a,
119  class Filter, class Print>
120  forceinline void
124  SymmetryImp<View>** syms, int nsyms,
126  VarValPrint<Var,Val> vvp) {
128  (home,x,vs,vsc,syms,nsyms,bf,vvp);
129  }
130 
131  template<class View, int n, class Val, unsigned int a,
132  class Filter, class Print>
137  : ViewValBrancher<View,n,Val,a,Filter,Print>(home,shared,b),
138  _nsyms(b._nsyms),
139  _prevPos(b._prevPos) {
141  for (int i = 0 ; i < _nsyms ; i++)
142  _syms[i] = b._syms[i]->copy(home, shared);
143  }
144 
145  template<class View, int n, class Val, unsigned int a,
146  class Filter, class Print>
147  Actor*
150  (home,shared,*this);
151  }
152 
153 
154  // Compute choice
155  template<class View, int n, class Val, unsigned int a,
156  class Filter, class Print>
157  const Choice*
159  // Making the PVC here is not so nice, I think.
161  const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
162 
163  // Compute symmetries.
164 
165  int choicePos = pvc->pos().pos;
166  int choiceVal = pvc->val();
167  delete c;
168 
169  _prevPos = choicePos;
170 
171  // TODO: It should be possible to use simpler containers than the
172  // STL ones here.
173  std::deque<Literal> queue;
174  std::set<Literal> seen;
175 
176  seen.insert(Literal(choicePos, choiceVal));
177  queue.push_back(Literal(choicePos, choiceVal));
178 
179  do {
180  Literal l = queue[0];
181  queue.pop_front();
182 
183  for (int i = 0 ; i < _nsyms ; i++) {
184  ArgArray<Literal> toExclude = _syms[i]->symmetric(l, this->x);
185  for (int j = 0 ; j < toExclude.size() ; ++j) {
186  if (seen.find(toExclude[j]) == seen.end())
187  queue.push_back(toExclude[j]);
188  seen.insert(toExclude[j]);
189  }
190  }
191  } while (queue.size() > 0);
192 
193  // Convert "seen" vector into array.
194  int nliterals = static_cast<int>(seen.size());
195  Literal* literals = new Literal[nliterals];
196  std::set<Literal>::iterator it = seen.begin();
197  for (int i = 0 ; i < nliterals ; i++) {
198  literals[i] = *it;
199  ++it;
200  }
201 
202  return new LDSBChoice<Val>(*this,a,choicePos,choiceVal, literals, nliterals);
203  }
204 
205 
206  template<class View, int n, class Val, unsigned int a,
207  class Filter, class Print>
208  const Choice*
210  Archive& e) {
211  (void) home;
212  int p; e >> p;
213  Val v; e >> v;
214  int nliterals; e >> nliterals;
215  Literal* literals = new Literal[nliterals];
216  for (int i = 0 ; i < nliterals ; i++) {
217  e >> literals[i]._variable;
218  e >> literals[i]._value;
219  }
220  return new LDSBChoice<Val>(*this,a,p,v, literals, nliterals);
221  }
222 
223  template <>
224  inline
225  ModEvent
226  prune<Int::IntView>(Space& home, Int::IntView x, int v) {
227  return x.nq(home, v);
228  }
229 
230  template <>
231  inline
232  ModEvent
233  prune<Int::BoolView>(Space& home, Int::BoolView x, int v) {
234  return x.nq(home, v);
235  }
236 
237 
238  template<class View, int n, class Val, unsigned int a,
239  class Filter, class Print>
240  ExecStatus
242  ::commit(Space& home, const Choice& c, unsigned int b) {
243  const LDSBChoice<Val>& pvc
244  = static_cast<const LDSBChoice<Val>&>(c);
245  int choicePos = pvc.pos().pos;
246  int choiceVal = pvc.val();
247 
248  if (b == 0) {
249  // Post the branching constraint.
251  ::commit(home, c, b);
252  GECODE_ES_CHECK(fromBase);
253  for (int i = 0 ; i < this->_nsyms ; i++)
254  this->_syms[i]->update(Literal(choicePos, choiceVal));
255  } else if (b == 1) {
256  // Post the branching constraint.
258  ::commit(home, c, b);
259  GECODE_ES_CHECK(fromBase);
260 
261  // Post prunings.
262  int nliterals = pvc.nliterals();
263  const Literal* literals = pvc.literals();
264  for (int i = 0 ; i < nliterals ; i++) {
265  const Literal& l = literals[i];
266  ModEvent me = prune<View>(home, this->x[l._variable], l._value);
267  GECODE_ME_CHECK(me);
268  }
269  }
270 
271  return ES_OK;
272  }
273 
274  template<class View, int n, class Val, unsigned int a,
275  class Filter, class Print>
276  size_t
278  home.ignore(*this,AP_DISPOSE,true);
281  }
282 
283  template<class View, int n, class Val, unsigned int a>
284  forceinline void
287  ViewSel<View>* vs[n],
289  SymmetryImp<View>** syms, int nsyms,
292  if (bf) {
293  if (vvp) {
295  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
296  } else {
298  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
299  }
300  } else {
301  if (vvp) {
303  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
304  } else {
306  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
307  }
308  }
309  }
310 
311 }}}
312 
313 // 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:242
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1669
virtual size_t size(void) const
Report size occupied.
Definition: brancher.hpp:84
A Literal is a pair of variable index and value.
Definition: ldsb.hh:50
Actor must always be disposed.
Definition: core.hpp:630
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.
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:285
int ModEvent
Type for modification events.
Definition: core.hpp:142
virtual void archive(Archive &e) const
Archive into e.
Definition: brancher.hpp:90
int _value
The value of the literal. For int and bool variables, this is the value itself; for set variables...
Definition: ldsb.hh:63
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:1748
Class storing a print function.
const Pos & pos(void) const
Return position in array.
Class without print function.
Base-class for both propagators and branchers.
Definition: core.hpp:696
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Gecode::IntSet d(v, 7)
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2868
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition: brancher.hpp:277
#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
struct Gecode::@579::NNF::@61::@63 a
For atomic nodes.
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:1446
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Argument array for non-primitive types.
Definition: array.hpp:727
Generic brancher by view and value selection.
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.
struct Gecode::@579::NNF::@61::@62 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:122
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:338
const Val & val(void) const
Return value to branch with.
virtual const Choice * choice(Space &home)
Return choice.
Position information.
View arrays.
Definition: array.hpp:234
~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
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3321
const int v[7]
Definition: distinct.cpp:263
int nliterals(void) const
Return number of literals.
Definition: brancher.hpp:80
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.
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition: core.hpp:4141
Choice for performing commit
Definition: core.hpp:1414
Archive representation
Definition: archive.hpp:45
ExecStatus
Definition: core.hpp:540
#define forceinline
Definition: config.hpp:173
Post propagator for SetVar x
Definition: set.hh:784
int _nsyms
Number of symmetry implementations.
Definition: ldsb.hh:344
Execution is okay.
Definition: core.hpp:544
SymmetryImp< View > ** _syms
Array of symmetry implementations.
Definition: ldsb.hh:342
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:702
const Literal * literals(void) const
Return literals.
Definition: brancher.hpp:76
LDSBBrancher(Space &home, bool share, LDSBBrancher &b)
Constructor for cloning b.
Definition: brancher.hpp:135
Gecode toplevel namespace
Home class for posting propagators
Definition: core.hpp:922
Choice storing position and value
virtual const Choice * choice(Space &home)
Return choice.
Definition: brancher.hpp:158
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
virtual Actor * copy(Space &home, bool share)
Perform cloning.
Definition: brancher.hpp:148
Boolean view for Boolean variables.
Definition: view.hpp:1315