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 namespace Gecode { namespace Set { namespace LDSB {
39 
40  template<class View, int n, class Val, unsigned int a,
41  class Filter, class Print>
44  ViewSel<View>* vs[n],
46  SymmetryImp<View>** syms, int nsyms,
49  : LDSBBrancher<View,n,Val,a,Filter,Print>
50  (home, x, vs, vsc, syms, nsyms, bf, vvp),
51  _prevPos(-1),
52  _copiedSyms(NULL),
53  _nCopiedSyms(0),
54  _stable(false) {
55  // Put all the value symmetries at the end of the list.
56  int seen = 0;
57  int dest = this->_nsyms - 1;
58  for (int i = 0 ; i < this->_nsyms - seen ; i++) {
59  if (dynamic_cast<ValueSymmetryImp<View>*>(this->_syms[i])) {
60  SymmetryImp<View>* t = this->_syms[i];
61  this->_syms[i] = this->_syms[dest];
62  this->_syms[dest] = t;
63  dest--;
64  seen++;
65  }
66  }
67  _nValueSymmetries = seen;
68  _nNonValueSymmetries = this->_nsyms - seen;
69  }
70 
71  template<class View, int n, class Val, unsigned int a,
72  class Filter, class Print>
76  : LDSBBrancher<View,n,Val,a,Filter,Print>(home,b),
77  _prevPos(b._prevPos),
78  _nNonValueSymmetries(b._nNonValueSymmetries),
79  _nValueSymmetries(b._nValueSymmetries),
80  _nCopiedSyms(b._nCopiedSyms),
81  _leftBranchValues(b._leftBranchValues),
82  _stable(b._stable) {
83  if (_nCopiedSyms > 0) {
85  for (int i = 0 ; i < _nCopiedSyms ; i++)
86  _copiedSyms[i] = static_cast<ValueSymmetryImp<View>*>(
87  b._copiedSyms[i]->copy(home));
88  } else {
89  _copiedSyms = NULL;
90  }
91  }
92 
102  template <class View>
105  // Calculate intersection and difference.
106  IntArgs intersection;
107  IntArgs difference;
108  int n = 0;
109  for (int i = s->values.next(s->values.offset()) ;
110  i <= s->values.max_bit() ; i = s->values.next(i+1)) {
111  n++;
112  if (usedValues.in(i))
113  intersection << i;
114  else
115  difference << i;
116  }
117 
118  for (IntSetValues v(usedValues) ; v() ; ++v) {
119  s->update(Literal(0, v.val()));
120  }
121 
122  if (intersection.size() < 2)
123  return NULL;
124  int *a = new int[intersection.size()];
125  for (int i = 0 ; i < intersection.size() ; i++) {
126  a[i] = intersection[i];
127  }
129  new (home) ValueSymmetryImp<View>(home, a, intersection.size());
130  delete [] a;
131  return ns;
132  }
133 
134  template<class View, int n, class Val, unsigned int a,
135  class Filter, class Print>
136  void
138  updatePart1(Space& home, int choicePos) {
139  if (_nValueSymmetries > 0) {
140  // If this is a different variable from the last commit, restore
141  // the old value symmetries and update the copy.
142  if (choicePos != _prevPos) {
143  if (_prevPos != -1) {
144  int i = 0;
145  for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
146  this->_syms[j] = _copiedSyms[i];
147  i++;
148  }
149 
150  for (int i = 0 ; i < _nCopiedSyms ; i++) {
153  if (ns) {
154  this->_syms = home.realloc<SymmetryImp<View>*>(this->_syms,
155  this->_nsyms,
156  this->_nsyms+1);
157  this->_syms[this->_nsyms] = ns;
158  this->_nsyms++;
159  this->_nValueSymmetries++;
160  }
161  }
162  }
163 
164  // Reset for current variable, make copy of value symmetries
166  _prevPos = choicePos;
167  if (_nCopiedSyms > 0) home.free(_copiedSyms, _nCopiedSyms);
170  int i = 0;
171  for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
173  static_cast<ValueSymmetryImp<View>*>(this->_syms[j]);
174  _copiedSyms[i] =
175  static_cast<ValueSymmetryImp<View>*>(vsi->copy(home));
176  i++;
177  }
178  }
179  }
180  }
181 
182  // Compute choice
183  template<class View, int n, class Val, unsigned int a,
184  class Filter, class Print>
185  const Choice*
187  // Making the PVC here is not so nice, I think.
189  const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
190 
191  // Compute symmetries.
192 
193  int choicePos = pvc->pos().pos;
194  delete c;
195 
196  assert(!_stable);
197  updatePart1(home, choicePos);
198 
200  }
201 
202  template<class View, int n, class Val, unsigned int a,
203  class Filter, class Print>
204  ExecStatus
206  ::commit(Space& home, const Choice& c, unsigned int b) {
207  const LDSBChoice<Val>& pvc
208  = static_cast<const LDSBChoice<Val>&>(c);
209  int choicePos = pvc.pos().pos;
210  int choiceVal = pvc.val();
211 
212  if (!_stable)
213  updatePart1(home, choicePos);
214 
215  if (b == 0) {
216  IntArgs ia;
217  for (IntSetValues v(_leftBranchValues) ; v() ; ++v) {
218  ia << v.val();
219  }
220  ia << choiceVal;
222 
223  // Post the branching constraint.
225  ::commit(home, c, b);
226  GECODE_ES_CHECK(fromBase);
227  for (int i = 0 ; i < this->_nsyms ; i++)
228  this->_syms[i]->update(Literal(choicePos, choiceVal));
229  } else if (b == 1) {
230  // Post the branching constraint.
232  ::commit(home, c, b);
233  GECODE_ES_CHECK(fromBase);
234 
235  // Post prunings.
236  int nliterals = pvc.nliterals();
237  const Literal* literals = pvc.literals();
238  for (int i = 0 ; i < nliterals ; i++) {
239  const Literal& l = literals[i];
240  ModEvent me = prune<View>(home, this->x[l._variable], l._value);
241  GECODE_ME_CHECK(me);
242  }
243  }
244 
245  return ES_OK;
246  }
247 
248  template<class View, int n, class Val, unsigned int a,
249  class Filter, class Print>
250  Actor*
253  (home,*this);
254  }
255 
256  template<class View, int n, class Val, unsigned int a,
257  class Filter, class Print>
258  forceinline void
262  SymmetryImp<View>** syms, int nsyms,
264  VarValPrint<Var,Val> vvp) {
266  (home,x,vs,vsc,syms,nsyms,bf,vvp);
267  }
268 
269  template<class View, int n, class Val, unsigned int a>
270  forceinline void
273  ViewSel<View>* vs[n],
275  SymmetryImp<View>** syms, int nsyms,
278  if (bf) {
279  if (vvp) {
281  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
282  } else {
284  ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
285  }
286  } else {
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  }
295  }
296 
297 }}}
298 
299 // STATISTICS: set-branch
int _nValueSymmetries
Number of value symmetries.
Definition: ldsb.hh:70
int _nNonValueSymmetries
Number of non-value symmetries.
Definition: ldsb.hh:68
NodeType t
Type of node.
Definition: bool-expr.cpp:234
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:62
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
void updatePart1(Space &home, int choicePos)
Part one of the update phase.
Definition: brancher.hpp:138
ValueSymmetryImp< View > * specialUpdate(Space &home, ValueSymmetryImp< View > *s, IntSet usedValues)
Bulk update of a value symmetry s, using usedValues.
Definition: brancher.hpp:104
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
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:2808
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition: brancher.hpp:206
int ModEvent
Type for modification events.
Definition: core.hpp:64
int _prevPos
Position of previous variable that was branched on.
Definition: ldsb.hh:66
Support::BitSetOffset< Space > values
Symmetric values.
Definition: ldsb.hh:211
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
void postldsbsetbrancher(Home home, ViewArray< View0 > &x, ViewSel< View0 > *vs[n0], ValSelCommitBase< View0, Val0 > *vsc, SymmetryImp< View0 > **syms, int nsyms, BranchFilter< typename View0::VarType > bf, VarValPrint< typename View0::VarType, Val0 > vvp)
Post LDSB brancher.
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
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
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:95
IntSet _leftBranchValues
Set of values used on left branches for the current variable.
Definition: ldsb.hh:77
Gecode::FloatVal c(-8, 8)
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)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
bool _stable
Is the state of the brancher "stable"?
Definition: ldsb.hh:86
Implementation of a value symmetry.
Definition: ldsb.hh:207
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
int _nCopiedSyms
Number of copied symmetries.
Definition: ldsb.hh:75
Symmetry-breaking brancher with generic view and value selection.
Definition: ldsb.hh:336
LDSBSetBrancher(Space &home, LDSBSetBrancher &b)
Constructor for cloning b.
Definition: brancher.hpp:74
const Val & val(void) const
Definition: view-val.hpp:169
Integer sets.
Definition: int.hh:174
virtual const Choice * choice(Space &home)
Return choice.
Definition: view-val.hpp:275
View arrays.
Definition: array.hpp:228
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:56
Passing integer arguments.
Definition: int.hh:608
static const IntSet empty
Empty set.
Definition: int.hh:263
void free(T *b, long unsigned int n)
Delete n objects allocated from space heap starting at b.
Definition: core.hpp:2783
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)
Implementation of a single symmetry.
Definition: ldsb.hh:166
void update(Literal)
Left-branch update.
Definition: sym-imp.hpp:162
virtual Actor * copy(Space &home)
Perform cloning.
Definition: brancher.hpp:251
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
Choice for performing commit
Definition: core.hpp:1338
ExecStatus
Definition: core.hpp:464
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:260
Value iterator for integer sets.
Definition: int.hh:313
ValueSymmetryImp< View > ** _copiedSyms
Copy of value symmetries from the first node where the current variable was branched on...
Definition: ldsb.hh:73
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
bool in(int n) const
Return whether n is included in the set.
Definition: int-set-1.hpp:140
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition: sym-imp.hpp:169
virtual const Choice * choice(Space &home)
Return choice.
Definition: brancher.hpp:186
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