Generated on Tue Jan 19 2021 06:15:49 for Gecode by doxygen 1.8.13
dom.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Matthias Balzer <matthias.balzer@itwm.fraunhofer.de>
5  *
6  * Copyright:
7  * Fraunhofer ITWM, 2017
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 <gecode/minimodel.hh>
35 
36 #include <cstddef>
37 #include <tuple>
38 #include <utility>
39 
41 namespace { namespace cxx14 {
42 
43  namespace detail {
44 
45  template<std::size_t...>
46  struct sequence {};
47 
48  template<std::size_t N, std::size_t... I>
49  struct make_sequence : make_sequence<N - 1, N - 1, I...> {};
50 
51  template<std::size_t... I>
52  struct make_sequence<0, I...> {
53  using type = sequence<I...>;
54  };
55  }
56 
57  template<std::size_t... I>
58  using index_sequence = detail::sequence<I...>;
59 
60  template<typename... Ts>
61  using index_sequence_for = typename detail::make_sequence<sizeof...(Ts)>::type;
62 }}
63 
64 
65 namespace Gecode {
66 
67  namespace {
69  template<typename... Args>
70  class DomArgs {
71  public:
73  DomArgs(Args...);
74 
75  protected:
77  template<std::size_t... I>
78  void apply(Home, BoolVar, IntPropLevel, cxx14::index_sequence<I...>);
79 
80  private:
82  std::tuple<Args...> _args;
83  };
84 
86  template<typename... Args>
87  class DomArgs<IntVar, Args...> {
88  public:
90  DomArgs(IntVar, Args...);
91 
92  protected:
94  template<std::size_t... I>
95  void apply(Home, BoolVar, IntPropLevel, cxx14::index_sequence<I...>);
96 
97  private:
99  std::tuple<IntVar, Args...> _args;
100  };
101 
102  /*
103  * Operations for argument buffer
104  *
105  */
106  template<typename... Args>
107  DomArgs<Args...>::DomArgs(Args... args)
108  : _args(std::forward<Args>(args)...) {}
109 
110  template<typename... Args>
111  template<std::size_t... I>
112  void
113  DomArgs<Args...>::apply(Home home, BoolVar b, IntPropLevel,
114  cxx14::index_sequence<I...>) {
115  dom(home, std::get<I>(_args)..., b);
116  }
117 
118  template<typename... Args>
119  DomArgs<IntVar, Args...>::DomArgs(IntVar x, Args... args)
120  : _args (x, std::forward<Args>(args)...) {}
121 
122  template<typename... Args>
123  template<std::size_t... I>
124  void
125  DomArgs<IntVar, Args...>::apply(Home home, BoolVar b, IntPropLevel ipl,
126  cxx14::index_sequence<I...>) {
127  dom(home, std::get<I>(_args)..., b, ipl);
128  }
129 
130 
132  template<typename... Args>
133  class DomExpr : public DomArgs<Args...>, public BoolExpr::Misc
134  {
135  public:
136  using DomArgs<Args...>::DomArgs;
137 
139  virtual void post(Home, BoolVar b, bool neg, IntPropLevel) override;
141  virtual ~DomExpr() = default;
142  };
143 
144  template<typename... Args>
145  void
146  DomExpr<Args...>::post(Home home, BoolVar b, bool neg, IntPropLevel ipl)
147  {
148  DomArgs<Args...>::apply(home, neg ? (!b).expr (home, ipl) : b, ipl,
149  cxx14::index_sequence_for<Args...>{});
150  }
151  }
152 
153 
154  /*
155  * Domain constraints
156  *
157  */
158  BoolExpr
159  dom(const IntVar& x, int n) {
160  return BoolExpr(new DomExpr<IntVar, int>(x, n));
161  }
162 
163  BoolExpr
164  dom(const IntVar& x, int l, int u) {
165  return BoolExpr(new DomExpr<IntVar, int, int>(x, l, u));
166  }
167 
168  BoolExpr
169  dom(const IntVar& x, const IntSet& s) {
170  return BoolExpr(new DomExpr<IntVar, IntSet>(x, s));
171  }
172 
173 #ifdef GECODE_HAS_SET_VARS
174  BoolExpr
175  dom(const SetVar& x, SetRelType rt, int i) {
176  return BoolExpr(new DomExpr<SetVar, SetRelType, int>(x, rt, i));
177  }
178 
179  BoolExpr
180  dom(const SetVar& x, SetRelType rt, int i, int j) {
181  return BoolExpr(new DomExpr<SetVar, SetRelType, int, int>(x, rt, i, j));
182  }
183 
184  BoolExpr
185  dom(const SetVar& x, SetRelType rt, const IntSet& s) {
186  return BoolExpr(new DomExpr<SetVar, SetRelType, IntSet>(x, rt, s));
187  }
188 #endif
189 
190 #ifdef GECODE_HAS_FLOAT_VARS
191  BoolExpr
192  dom(const FloatVar& x, const FloatVal& n) {
193  return BoolExpr(new DomExpr<FloatVar, FloatVal>(x, n));
194  }
195 
196  BoolExpr
198  return BoolExpr(new DomExpr<FloatVar, FloatNum, FloatNum>(x, l, u));
199  }
200 #endif
201 }
202 
203 // STATISTICS: minimodel-any
void dom(Home home, const SetVarArgs &x, const SetVarArgs &d)
Constrain domain of according to domain of for all .
Definition: dom.cpp:645
SetRelType
Common relation types for sets.
Definition: set.hh:643
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:238
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntPropLevel)
Post propagator for .
Definition: sequence.cpp:47
Definition: dom.cpp:41
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
Integer sets.
Definition: int.hh:174
Boolean expressions.
Definition: minimodel.hh:1225
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:627
union Gecode::@593::NNF::@62 u
Union depending on nodetype t.
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Float value type.
Definition: float.hh:334
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Set variables
Definition: set.hh:127
Integer variables.
Definition: int.hh:371
Float variables.
Definition: float.hh:870
Gecode toplevel namespace
double FloatNum
Floating point number base type.
Definition: float.hh:106
bool neg
Is atomic formula negative.
Definition: bool-expr.cpp:247