Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
rbs.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Copyright:
7  * Guido Tack, 2012
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 
39 #include <gecode/search/seq/rbs.hh>
40 
41 namespace Gecode { namespace Search { namespace Seq {
42 
43  bool
44  RestartStop::stop(const Statistics& s, const Options& o) {
45  // Stop if the fail limit for the engine says so
46  if (s.fail > l) {
47  e_stopped = true;
48  m_stat.restart++;
49  return true;
50  }
51  // Stop if the stop object for the meta engine says so
52  if ((m_stop != NULL) && m_stop->stop(m_stat+s,o)) {
53  e_stopped = false;
54  return true;
55  }
56  return false;
57  }
58 
59  Space*
60  RBS::next(void) {
61  if (restart) {
62  restart = false;
63  sslr++;
64  NoGoods& ng = e->nogoods();
65  // Reset number of no-goods found
66  ng.ng(0);
67  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
68  bool r = master->master(mi);
69  stop->m_stat.nogood += ng.ng();
70  if (master->status(stop->m_stat) == SS_FAILED) {
71  stop->update(e->statistics());
72  delete master;
73  master = NULL;
74  e->reset(NULL);
75  return NULL;
76  } else if (r) {
77  stop->update(e->statistics());
78  Space* slave = master;
79  master = master->clone();
80  complete = slave->slave(mi);
81  e->reset(slave);
82  sslr = 0;
83  stop->m_stat.restart++;
84  }
85  }
86  while (true) {
87  Space* n = e->next();
88  if (n != NULL) {
89  // The engine found a solution
90  restart = true;
91  delete last;
92  last = n->clone();
93  return n;
94  } else if ( (!complete && !e->stopped()) ||
95  (e->stopped() && stop->enginestopped()) ) {
96  // The engine must perform a true restart
97  // The number of the restart has been incremented in the stop object
98  sslr = 0;
99  NoGoods& ng = e->nogoods();
100  ng.ng(0);
101  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
102  (void) master->master(mi);
103  stop->m_stat.nogood += ng.ng();
104  long unsigned int nl = ++(*co);
105  stop->limit(e->statistics(),nl);
106  if (master->status(stop->m_stat) == SS_FAILED)
107  return NULL;
108  Space* slave = master;
109  master = master->clone();
110  complete = slave->slave(mi);
111  e->reset(slave);
112  } else {
113  return NULL;
114  }
115  }
116  GECODE_NEVER;
117  return NULL;
118  }
119 
121  RBS::statistics(void) const {
122  return stop->metastatistics()+e->statistics();
123  }
124 
125  void
127  if (!best)
128  throw NoBest("RBS::constrain");
129  if (last != NULL) {
130  last->constrain(b);
131  if (last->status() == SS_FAILED) {
132  delete last;
133  } else {
134  return;
135  }
136  }
137  last = b.clone();
138  master->constrain(b);
139  e->constrain(b);
140  }
141 
142  bool
143  RBS::stopped(void) const {
144  /*
145  * What might happen during parallel search is that the
146  * engine has been stopped but the meta engine has not, so
147  * the meta engine does not perform a restart. However the
148  * invocation of next will do so and no restart will be
149  * missed.
150  */
151  return e->stopped();
152  }
153 
154  RBS::~RBS(void) {
155  delete e;
156  delete master;
157  delete last;
158  delete co;
159  delete stop;
160  }
161 
162 }}}
163 
164 // STATISTICS: search-seq
Search engine statistics
Definition: search.hh:149
Search engine options
Definition: search.hh:748
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition: rbs.cpp:44
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:152
virtual Statistics statistics(void) const
Return statistics.
Definition: rbs.cpp:121
Computation spaces.
Definition: core.hpp:1668
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition: rbs.cpp:60
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: rbs.cpp:143
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3144
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:807
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
Definition: core.cpp:829
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
No-goods recorded from restarts.
Definition: core.hpp:1514
unsigned long int restart
Number of restarts.
Definition: search.hh:158
Exception: Best solution search is not supported
Definition: exception.hpp:64
Gecode toplevel namespace
virtual bool stop(const Statistics &s, const Options &o)=0
Stop search, if returns true.
Information passed by meta search engines.
Definition: core.hpp:1539
virtual ~RBS(void)
Destructor.
Definition: rbs.cpp:154
Space is failed
Definition: core.hpp:1608
unsigned long int ng(void) const
Return number of no-goods posted.
Definition: core.hpp:2975
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: rbs.cpp:126
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60