Generated on Sat Nov 9 2013 19:18:31 for Gecode by doxygen 1.8.4
nogoods.hh
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2013
8  *
9  * Last modified:
10  * $Date: 2013-10-30 16:01:30 +0100 (Wed, 30 Oct 2013) $ by $Author: schulte $
11  * $Revision: 14038 $
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 #ifndef __GECODE_SEARCH_META_NO_GOODS_HH__
39 #define __GECODE_SEARCH_META_NO_GOODS_HH__
40 
41 #include <gecode/search.hh>
42 
43 namespace Gecode { namespace Search { namespace Meta {
44 
46  class NoNGL : public NGL {
47  public:
49  NoNGL(void);
51  NoNGL(Space& home);
53  NoNGL(Space& home, bool share, NoNGL& ngl);
55  virtual void subscribe(Space& home, Propagator& p);
57  virtual void cancel(Space& home, Propagator& p);
59  virtual NGL::Status status(const Space& home) const;
61  virtual ExecStatus prune(Space& home);
63  virtual NGL* copy(Space& home, bool share);
64  };
65 
67  class NoGoodsProp : public Propagator {
68  protected:
72  unsigned int n;
74  NoGoodsProp(Home home, NGL* root);
76  NoGoodsProp(Space& home, bool shared, NoGoodsProp& p);
77  public:
79  virtual Actor* copy(Space& home, bool share);
81  virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
83  virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
85  template<class Path>
86  static ExecStatus post(Space& home, Path& p);
88  virtual size_t dispose(Space& home);
89  };
90 
92  NoNGL::NoNGL(void) {}
93 
96  : NGL(home) {}
97 
99  NoNGL::NoNGL(Space& home, bool share, NoNGL& ngl)
100  : NGL(home,share,ngl) {}
101 
102 
103 
106  : Propagator(home), root(root0), n(0U) {
107  // Create subscriptions
108  root->subscribe(home,*this); n++;
109  bool notice = root->notice();
110  NGL* l = root->next();
111  while ((l != NULL) && l->leaf()) {
112  l->subscribe(home,*this); n++;
113  notice = notice || l->notice();
114  l = l->next();
115  }
116  if (l != NULL) {
117  l->subscribe(home,*this); n++;
118  }
119  while (!notice && (l != NULL)) {
120  notice = notice || l->notice();
121  l = l->next();
122  }
123  if (notice)
124  home.notice(*this,AP_DISPOSE);
125  }
126 
129  : Propagator(home,shared,p), n(p.n) {
130  assert(p.root != NULL);
131  NoNGL s;
132  NGL* c = &s;
133  for (NGL* pc = p.root; pc != NULL; pc = pc->next()) {
134  NGL* n = pc->copy(home,shared);
135  n->leaf(pc->leaf());
136  c->next(n); c=n;
137  }
138  root = s.next();
139  }
140 
141 
142 
143  template<class Path>
145  NoGoodsProp::post(Space& home, Path& p) {
146  int s = 0;
147  int n = std::min(p.ds.entries(),p.ngdl());
148 
149  unsigned long int n_nogood = 0;
150 
151  // Eliminate the alternatives which are not no-goods at the end
152  while ((n > s) && (p.ds[n-1].truealt() == 0U))
153  n--;
154 
155  // A sentinel element
156  NoNGL nn;
157  // Current no-good literal
158  NGL* c = &nn;
159 
160  // Commit no-goods at the beginning
161  while ((s < n) && (p.ds[s].truealt() > 0U))
162  // Try whether this is a rightmost alternative
163  if (p.ds[s].rightmost()) {
164  // No literal needed, directly try to commit
165  home.trycommit(*p.ds[s].choice(),p.ds[s].truealt());
166  s++;
167  } else {
168  // Prune using no-good literals
169  for (unsigned int a=0U; a<p.ds[s].truealt(); a++) {
170  NGL* l = home.ngl(*p.ds[s].choice(),a);
171  // Does the brancher support no-good literals?
172  if (l == NULL)
173  return ES_OK;
174  GECODE_ES_CHECK(l->prune(home));
175  }
176  // Add literal as root if needed and stop
177  if (NGL* l = home.ngl(*p.ds[s].choice(),p.ds[s].truealt())) {
178  c = c->add(l,false);
179  s++; break;
180  }
181  }
182 
183  // There are no literals
184  if (home.failed())
185  return ES_FAILED;
186  if (s >= n)
187  return ES_OK;
188 
189  // There must be at least two literals
190  assert((n-s > 1) ||
191  ((n-s == 1) && (c != &nn)));
192 
193  // Remember the last leaf
194  NGL* ll = NULL;
195 
196  // Create literals
197  for (int i=s; i<n; i++) {
198  // Add leaves
199  for (unsigned int a=0U; a<p.ds[i].truealt(); a++) {
200  NGL* l = home.ngl(*p.ds[i].choice(),a);
201  if (l == NULL) {
202  // The brancher does not support no-goods
203  if (ll == NULL)
204  return ES_OK;
205  ll->next(NULL);
206  break;
207  }
208  c = c->add(l,true); ll = c;
209  n_nogood++;
210  }
211  // Check whether to add an additional subtree
212  if (NGL* l = home.ngl(*p.ds[i].choice(),p.ds[i].truealt())) {
213  c = c->add(l,false);
214  } else if (!p.ds[i].rightmost()) {
215  // The brancher does not support no-goods
216  if (ll == NULL)
217  return ES_OK;
218  ll->next(NULL);
219  break;
220  }
221  }
222 
223  p.ng(n_nogood);
224 
225  (void) new (home) NoGoodsProp(home,nn.next());
226  return ES_OK;
227  }
228 
229 }}}
230 
231 #endif
232 
233 // STATISTICS: search-other