Generated on for Gecode by doxygen 1.17.0
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 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34namespace Gecode { namespace Set { namespace LDSB {
35
36 template<class View, int n, class Val, unsigned int a,
37 class Filter, class Print>
39 ::LDSBSetBrancher(Home home, ViewArray<View>& x,
40 ViewSel<View>* vs[n],
41 ValSelCommitBase<View,Val>* vsc,
42 SymmetryImp<View>** syms, int nsyms,
45 : LDSBBrancher<View,n,Val,a,Filter,Print>
46 (home, x, vs, vsc, syms, nsyms, bf, vvp),
47 _prevPos(-1),
48 _copiedSyms(nullptr),
49 _nCopiedSyms(0),
50 _stable(false) {
51 // Put all the value symmetries at the end of the list.
52 int seen = 0;
53 int dest = this->_nsyms - 1;
54 for (int i = 0 ; i < this->_nsyms - seen ; i++) {
55 if (dynamic_cast<ValueSymmetryImp<View>*>(this->_syms[i])) {
56 SymmetryImp<View>* t = this->_syms[i];
57 this->_syms[i] = this->_syms[dest];
58 this->_syms[dest] = t;
59 dest--;
60 seen++;
61 }
62 }
63 _nValueSymmetries = seen;
64 _nNonValueSymmetries = this->_nsyms - seen;
65 }
66
67 template<class View, int n, class Val, unsigned int a,
68 class Filter, class Print>
69 LDSBSetBrancher<View,n,Val,a,Filter,Print>::
70 LDSBSetBrancher(Space& home,
71 LDSBSetBrancher<View,n,Val,a,Filter,Print>& b)
72 : LDSBBrancher<View,n,Val,a,Filter,Print>(home,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) {
80 _copiedSyms = home.alloc<ValueSymmetryImp<View>*>(_nCopiedSyms);
81 for (int i = 0 ; i < _nCopiedSyms ; i++)
82 _copiedSyms[i] = static_cast<ValueSymmetryImp<View>*>(
83 b._copiedSyms[i]->copy(home));
84 } else {
85 _copiedSyms = nullptr;
86 }
87 }
88
98 template <class View>
99 ValueSymmetryImp<View>*
101 // Calculate intersection and difference.
102 IntArgs intersection;
103 IntArgs difference;
104 for (int i = s->values.next(s->values.offset()) ;
105 i <= s->values.max_bit() ; i = s->values.next(i+1)) {
106 if (usedValues.in(i))
107 intersection << i;
108 else
109 difference << i;
110 }
111
112 for (IntSetValues v(usedValues) ; v() ; ++v) {
113 s->update(Literal(0, v.val()));
114 }
115
116 if (intersection.size() < 2)
117 return nullptr;
118 int *a = new int[intersection.size()];
119 for (int i = 0 ; i < intersection.size() ; i++) {
120 a[i] = intersection[i];
121 }
123 new (home) ValueSymmetryImp<View>(home, a, intersection.size());
124 delete [] a;
125 return ns;
126 }
127
128 template<class View, int n, class Val, unsigned int a,
129 class Filter, class Print>
130 void
132 updatePart1(Space& home, int choicePos) {
133 if (_nValueSymmetries > 0) {
134 // If this is a different variable from the last commit, restore
135 // the old value symmetries and update the copy.
136 if (choicePos != _prevPos) {
137 if (_prevPos != -1) {
138 int i = 0;
139 for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
140 this->_syms[j] = _copiedSyms[i];
141 i++;
142 }
143
144 for (int i = 0 ; i < _nCopiedSyms ; i++) {
147 if (ns) {
148 this->_syms = home.realloc<SymmetryImp<View>*>(this->_syms,
149 this->_nsyms,
150 this->_nsyms+1);
151 this->_syms[this->_nsyms] = ns;
152 this->_nsyms++;
153 this->_nValueSymmetries++;
154 }
155 }
156 }
157
158 // Reset for current variable, make copy of value symmetries
160 _prevPos = choicePos;
164 {
165 int i = 0;
166 for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
168 static_cast<ValueSymmetryImp<View>*>(this->_syms[j]);
169 _copiedSyms[i] =
170 static_cast<ValueSymmetryImp<View>*>(vsi->copy(home));
171 i++;
172 }
173 }
174 }
175 }
176 }
177
178 // Compute choice
179 template<class View, int n, class Val, unsigned int a,
180 class Filter, class Print>
181 const Choice*
183 // Making the PVC here is not so nice, I think.
185 const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
186
187 // Compute symmetries.
188
189 int choicePos = pvc->pos().pos;
190 delete c;
191
192 assert(!_stable);
193 updatePart1(home, choicePos);
194
196 }
197
198 template<class View, int n, class Val, unsigned int a,
199 class Filter, class Print>
202 ::commit(Space& home, const Choice& c, unsigned int b) {
203 const LDSBChoice<Val>& pvc
204 = static_cast<const LDSBChoice<Val>&>(c);
205 int choicePos = pvc.pos().pos;
206 int choiceVal = pvc.val();
207
208 if (!_stable)
209 updatePart1(home, choicePos);
210
211 if (b == 0) {
212 IntArgs ia;
213 for (IntSetValues v(_leftBranchValues) ; v() ; ++v) {
214 ia << v.val();
215 }
216 ia << choiceVal;
218
219 // Post the branching constraint.
221 ::commit(home, c, b);
222 GECODE_ES_CHECK(fromBase);
223 for (int i = 0 ; i < this->_nsyms ; i++)
224 this->_syms[i]->update(Literal(choicePos, choiceVal));
225 } else if (b == 1) {
226 // Post the branching constraint.
228 ::commit(home, c, b);
229 GECODE_ES_CHECK(fromBase);
230
231 // Post prunings.
232 int nliterals = pvc.nliterals();
233 const Literal* literals = pvc.literals();
234 for (int i = 0 ; i < nliterals ; i++) {
235 const Literal& l = literals[i];
236 ModEvent me = prune<View>(home, this->x[l._variable], l._value);
237 GECODE_ME_CHECK(me);
238 }
239 }
240
241 return ES_OK;
242 }
243
244 template<class View, int n, class Val, unsigned int a,
245 class Filter, class Print>
246 Actor*
251
252 template<class View, int n, class Val, unsigned int a,
253 class Filter, class Print>
254 forceinline void
264
265 template<class View, int n, class Val, unsigned int a>
266 forceinline void
269 ViewSel<View>* vs[n],
271 SymmetryImp<View>** syms, int nsyms,
274 if (bf) {
275 if (vvp) {
277 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
278 } else {
280 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
281 }
282 } else {
283 if (vvp) {
285 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
286 } else {
288 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
289 }
290 }
291 }
292
293}}}
294
295// STATISTICS: set-branch
Base-class for both propagators and branchers.
Definition core.hpp:635
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1597
Class without print function.
Definition print.hpp:73
Class storing a print function.
Definition print.hpp:47
friend class Space
Definition core.hpp:1455
friend class Choice
Definition core.hpp:1456
Choice for performing commit
Definition core.hpp:1423
Home class for posting propagators
Definition core.hpp:863
Passing integer arguments.
Definition int.hh:652
Value iterator for integer sets.
Definition int.hh:351
Integer sets.
Definition int.hh:178
bool in(int n) const
Return whether n is included in the set.
static const IntSet empty
Empty set.
Definition int.hh:301
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:332
int _nsyms
Number of symmetry implementations.
Definition ldsb.hh:338
SymmetryImp< View > ** _syms
Array of symmetry implementations.
Definition ldsb.hh:336
virtual const Choice * choice(Space &home)
Return choice.
Definition brancher.hpp:148
Choice storing position and value, and symmetric literals to be excluded on the right branch.
Definition ldsb.hh:301
int nliterals(void) const
Return number of literals.
Definition brancher.hpp:76
const Literal * literals(void) const
Return literals.
Definition brancher.hpp:72
A Literal is a pair of variable index and value.
Definition ldsb.hh:46
int _variable
Variable index. The ViewArray that the index is meant for is assumed to be known by context.
Definition ldsb.hh:55
int _value
The value of the literal. For int and bool variables, this is the value itself; for set variables,...
Definition ldsb.hh:59
Implementation of a single symmetry.
Definition ldsb.hh:162
Implementation of a value symmetry.
Definition ldsb.hh:204
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition sym-imp.hpp:165
Support::BitSetOffset< Space > values
Symmetric values.
Definition ldsb.hh:207
void update(Literal)
Left-branch update.
Definition sym-imp.hpp:158
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
Choice storing position and value
Definition view-val.hpp:46
const Val & val(void) const
Definition view-val.hpp:165
const int pos
Position of view.
Definition view.hpp:47
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:58
bool _stable
Is the state of the brancher "stable"?
Definition ldsb.hh:82
int _nCopiedSyms
Number of copied symmetries.
Definition ldsb.hh:71
int _prevPos
Position of previous variable that was branched on.
Definition ldsb.hh:62
ValueSymmetryImp< View > ** _copiedSyms
Copy of value symmetries from the first node where the current variable was branched on.
Definition ldsb.hh:69
virtual Actor * copy(Space &home)
Perform cloning.
Definition brancher.hpp:247
IntSet _leftBranchValues
Set of values used on left branches for the current variable.
Definition ldsb.hh:73
int _nNonValueSymmetries
Number of non-value symmetries.
Definition ldsb.hh:64
LDSBSetBrancher(Space &home, LDSBSetBrancher &b)
Constructor for cloning b.
void updatePart1(Space &home, int choicePos)
Part one of the update phase.
Definition brancher.hpp:132
int _nValueSymmetries
Number of value symmetries.
Definition ldsb.hh:66
virtual const Choice * choice(Space &home)
Return choice.
Definition brancher.hpp:182
Computation spaces.
Definition core.hpp:1775
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:2952
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition core.hpp:2901
void free(T *b, long unsigned int n)
Delete n objects allocated from space heap starting at b.
Definition core.hpp:2927
View arrays.
Definition array.hpp:255
ViewArray< View > x
Views to branch on.
Definition view.hpp:83
ViewSel< View > * vs[n]
View selection objects.
Definition view.hpp:87
Abstract class for view selection.
Definition view-sel.hpp:44
virtual const Choice * choice(Space &home)
Return choice.
Definition view-val.hpp:271
View::VarType Var
The corresponding variable.
Definition view-val.hpp:97
ViewValBrancher(Space &home, ViewValBrancher &b)
Constructor for cloning b.
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
Definition view-val.hpp:99
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
ModEvent prune(Space &home, View x, int v)
Exclude value v from variable view x.
Symmetry breaking for set variables.
void postldsbsetbrancher(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)
Definition brancher.hpp:267
ValueSymmetryImp< View > * specialUpdate(Space &home, ValueSymmetryImp< View > *s, IntSet usedValues)
Bulk update of a value symmetry s, using usedValues.
Definition brancher.hpp:100
Finite integer sets.
Definition var-imp.hpp:145
Gecode toplevel namespace
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
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:40
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:143
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
int ModEvent
Type for modification events.
Definition core.hpp:67