File: branch.hh

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (344 lines) | stat: -rw-r--r-- 12,372 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2017
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifndef GECODE_FLATZINC_BRANCH_HH
#define GECODE_FLATZINC_BRANCH_HH

#include <gecode/int.hh>
#include <gecode/int/branch.hh>
#include <gecode/flatzinc.hh>

namespace Gecode { namespace FlatZinc {

  /// Which integer or Boolean variable to select for branching
  class IntBoolVarBranch : public VarBranch<IntVar> {
  public:
    /// Which variable selection
    enum Select {
      SEL_AFC_MAX,         ///< With largest accumulated failure count
      SEL_ACTION_MAX,      ///< With highest action
      SEL_CHB_MAX,         ///< With highest CHB Q-score
      SEL_AFC_SIZE_MAX,    ///< With largest accumulated failure count divided by domain size
      SEL_ACTION_SIZE_MAX, ///< With largest action divided by domain size
      SEL_CHB_SIZE_MAX     ///< With largest CHB Q-score divided by domain size
    };
  protected:
    /// Which variable to select
    Select s;
    /// Integer AFC
    IntAFC iafc;
    /// Boolean AFC
    BoolAFC bafc;
    /// Integer action
    IntAction iaction;
    /// Boolean action
    BoolAction baction;
    /// Integer CHB
    IntCHB ichb;
    /// Boolean CHB
    BoolCHB bchb;
  public:
    /// Initialize with selection strategy \a s and decay factor \a d
    IntBoolVarBranch(Select s, double d);
    /// Initialize with selection strategy \a s and AFC \a i and \a b
    IntBoolVarBranch(Select s, IntAFC i, BoolAFC b);
    /// Initialize with selection strategy \a s and action \a i and \a b
    IntBoolVarBranch(Select s, IntAction i, BoolAction b);
    /// Initialize with selection strategy \a s and CHB \a i and \a b
    IntBoolVarBranch(Select s, IntCHB i, BoolCHB b);
    /// Return selection strategy
    Select select(void) const;
    /// Return integer AFC
    IntAFC intafc(void) const;
    /// Return Boolean AFC
    BoolAFC boolafc(void) const;
    /// Return integer action
    IntAction intaction(void) const;
    /// Return Boolean action
    BoolAction boolaction(void) const;
    /// Return integer CHB
    IntCHB intchb(void) const;
    /// Return Boolean AFC
    BoolCHB boolchb(void) const;
    /// Expand AFC, action, and CHB
    void expand(Home home, const IntVarArgs& x, const BoolVarArgs& y);
  };

  /// Variable selection for both integer and Boolean variables
  //@{
  /// Select variable with largest accumulated failure count
  IntBoolVarBranch INTBOOL_VAR_AFC_MAX(double d=1.0);
  /// Select variable with largest accumulated failure count
  IntBoolVarBranch INTBOOL_VAR_AFC_MAX(IntAFC ia, BoolAFC ba);
  /// Select variable with highest action
  IntBoolVarBranch INTBOOL_VAR_ACTION_MAX(double d=1.0);
  /// Select variable with highest action
  IntBoolVarBranch INTBOOL_VAR_ACTION_MAX(IntAction ia, BoolAction ba);
  /// Select variable with largest CHB Q-score
  IntBoolVarBranch INTBOOL_VAR_CHB_MAX(double d=1.0);
  /// Select variable with largest CHB Q-score
  IntBoolVarBranch INTBOOL_VAR_CHB_MAX(IntCHB ic, BoolCHB bc);
  /// Select variable with largest accumulated failure count divided by domain size
  IntBoolVarBranch INTBOOL_VAR_AFC_SIZE_MAX(double d=1.0);
  /// Select variable with largest accumulated failure count divided by domain size
  IntBoolVarBranch INTBOOL_VAR_AFC_SIZE_MAX(IntAFC ia, BoolAFC ba);
  /// Select variable with largest action divided by domain size
  IntBoolVarBranch INTBOOL_VAR_ACTION_SIZE_MAX(double d=1.0);
  /// Select variable with largest action divided by domain size
  IntBoolVarBranch INTBOOL_VAR_ACTION_SIZE_MAX(IntAction ia, BoolAction ba);
  /// Select variable with largest CHB Q-score divided by domain size
  IntBoolVarBranch INTBOOL_VAR_CHB_SIZE_MAX(double d=1.0);
  /// Select variable with largest CHB Q-score divided by domain size
  IntBoolVarBranch INTBOOL_VAR_CHB_SIZE_MAX(IntCHB ic, BoolCHB bc);
  //@}
  
