Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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.org
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
34#include <deque>
35#include <set>
36
37namespace Gecode { namespace Int { namespace LDSB {
38
41 : _variable(-1), _value(-1) {}
42
44 Literal::Literal(int idx, int val)
45 : _variable(idx), _value(val) {}
46
48 bool
49 Literal::operator <(const Literal &rhs) const {
50 int d = rhs._variable - _variable;
51 if (d > 0) return true;
52 else if (d == 0) return rhs._value > _value;
53 else return false;
54 }
55
56
57 template<class Val>
58 inline
59 LDSBChoice<Val>::LDSBChoice(const Brancher& b, unsigned int a, const Pos& p,
60 const Val& n, const Literal* literals,
61 int nliterals)
62 : PosValChoice<Val>(b,a,p,n), _literals(literals), _nliterals(nliterals)
63 {}
64
65 template<class Val>
67 delete [] _literals;
68 }
69
70 template<class Val>
71 forceinline const Literal*
72 LDSBChoice<Val>::literals(void) const { return _literals; }
73
74 template<class Val>
75 forceinline int
76 LDSBChoice<Val>::nliterals(void) const { return _nliterals; }
77
78 template<class Val>
79 void
82 e << _nliterals;
83 for (int i = 0 ; i < _nliterals ; i++) {
84 e << _literals[i]._variable;
85 e << _literals[i]._value;
86 }
87 }
88
89
90
91 template<class View, int n, class Val, unsigned int a,
92 class Filter, class Print>
95 ViewSel<View>* vs[n],
97 SymmetryImp<View>** syms, int nsyms,
100 : ViewValBrancher<View,n,Val,a,Filter,Print>(home, x, vs, vsc, bf, vvp),
101 _syms(syms),
102 _nsyms(nsyms),
103 _prevPos(-1)
104 {
105 home.notice(*this, AP_DISPOSE, true);
106 }
107
108 template<class View, int n, class Val, unsigned int a,
109 class Filter, class Print>
110 forceinline void
120
121 template<class View, int n, class Val, unsigned int a,
122 class Filter, class Print>
125 LDSBBrancher(Space& home,
127 : ViewValBrancher<View,n,Val,a,Filter,Print>(home,b),
128 _nsyms(b._nsyms),
129 _prevPos(b._prevPos) {
131 for (int i = 0 ; i < _nsyms ; i++)
132 _syms[i] = b._syms[i]->copy(home);
133 }
134
135 template<class View, int n, class Val, unsigned int a,
136 class Filter, class Print>
137 Actor*
142
143
144 // Compute choice
145 template<class View, int n, class Val, unsigned int a,
146 class Filter, class Print>
147 const Choice*
149 // Making the PVC here is not so nice, I think.
151 const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
152
153 // Compute symmetries.
154
155 int choicePos = pvc->pos().pos;
156 int choiceVal = pvc->val();
157 delete c;
158
159 _prevPos = choicePos;
160
161 // TODO: It should be possible to use simpler containers than the
162 // STL ones here.
163 std::deque<Literal> queue;
164 std::set<Literal> seen;
165
166 seen.insert(Literal(choicePos, choiceVal));
167 queue.push_back(Literal(choicePos, choiceVal));
168
169 do {
170 Literal l = queue[0];
171 queue.pop_front();
172
173 for (int i = 0 ; i < _nsyms ; i++) {
174 ArgArray<Literal> toExclude = _syms[i]->symmetric(l, this->x);
175 for (int j = 0 ; j < toExclude.size() ; ++j) {
176 if (seen.find(toExclude[j]) == seen.end())
177 queue.push_back(toExclude[j]);
178 seen.insert(toExclude[j]);
179 }
180 }
181 } while (queue.size() > 0);
182
183 // Convert "seen" vector into array.
184 int nliterals = static_cast<int>(seen.size());
185 Literal* literals = new Literal[nliterals];
186 std::set<Literal>::iterator it = seen.begin();
187 for (int i = 0 ; i < nliterals ; i++) {
188 literals[i] = *it;
189 ++it;
190 }
191
192 return new LDSBChoice<Val>(*this,a,choicePos,choiceVal, literals, nliterals);
193 }
194
195
196 template<class View, int n, class Val, unsigned int a,
197 class Filter, class Print>
198 const Choice*
200 Archive& e) {
201 (void) home;
202 int p; e >> p;
203 Val v; e >> v;
204 int nliterals; e >> nliterals;
205 Literal* literals = new Literal[nliterals];
206 for (int i = 0 ; i < nliterals ; i++) {
207 e >> literals[i]._variable;
208 e >> literals[i]._value;
209 }
210 return new LDSBChoice<Val>(*this,a,p,v, literals, nliterals);
211 }
212
213 template <>
214 inline
216 prune<Int::IntView>(Space& home, Int::IntView x, int v) {
217 return x.nq(home, v);
218 }
219
220 template <>
221 inline
224 return x.nq(home, v);
225 }
226
227
228 template<class View, int n, class Val, unsigned int a,
229 class Filter, class Print>
232 ::commit(Space& home, const Choice& c, unsigned int b) {
233 const LDSBChoice<Val>& pvc
234 = static_cast<const LDSBChoice<Val>&>(c);
235 int choicePos = pvc.pos().pos;
236 int choiceVal = pvc.val();
237
238 if (b == 0) {
239 // Post the branching constraint.
241 ::commit(home, c, b);
242 GECODE_ES_CHECK(fromBase);
243 for (int i = 0 ; i < this->_nsyms ; i++)
244 this->_syms[i]->update(Literal(choicePos, choiceVal));
245 } else if (b == 1) {
246 // Post the branching constraint.
248 ::commit(home, c, b);
249 GECODE_ES_CHECK(fromBase);
250
251 // Post prunings.
252 int nliterals = pvc.nliterals();
253 const Literal* literals = pvc.literals();
254 for (int i = 0 ; i < nliterals ; i++) {
255 const Literal& l = literals[i];
256 ModEvent me = prune<View>(home, this->x[l._variable], l._value);
257 GECODE_ME_CHECK(me);
258 }
259 }
260
261 return ES_OK;
262 }
263
264 template<class View, int n, class Val, unsigned int a,
265 class Filter, class Print>
266 size_t
272
273 template<class View, int n, class Val, unsigned int a>
274 forceinline void
277 ViewSel<View>* vs[n],
279 SymmetryImp<View>** syms, int nsyms,
282 if (bf) {
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 } else {
291 if (vvp) {
293 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
294 } else {
296 ::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
297 }
298 }
299 }
300
301}}}
302
303// STATISTICS: int-branch
NNF * l
Left subtree.
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int p
Number of positive literals for node type.
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Base-class for both propagators and branchers.
Definition core.hpp:628
Archive representation
Definition archive.hpp:42
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Argument array for non-primitive types.
Definition array.hpp:691
Class without print function.
Definition print.hpp:73
Class storing a print function.
Definition print.hpp:47
Base-class for branchers.
Definition core.hpp:1442
Choice for performing commit
Definition core.hpp:1412
Home class for posting propagators
Definition core.hpp:856
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3219
Boolean view for Boolean variables.
Definition view.hpp:1380
Integer view for integer variables.
Definition view.hpp:129
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:332
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:112
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
virtual Actor * copy(Space &home)
Perform cloning.
Definition brancher.hpp:138
LDSBBrancher(Space &home, LDSBBrancher &b)
Constructor for cloning b.
Definition brancher.hpp:125
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition brancher.hpp:267
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
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:59
virtual void archive(Archive &e) const
Archive into e.
Definition brancher.hpp:80
~LDSBChoice(void)
Destructor.
Definition brancher.hpp:66
A Literal is a pair of variable index and value.
Definition ldsb.hh:46
bool operator<(const Literal &rhs) const
Less than. The ordering is the lexicographical order on the (variable,value) pair.
Definition brancher.hpp:49
Literal(void)
Constructor for an empty literal.
Definition brancher.hpp:40
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
virtual SymmetryImp< View > * copy(Space &home) const =0
Copy function.
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
virtual void archive(Archive &e) const
Archive into e.
Definition view-val.hpp:171
Position information.
Definition view.hpp:44
const int pos
Position of view.
Definition view.hpp:47
Computation spaces.
Definition core.hpp:1742
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition core.hpp:2837
Base class for value selection and commit.
View arrays.
Definition array.hpp:253
Abstract class for view selection.
Definition view-sel.hpp:44
Generic brancher by view and value selection.
Definition view-val.hpp:91
View::VarType Var
The corresponding variable.
Definition view-val.hpp:97
virtual const Choice * choice(Space &home)
Return choice.
Definition view-val.hpp:271
void update(const NoOffset &)
Integer-precision integer scale view.
Definition view.hpp:638
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
#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
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
ModEvent prune(Space &home, View x, int v)
Exclude value \v from variable view \x.
ModEvent prune< Int::BoolView >(Space &home, Int::BoolView x, int v)
Definition brancher.hpp:223
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:275
ModEvent prune< Int::IntView >(Space &home, Int::IntView x, int v)
Definition brancher.hpp:216
Gecode toplevel namespace
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
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
ExecStatus
Definition core.hpp:472
@ ES_OK
Execution is okay.
Definition core.hpp:476
Post propagator for SetVar x
Definition set.hh:767
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
int ModEvent
Type for modification events.
Definition core.hpp:62
#define forceinline
Definition config.hpp:187