Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
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: 2015-01-08 15:07:24 +0100 (Thu, 08 Jan 2015) $ by $Author: schulte $
15  * $Revision: 14341 $
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 reset(Space* s);
77  NoGoods& nogoods(void);
79  ~BAB(void);
80  };
81 
83  BAB::BAB(Space* s, const Options& o)
84  : opt(o), path(static_cast<int>(opt.nogoods_limit)),
85  d(0), mark(0), best(NULL) {
86  if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
87  fail++;
88  cur = NULL;
89  if (!o.clone)
90  delete s;
91  } else {
92  cur = snapshot(s,opt);
93  }
94  }
95 
97  BAB::next(void) {
98  /*
99  * The engine maintains the following invariant:
100  * - If the current space (cur) is not NULL, the path always points
101  * to exactly that space.
102  * - If the current space (cur) is NULL, the path always points
103  * to the next space (if there is any).
104  *
105  * This invariant is needed so that no-goods can be extracted properly
106  * when the engine is stopped or has found a solution.
107  *
108  * An additional invariant maintained by the engine is:
109  * For all nodes stored at a depth less than mark, there
110  * is no guarantee of betterness. For those above the mark,
111  * betterness is guaranteed.
112  *
113  */
114  start();
115  while (true) {
116  if (stop(opt))
117  return NULL;
118  // Recompute and add constraint if necessary
119  while (cur == NULL) {
120  if (path.empty())
121  return NULL;
122  cur = path.recompute(d,opt.a_d,*this,*best,mark);
123  if (cur != NULL)
124  break;
125  path.next();
126  }
127  node++;
128  switch (cur->status(*this)) {
129  case SS_FAILED:
130  fail++;
131  delete cur;
132  cur = NULL;
133  path.next();
134  break;
135  case SS_SOLVED:
136  // Deletes all pending branchers
137  (void) cur->choice();
138  delete best;
139  best = cur;
140  cur = NULL;
141  path.next();
142  mark = path.entries();
143  return best->clone();
144  case SS_BRANCH:
145  {
146  Space* c;
147  if ((d == 0) || (d >= opt.c_d)) {
148  c = cur->clone();
149  d = 1;
150  } else {
151  c = NULL;
152  d++;
153  }
154  const Choice* ch = path.push(*this,cur,c);
155  cur->commit(*ch,0);
156  break;
157  }
158  default:
159  GECODE_NEVER;
160  }
161  }
162  GECODE_NEVER;
163  return NULL;
164  }
165 
167  BAB::statistics(void) const {
168  return *this;
169  }
170 
171  forceinline void
173  delete best;
174  best = NULL;
175  path.reset();
176  d = mark = 0U;
177  delete cur;
178  if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
179  cur = NULL;
180  } else {
181  cur = s;
182  }
183  Worker::reset();
184  }
185 
187  BAB::nogoods(void) {
188  return path;
189  }
190 
191  forceinline
192  BAB::~BAB(void) {
193  path.reset();
194  delete best;
195  delete cur;
196  }
197 
198 }}}
199 
200 #endif
201 
202 // STATISTICS: search-sequential
unsigned int a_d
Create a clone during recomputation if distance is greater than a_d (adaptive distance) ...
Definition: search.hh:213
Space must be branched (at least one brancher left)
Definition: core.hpp:1303
Search engine statistics
Definition: search.hh:136
unsigned int c_d
Create a clone after every c_d commits (commit distance)
Definition: search.hh:211
Space * clone(bool share=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:2854
Search engine options
Definition: search.hh:204
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:192
unsigned long int fail
Number of failed nodes in search tree.
Definition: search.hh:139
void path(Home home, int offset, const IntVarArgs &x, IntVar s, IntVar e, IntConLevel icl)
Post propagator such that x forms a Hamiltonian path.
Definition: circuit.cpp:128
void * mark(void *p)
Return marked pointer for p.
BAB(Space *s, const Options &o)
Initialize with space s and search options o.
Definition: bab.hh:83
Computation spaces.
Definition: core.hpp:1362
Implementation of depth-first branch-and-bound search engine.
Definition: bab.hh:53
NoGoods & nogoods(void)
Return no-goods.
Definition: bab.hh:187
Gecode::IntSet d(v, 7)
int entries(void) const
Return number of entries on stack.
Definition: path.hh:270
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:97
Options opt
The options.
Definition: test.cpp:101
Statistics statistics(void) const
Return statistics.
Definition: bab.hh:167
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:2862
bool clone
Whether engines create a clone when being initialized.
Definition: search.hh:207
void reset(void)
Reset stack.
Definition: path.hh:284
Search worker statistics
Definition: worker.hh:48
Choice for performing commit
Definition: core.hpp:1036
No-goods recorded from restarts.
Definition: core.hpp:1240
#define forceinline
Definition: config.hpp:132
void next(void)
Generate path for next node.
Definition: path.hh:234
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
Space * snapshot(Space *s, const Options &o, bool share=true)
Clone space s dependening on options o.
Definition: support.hh:73
Gecode toplevel namespace
unsigned long int node
Number of nodes expanded.
Definition: search.hh:141
const unsigned int nogoods_limit
Depth limit for no-good generation during search.
Definition: search.hh:107
Space is failed
Definition: core.hpp:1301
const Choice * choice(void)
Create new choice for current brancher.
Definition: core.cpp:366
#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
bool empty(void) const
Test whether path is empty.
Definition: path.hh:251
void reset(void)
Reset.
Definition: statistics.hpp:43
Space is solved (no brancher left)
Definition: core.hpp:1302