Generated on Tue Jan 19 2021 06:15:49 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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 
35 #include <gecode/search/seq/rbs.hh>
36 
37 namespace Gecode { namespace Search { namespace Seq {
38 
39  bool
40  RestartStop::stop(const Statistics& s, const Options& o) {
41  // Stop if the fail limit for the engine says so
42  if (s.fail > l) {
43  e_stopped = true;
44  m_stat.restart++;
45  return true;
46  }
47  // Stop if the stop object for the meta engine says so
48  if ((m_stop != NULL) && m_stop->stop(m_stat+s,o)) {
49  e_stopped = false;
50  return true;
51  }
52  return false;
53  }
54 
55  Space*
56  RBS::next(void) {
57  if (restart) {
58  restart = false;
59  sslr++;
60  NoGoods& ng = e->nogoods();
61  // Reset number of no-goods found
62  ng.ng(0);
63  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
64  bool r = master->master(mi);
65  stop->m_stat.nogood += ng.ng();
66  if (master->status(stop->m_stat) == SS_FAILED) {
67  stop->update(e->statistics());
68  delete master;
69  master = NULL;
70  e->reset(NULL);
71  return NULL;
72  } else if (r) {
73  stop->update(e->statistics());
74  Space* slave = master;
75  master = master->clone();
76  complete = slave->slave(mi);
77  e->reset(slave);
78  sslr = 0;
79  stop->m_stat.restart++;
80  }
81  }
82  while (true) {
83  Space* n = e->next();
84  if (n != NULL) {
85  // The engine found a solution
86  restart = true;
87  delete last;
88  last = n->clone();
89  return n;
90  } else if ( (!complete && !e->stopped()) ||
91  (e->stopped() && stop->enginestopped()) ) {
92  // The engine must perform a true restart
93  // The number of the restart has been incremented in the stop object
94  sslr = 0;
95  NoGoods& ng = e->nogoods();
96  ng.ng(0);
97  MetaInfo mi(stop->m_stat.restart,sslr,e->statistics().fail,last,ng);
98  (void) master->master(mi);
99  stop->m_stat.nogood += ng.ng();
100  long unsigned int nl = ++(*co);
101  stop->limit(e->statistics(),nl);
102  if (master->status(stop->m_stat) == SS_FAILED)
103  return NULL;
104  Space* slave = master;
105  master = master->clone();
106  complete = slave->slave(mi);
107  e->reset(slave);
108  } else {
109  return NULL;
110  }
111  }
112  GECODE_NEVER;
113  return NULL;
114  }
115 
117  RBS::statistics(void) const {
118  return stop->metastatistics()+e->statistics();
119  }
120 
121  void
123  if (!best)
124  throw NoBest("RBS::constrain");
125  if (last != NULL) {
126  last->constrain(b);
127  if (last->status() == SS_FAILED) {
128  delete last;
129  } else {
130  return;
131  }
132  }
133  last = b.clone();
134  master->constrain(b);
135  e->constrain(b);
136  }
137 
138  bool
139  RBS::stopped(void) const {
140  /*
141  * What might happen during parallel search is that the
142  * engine has been stopped but the meta engine has not, so
143  * the meta engine does not perform a restart. However the
144  * invocation of next will do so and no restart will be
145  * missed.
146  */
147  return e->stopped();
148  }
149 
150  RBS::~RBS(void) {
151  delete e;
152  delete master;
153  delete last;
154  delete co;
155  delete stop;
156  }
157 
158 }}}
159 
160 // STATISTICS: search-seq
Search engine statistics
Definition: search.hh:147
Search engine options
Definition: search.hh:746
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition: rbs.cpp:40
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:150
virtual Statistics statistics(void) const
Return statistics.
Definition: rbs.cpp:117
Computation spaces.
Definition: core.hpp:1701
virtual Space * next(void)
Return next solution (NULL, if none exists or search has been stopped)
Definition: rbs.cpp:56
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition: rbs.cpp:139
Space * clone(CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3181
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:813
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
Definition: core.cpp:835
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
No-goods recorded from restarts.
Definition: core.hpp:1547
unsigned long int restart
Number of restarts.
Definition: search.hh:156
Exception: Best solution search is not supported
Definition: exception.hpp:60
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:1572
virtual ~RBS(void)
Destructor.
Definition: rbs.cpp:150
Space is failed
Definition: core.hpp:1641
unsigned long int ng(void) const
Return number of no-goods posted.
Definition: core.hpp:3012
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: rbs.cpp:122
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56