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-02 17:10:16 +0200 (Thu, 02 May 2013) $ by $Author: schulte $
11  * $Revision: 13603 $
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 { namespace Set { namespace LDSB {
39 
40  template<class View, int n, class Val, unsigned int a>
43  ViewSel<View>* vs[n],
45  SymmetryImp<View>** syms, int nsyms,
46  SetBranchFilter bf,
47  VarValPrint vvp)
48  : LDSBBrancher<View,n,Val,a>(home, x, vs, vsc, syms, nsyms, bf, vvp),
49  _prevPos(-1),
50  _copiedSyms(NULL),
51  _nCopiedSyms(0),
52  _stable(false) {
53  // Put all the value symmetries at the end of the list.
54  int seen = 0;
55  int dest = this->_nsyms - 1;
56  for (int i = 0 ; i < this->_nsyms - seen ; i++) {
57  if (dynamic_cast<ValueSymmetryImp<View>*>(this->_syms[i])) {
58  SymmetryImp<View>* t = this->_syms[i];
59  this->_syms[i] = this->_syms[dest];
60  this->_syms[dest] = t;
61  dest--;
62  seen++;
63  }
64  }
65  _nValueSymmetries = seen;
66  _nNonValueSymmetries = this->_nsyms - seen;
67  }
68 
69  template<class View, int n, class Val, unsigned int a>
72  : LDSBBrancher<View,n,Val,a>(home,shared,b),
73  _prevPos(b._prevPos),
74  _nNonValueSymmetries(b._nNonValueSymmetries),
75  _nValueSymmetries(b._nValueSymmetries),
76  _nCopiedSyms(b._nCopiedSyms),
77  _leftBranchValues(b._leftBranchValues),
78  _stable(b._stable) {
79  if (_nCopiedSyms > 0) {
81  for (int i = 0 ; i < _nCopiedSyms ; i++)
82  _copiedSyms[i] = static_cast<ValueSymmetryImp<View>*>(
83  b._copiedSyms[i]->copy(home, shared));
84  } else {
85  _copiedSyms = NULL;
86  }
87  }
88 
98  template <class View>
101  // Calculate intersection and difference.
102  IntArgs intersection;
103  IntArgs difference;
104  int n = 0;
105  for (int i = s->values.next(s->values.offset()) ;
106  i <= s->values.max_bit() ; i = s->values.next(i+1)) {
107  n++;
108  if (usedValues.in(i))
109  intersection << i;
110  else
111  difference << i;
112  }
113 
114  for (IntSetValues v(usedValues) ; v() ; ++v) {
115  s->update(Literal(0, v.val()));
116  }
117 
118  if (intersection.size() < 2)
119  return NULL;
120  int *a = new int[intersection.size()];
121  for (int i = 0 ; i < intersection.size() ; i++) {
122  a[i] = intersection[i];
123  }
125  new (home) ValueSymmetryImp<View>(home, a, intersection.size());
126  delete [] a;
127  return ns;
128  }
129 
130  template<class View, int n, class Val, unsigned int a>
131  void
133  updatePart1(Space& home, int choicePos) {
134  if (_nValueSymmetries > 0) {
135  // If this is a different variable from the last commit, restore
136  // the old value symmetries and update the copy.
137  if (choicePos != _prevPos) {
138  if (_prevPos != -1) {
139  int i = 0;
140  for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
141  this->_syms[j] = _copiedSyms[i];
142  i++;
143  }
144 
145  for (int i = 0 ; i < _nCopiedSyms ; i++) {
147  specialUpdate(home, _copiedSyms[i], _leftBranchValues);
148  if (ns) {
149  this->_syms = home.realloc<SymmetryImp<View>*>(this->_syms,
150  this->_nsyms,
151  this->_nsyms+1);
152  this->_syms[this->_nsyms] = ns;
153  this->_nsyms++;
154  this->_nValueSymmetries++;
155  }
156  }
157  }
158 
159  // Reset for current variable, make copy of value symmetries
160  _leftBranchValues = IntSet::empty;
161  _prevPos = choicePos;
162  if (_nCopiedSyms > 0) home.free(_copiedSyms, _nCopiedSyms);
163  _nCopiedSyms = _nValueSymmetries;
164  _copiedSyms = home.alloc<ValueSymmetryImp<View>*>(_nCopiedSyms);
165  int i = 0;
166  for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
167  ValueSymmetryImp<View>* vsi =
168  static_cast<ValueSymmetryImp<View>*>(this->_syms[j]);
169  _copiedSyms[i] =
170  static_cast<ValueSymmetryImp<View>*>(vsi->copy(home, false));
171  i++;
172  }
173  }
174  }
175  }
176 
177  // Compute choice
178  template<class View, int n, class Val, unsigned int a>
179  const Choice*
181  // Making the PVC here is not so nice, I think.
183  const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
184 
185  // Compute symmetries.
186 
187  int choicePos = pvc->pos().pos;
188  delete c;
189 
190  assert(!_stable);
191  updatePart1(home, choicePos);
192 
194  }
195 
196  template<class View, int n, class Val, unsigned int a>
197  ExecStatus
199  ::commit(Space& home, const Choice& c, unsigned int b) {
200  const LDSBChoice<Val>& pvc
201  = static_cast<const LDSBChoice<Val>&>(c);
202  int choicePos = pvc.pos().pos;
203  int choiceVal = pvc.val();
204 
205  if (!_stable)
206  updatePart1(home, choicePos);
207 
208  if (b == 0) {
209  IntArgs ia;
210  for (IntSetValues v(_leftBranchValues) ; v() ; ++v) {
211  ia << v.val();
212  }
213  ia << choiceVal;
214  _leftBranchValues = IntSet(ia);
215 
216  // Post the branching constraint.
217  ExecStatus fromBase = ViewValBrancher<View,n,Val,a>::commit(home, c, b);
218  GECODE_ES_CHECK(fromBase);
219  for (int i = 0 ; i < this->_nsyms ; i++)
220  this->_syms[i]->update(Literal(choicePos, choiceVal));
221  } else if (b == 1) {
222  // Post the branching constraint.
223  ExecStatus fromBase = ViewValBrancher<View,n,Val,a>::commit(home, c, b);
224  GECODE_ES_CHECK(fromBase);
225 
226  // Post prunings.
227  int nliterals = pvc.nliterals();
228  const Literal* literals = pvc.literals();
229  for (int i = 0 ; i < nliterals ; i++) {
230  const Literal& l = literals[i];
231  ModEvent me = prune<View>(home, this->x[l._variable], l._value);
232  GECODE_ME_CHECK(me);
233  }
234  }
235 
236  return ES_OK;
237  }
238 
239  template<class View, int n, class Val, unsigned int a>
240  Actor*
242  return new (home) LDSBSetBrancher<View,n,Val,a>(home,shared,*this);
243  }
244 
245  template<class View, int n, class Val, unsigned int a>
250  SymmetryImp<View>** syms, int nsyms,
251  SetBranchFilter bf,
252  VarValPrint vvp) {
253  return *new (home) LDSBSetBrancher<View,n,Val,a>(home,x,vs,vsc,syms,nsyms,bf,vvp);
254  }
255 
256 }}}
257 
258 // STATISTICS: set-branch
const Pos & pos(void) const
Return position in array.
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:61
const Val & val(void) const
Return value to branch with.
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition: brancher.hpp:199
A Literal is a pair of variable index and value.
Definition: ldsb.hh:50
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:140
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
Handle for brancher.
Definition: core.hpp:1157
ValueSymmetryImp< View > * specialUpdate(Space &home, ValueSymmetryImp< View > *s, IntSet usedValues)
Bulk update of a value symmetry s, using usedValues.
Definition: brancher.hpp:100
T * realloc(T *b, long unsigned int n, long unsigned int m)
Reallocate block of n objects starting at b to m objects of type T from the space heap...
Definition: core.hpp:2448
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
Support::BitSetOffset< Space > values
Symmetric values.
Definition: ldsb.hh:209
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
Base-class for both propagators and branchers.
Definition: core.hpp:666
int nliterals(void) const
Return number of literals.
Definition: brancher.hpp:80
ValueSymmetryImp< View > ** _copiedSyms
Copy of value symmetries from the first node where the current variable was branched on...
Definition: ldsb.hh:77
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)
void updatePart1(Space &home, int choicePos)
Part one of the update phase.
Definition: brancher.hpp:133
Choice storing position and value, and symmetric literals to be excluded on the right branch...
Definition: ldsb.hh:303
LDSBSetBrancher(Space &home, bool share, LDSBSetBrancher &b)
Constructor for cloning b.
Definition: brancher.hpp:71
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Implementation of a value symmetry.
Definition: ldsb.hh:205
int _nCopiedSyms
Number of copied symmetries.
Definition: ldsb.hh:79
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:335
virtual const Choice * choice(Space &home)
Return choice.
Definition: brancher.hpp:180
Integer sets.
Definition: int.hh:171
View arrays.
Definition: array.hpp:234
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Passing integer arguments.
Definition: int.hh:607
static const IntSet empty
Empty set.
Definition: int.hh:262
void free(T *b, long unsigned int n)
Delete n objects allocated from space heap starting at b.
Definition: core.hpp:2423
const int v[7]
Definition: distinct.cpp:207
Implementation of a single symmetry.
Definition: ldsb.hh:166
void update(Literal)
Left-branch update.
Definition: sym-imp.hpp:161
virtual const Choice * choice(Space &home)
Return choice.
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Choice for performing commit
Definition: core.hpp:1036
ExecStatus
Definition: core.hpp:523
static BrancherHandle post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **_syms, int _nsyms, SetBranchFilter bf, VarValPrint vvp)
Brancher post function.
Definition: brancher.hpp:248
#define forceinline
Definition: config.hpp:132
Value iterator for integer sets.
Definition: int.hh:312
bool(* SetBranchFilter)(const Space &home, SetVar x, int i)
Branch filter function type for set variables.
Definition: set.hh:1102
virtual Actor * copy(Space &home, bool share)
Perform cloning.
Definition: brancher.hpp:241
SymmetryImp< View > * copy(Space &home, bool share) const
Copy function.
Definition: sym-imp.hpp:169
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
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
Home class for posting propagators
Definition: core.hpp:717
Choice storing position and value
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.