Generated on Sat Feb 7 2015 02:01:24 for Gecode by doxygen 1.8.9.1
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: 2013-05-20 13:21:09 +0200 (Mon, 20 May 2013) $ by $Author: schulte $
11  * $Revision: 13644 $
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>
104  ViewSel<View>* vs[n],
106  SymmetryImp<View>** syms, int nsyms,
107  BranchFilter bf,
108  VarValPrint vvp)
109  : ViewValBrancher<View,n,Val,a>(home, x, vs, vsc, bf, vvp),
110  _syms(syms),
111  _nsyms(nsyms),
112  _prevPos(-1)
113  {
114  home.notice(*this, AP_DISPOSE, true);
115  }
116 
117  template<class View, int n, class Val, unsigned int a>
122  SymmetryImp<View>** syms, int nsyms,
123  BranchFilter bf, VarValPrint vvp) {
124  return *new (home) LDSBBrancher<View,n,Val,a>(home,x,vs,vsc,syms,nsyms,bf,vvp);
125  }
126 
127  template<class View, int n, class Val, unsigned int a>
131  : ViewValBrancher<View,n,Val,a>(home,shared,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, shared);
137  }
138 
139  template<class View, int n, class Val, unsigned int a>
140  Actor*
142  return new (home) LDSBBrancher<View,n,Val,a>(home,shared,*this);
143  }
144 
145 
146  // Compute choice
147  template<class View, int n, class Val, unsigned int a>
148  const Choice*
150  // Making the PVC here is not so nice, I think.
152  const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
153 
154  // Compute symmetries.
155 
156  int choicePos = pvc->pos().pos;
157  int choiceVal = pvc->val();
158  delete c;
159 
160  _prevPos = choicePos;
161 
162  // TODO: It should be possible to use simpler containers than the
163  // STL ones here.
164  std::deque<Literal> queue;
165  std::set<Literal> seen;
166 
167  seen.insert(Literal(choicePos, choiceVal));
168  queue.push_back(Literal(choicePos, choiceVal));
169 
170  do {
171  Literal l = queue[0];
172  queue.pop_front();
173 
174  for (int i = 0 ; i < _nsyms ; i++) {
175  ArgArray<Literal> toExclude = _syms[i]->symmetric(l, this->x);
176  for (int j = 0 ; j < toExclude.size() ; ++j) {
177  if (seen.find(toExclude[j]) == seen.end())
178  queue.push_back(toExclude[j]);
179  seen.insert(toExclude[j]);
180  }
181  }
182  } while (queue.size() > 0);
183 
184  // Convert "seen" vector into array.
185  int nliterals = seen.size();
186  Literal* literals = new Literal[nliterals];
187  std::set<Literal>::iterator it = seen.begin();
188  for (int i = 0 ; i < nliterals ; i++) {
189  literals[i] = *it;
190  ++it;
191  }
192 
193  return new LDSBChoice<Val>(*this,a,choicePos,choiceVal, literals, nliterals);
194  }
195 
196 
197  template<class View, int n, class Val, unsigned int a>
198  const Choice*
200  (void) home;
201  int p; e >> p;
202  Val v; e >> v;
203  int nliterals; e >> nliterals;
204  Literal* literals = new Literal[nliterals];
205  for (int i = 0 ; i < nliterals ; i++) {
206  e >> literals[i]._variable;
207  e >> literals[i]._value;
208  }
209  return new LDSBChoice<Val>(*this,a,p,v, literals, nliterals);
210  }
211 
212  template <>
213  inline
214  ModEvent
215  prune<Int::IntView>(Space& home, Int::IntView x, int v) {
216  return x.nq(home, v);
217  }
218 
219  template <>
220  inline
221  ModEvent
222  prune<Int::BoolView>(Space& home, Int::BoolView x, int v) {
223  return x.nq(home, v);
224  }
225 
226 
227  template<class View, int n, class Val, unsigned int a>
228  ExecStatus
230  ::commit(Space& home, const Choice& c, unsigned int b) {
231  const LDSBChoice<Val>& pvc
232  = static_cast<const LDSBChoice<Val>&>(c);
233  int choicePos = pvc.pos().pos;
234  int choiceVal = pvc.val();
235 
236  if (b == 0) {
237  // Post the branching constraint.
238  ExecStatus fromBase = ViewValBrancher<View,n,Val,a>::commit(home, c, b);
239  GECODE_ES_CHECK(fromBase);
240  for (int i = 0 ; i < this->_nsyms ; i++)
241  this->_syms[i]->update(Literal(choicePos, choiceVal));
242  } else if (b == 1) {
243  // Post the branching constraint.
244  ExecStatus fromBase = ViewValBrancher<View,n,Val,a>::commit(home, c, b);
245  GECODE_ES_CHECK(fromBase);
246 
247  // Post prunings.
248  int nliterals = pvc.nliterals();
249  const Literal* literals = pvc.literals();
250  for (int i = 0 ; i < nliterals ; i++) {
251  const Literal& l = literals[i];
252  ModEvent me = prune<View>(home, this->x[l._variable], l._value);
253  GECODE_ME_CHECK(me);
254  }
255  }
256 
257  return ES_OK;
258  }
259 
260  template<class View, int n, class Val, unsigned int a>
261  size_t
263  home.ignore(*this,AP_DISPOSE,true);
265  return sizeof(LDSBBrancher<View,n,Val,a>);
266  }
267 
268 }}}
269 
270 // STATISTICS: int-branch
const Pos & pos(void) const
Return position in array.
int _nsyms
Number of symmetry implementations.
Definition: ldsb.hh:341
const Val & val(void) const
Return value to branch with.
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
A Literal is a pair of variable index and value.
Definition: ldsb.hh:50
Actor must always be disposed.
Definition: core.hpp:610
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
bool operator<(const Literal &rhs) const
Less than. The ordering is the lexicographical order on the (variable,value) pair.
Definition: brancher.hpp:53
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
Handle for brancher.
Definition: core.hpp:1157
virtual Actor * copy(Space &home, bool share)
Perform cloning.
Definition: brancher.hpp:141
virtual size_t size(void) const
Report size occupied.
Definition: brancher.hpp:84
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
int ModEvent
Type for modification events.
Definition: core.hpp:146
virtual const Choice * choice(Space &home)
Return choice.
Definition: brancher.hpp:149
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:1362
SymmetryImp< View > ** _syms
Array of symmetry implementations.
Definition: ldsb.hh:339
Base-class for both propagators and branchers.
Definition: core.hpp:666
int nliterals(void) const
Return number of literals.
Definition: brancher.hpp:80
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:2397
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
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:303
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
Argument array for non-primitive types.
Definition: array.hpp:727
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition: brancher.hpp:262
Generic brancher by view and value selection.
Literal(void)
Constructor for an empty literal.
Definition: brancher.hpp:44
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:335
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:45
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:2849
const int v[7]
Definition: distinct.cpp:207
Integer view for integer variables.
Definition: view.hpp:129
Implementation of a single symmetry.
Definition: ldsb.hh:166
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
static BrancherHandle post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **syms, int nsyms, BranchFilter bf, VarValPrint vvp)
Brancher post function.
Definition: brancher.hpp:120
Execution is okay.
Definition: core.hpp:527
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
virtual void archive(Archive &e) const
Archive into e.
Definition: brancher.hpp:90
LDSBBrancher(Space &home, bool share, LDSBBrancher &b)
Constructor for cloning b.
Definition: brancher.hpp:130
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
const Literal * literals(void) const
Return literals.
Definition: brancher.hpp:76
virtual void archive(Archive &e) const
Archive into e.
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition: brancher.hpp:230
Home class for posting propagators
Definition: core.hpp:717
Choice storing position and value
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Boolean view for Boolean variables.
Definition: view.hpp:1315