Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
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: 2014-10-21 17:09:50 +0200 (Tue, 21 Oct 2014) $ by $Author: schulte $
11  * $Revision: 14258 $
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, const 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, const 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  const_cast<Path&>(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
virtual void subscribe(Space &home, Propagator &p)=0
Subscribe propagator p to all views of the no-good literal.
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
virtual ExecStatus prune(Space &home)=0
Propagate the negation of the no-good literal.
NGL * next(void) const
Return pointer to next literal.
Definition: core.hpp:3196
Actor must always be disposed.
Definition: core.hpp:610
long long int ll(int x)
Cast x into a long long int.
Definition: mult.hpp:57
virtual bool notice(void) const
Whether dispose must always be called (returns false)
Definition: core.cpp:693
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: nogoods.cpp:87
Status
The status of a no-good literal.
Definition: core.hpp:977
Base-class for propagators.
Definition: core.hpp:755
unsigned int n
Number of no-good literals with subscriptions.
Definition: nogoods.hh:72
Computation spaces.
Definition: core.hpp:1362
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3442
Base-class for both propagators and branchers.
Definition: core.hpp:666
virtual void cancel(Space &home, Propagator &p)
Cancel propagator p from all views of the no-good literal.
Definition: nogoods.cpp:57
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:84
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Execution has resulted in failure.
Definition: core.hpp:525
NoGoodsProp(Home home, NGL *root)
Constructor for creation.
Definition: nogoods.hh:105
No-good propagator.
Definition: nogoods.hh:67
ModEventDelta med
A set of modification events (used during propagation)
Definition: core.hpp:764
static ExecStatus post(Space &home, const Path &p)
Post propagator for path p.
Definition: nogoods.hh:145
NoNGL(void)
Constructor for creation.
Definition: nogoods.hh:92
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:2849
virtual NGL * copy(Space &home, bool share)
Create copy.
Definition: nogoods.cpp:71
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary)
Definition: nogoods.cpp:82
Propagation cost.
Definition: core.hpp:537
ExecStatus
Definition: core.hpp:523
virtual NGL::Status status(const Space &home) const
Test the status of the no-good literal.
Definition: nogoods.cpp:61
virtual void subscribe(Space &home, Propagator &p)
Subscribe propagator p to all views of the no-good literal.
Definition: nogoods.cpp:53
#define forceinline
Definition: config.hpp:132
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition: nogoods.cpp:183
void trycommit(const Choice &c, unsigned int a, CommitStatistics &stat=unused_commit)
If possible, commit choice c for alternative a.
Definition: core.hpp:2867
virtual Actor * copy(Space &home, bool share)
Perform copying during cloning.
Definition: nogoods.cpp:77
Class for a sentinel no-good literal.
Definition: nogoods.hh:46
Execution is okay.
Definition: core.hpp:527
NGL * add(NGL *n, bool l)
Add node n and mark it as leaf l and return n.
Definition: core.hpp:3208
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
Gecode toplevel namespace
bool leaf(void) const
Test whether literal is a leaf.
Definition: core.hpp:3192
NGL * root
Root of no-good literal tree.
Definition: nogoods.hh:70
virtual ExecStatus prune(Space &home)
Propagate the negation of the no-good literal.
Definition: nogoods.cpp:66
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
Home class for posting propagators
Definition: core.hpp:717
NGL * ngl(const Choice &c, unsigned int a)
Create no-good literal for choice c and alternative a.
Definition: core.cpp:440
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
No-good literal recorded during search.
Definition: core.hpp:971