Generated on Thu Mar 16 2017 03:24:23 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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11  * $Revision: 14967 $
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;
80  master = master->clone(shared_data,shared_info);
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();
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;
110  master = master->clone(shared_data,shared_info);
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();
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
Search engine options
Definition: search.hh:446
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:143
Space * clone(bool share_data=true, bool share_info=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3258
Computation spaces.
Definition: core.hpp:1672
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
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:646
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:668
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
No-goods recorded from restarts.
Definition: core.hpp:1517
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition: rbs.cpp:44
unsigned long int restart
Number of restarts.
Definition: search.hh:149
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:1542
Space is failed
Definition: core.hpp:1611
unsigned long int ng(void) const
Return number of no-goods posted.
Definition: core.hpp:3072
struct Gecode::@554::NNF::@60::@61 b
For binary nodes (and, or, eqv)
virtual ~RBS(void)
Destructor.
Definition: rbs.cpp:155
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60