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, 2012
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#include <functional>
35
36namespace Gecode {
37
38 /**
39 * \brief Tie-break limit function
40 *
41 * Here the value \a w is the worst and \b is the best merit value
42 * found. The function must return the merit value that is considered
43 * the limit for breaking ties.
44 *
45 * \ingroup TaskModelBranch
46 */
47 typedef std::function<double(const Space& home, double w, double b)>
48 BranchTbl;
49
50 /**
51 * \brief Variable branching information
52 * \ingroup TaskModelBranch
53 */
54 template<class Var>
55 class VarBranch {
56 public:
57 /// Corresponding merit function
58 typedef typename BranchTraits<Var>::Merit MeritFunction;
59 protected:
60 /// Tie-breaking limit function
61 BranchTbl _tbl;
62 /// Random number generator
63 Rnd _rnd;
64 /// Decay information for AFC and action
65 double _decay;
66 /// AFC information
67 AFC _afc;
68 /// Action information
69 Action _act;
70 /// CHB information
71 CHB _chb;
72 /// Merit function
73 MeritFunction _mf;
74 public:
75 /// Initialize
76 VarBranch(void);
77 /// Initialize with tie-break limit function \a t
78 VarBranch(BranchTbl t);
79 /// Initialize with random number generator \a r
80 VarBranch(Rnd r);
81 /// Initialize with decay factor \a d and tie-break limit function \a t
82 VarBranch(double d, BranchTbl t);
83 /// Initialize with AFC \a a and tie-break limit function \a t
84 VarBranch(AFC a, BranchTbl t);
85 /// Initialize with action \a a and tie-break limit function \a t
86 VarBranch(Action a, BranchTbl t);
87 /// Initialize with CHB \a c and tie-break limit function \a t
88 VarBranch(CHB c, BranchTbl t);
89 /// Initialize with merit function \a f and tie-break limit function \a t
90 VarBranch(MeritFunction f, BranchTbl t);
91 /// Return tie-break limit function
92 BranchTbl tbl(void) const;
93 /// Return random number generator
94 Rnd rnd(void) const;
95 /// Return decay factor
96 double decay(void) const;
97 /// Return AFC
98 AFC afc(void) const;
99 /// Set AFC to \a a
100 void afc(AFC a);
101 /// Return action
102 Action action(void) const;
103 /// Set action to \a a
104 void action(Action a);
105 /// Return CHB
106 CHB chb(void) const;
107 /// Set CHB to \a chb
108 void chb(CHB chb);
109 /// Return merit function
110 MeritFunction merit(void) const;
111 };
112
113 // Variable branching
114 template<class Var>
115 inline
116 VarBranch<Var>::VarBranch(void)
117 : _tbl(nullptr), _decay(1.0) {}
118
119 template<class Var>
120 inline
121 VarBranch<Var>::VarBranch(BranchTbl t)
122 : _tbl(t), _decay(1.0) {}
123
124 template<class Var>
125 inline
126 VarBranch<Var>::VarBranch(double d, BranchTbl t)
127 : _tbl(t), _decay(d) {}
128
129 template<class Var>
130 inline
131 VarBranch<Var>::VarBranch(AFC a, BranchTbl t)
132 : _tbl(t), _decay(1.0), _afc(a) {
133 if (!_afc)
134 throw UninitializedAFC("VarBranch<Var>::VarBranch");
135 }
136
137 template<class Var>
138 inline
139 VarBranch<Var>::VarBranch(Action a, BranchTbl t)
140 : _tbl(t), _decay(1.0), _act(a) {
141 if (!_act)
142 throw UninitializedAction("VarBranch<Var>::VarBranch");
143 }
144
145 template<class Var>
146 inline
147 VarBranch<Var>::VarBranch(CHB c, BranchTbl t)
148 : _tbl(t), _decay(1.0), _chb(c) {
149 if (!_chb)
150 throw UninitializedCHB("VarBranch<Var>::VarBranch");
151 }
152
153 template<class Var>
154 inline
155 VarBranch<Var>::VarBranch(Rnd r)
156 : _tbl(nullptr), _rnd(r), _decay(1.0) {
157 if (!_rnd)
158 throw UninitializedRnd("VarBranch<Var>::VarBranch");
159 }
160
161 template<class Var>
162 inline
163 VarBranch<Var>::VarBranch(MeritFunction f, BranchTbl t)
164 : _tbl(t), _decay(1.0), _mf(f) {}
165
166 template<class Var>
167 inline BranchTbl
168 VarBranch<Var>::tbl(void) const {
169 return _tbl;
170 }
171
172 template<class Var>
173 inline Rnd
174 VarBranch<Var>::rnd(void) const {
175 return _rnd;
176 }
177
178 template<class Var>
179 inline double
180 VarBranch<Var>::decay(void) const {
181 return _decay;
182 }
183
184 template<class Var>
185 inline AFC
186 VarBranch<Var>::afc(void) const {
187 return _afc;
188 }
189
190 template<class Var>
191 inline void
192 VarBranch<Var>::afc(AFC a) {
193 _afc=a;
194 }
195
196 template<class Var>
197 inline Action
198 VarBranch<Var>::action(void) const {
199 return _act;
200 }
201
202 template<class Var>
203 inline void
204 VarBranch<Var>::action(Action a) {
205 _act=a;
206 }
207
208 template<class Var>
209 inline CHB
210 VarBranch<Var>::chb(void) const {
211 return _chb;
212 }
213
214 template<class Var>
215 inline void
216 VarBranch<Var>::chb(CHB chb) {
217 _chb=chb;
218 }
219
220 template<class Var>
221 inline typename VarBranch<Var>::MeritFunction
222 VarBranch<Var>::merit(void) const {
223 return _mf;
224 }
225
226}
227
228// STATISTICS: kernel-branch