1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2004, 2016
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace Search { namespace Seq {
/*
* Nodes for the probing engine (just remember next alternative
* to try)
*
*/
template<class Tracer>
forceinline
Probe<Tracer>::Node::Node(void) {}
template<class Tracer>
forceinline
Probe<Tracer>::Node::Node(Space* s, const Choice* c, unsigned int a,
unsigned int nid)
: _space(s), _choice(c), _alt(a), _nid(nid) {}
template<class Tracer>
forceinline Space*
Probe<Tracer>::Node::space(void) const {
return _space;
}
template<class Tracer>
forceinline const Choice*
Probe<Tracer>::Node::choice(void) const {
return _choice;
}
template<class Tracer>
forceinline unsigned int
Probe<Tracer>::Node::alt(void) const {
return _alt;
}
template<class Tracer>
forceinline unsigned int
Probe<Tracer>::Node::nid(void) const {
return _nid;
}
template<class Tracer>
forceinline void
Probe<Tracer>::Node::next(void) {
_alt--;
}
/*
* The probing engine: computes all solutions with
* exact number of discrepancies (solutions with
* fewer discrepancies are discarded)
*
*/
template<class Tracer>
forceinline
Probe<Tracer>::Probe(const Options& opt)
: tracer(opt.tracer), ds(heap) {
tracer.engine(SearchTracer::EngineType::LDS, 1U);
tracer.worker();
}
template<class Tracer>
forceinline void
Probe<Tracer>::init(Space* s) {
cur = s; d = 0U; exhausted = true;
if (tracer)
tracer.ei()->invalidate();
}
template<class Tracer>
forceinline void
Probe<Tracer>::reset(Space* s, unsigned int d0) {
tracer.round();
delete cur;
while (!ds.empty())
delete ds.pop().space();
cur = s; d = d0; exhausted = true;
if (tracer)
tracer.ei()->invalidate();
Worker::reset(0);
}
template<class Tracer>
forceinline Statistics
Probe<Tracer>::statistics(void) const {
return *this;
}
template<class Tracer>
forceinline bool
Probe<Tracer>::done(void) const {
return exhausted;
}
template<class Tracer>
forceinline
Probe<Tracer>::~Probe(void) {
tracer.done();
delete cur;
while (!ds.empty())
delete ds.pop().space();
}
template<class Tracer>
forceinline Space*
Probe<Tracer>::next(const Options& opt) {
start();
while (true) {
if (cur == nullptr) {
backtrack:
if (ds.empty())
return nullptr;
if (stop(opt))
return nullptr;
unsigned int a = ds.top().alt();
const Choice* ch = ds.top().choice();
unsigned int nid = ds.top().nid();
if (a == 0) {
cur = ds.pop().space();
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
cur->commit(*ch,0);
delete ch;
} else {
ds.top().next();
cur = ds.top().space()->clone();
if (tracer)
tracer.ei()->init(tracer.wid(), nid, a, *cur, *ch);
cur->commit(*ch,a);
}
node++;
d++;
}
check_discrepancy:
if (d == 0) {
Space* s = cur;
while (s->status(*this) == SS_BRANCH) {
if (stop(opt)) {
cur = s;
return nullptr;
}
const Choice* ch = s->choice();
if (ch->alternatives() > 1)
exhausted = false;
if (tracer) {
unsigned int nid = tracer.nid();
SearchTracer::NodeInfo ni(SearchTracer::NodeType::BRANCH,
tracer.wid(), nid, *s, ch);
tracer.node(*tracer.ei(),ni);
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
}
s->commit(*ch,0);
node++;
delete ch;
}
cur = nullptr;
if (s->failed()) {
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::FAILED,
tracer.wid(), tracer.nid(), *s);
tracer.node(*tracer.ei(),ni);
}
fail++;
delete s;
goto backtrack;
} else {
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::SOLVED,
tracer.wid(), tracer.nid(), *s);
tracer.node(*tracer.ei(),ni);
}
// Deletes all pending branchings
(void) s->choice();
return s;
}
} else {
node++;
switch (cur->status(*this)) {
case SS_FAILED:
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::FAILED,
tracer.wid(), tracer.nid(), *cur);
tracer.node(*tracer.ei(),ni);
}
fail++;
delete cur;
cur = nullptr;
goto backtrack;
case SS_SOLVED:
if (tracer) {
tracer.skip(*tracer.ei());
}
delete cur;
cur = nullptr;
goto backtrack;
case SS_BRANCH:
{
const Choice* ch = cur->choice();
unsigned int alt = ch->alternatives();
unsigned int nid = tracer.nid();
if (tracer) {
SearchTracer::NodeInfo ni(SearchTracer::NodeType::BRANCH,
tracer.wid(), nid, *cur, ch);
tracer.node(*tracer.ei(),ni);
}
if (alt > 1) {
if (d < alt-1)
exhausted = false;
unsigned int d_a = (d >= alt-1) ? alt-1 : d;
Space* cc = cur->clone();
Node sn(cc,ch,d_a-1,nid);
ds.push(sn);
stack_depth(static_cast<unsigned long int>(ds.entries()));
if (tracer)
tracer.ei()->init(tracer.wid(), nid, d_a, *cur, *ch);
cur->commit(*ch,d_a);
d -= d_a;
} else {
if (tracer)
tracer.ei()->init(tracer.wid(), nid, 0, *cur, *ch);
cur->commit(*ch,0);
node++;
delete ch;
}
goto check_discrepancy;
}
default: GECODE_NEVER;
}
}
}
}
template<class Tracer>
forceinline
LDS<Tracer>::LDS(Space* s, const Options& o)
: opt(o), e(opt), root(nullptr), d(0) {
e.node = 1;
if (s->status(e) == SS_FAILED) {
e.fail++;
e.init(nullptr);
} else {
Space* c = snapshot(s,opt);
if (opt.d_l > 0) {
root = c->clone();
}
e.init(c);
}
}
template<class Tracer>
Space*
LDS<Tracer>::next(void) {
while (true) {
Space* s = e.next(opt);
if (s != nullptr)
return s;
if (((s == nullptr) && e.stopped()) || (++d > opt.d_l) || e.done())
break;
if (d == opt.d_l) {
if (root != nullptr)
e.reset(root,d);
root = nullptr;
} else if (root != nullptr) {
e.reset(root->clone(),d);
}
}
return nullptr;
}
template<class Tracer>
bool
LDS<Tracer>::stopped(void) const {
return e.stopped();
}
template<class Tracer>
Statistics
LDS<Tracer>::statistics(void) const {
return e.statistics();
}
template<class Tracer>
forceinline void
LDS<Tracer>::reset(Space* s) {
delete root; root=nullptr; d=0;
e.node = 1;
if ((s == nullptr) || (s->status(e) == SS_FAILED)) {
delete s;
e.fail++;
e.reset(nullptr,0);
} else {
if (opt.d_l > 0) {
root = s->clone();
}
e.reset(s,0);
}
}
template<class Tracer>
forceinline void
LDS<Tracer>::constrain(const Space& b) {
(void) b;
assert(false);
}
template<class Tracer>
LDS<Tracer>::~LDS(void) {
delete root;
}
}}}
// STATISTICS: search-seq
|