Generated on Tue Jul 18 2017 18:41:42 for Gecode by doxygen 1.8.13
bab.hh
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.org>
5  *
6  * Contributing authors:
7  * Guido Tack <tack@gecode.org>
8  *
9  * Copyright:
10  * Christian Schulte, 2004
11  * Guido Tack, 2004
12  *
13  * Last modified:
14  * $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
15  * $Revision: 14967 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 #ifndef __GECODE_SEARCH_SEQUENTIAL_BAB_HH__
43 #define __GECODE_SEARCH_SEQUENTIAL_BAB_HH__
44 
45 #include <gecode/search.hh>
46 #include <gecode/search/support.hh>
47 #include <gecode/search/worker.hh>
49 
50 namespace Gecode { namespace Search { namespace Sequential {
51 
53  class BAB : public Worker {
54  private:
56  Options opt;
58  Path path;
60  Space* cur;
62  unsigned int d;
64  int mark;
66  Space* best;
67  public:
69  BAB(Space* s, const Options& o);
71  Space* next(void);
73  Statistics statistics(void) const;
75  void constrain(const Space& b);
77  void reset(Space* s);
79  NoGoods& nogoods(void);
81  ~BAB(void);
82  };
83 
85  BAB::BAB(Space* s, const Options& o)
86  : opt(o), path(opt.nogoods_limit), d(0), mark(0), best(NULL) {
87  if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
88  fail++;
89  cur = NULL;
90  if (!o.clone)
91  delete s;
92  } else {
93  cur = snapshot(s,opt);
94  }
95  }
96 
98  BAB::next(void) {
99  /*
100  * The engine maintains the following invariant:
101  * - If the current space (cur) is not NULL, the path always points
102  * to exactly that space.
103  * - If the current space (cur) is NULL, the path always points
104  * to the next space (if there is any).
105  *
106  * This invariant is needed so that no-goods can be extracted properly
107  * when the engine is stopped or has found a solution.
108  *
109  * An additional invariant maintained by the engine is:
110  * For all nodes stored at a depth less than mark, there
111  * is no guarantee of betterness. For those above the mark,
112  * betterness is guaranteed.
113  *
114  */
115  start();
116  while (true) {
117  if (stop(opt))
118  return NULL;
119  // Recompute and add constraint if necessary
120  while (cur == NULL) {
121  if (path.empty())
122  return NULL;
123  cur = path.recompute(d,opt.a_d,*this,*best,mark);
124  if (cur != NULL)
125  break;
126  path.next();
127  }
128  node++;
129  switch (cur->status(*this)) {
130  case SS_FAILED:
131  fail++;
132  delete cur;
133  cur = NULL;
134  path.next();
135  break;
136  case SS_SOLVED:
137  // Deletes all pending branchers
138  (void) cur->choice();
139  delete best;
140  best = cur;
141  cur = NULL;
142  path.next();
143  mark = path.entries();
144  return best->clone();
145  case SS_BRANCH:
146  {
147  Space* c;
148  if ((d == 0) || (d >= opt.c_d)) {
149  c = cur->clone();
150  d = 1;
151  } else {
152  c = NULL;
153  d++;
154  }
155  const Choice* ch = path.push(*this,cur,c);
156  cur->commit(*ch,0);
157  break;
158  }
159  default:
160  GECODE_NEVER;
161  }
162  }
163  GECODE_NEVER;
164  return NULL;
165  }
166 
168  BAB::statistics(void) const {
169  return *this;
170  }
171 
172  forceinline void
174  if (best != NULL) {
175  // Check whether b is in fact better than best
176  best->constrain(b);
177  if (best->status(*this) != SS_FAILED)
178  return;
179  else
180  delete best;
181  }
182  best = b.clone();
183  if (cur != NULL)
184  cur->constrain(b);
185  mark = path.entries();
186  }
187 
188  forceinline void
190  delete best;
191  best = NULL;
192  path.reset();
193  d = 0;
194  mark = 0;
195  delete cur;
196  if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
197  delete s;
198  cur = NULL;
199  } else {
200  cur = s;
201  }
202  Worker::reset();
203  }
204 
206  BAB::nogoods(void) {
207  return path;
208  }
209 
211  BAB::~BAB(void) {
212  path.reset();
213  delete best;
214  delete cur;
215  }
216 
217 }}}
218 
219 #endif
220 
221 // STATISTICS: search-sequential
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance) ...
Definition: search.hh:455
Space must be branched (at least one brancher left)
Definition: core.hpp:1691
Search engine statistics
Definition: search.hh:140
unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:453
Search engine options
Definition: search.hh:446
const Choice * push(Worker &stat, Space *s, Space *c)
Push space c (a clone of s or NULL)
Definition: path.hh:222
Depth-first path (stack of edges) supporting recomputation.
Definition: path.hh:61
~BAB(void)
Destructor.
Definition: bab.hh:211
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:143
bool empty(void) const
Test whether path is empty.
Definition: path.hh:251
Space * clone(bool share_data=true, bool share_info=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3326
BAB(Space *s, const Options &o)
Initialize with space s and search options o.
Definition: bab.hh:85
Computation spaces.
Definition: core.hpp:1748
Implementation of depth-first branch-and-bound search engine.
Definition: bab.hh:53
NoGoods & nogoods(void)
Return no-goods.
Definition: bab.hh:206
void start(void)
Reset stop information.
Definition: worker.hh:78
Gecode::FloatVal c(-8, 8)
Space * next(void)
Search for next better solution
Definition: bab.hh:98
Space * recompute(unsigned int &d, unsigned int a_d, Worker &s)
Recompute space according to path.
Definition: path.hh:290
void commit(const Choice &c, unsigned int a, CommitStatistics &stat=unused_commit)
Commit choice c for alternative a.
Definition: core.hpp:3334
virtual void constrain(const Space &best)
Constrain function for best solution search.
Definition: core.cpp:819
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
bool clone
Whether engines create a clone when being initialized.
Definition: search.hh:449
void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: bab.hh:173
void reset(void)
Reset stack.
Definition: path.hh:284
Search worker statistics
Definition: worker.hh:48
Choice for performing commit
Definition: core.hpp:1414
No-goods recorded from restarts.
Definition: core.hpp:1595
#define forceinline
Definition: config.hpp:173
void next(void)
Generate path for next node.
Definition: path.hh:234
int entries(void) const
Return number of entries on stack.
Definition: path.hh:270
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
Space * snapshot(Space *s, const Options &o, bool share=true)
Clone space s dependening on options o.
Definition: support.hh:75
Gecode toplevel namespace
unsigned long int node
Number of nodes expanded.
Definition: search.hh:145
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:127
Space is failed
Definition: core.hpp:1689
const Choice * choice(void)
Create new choice for current brancher.
Definition: core.cpp:503
Statistics statistics(void) const
Return statistics.
Definition: bab.hh:168
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
bool stop(const Options &o)
Check whether engine must be stopped.
Definition: worker.hh:83
void reset(void)
Reset.
Definition: statistics.hpp:43
Space is solved (no brancher left)
Definition: core.hpp:1690