Generated on for Gecode by doxygen 1.15.0
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.dev>
5 *
6 * Contributing authors:
7 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
8 *
9 * Copyright:
10 * Guido Tack, 2012
11 * Mikael Zayenz Lagerkvist, 2026
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
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
41namespace Gecode { namespace Search { namespace Seq {
42
43 bool
44 RestartStop::stop(const Statistics& s, const Options& o) {
45 Support::Lock lock(m);
46 // Stop if the fail limit for the engine says so
47 if (s.fail > l) {
48 if (!e_stopped)
49 m_stat.restart++;
50 e_stopped = true;
51 return true;
52 }
53 // Stop if the stop object for the meta engine says so
54 if ((m_stop != nullptr) && m_stop->stop(m_stat+s,o)) {
55 e_stopped = false;
56 return true;
57 }
58 return false;
59 }
60
61 Space*
62 RBS::next(void) {
63 if (restart) {
64 restart = false;
65 sslr++;
66 NoGoods& ng = e->nogoods();
67 // Reset number of no-goods found
68 ng.ng(0);
69 MetaInfo mi(stop->restarts(),MetaInfo::RR_SOL,sslr,e->statistics().fail,last,ng);
70 bool r = master->master(mi);
71 stop->nogood(ng.ng());
72 if (stop->status(master) == SS_FAILED) {
73 stop->update(e->statistics());
74 delete master;
75 master = nullptr;
76 e->reset(nullptr);
77 return nullptr;
78 } else if (r) {
79 stop->update(e->statistics());
80 Space* slave = master;
81 master = master->clone();
82 complete = slave->slave(mi);
83 e->reset(slave);
84 sslr = 0;
85 stop->restart();
86 }
87 }
88 while (true) {
89 Space* n = e->next();
90 if (n != nullptr) {
91 // The engine found a solution
92 restart = true;
93 delete last;
94 last = n->clone();
95 return n;
96 } else if ( (!complete && !e->stopped()) ||
97 (e->stopped() && stop->enginestopped()) ) {
98 // The engine must perform a true restart
99 // The number of the restart has been incremented in the stop object
100 if (!complete && !e->stopped())
101 stop->restart();
102 sslr = 0;
103 NoGoods& ng = e->nogoods();
104 ng.ng(0);
105 MetaInfo mi(stop->restarts(),e->stopped() ? MetaInfo::RR_LIM : MetaInfo::RR_CMPL,sslr,e->statistics().fail,last,ng);
106 (void) master->master(mi);
107 stop->nogood(ng.ng());
108 unsigned long long int nl = ++(*co);
109 stop->limit(e->statistics(),nl);
110 if (stop->status(master) == SS_FAILED)
111 return nullptr;
112 Space* slave = master;
113 master = master->clone();
114 complete = slave->slave(mi);
115 e->reset(slave);
116 } else {
117 return nullptr;
118 }
119 }
121 return nullptr;
122 }
123
125 RBS::statistics(void) const {
126 return stop->metastatistics()+e->statistics();
127 }
128
129 void
131 if (!best)
132 throw NoBest("RBS::constrain");
133 if (last != nullptr) {
134 last->constrain(b);
135 if (last->status() == SS_FAILED) {
136 delete last;
137 } else {
138 return;
139 }
140 }
141 last = b.clone();
142 master->constrain(b);
143 e->constrain(b);
144 }
145
146 bool
147 RBS::stopped(void) const {
148 /*
149 * What might happen during parallel search is that the
150 * engine has been stopped but the meta engine has not, so
151 * the meta engine does not perform a restart. However the
152 * invocation of next will do so and no restart will be
153 * missed.
154 */
155 return e->stopped();
156 }
157
158 RBS::~RBS(void) {
159 delete e;
160 delete master;
161 delete last;
162 delete co;
163 delete stop;
164 }
165
166}}}
167
168// STATISTICS: search-seq
Information passed by meta search engines.
Definition core.hpp:1628
@ RR_SOL
Restarting after a solution is found.
Definition core.hpp:1644
@ RR_CMPL
Restarting after exhausting search space.
Definition core.hpp:1646
@ RR_LIM
Restarting after reaching restart limit.
Definition core.hpp:1648
No-goods recorded from restarts.
Definition core.hpp:1599
unsigned long int ng(void) const
Return number of no-goods posted.
Definition core.hpp:3132
Exception: Best solution search is not supported
Definition exception.hpp:60
Search engine options
Definition search.hh:751
RestartStop * stop
The stop control object.
Definition rbs.hh:97
virtual Space * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
Definition rbs.cpp:62
bool best
Whether the engine performs best solution search.
Definition rbs.hh:105
Space * master
The master space to restart from.
Definition rbs.hh:91
bool restart
Whether a restart must be performed when next is called.
Definition rbs.hh:103
bool complete
Whether search for the next solution will be complete.
Definition rbs.hh:101
Engine * e
The actual engine.
Definition rbs.hh:89
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition rbs.cpp:147
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition rbs.cpp:130
Space * last
The last solution space (possibly nullptr).
Definition rbs.hh:93
unsigned long int sslr
How many solutions since the last restart.
Definition rbs.hh:99
Cutoff * co
The cutoff object.
Definition rbs.hh:95
virtual ~RBS(void)
Destructor.
Definition rbs.cpp:158
virtual Statistics statistics(void) const
Return statistics.
Definition rbs.cpp:125
virtual bool stop(const Statistics &s, const Options &o)
Return true if meta engine must be stopped.
Definition rbs.cpp:44
Search engine statistics
Definition search.hh:151
unsigned long long int fail
Number of failed nodes in search tree.
Definition search.hh:154
Computation spaces.
Definition core.hpp:1775
A lock as a scoped frontend for a mutex.
Definition thread.hpp:114
virtual bool slave(const MetaInfo &mi)
Slave configuration function for meta search engines.
Definition core.cpp:918
Space * clone(void) const
Clone space.
Definition core.hpp:3312
@ SS_FAILED
Space is failed
Definition core.hpp:1715
Search engines
Gecode toplevel namespace
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56