Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
nogoods.hpp
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$ by $Author$
11  * $Revision$
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 namespace Gecode { namespace Search {
39 
41  NoNGL::NoNGL(void) {}
42 
45  : NGL(home) {}
46 
48  NoNGL::NoNGL(Space& home, NoNGL& ngl)
49  : NGL(home,ngl) {}
50 
51 
52 
55  : Propagator(Home(home)), root(root0), n(0U) {
56  // Create subscriptions
57  root->subscribe(home,*this); n++;
58  bool notice = root->notice();
59  NGL* l = root->next();
60  while ((l != NULL) && l->leaf()) {
61  l->subscribe(home,*this); n++;
62  notice = notice || l->notice();
63  l = l->next();
64  }
65  if (l != NULL) {
66  l->subscribe(home,*this); n++;
67  }
68  while (!notice && (l != NULL)) {
69  notice = notice || l->notice();
70  l = l->next();
71  }
72  if (notice)
73  home.notice(*this,AP_DISPOSE);
74  }
75 
78  : Propagator(home,p), n(p.n) {
79  assert(p.root != NULL);
80  NoNGL s;
81  NGL* c = &s;
82  for (NGL* pc = p.root; pc != NULL; pc = pc->next()) {
83  NGL* n = pc->copy(home);
84  n->leaf(pc->leaf());
85  c->next(n); c=n;
86  }
87  root = s.next();
88  }
89 
90 
91 
92  template<class Path>
94  NoGoodsProp::post(Space& home, const Path& p) {
95  int s = 0;
96  int n = std::min(p.ds.entries(),static_cast<int>(p.ngdl()));
97 
98  unsigned long int n_nogood = 0;
99 
100  // Eliminate the alternatives which are not no-goods at the end
101  while ((n > s) && (p.ds[n-1].truealt() == 0U))
102  n--;
103 
104  // A sentinel element
105  NoNGL nn;
106  // Current no-good literal
107  NGL* c = &nn;
108 
109  // Commit no-goods at the beginning
110  while ((s < n) && (p.ds[s].truealt() > 0U))
111  // Try whether this is a rightmost alternative
112  if (p.ds[s].rightmost()) {
113  // No literal needed, directly try to commit
114  home.trycommit(*p.ds[s].choice(),p.ds[s].truealt());
115  s++;
116  } else {
117  // Prune using no-good literals
118  for (unsigned int a=0U; a<p.ds[s].truealt(); a++) {
119  NGL* l = home.ngl(*p.ds[s].choice(),a);
120  // Does the brancher support no-good literals?
121  if (l == NULL)
122  return ES_OK;
123  GECODE_ES_CHECK(l->prune(home));
124  }
125  // Add literal as root if needed and stop
126  if (NGL* l = home.ngl(*p.ds[s].choice(),p.ds[s].truealt())) {
127  c = c->add(l,false);
128  s++; break;
129  }
130  }
131 
132  // There are no literals
133  if (home.failed())
134  return ES_FAILED;
135  if (s >= n)
136  return ES_OK;
137 
138  // There must be at least two literals
139  assert((n-s > 1) ||
140  ((n-s == 1) && (c != &nn)));
141 
142  // Remember the last leaf
143  NGL* ll = NULL;
144 
145  // Create literals
146  for (int i=s; i<n; i++) {
147  // Add leaves
148  for (unsigned int a=0U; a<p.ds[i].truealt(); a++) {
149  NGL* l = home.ngl(*p.ds[i].choice(),a);
150  if (l == NULL) {
151  // The brancher does not support no-goods
152  if (ll == NULL)
153  return ES_OK;
154  ll->next(NULL);
155  break;
156  }
157  c = c->add(l,true); ll = c;
158  n_nogood++;
159  }
160  // Check whether to add an additional subtree
161  if (NGL* l = home.ngl(*p.ds[i].choice(),p.ds[i].truealt())) {
162  c = c->add(l,false);
163  } else if (!p.ds[i].rightmost()) {
164  // The brancher does not support no-goods
165  if (ll == NULL)
166  return ES_OK;
167  ll->next(NULL);
168  break;
169  }
170  }
171 
172  const_cast<Path&>(p).ng(n_nogood);
173 
174  (void) new (home) NoGoodsProp(home,nn.next());
175  return ES_OK;
176  }
177 
178 }}
179 
180 // 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
static ExecStatus post(Space &home, const Path &p)
Post propagator for path p.
Definition: nogoods.hpp:94
virtual ExecStatus prune(Space &home)=0
Propagate the negation of the no-good literal.
Actor must always be disposed.
Definition: core.hpp:554
NoNGL(void)
Constructor for creation.
Definition: nogoods.hpp:41
long long int ll(int x)
Cast x into a long long int.
Definition: mult.hpp:57
Base-class for propagators.
Definition: core.hpp:1016
#define forceinline
Definition: config.hpp:182
Computation spaces.
Definition: core.hpp:1668
NoGoodsProp(Space &home, NGL *root)
Constructor for creation.
Definition: nogoods.hpp:54
No-good propagator.
Definition: nogoods.hh:69
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition: macros.hpp:95
NGL * root
Root of no-good literal tree.
Definition: nogoods.hh:72
Gecode::FloatVal c(-8, 8)
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
bool leaf(void) const
Test whether literal is a leaf.
Definition: core.hpp:3659
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
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:466
bool failed(void) const
Check whether space is failed.
Definition: core.hpp:3914
NGL * next(void) const
Return pointer to next literal.
Definition: core.hpp:3663
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition: core.hpp:3929
unsigned int n
Number of no-good literals with subscriptions.
Definition: nogoods.hh:74
ExecStatus
Definition: core.hpp:464
virtual bool notice(void) const
Whether dispose must always be called (returns false)
Definition: core.cpp:862
void trycommit(const Choice &c, unsigned int a, CommitStatistics &stat=unused_commit)
If possible, commit choice c for alternative a.
Definition: core.hpp:3157
Execution is okay.
Definition: core.hpp:468
NGL * add(NGL *n, bool l)
Add node n and mark it as leaf l and return n.
Definition: core.hpp:3675
Gecode toplevel namespace
Home class for posting propagators
Definition: core.hpp:846
Class for a sentinel no-good literal.
Definition: nogoods.hh:46
NGL * ngl(const Choice &c, unsigned int a)
Create no-good literal for choice c and alternative a.
Definition: core.cpp:610
No-good literal recorded during search.
Definition: core.hpp:1266