Generated on Sat Jul 28 2018 17:18:35 for Gecode by doxygen 1.8.14
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: 2017-03-28 16:18:06 +0200 (Tue, 28 Mar 2017) $ by $Author: schulte $
11  * $Revision: 15620 $
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 
40 
41 namespace Gecode { namespace Search { namespace Meta {
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 
60  Space*
61  RBS::next(void) {
62  if (restart) {
63  restart = false;
64  sslr++;
65  NoGoods& ng = e->nogoods();
66  // Reset number of no-goods found
67  ng.ng(0);
68  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
69  bool r = master->master(mi);
70  stop->m_stat.nogood += ng.ng();
71  if (master->status(stop->m_stat) == SS_FAILED) {
72  stop->update(e->statistics());
73  delete master;
74  master = NULL;
75  e->reset(NULL);
76  return NULL;
77  } else if (r) {
78  stop->update(e->statistics());
79  Space* slave = master;
81  complete = slave->slave(mi);
82  e->reset(slave);
83  sslr = 0;
84  stop->m_stat.restart++;
85  }
86  }
87  while (true) {
88  Space* n = e->next();
89  if (n != NULL) {
90  // The engine found a solution
91  restart = true;
92  delete last;
93  last = n->clone(shared_data);
94  return n;
95  } else if ( (!complete && !e->stopped()) ||
96  (e->stopped() && stop->enginestopped()) ) {
97  // The engine must perform a true restart
98  // The number of the restart has been incremented in the stop object
99  sslr = 0;
100  NoGoods& ng = e->nogoods();
101  ng.ng(0);
102  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
103  (void) master->master(mi);
104  stop->m_stat.nogood += ng.ng();
105  long unsigned int nl = ++(*co);
106  stop->limit(e->statistics(),nl);
107  if (master->status(stop->m_stat) == SS_FAILED)
108  return NULL;
109  Space* slave = master;
111  complete = slave->slave(mi);
112  e->reset(slave);
113  } else {
114  return NULL;
115  }
116  }
117  GECODE_NEVER;
118  return NULL;
119  }
120 
122  RBS::statistics(void) const {
123  return stop->metastatistics()+e->statistics();
124  }
125 
126  void
128  if (!best)
129  throw NoBest("RBS::constrain");
130  if (last != NULL) {
131  last->constrain(b);
132  if (last->status() == SS_FAILED) {
133  delete last;
134  } else {
135  return;
136  }
137  }
138  last = b.clone(shared_data);
139  master->constrain(b);
140  e->constrain(b);
141  }
142 
143  bool
144  RBS::stopped(void) const {
145  /*
146  * What might happen during parallel search is that the
147  * engine has been stopped but the meta engine has not, so
148  * the meta engine does not perform a restart. However the
149  * invocation of next will do so and no restart will be
150  * missed.
151  */
152  return e->stopped();
153  }
154 
155  RBS::~RBS(void) {
156  delete e;
157  delete master;
158  delete last;
159  delete co;
160  delete stop;
161  }
162 
163 }}}
164 
165 // STATISTICS: search-meta
Search engine statistics
Definition: search.hh:140
Cutoff * co
The cutoff object.
Definition: rbs.hh:85
Search engine options
Definition: search.hh:446
virtual void reset(Space *s)
Reset engine to restart at space s (does nothing)
Definition: engine.cpp:48
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:143
unsigned long int nogood
Number of no-goods posted.
Definition: search.hh:151
Space * clone(bool share_data=true, bool share_info=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3326
Computation spaces.
Definition: core.hpp:1748
unsigned long int sslr
How many solutions since the last restart.
Definition: rbs.hh:89
bool restart
Whether a restart must be performed when next is called.
Definition: rbs.hh:97
bool enginestopped(void) const
Return whether the engine has been stopped.
Definition: rbs.hpp:58
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:144
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:819
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
bool best
Whether the engine performs best solution search.
Definition: rbs.hh:99
virtual Statistics statistics(void) const
Return statistics.
Definition: rbs.cpp:122
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
Definition: core.cpp:841
RestartStop * stop
The stop control object.
Definition: rbs.hh:87
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition: rbs.cpp:61
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: rbs.cpp:127
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:784
Space * last
The last solution space (possibly NULL)
Definition: rbs.hh:83
bool shared_data
Whether the slave can share data with the master.
Definition: rbs.hh:91
No-goods recorded from restarts.
Definition: core.hpp:1595
virtual NoGoods & nogoods(void)
Return no-goods (the no-goods are empty)
Definition: engine.cpp:52
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition: rbs.cpp:44
virtual Space * next(void)=0
Return next solution (NULL, if none exists or search has been stopped)
virtual void constrain(const Space &b)
Constrain future solutions to be better than b (raises exception)
Definition: engine.cpp:43
Space * master
The master space to restart from.
Definition: rbs.hh:81
virtual bool master(const MetaInfo &mi)
Master configuration function for meta search engines.
Definition: core.cpp:823
Engine * e
The actual engine.
Definition: rbs.hh:79
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
void update(const Search::Statistics &s)
Update statistics.
Definition: rbs.hpp:53
unsigned long int restart
Number of restarts.
Definition: search.hh:149
Statistics metastatistics(void) const
Return statistics for the meta engine.
Definition: rbs.hpp:63
Exception: Best solution search is not supported
Definition: exception.hpp:64
virtual Statistics statistics(void) const =0
Return statistics.
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:1620
Space is failed
Definition: core.hpp:1689
unsigned long int ng(void) const
Return number of no-goods posted.
Definition: core.hpp:3157
void limit(const Statistics &s, unsigned long int l)
Set current limit for the engine to l fails.
Definition: rbs.hpp:46
virtual ~RBS(void)
Destructor.
Definition: rbs.cpp:155
bool complete
Whether search for the next solution will be complete.
Definition: rbs.hh:95
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
bool shared_info
Whether the slave can share info (AFC) with the master.
Definition: rbs.hh:93
virtual bool stopped(void) const =0
Check whether engine has been stopped.