this repo has no description
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, 2015
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#ifndef GECODE_SEARCH_SEQ_PBS_HH
35#define GECODE_SEARCH_SEQ_PBS_HH
36
37#include <gecode/search.hh>
38
39namespace Gecode { namespace Search { namespace Seq {
40
41 /// Shared stop information
42 class SharedStopInfo {
43 public:
44 /// Whether search stopped because the slice is done
45 bool done;
46 /// The current failure limit, incremented for each slice
47 unsigned long long int l;
48 };
49
50 /// Stop object used for controling slaves in a portfolio
51 class GECODE_SEARCH_EXPORT PortfolioStop : public Stop {
52 private:
53 /// The stop object for the slaves
54 Stop* so;
55 /// Pointer to shared stop information
56 SharedStopInfo* ssi;
57 public:
58 /// Initialize
59 PortfolioStop(Stop* so);
60 /// Intialize shared stop information
61 void share(SharedStopInfo* ssi);
62 /// Return true if portfolio engine must be stopped
63 virtual bool stop(const Statistics& s, const Options& o);
64 };
65
66 /// Runnable slave of a portfolio master
67 class Slave {
68 protected:
69 /// The slave engine
70 Engine* slave;
71 /// Stop object
72 Stop* stop;
73 public:
74 /// Default constructor
75 Slave(void);
76 /// Copy constructor
77 Slave(const Slave& s) = default;
78 /// Assigment operator
79 Slave& operator =(const Slave& s) = default;
80 /// Initialize with slave \a s and its stop object \a so
81 void init(Engine* s, Stop* so);
82 /// Return next solution
83 Space* next(void);
84 /// Return statistics of slave
85 Statistics statistics(void) const;
86 /// Check whether slave has been stopped
87 bool stopped(void) const;
88 /// Constrain with better solution \a b
89 void constrain(const Space& b);
90 /// Perform one run
91 void run(void);
92 /// Delete slave
93 ~Slave(void);
94 };
95
96 /// Sequential portfolio engine implementation
97 template<bool best>
98 class GECODE_SEARCH_EXPORT PBS : public Engine {
99 protected:
100 /// Master statistics
101 Statistics stat;
102 /// Shared slave information
103 SharedStopInfo ssi;
104 /// Size of a slice
105 unsigned int slice;
106 /// Slaves
107 Slave* slaves;
108 /// Number of slave engines
109 unsigned int n_slaves;
110 /// Current slave to run
111 unsigned int cur;
112 /// Whether a slave has been stopped
113 bool slave_stop;
114 public:
115 /// Initialize
116 PBS(Engine** slaves, Stop** stops, unsigned int n,
117 const Statistics& stat, const Search::Options& opt);
118 /// Return next solution (nullptr, if none exists or search has been stopped)
119 virtual Space* next(void);
120 /// Return statistics
121 virtual Statistics statistics(void) const;
122 /// Check whether engine has been stopped
123 virtual bool stopped(void) const;
124 /// Constrain future solutions to be better than \a b
125 virtual void constrain(const Space& b);
126 /// Destructor
127 virtual ~PBS(void);
128 };
129
130}}}
131
132#include <gecode/search/seq/pbs.hpp>
133
134#endif
135
136// STATISTICS: search-seq