Generated on Sat Nov 9 2013 19:18:29 for Gecode by doxygen 1.8.4
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