Generated on Sat Jan 12 2019 20:58:51 for Gecode by doxygen 1.8.13
unary.cpp
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  * Guido Tack <tack@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2009
9  * Guido Tack, 2010
10  *
11  * This file is part of Gecode, the generic constraint
12  * development environment:
13  * http://www.gecode.org
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining
16  * a copy of this software and associated documentation files (the
17  * "Software"), to deal in the Software without restriction, including
18  * without limitation the rights to use, copy, modify, merge, publish,
19  * distribute, sublicense, and/or sell copies of the Software, and to
20  * permit persons to whom the Software is furnished to do so, subject to
21  * the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 #include <gecode/int/unary.hh>
37 #include <gecode/int/distinct.hh>
38 
39 #include <algorithm>
40 
41 namespace Gecode {
42 
43  void
44  unary(Home home, const IntVarArgs& s, const IntArgs& p, IntPropLevel ipl) {
45  using namespace Gecode::Int;
46  using namespace Gecode::Int::Unary;
47  if (same(s))
48  throw Int::ArgumentSame("Int::unary");
49  if (s.size() != p.size())
50  throw Int::ArgumentSizeMismatch("Int::unary");
51  for (int i=0; i<p.size(); i++) {
52  Int::Limits::nonnegative(p[i],"Int::unary");
53  Int::Limits::check(static_cast<long long int>(s[i].max()) + p[i],
54  "Int::unary");
55  }
57  bool allOne = true;
58  for (int i=0; i<p.size(); i++) {
59  if (p[i] != 1) {
60  allOne = false;
61  break;
62  }
63  }
64  if (allOne) {
65  ViewArray<IntView> xv(home,s);
66  switch (vbd(ipl)) {
67  case IPL_BND:
69  break;
70  case IPL_DOM:
72  break;
73  default:
75  }
76  } else {
77  TaskArray<ManFixPTask> t(home,s.size());
78  for (int i=0; i<s.size(); i++)
79  t[i].init(s[i],p[i]);
80  GECODE_ES_FAIL(manpost(home,t,ipl));
81  }
82  }
83 
84  void
85  unary(Home home, const TaskTypeArgs& t,
86  const IntVarArgs& flex, const IntArgs& fix, IntPropLevel ipl) {
87  using namespace Gecode::Int;
88  using namespace Gecode::Int::Unary;
89  if ((flex.size() != fix.size()) || (flex.size() != t.size()))
90  throw Int::ArgumentSizeMismatch("Int::unary");
91  for (int i=0; i<fix.size(); i++) {
92  if (t[i] == TT_FIXP)
93  Int::Limits::nonnegative(fix[i],"Int::unary");
94  else
95  Int::Limits::check(fix[i],"Int::unary");
96  Int::Limits::check(static_cast<long long int>(flex[i].max()) + fix[i],
97  "Int::unary");
98  }
100  bool fixp = true;
101  for (int i=0; i<t.size(); i++)
102  if (t[i] != TT_FIXP) {
103  fixp = false; break;
104  }
105  if (fixp) {
106  unary(home, flex, fix, ipl);
107  } else {
108  TaskArray<ManFixPSETask> tasks(home,flex.size());
109  for (int i=0; i<flex.size(); i++)
110  tasks[i].init(t[i],flex[i],fix[i]);
111  GECODE_ES_FAIL(manpost(home,tasks,ipl));
112  }
113  }
114 
115  void
116  unary(Home home, const IntVarArgs& s, const IntArgs& p,
117  const BoolVarArgs& m, IntPropLevel ipl) {
118  using namespace Gecode::Int;
119  using namespace Gecode::Int::Unary;
120  if (same(s))
121  throw Int::ArgumentSame("Int::unary");
122  if ((s.size() != p.size()) || (s.size() != m.size()))
123  throw Int::ArgumentSizeMismatch("Int::unary");
124  for (int i=0; i<p.size(); i++) {
125  Int::Limits::nonnegative(p[i],"Int::unary");
126  Int::Limits::check(static_cast<long long int>(s[i].max()) + p[i],
127  "Int::unary");
128  }
129  bool allMandatory = true;
130  for (int i=0; i<m.size(); i++) {
131  if (!m[i].one()) {
132  allMandatory = false;
133  break;
134  }
135  }
136  if (allMandatory) {
137  unary(home,s,p,ipl);
138  } else {
139  GECODE_POST;
140  TaskArray<OptFixPTask> t(home,s.size());
141  for (int i=0; i<s.size(); i++)
142  t[i].init(s[i],p[i],m[i]);
143  GECODE_ES_FAIL(optpost(home,t,ipl));
144  }
145  }
146 
147  void
148  unary(Home home, const TaskTypeArgs& t,
149  const IntVarArgs& flex, const IntArgs& fix, const BoolVarArgs& m,
150  IntPropLevel ipl) {
151  using namespace Gecode::Int;
152  using namespace Gecode::Int::Unary;
153  if ((flex.size() != fix.size()) || (flex.size() != t.size()) ||
154  (flex.size() != m.size()))
155  throw Int::ArgumentSizeMismatch("Int::unary");
156  bool fixp = true;
157  for (int i=0; i<fix.size(); i++) {
158  if (t[i] == TT_FIXP) {
159  Int::Limits::nonnegative(fix[i],"Int::unary");
160  } else {
161  fixp = false;
162  Int::Limits::check(fix[i],"Int::unary");
163  }
164  Int::Limits::check(static_cast<long long int>(flex[i].max()) + fix[i],
165  "Int::unary");
166  }
167  GECODE_POST;
168  bool allMandatory = true;
169  for (int i=0; i<m.size(); i++) {
170  if (!m[i].one()) {
171  allMandatory = false;
172  break;
173  }
174  }
175  if (allMandatory) {
176  unary(home,t,flex,fix,ipl);
177  } else {
178  if (fixp) {
179  TaskArray<OptFixPTask> tasks(home,flex.size());
180  for (int i=0; i<flex.size(); i++)
181  tasks[i].init(flex[i],fix[i],m[i]);
182  GECODE_ES_FAIL(optpost(home,tasks,ipl));
183  } else {
184  TaskArray<OptFixPSETask> tasks(home,flex.size());
185  for (int i=0; i<flex.size(); i++)
186  tasks[i].init(t[i],flex[i],fix[i],m[i]);
187  GECODE_ES_FAIL(optpost(home,tasks,ipl));
188  }
189  }
190  }
191 
192  void
193  unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
194  const IntVarArgs& e, IntPropLevel ipl) {
195  using namespace Gecode::Int;
196  using namespace Gecode::Int::Unary;
197  if ((s.size() != p.size()) || (s.size() != e.size()))
198  throw Int::ArgumentSizeMismatch("Int::unary");
199  GECODE_POST;
200  for (int i=0; i<p.size(); i++) {
201  IntView pi(p[i]);
202  GECODE_ME_FAIL(pi.gq(home,0));
203  }
204  bool fixP = true;
205  for (int i=0; i<p.size(); i++) {
206  if (!p[i].assigned()) {
207  fixP = false;
208  break;
209  }
210  }
211  if (fixP) {
212  IntArgs pp(p.size());
213  for (int i=0; i<p.size(); i++)
214  pp[i] = p[i].val();
215  unary(home,s,pp,ipl);
216  } else {
217  TaskArray<ManFlexTask> t(home,s.size());
218  for (int i=0; i<s.size(); i++)
219  t[i].init(s[i],p[i],e[i]);
220  GECODE_ES_FAIL(manpost(home,t,ipl));
221  }
222  }
223 
224  void
225  unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
226  const IntVarArgs& e, const BoolVarArgs& m, IntPropLevel ipl) {
227  using namespace Gecode::Int;
228  using namespace Gecode::Int::Unary;
229  if ((s.size() != p.size()) || (s.size() != m.size()) ||
230  (s.size() != e.size()))
231  throw Int::ArgumentSizeMismatch("Int::unary");
232  GECODE_POST;
233  for (int i=0; i<p.size(); i++) {
234  IntView pi(p[i]);
235  GECODE_ME_FAIL(pi.gq(home,0));
236  }
237  bool allMandatory = true;
238  for (int i=0; i<m.size(); i++) {
239  if (!m[i].one()) {
240  allMandatory = false;
241  break;
242  }
243  }
244  if (allMandatory) {
245  unary(home,s,p,e,ipl);
246  } else {
247  TaskArray<OptFlexTask> t(home,s.size());
248  for (int i=0; i<s.size(); i++)
249  t[i].init(s[i],p[i],e[i],m[i]);
250  GECODE_ES_FAIL(optpost(home,t,ipl));
251  }
252  }
253 
254 }
255 
256 // STATISTICS: int-post
Bounds propagation.
Definition: int.hh:978
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition: ipl.hpp:37
NodeType t
Type of node.
Definition: bool-expr.cpp:230
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1569
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:46
ExecStatus manpost(Home home, TaskArray< ManTask > &t, IntPropLevel ipl)
Post mandatory task propagator according to propagation level.
Definition: post.hpp:38
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:49
Domain consistent distinct propagator.
Definition: distinct.hh:283
void nonnegative(int n, const char *l)
Check whether n is in range and nonnegative, otherwise throw out of limits with information l...
Definition: limits.hpp:68
Int for unary resources
Definition: detectable.hpp:34
Task array.
Definition: task.hh:165
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Argument array for non-primitive types.
Definition: array.hpp:638
Gecode::IntArgs i({1, 2, 3, 4})
const int * pi[]
Definition: photo.cpp:14262
Passing integer variables.
Definition: int.hh:656
Passing integer arguments.
Definition: int.hh:628
Passing Boolean variables.
Definition: int.hh:712
bool same(VarArgArray< Var > x, VarArgArray< Var > y)
Definition: array.hpp:1899
ExecStatus optpost(Home home, TaskArray< OptTask > &t, IntPropLevel ipl)
Post optional task propagator according to propagation level.
Definition: post.hpp:53
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Naive value distinct propagator.
Definition: distinct.hh:64
Integer view for integer variables.
Definition: view.hpp:129
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
Exception: Arguments contain same variable multiply
Definition: exception.hpp:80
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
ModEvent gq(Space &home, int n)
Restrict domain values to be greater or equal than n.
Definition: int.hpp:139
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition: macros.hpp:77
Bounds consistent distinct propagator.
Definition: distinct.hh:152
Gecode toplevel namespace
Finite domain integers.
Definition: abs.hpp:38
#define GECODE_POST
Check for failure in a constraint post function.
Definition: macros.hpp:40
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition: limits.hpp:46
Home class for posting propagators
Definition: core.hpp:853
Exception: Arguments are of different size
Definition: exception.hpp:73
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition: macros.hpp:103
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl)
Post propagators for scheduling tasks on unary resources.
Definition: unary.cpp:44