  /// Select by maximal AFC
  class MeritMaxAFC {
  protected:
    /// Integer AFC information
    IntAFC iafc;
    /// Boolean AFC information
    BoolAFC bafc;
  public:
    /// Constructor for initialization
    MeritMaxAFC(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxAFC(Space& home, MeritMaxAFC& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// Select by maximal AFC over size
  class MeritMaxAFCSize {
  protected:
    /// Integer AFC information
    IntAFC iafc;
    /// Boolean AFC information
    BoolAFC bafc;
  public:
    /// Constructor for initialization
    MeritMaxAFCSize(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxAFCSize(Space& home, MeritMaxAFCSize& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// Select by maximal Action
  class MeritMaxAction {
  protected:
    /// Integer Action information
    IntAction iaction;
    /// Boolean Action information
    BoolAction baction;
  public:
    /// Constructor for initialization
    MeritMaxAction(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxAction(Space& home, MeritMaxAction& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// Select by maximal Action over size
  class MeritMaxActionSize {
  protected:
    /// Integer Action information
    IntAction iaction;
    /// Boolean Action information
    BoolAction baction;
  public:
    /// Constructor for initialization
    MeritMaxActionSize(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxActionSize(Space& home, MeritMaxActionSize& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// Select by maximal CHB
  class MeritMaxCHB {
  protected:
    /// Integer CHB information
    IntCHB ichb;
    /// Boolean CHB information
    BoolCHB bchb;
  public:
    /// Constructor for initialization
    MeritMaxCHB(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxCHB(Space& home, MeritMaxCHB& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// Select by maximal CHB over size
  class MeritMaxCHBSize {
  protected:
    /// Integer CHB information
    IntCHB ichb;
    /// Boolean CHB information
    BoolCHB bchb;
  public:
    /// Constructor for initialization
    MeritMaxCHBSize(Space& home, const IntBoolVarBranch& ibvb);
    /// Constructor for cloning
    MeritMaxCHBSize(Space& home, MeritMaxCHBSize& m);
    /// Return merit
    double operator()(Int::IntView x, int i) const;
    /// Return merit
    double operator()(Int::BoolView x, int i) const;
    /// Dispose
    void dispose(void);
  };

  /// %Choice storing position and value
  class GECODE_VTABLE_EXPORT PosIntChoice : public Choice {
  private:
    /// Position of view to assign
    int _pos;
    /// Value to assign to
    int _val;
  public:
    /// Initialize choice for brancher \a b, number of alternatives \a a, position \a p, and value \a n
    PosIntChoice(const Brancher& b, unsigned int a, int p, int n);
    /// Return position of view to assign
    int pos(void) const;
    /// Return value to assign to
    int val(void) const;
    /// Archive into \a e
    virtual void archive(Archive& e) const;
  };

  /// Base-class for brancher for integer and Boolean views
  class IntBoolBrancherBase : public Brancher {
  protected:
    /// Integer views to branch on
    ViewArray<Int::IntView> x;
    /// Boolean views to branch on
    ViewArray<Int::BoolView> y;
    /// Unassigned views start here (might be in \a x or \a y)
    mutable int start;
    /// Integer value selection and commit object
    ValSelCommitBase<Int::IntView,int>* xvsc;
    /// Boolean value selection and commit object
    ValSelCommitBase<Int::BoolView,int>* yvsc;
    /// Constructor for cloning \a b
    IntBoolBrancherBase(Space& home, IntBoolBrancherBase& b);
    /// Constructor for creation
    IntBoolBrancherBase(Home home,
                        ViewArray<Int::IntView> x, ViewArray<Int::BoolView> y,
                        ValSelCommitBase<Int::IntView,int>* xvsc,
                        ValSelCommitBase<Int::BoolView,int>* yvsc);
  public:
    /// Check status of brancher, return true if alternatives left
    virtual bool status(const Space& home) const;
    /// Return choice
    virtual const Choice* choice(Space& home) = 0;
    /// Return choice
    virtual const Choice* choice(const Space& home, Archive& e);
    /// Perform commit for choice \a c and alternative \a b
    virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
    /// Create no-good literal for choice \a c and alternative \a b
    virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
    /// Print branch for choice \a c and alternative \a b
    virtual void print(const Space& home, const Choice& c, unsigned int b,
                       std::ostream& o) const;
    /// Delete brancher and return its size
    virtual size_t dispose(Space& home);
  };

  /// Brancher for integer and Boolean views
  template<class Merit>
  class IntBoolBrancher : public IntBoolBrancherBase {
  protected:
    /// Selection by maximal merit
    Merit merit;
    /// Constructor for cloning \a b
    IntBoolBrancher(Space& home, IntBoolBrancher& b);
    /// Constructor for creation
    IntBoolBrancher(Home home,
                    ViewArray<Int::IntView> x, ViewArray<Int::BoolView> y,
                    Merit& m,
                    ValSelCommitBase<Int::IntView,int>* xvsc,
                    ValSelCommitBase<Int::BoolView,int>* yvsc);
  public:
    /// Return choice
    virtual const Choice* choice(Space& home);
    /// Perform cloning
    virtual Actor* copy(Space& home);
    /// Post brancher
    static void post(Home home,
                     ViewArray<Int::IntView> x, ViewArray<Int::BoolView> y,
                     Merit& m,
                     ValSelCommitBase<Int::IntView,int>* xvsc,
                     ValSelCommitBase<Int::BoolView,int>* yvsc);
    /// Delete brancher and return its size
    virtual size_t dispose(Space& home);
  };

  /// Map respective integer value selection to Boolean value selection
  BoolValBranch i2b(const IntValBranch& ivb);

  /// Branch function for integer and Boolean variables
  GECODE_FLATZINC_EXPORT void
  branch(Home home, const IntVarArgs& x, const BoolVarArgs& y,
         IntBoolVarBranch vars, IntValBranch vals);

}}

#include <gecode/flatzinc/branch.hpp>

#endif

// STATISTICS: flatzinc-branch