Generated on for Gecode by doxygen 1.17.0
pbs.hpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 *
6 * Copyright:
7 * Christian Schulte, 2015
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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#include <climits>
35
36namespace Gecode { namespace Search { namespace Seq {
37
38
41 : so(so0), po(po0) {}
42 forceinline void
44 ssi = ssi0;
45 }
46
47
50 : slave(nullptr), stop(nullptr) {}
51 forceinline void
53 slave = e; stop = s;
54 }
57 return slave->next();
58 }
60 Slave::statistics(void) const {
61 return slave->statistics();
62 }
63 forceinline bool
64 Slave::stopped(void) const {
65 return slave->stopped();
66 }
67 forceinline bool
68 Slave::limited(void) const {
69#ifdef GECODE_HAS_EXPERIMENTAL
70 return slave->limited();
71#else
72 return false;
73#endif
74 }
75 forceinline void
77 slave->constrain(b);
78 }
81 delete slave;
82 delete stop;
83 }
84
85
86 template<bool best>
88 PBS<best>::PBS(Engine** e, Stop** s, unsigned int n,
89 const Statistics& stat0,
90 const Search::Options& opt)
91 : stat(stat0), slice(opt.slice),
92 slaves(heap.alloc<Slave>(n)), n_slaves(n), cur(0),
93 slave_stop(false), incomplete(false) {
94 ssi.done = false;
95 ssi.l = opt.slice;
96
97 for (unsigned int i=0U; i<n; i++) {
98 slaves[i].init(e[i],static_cast<PortfolioStop*>(s[i]));
99 static_cast<PortfolioStop*>(s[i])->share(&ssi);
100 }
101 }
102
103 template<bool best>
104 Space*
106 slave_stop = false;
107 unsigned int n_exhausted = 0;
108 while (n_slaves > 0) {
109 ssi.done = false;
110 if (Space* s = slaves[cur].next()) {
111 // Constrain other slaves
112 if (best) {
113 for (unsigned int i=0U; i<cur; i++)
114 slaves[i].constrain(*s);
115 for (unsigned int i=cur+1; i<n_slaves; i++)
116 slaves[i].constrain(*s);
117 }
118 return s;
119 }
120 if (slaves[cur].stopped() && !slaves[cur].limited()) {
121 if (ssi.done) {
122 cur++; n_exhausted++;
123 } else {
124 slave_stop = true;
125 return nullptr;
126 }
127 } else {
128 // The slave is done or stopped at a local limit (then search is incomplete)
129 if (slaves[cur].limited())
130 incomplete = true;
131 // Kill the slave after saving the statistics
132 stat += slaves[cur].statistics();
133 slaves[cur].~Slave();
135 if (n_slaves == 1)
136 // Disable stopping by setting a high limit
137 ssi.l = ULONG_MAX;
138 }
139 if (n_exhausted == n_slaves) {
140 n_exhausted = 0;
141 // Increment by one slice
142 ssi.l += slice;
143 }
144 if (cur == n_slaves)
145 cur = 0;
146 }
148 return nullptr;
149 }
150
151 template<bool best>
152 bool
153 PBS<best>::stopped(void) const {
154 return slave_stop;
155 }
156
157 template<bool best>
160 Statistics s(stat);
161 for (unsigned int i=0U; i<n_slaves; i++)
162 s += slaves[i].statistics();
163 return s;
164 }
165
166 template<bool best>
167 void
169 if (!best)
170 throw NoBest("PBS::constrain");
171 for (unsigned int i=0U; i<n_slaves; i++)
172 slaves[i].constrain(b);
173 }
174
175 template<bool best>
177 for (unsigned int i=0U; i<n_slaves; i++)
178 slaves[i].~Slave();
179 // Note that n_slaves might be different now!
180 heap.rfree(slaves);
181 }
182
183}}}
184
185// STATISTICS: search-seq
Search engine implementation interface
Definition search.hh:1063
Engine(void)
Construct a meta engine without worker control.
Definition engine.cpp:39
virtual bool limited(void) const
Whether the engine stopped at a local limit (such as a CABS width limit) and cannot continue.
Definition engine.cpp:54
Exception: Best solution search is not supported
Definition exception.hpp:60
Search engine options
Definition search.hh:848
Slave * slaves
Slaves.
Definition pbs.hh:111
bool incomplete
Whether a slave stopped at a local limit.
Definition pbs.hh:119
SharedStopInfo ssi
Shared slave information.
Definition pbs.hh:107
virtual ~PBS(void)
Destructor.
Definition pbs.hpp:176
unsigned int n_slaves
Number of slave engines.
Definition pbs.hh:113
bool slave_stop
Whether a slave has been stopped.
Definition pbs.hh:117
unsigned int slice
Size of a slice.
Definition pbs.hh:109
virtual Space * next(void)
Return next solution (nullptr, if none exists or search has been stopped).
Definition pbs.hpp:105
Statistics stat
Master statistics.
Definition pbs.hh:105
virtual bool stopped(void) const
Check whether engine has been stopped.
Definition pbs.hpp:153
virtual Statistics statistics(void) const
Return statistics.
Definition pbs.hpp:159
unsigned int cur
Current slave to run.
Definition pbs.hh:115
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition pbs.hpp:168
PBS(Engine **slaves, Stop **stops, unsigned int n, const Statistics &stat, const Search::Options &opt)
Initialize.
Definition pbs.hpp:88
Stop object used for controlling slaves in a portfolio.
Definition pbs.hh:51
void share(SharedStopInfo *ssi)
Initialize shared stop information.
Definition pbs.hpp:43
PortfolioStop(Stop *so, Stop *po=nullptr)
Initialize with slave stop so and portfolio stop po.
Definition pbs.hpp:40
Shared stop information.
Definition pbs.hh:42
Runnable slave of a portfolio master.
Definition pbs.hh:69
bool stopped(void) const
Check whether slave has been stopped.
Definition pbs.hpp:64
Space * next(void)
Return next solution.
Definition pbs.hpp:56
Engine * slave
The slave engine.
Definition pbs.hh:72
Slave(void)
Default constructor.
Definition pbs.hpp:49
void constrain(const Space &b)
Constrain with better solution b.
Definition pbs.hpp:76
Stop * stop
Stop object.
Definition pbs.hh:74
bool limited(void) const
Check whether slave stopped at a local limit.
Definition pbs.hpp:68
void init(Engine *s, Stop *so)
Initialize with slave s and its stop object so.
Definition pbs.hpp:52
Statistics statistics(void) const
Return statistics of slave.
Definition pbs.hpp:60
~Slave(void)
Delete slave.
Definition pbs.hpp:80
Search engine statistics
Definition search.hh:155
Base-class for Stop-object.
Definition search.hh:919
Stop(void)
Default constructor.
Definition stop.hpp:45
Computation spaces.
Definition core.hpp:1787
Heap heap
The single global heap.
Definition heap.cpp:44
Search engines
Gecode toplevel namespace
#define forceinline
Definition config.hpp:144