Generated on for Gecode by doxygen 1.17.0
bab.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 * Contributing authors:
7 * Guido Tack <tack@gecode.dev>
8 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
9 *
10 * Copyright:
11 * Christian Schulte, 2004
12 * Guido Tack, 2004
13 * Mikael Zayenz Lagerkvist, 2026
14 *
15 * This file is part of Gecode, the generic constraint
16 * development environment:
17 * http://www.gecode.dev
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining
20 * a copy of this software and associated documentation files (the
21 * "Software"), to deal in the Software without restriction, including
22 * without limitation the rights to use, copy, modify, merge, publish,
23 * distribute, sublicense, and/or sell copies of the Software, and to
24 * permit persons to whom the Software is furnished to do so, subject to
25 * the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be
28 * included in all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 *
38 */
39
40namespace Gecode { namespace Search { namespace Seq {
41
42 template<class Tracer>
43 forceinline
45 : tracer(o.tracer), opt(o), path(opt.nogoods_limit), d(0), mark(0),
46 best(nullptr) {
47 if (tracer) {
48 tracer.engine(SearchTracer::EngineType::BAB, 1U);
49 tracer.worker();
50 }
51 if ((s == nullptr) || (s->status(*this) == SS_FAILED)) {
52 fail++;
53 cur = nullptr;
54 if (!o.clone)
55 delete s;
56 } else {
57 cur = snapshot(s,opt);
58 }
59 }
60
61 template<class Tracer>
62 forceinline Space*
64 /*
65 * The engine maintains the following invariant:
66 * - If the current space (cur) is not nullptr, the path always points
67 * to exactly that space.
68 * - If the current space (cur) is nullptr, the path always points
69 * to the next space (if there is any).
70 *
71 * This invariant is needed so that no-goods can be extracted properly
72 * when the engine is stopped or has found a solution.
73 *
74 * An additional invariant maintained by the engine is:
75 * For all nodes stored at a depth less than mark, there
76 * is no guarantee of betterness. For those above the mark,
77 * betterness is guaranteed.
78 *
79 */
80 start();
81 while (true) {
82 if (stop(opt))
83 return nullptr;
84 // Recompute and add constraint if necessary
85 while (cur == nullptr) {
86 if (path.empty())
87 return nullptr;
88 cur = path.recompute(d,opt.a_d,*this,best,mark,tracer);
89 if (cur != nullptr)
90 break;
91 path.next();
92 }
93 node++;
95 if (tracer && (path.entries() > 0)) {
96 typename Path<Tracer>::Edge& top = path.top();
97 ei.init(tracer.wid(), top.nid(), top.truealt(), *cur, *top.choice());
98 }
99 unsigned int nid = tracer.nid();
100 switch (cur->status(*this)) {
101 case SS_FAILED:
102 if (tracer) {
104 tracer.wid(), nid, *cur);
105 tracer.node(ei,ni);
106 }
107 fail++;
108 delete cur;
109 cur = nullptr;
110 path.next();
111 break;
112 case SS_SOLVED:
113 {
114 if (tracer) {
116 tracer.wid(), nid, *cur);
117 tracer.node(ei,ni);
118 }
119 // Deletes all pending branchers
120 (void) cur->choice();
121 delete best;
122 best = cur;
123 cur = nullptr;
124 path.next();
125 mark = path.entries();
126 }
127 return best->clone();
128 case SS_BRANCH:
129 {
130 Space* c;
131 if ((d == 0) || (d >= opt.c_d)) {
132 c = cur->clone();
133 d = 1;
134 } else {
135 c = nullptr;
136 d++;
137 }
138 const Choice* ch = path.push(*this,cur,c,nid);
139 if (tracer) {
141 tracer.wid(), nid, *cur, ch);
142 tracer.node(ei,ni);
143 }
144 cur->commit(*ch,0);
145 break;
146 }
147 default:
149 }
150 }
152 return nullptr;
153 }
154
155 template<class Tracer>
156 forceinline Statistics
158 return *this;
159 }
160
161 template<class Tracer>
162 forceinline void
164 if (best != nullptr) {
165 // Check whether b is in fact better than best
166 best->constrain(b);
167 if (best->status(*this) != SS_FAILED)
168 return;
169 else
170 delete best;
171 }
172 best = b.clone();
173 if (cur != nullptr)
174 cur->constrain(b);
175 mark = path.entries();
176 }
177
178 template<class Tracer>
179 forceinline void
181 tracer.round();
182 delete best;
183 best = nullptr;
184 path.reset();
185 d = 0;
186 mark = 0;
187 delete cur;
188 if ((s == nullptr) || (s->status(*this) == SS_FAILED)) {
189 delete s;
190 cur = nullptr;
191 } else {
192 cur = s;
193 }
195 }
196
197 template<class Tracer>
198 forceinline NoGoods&
200 return path;
201 }
202
203 template<class Tracer>
204 forceinline
206 tracer.done();
207 path.reset();
208 delete best;
209 delete cur;
210 }
211
212}}}
213
214// STATISTICS: search-seq
Choice for performing commit
Definition core.hpp:1423
No-goods recorded from restarts.
Definition core.hpp:1599
void init(unsigned int wid, unsigned int nid, unsigned int a)
Initialize.
Definition tracer.hpp:107
unsigned int nid(void) const
Return parent node id.
Definition tracer.hpp:142
@ FAILED
A solution node.
Definition search.hh:282
@ BRANCH
A failed node.
Definition search.hh:283
@ BAB
Engine is a BAB engine.
Definition search.hh:199
Search engine options
Definition search.hh:751
BAB(Space *s, const Options &o)
Initialize with space s and search options o.
Definition bab.hpp:44
~BAB(void)
Destructor.
Definition bab.hpp:205
NoGoods & nogoods(void)
Return no-goods.
Definition bab.hpp:199
Space * next(void)
Search for next better solution
Definition bab.hpp:63
void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition bab.hpp:163
Statistics statistics(void) const
Return statistics.
Definition bab.hpp:157
Search tree edge for recomputation
Definition path.hh:70
unsigned int truealt(void) const
Return true number for alternatives (excluding lao optimization).
Definition path.hpp:71
unsigned int nid(void) const
Return node identifier.
Definition path.hpp:103
const Choice * choice(void) const
Return choice.
Definition path.hpp:97
Search engine statistics
Definition search.hh:151
unsigned long long int fail
Number of failed nodes in search tree.
Definition search.hh:154
unsigned long long int node
Number of nodes expanded.
Definition search.hh:156
void start(void)
Reset stop information.
Definition worker.hh:74
bool stop(const Options &o)
Check whether engine must be stopped.
Definition worker.hh:79
Computation spaces.
Definition core.hpp:1775
void reset(void)
Reset information.
Definition core.hpp:4866
virtual void constrain(const Space &best)
Constrain function for best solution search.
Space * clone(void) const
Clone space.
Definition core.hpp:3312
SpaceStatus status(StatusStatistics &stat)
Query space status.
@ SS_BRANCH
Space must be branched (at least one brancher left)
Definition core.hpp:1717
@ SS_SOLVED
Space is solved (no brancher left)
Definition core.hpp:1716
@ 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