Generated on Sat Jan 20 2018 22:21:18 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  * Copyright:
7  * Christian Schulte, 2009
8  *
9  * Last modified:
10  * $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11  * $Revision: 14967 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #ifndef __GECODE_SEARCH_PARALLEL_BAB_HH__
39 #define __GECODE_SEARCH_PARALLEL_BAB_HH__
40 
42 
43 namespace Gecode { namespace Search { namespace Parallel {
44 
46  class BAB : public Engine {
47  protected:
49  class Worker : public Engine::Worker {
50  protected:
52  int mark;
55  public:
57  Worker(Space* s, BAB& e);
59  BAB& engine(void) const;
61  virtual void run(void);
63  void better(Space* b);
65  void find(void);
67  void reset(Space* s, unsigned int ngdl);
69  virtual ~Worker(void);
70  };
75  public:
77  Worker* worker(unsigned int i) const;
78 
80 
81  void solution(Space* s);
84 
86 
87  BAB(Space* s, const Options& o);
90  virtual Statistics statistics(void) const;
92  virtual void reset(Space* s);
94  virtual void constrain(const Space& b);
97  virtual NoGoods& nogoods(void);
99  virtual ~BAB(void);
101  };
102 
103 
104 
105  /*
106  * Engine: basic access routines
107  */
109  BAB::Worker::engine(void) const {
110  return static_cast<BAB&>(_engine);
111  }
113  BAB::worker(unsigned int i) const {
114  return _worker[i];
115  }
116 
117  forceinline void
118  BAB::Worker::reset(Space* s, unsigned int ngdl) {
119  delete cur;
120  delete best;
121  best = NULL;
122  path.reset((s == NULL) ? 0 : ngdl);
123  d = 0;
124  mark = 0;
125  idle = false;
126  if ((s == NULL) || (s->status(*this) == SS_FAILED)) {
127  delete s;
128  cur = NULL;
129  } else {
130  cur = s;
131  }
133  }
134 
135 
136  /*
137  * Engine: initialization
138  */
141  : Engine::Worker(s,e), mark(0), best(NULL) {}
142 
144  BAB::BAB(Space* s, const Options& o)
145  : Engine(o), best(NULL) {
146  // Create workers
147  _worker = static_cast<Worker**>
148  (heap.ralloc(workers() * sizeof(Worker*)));
149  // The first worker gets the entire search tree
150  _worker[0] = new Worker(s,*this);
151  // All other workers start with no work
152  for (unsigned int i=1; i<workers(); i++)
153  _worker[i] = new Worker(NULL,*this);
154  // Block all workers
155  block();
156  // Create and start threads
157  for (unsigned int i=0; i<workers(); i++)
159  }
160 
161 
162  /*
163  * Engine: search control
164  */
165  forceinline void
167  m.acquire();
168  delete best;
169  best = b->clone(false);
170  mark = path.entries();
171  if (cur != NULL)
172  cur->constrain(*best);
173  m.release();
174  }
175  forceinline void
177  m_search.acquire();
178  if (best != NULL) {
179  s->constrain(*best);
180  if (s->status() == SS_FAILED) {
181  delete s;
182  m_search.release();
183  return;
184  } else {
185  delete best;
186  best = s->clone();
187  }
188  } else {
189  best = s->clone();
190  }
191  // Announce better solutions
192  for (unsigned int i=0; i<workers(); i++)
193  worker(i)->better(best);
194  bool bs = signal();
195  solutions.push(s);
196  if (bs)
197  e_search.signal();
198  m_search.release();
199  }
200 
201 
202  /*
203  * Worker: finding and stealing working
204  */
205  forceinline void
207  // Try to find new work (even if there is none)
208  for (unsigned int i=0; i<engine().workers(); i++) {
209  unsigned long int r_d = 0ul;
210  if (Space* s = engine().worker(i)->steal(r_d)) {
211  // Reset this guy
212  m.acquire();
213  idle = false;
214  // Not idle but also does not have the root of the tree
215  path.ngdl(0);
216  d = 0;
217  cur = s;
218  mark = 0;
219  if (best != NULL)
220  cur->constrain(*best);
222  m.release();
223  return;
224  }
225  }
226  }
227 
228 }}}
229 
230 #endif
231 
232 // STATISTICS: search-parallel
unsigned int workers(void) const
Return number of workers.
Definition: engine.hh:209
Worker(void)
Initialize.
Definition: worker.hh:74
Path path
Current path ins search tree.
Definition: engine.hh:59
Search engine statistics
Definition: search.hh:140
virtual ~BAB(void)
Destructor.
Definition: bab.cpp:256
Worker * worker(unsigned int i) const
Provide access to worker i.
Definition: bab.hh:113
void idle(void)
Report that worker is idle.
Definition: engine.hh:298
Search engine options
Definition: search.hh:446
Statistics statistics(void)
Return statistics.
Definition: engine.hh:282
static void run(Runnable *r)
Construct a new thread and run r.
Definition: thread.hpp:120
void path(Home home, int offset, const IntVarArgs &x, IntVar s, IntVar e, IntPropLevel ipl)
Post propagator such that x forms a Hamiltonian path.
Definition: circuit.cpp:128
void acquire(void)
Acquire the mutex and possibly block.
Definition: none.hpp:46
void * ralloc(size_t s)
Allocate s bytes from heap.
Definition: heap.hpp:361
Space * clone(bool share_data=true, bool share_info=true, CloneStatistics &stat=unused_clone) const
Clone space.
Definition: core.hpp:3326
void * mark(void *p)
Return marked pointer for unmarked pointer p.
void block(void)
Block all workers.
Definition: engine.hh:227
Engine * engine(Engine **slaves, Stop **stops, unsigned int n_slaves, const Statistics &stat, const Search::Options &opt, bool best)
Create sequential portfolio engine.
Definition: pbs.cpp:48
void reset(unsigned int l)
Reset stack and set no-good depth limit to l.
Definition: path.hh:309
void signal(void)
Signal the event.
Definition: none.hpp:63
Computation spaces.
Definition: core.hpp:1748
Parallel branch-and-bound search worker
Definition: bab.hh:49
void better(Space *b)
Accept better solution b.
Definition: bab.hh:166
Gecode::IntSet d(v, 7)
void release(void)
Release the mutex.
Definition: none.hpp:52
BAB(Space *s, const Options &o)
Initialize for space s with options o.
Definition: bab.hh:144
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual void constrain(const Space &b)
Constrain future solutions to be better than b.
Definition: bab.cpp:58
unsigned int d
Distance until next clone.
Definition: engine.hh:63
bool idle
Whether the worker is idle.
Definition: engine.hh:65
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)
Space * cur
Current space being explored.
Definition: engine.hh:61
virtual ~Worker(void)
Destructor.
Definition: bab.cpp:252
void find(void)
Try to find some work.
Definition: bab.hh:206
Support::Event e_search
Event for search (solution found, no more solutions, search stopped)
Definition: engine.hh:167
Space * best
Best solution found so far.
Definition: bab.hh:54
bool signal(void) const
Whether search state changed such that signal is needed.
Definition: engine.hh:294
Parallel depth-first search engine
Definition: engine.hh:49
int mark
Number of entries not yet constrained to be better.
Definition: bab.hh:52
No-goods recorded from restarts.
Definition: core.hpp:1595
Heap heap
The single global heap.
Definition: heap.cpp:48
Parallel branch-and-bound engine
Definition: bab.hh:46
#define forceinline
Definition: config.hpp:173
BAB & engine(void) const
Provide access to engine.
Definition: bab.hh:109
void solution(Space *s)
Report solution s.
Definition: bab.hh:176
Parallel depth-first search worker
Definition: engine.hh:52
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
Support::DynamicQueue< Space *, Heap > solutions
Queue of solutions.
Definition: engine.hh:169
Engine & _engine
Reference to engine.
Definition: engine.hh:55
Gecode toplevel namespace
Worker ** _worker
Array of worker references.
Definition: bab.hh:72
Space * best
Best solution so far.
Definition: bab.hh:74
virtual void run(void)
Start execution of worker.
Definition: bab.cpp:79
Space is failed
Definition: core.hpp:1689
NoGoods & nogoods(void)
Return no-goods.
Definition: engine.hh:423
void reset(void)
Reset.
Definition: statistics.hpp:43
Support::Mutex m_search
Mutex for search.
Definition: engine.hh:165