Generated on Fri Jun 10 2016 02:49:43 for Gecode by doxygen 1.8.11
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: 2015-03-19 14:02:56 +0100 (Thu, 19 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14468 $
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.hh>
40 
41 namespace Gecode { namespace Search { namespace Meta {
42 
43  /*
44  * Stopping for meta search engines
45  *
46  */
47 
48  bool
49  RestartStop::stop(const Statistics& s, const Options& o) {
50  // Stop if the fail stop object for the engine says so
51  if (e_stop->stop(s,o)) {
52  e_stopped = true;
53  m_stat.restart++;
54  return true;
55  }
56  // Stop if the stop object for the meta engine says so
57  if ((m_stop != NULL) && m_stop->stop(m_stat+s,o)) {
58  e_stopped = false;
59  return true;
60  }
61  return false;
62  }
63 
64 
65  Space*
66  RBS::next(void) {
67  if (restart) {
68  restart = false;
69  sslr++;
70  NoGoods& ng = e->nogoods();
71  // Reset number of no-goods found
72  ng.ng(0);
73  CRI cri(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
74  bool r = master->master(cri);
75  stop->m_stat.nogood += ng.ng();
76  if (master->status(stop->m_stat) == SS_FAILED) {
77  stop->update(e->statistics());
78  delete master;
79  master = NULL;
80  e->reset(NULL);
81  return NULL;
82  } else if (r) {
83  stop->update(e->statistics());
84  Space* slave = master;
85  master = master->clone(shared);
86  complete = slave->slave(cri);
87  e->reset(slave);
88  sslr = 0;
89  stop->m_stat.restart++;
90  }
91  }
92  while (true) {
93  Space* n = e->next();
94  if (n != NULL) {
95  // The engine found a solution
96  restart = true;
97  delete last;
98  last = n->clone();
99  return n;
100  } else if ( (!complete && !e->stopped()) ||
101  (e->stopped() && stop->enginestopped()) ) {
102  // The engine must perform a true restart
103  // The number of the restart has been incremented in the stop object
104  sslr = 0;
105  NoGoods& ng = e->nogoods();
106  ng.ng(0);
107  CRI cri(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
108  (void) master->master(cri);
109  stop->m_stat.nogood += ng.ng();
110  long unsigned int nl = ++(*co);
111  stop->limit(e->statistics(),nl);
112  if (master->status(stop->m_stat) == SS_FAILED)
113  return NULL;
114  Space* slave = master;
115  master = master->clone(shared);
116  complete = slave->slave(cri);
117  e->reset(slave);
118  } else {
119  return NULL;
120  }
121  }
122  GECODE_NEVER;
123  return NULL;
124  }
125 
127  RBS::statistics(void) const {
128  return stop->metastatistics()+e->statistics();
129  }
130 
131  bool
132  RBS::stopped(void) const {
133  /*
134  * What might happen during parallel search is that the
135  * engine has been stopped but the meta engine has not, so
136  * the meta engine does not perform a restart. However the
137  * invocation of next will do so and no restart will be
138  * missed.
139  */
140  return e->stopped();
141  }
142 
143  RBS::~RBS(void) {
144  // Deleting e also deletes stop
145  delete e;
146  delete master;
147  delete last;
148  delete co;
149  }
150 
151 }}}
152 
153 // STATISTICS: search-meta
unsigned long int ng(void) const
Return number of no-goods posted.
Definition: core.hpp:2686
Search engine statistics
Definition: search.hh:136
Space * clone(bool share=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:2854
Search engine options
Definition: search.hh:449
Current restart information during search.
Definition: core.hpp:1265
Computation spaces.
Definition: core.hpp:1362
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 stop(const Statistics &s, const Options &o)
Return true if failure limit is exceeded.
Definition: stop.cpp:75
virtual bool slave(const CRI &cri)
Slave configuration function for restart meta search engine.
Definition: core.cpp:658
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition: rbs.cpp:66
No-goods recorded from restarts.
Definition: core.hpp:1240
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition: rbs.cpp:49
virtual Search::Statistics statistics(void) const
Return statistics.
Definition: rbs.cpp:127
unsigned long int restart
Number of restarts.
Definition: search.hh:145
bool shared(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether views share same variable.
Definition: view.hpp:662
Gecode toplevel namespace
virtual bool stop(const Statistics &s, const Options &o)=0
Stop search, if returns true.
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: rbs.cpp:132
Space is failed
Definition: core.hpp:1301
virtual ~RBS(void)
Destructor.
Definition: rbs.cpp:143
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60