Generated on Sat Jun 2 2018 07:17:44 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 (s.same())
48  throw Int::ArgumentSame("Int::unary");
49  if (s.size() != p.size())
50  throw Int::ArgumentSizeMismatch("Int::unary");
51  for (int 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=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=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=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=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=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 (s.same())
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=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=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=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=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=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=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=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=p.size(); i--; ) {
201  rel(home, p[i], IRT_GQ, 0);
202  }
203  bool fixP = true;
204  for (int i=p.size(); i--;) {
205  if (!p[i].assigned()) {
206  fixP = false;
207  break;
208  }
209  }
210  if (fixP) {
211  IntArgs pp(p.size());
212  for (int i=p.size(); i--;)
213  pp[i] = p[i].val();
214  unary(home,s,pp,ipl);
215  } else {
216  TaskArray<ManFlexTask> t(home,s.size());
217  for (int i=s.size(); i--; )
218  t[i].init(s[i],p[i],e[i]);
219  GECODE_ES_FAIL(manpost(home,t,ipl));
220  }
221  }
222 
223  void
224  unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
225  const IntVarArgs& e, const BoolVarArgs& m, IntPropLevel ipl) {
226  using namespace Gecode::Int;
227  using namespace Gecode::Int::Unary;
228  if ((s.size() != p.size()) || (s.size() != m.size()) ||
229  (s.size() != e.size()))
230  throw Int::ArgumentSizeMismatch("Int::unary");
231  GECODE_POST;
232  for (int i=p.size(); i--; ) {
233  rel(home, p[i], IRT_GQ, 0);
234  }
235  bool allMandatory = true;
236  for (int i=m.size(); i--;) {
237  if (!m[i].one()) {
238  allMandatory = false;
239  break;
240  }
241  }
242  if (allMandatory) {
243  unary(home,s,p,e,ipl);
244  } else {
245  TaskArray<OptFlexTask> t(home,s.size());
246  for (int i=s.size(); i--; )
247  t[i].init(s[i],p[i],e[i],m[i]);
248  GECODE_ES_FAIL(optpost(home,t,ipl));
249  }
250  }
251 
252 }
253 
254 // STATISTICS: int-post
Bounds propagation.
Definition: int.hh:953
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:1653
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
Argument array for primtive types.
Definition: array.hpp:624
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:49
Domain consistent distinct propagator.
Definition: distinct.hh:249
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
Greater or equal ( )
Definition: int.hh:905
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Gecode::IntArgs i(4, 1, 2, 3, 4)
bool same(void) const
Test whether array contains same variable multiply.
Definition: array.hpp:2065
Passing integer variables.
Definition: int.hh:633
Passing integer arguments.
Definition: int.hh:604
Passing Boolean variables.
Definition: int.hh:687
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:949
Naive value distinct propagator.
Definition: distinct.hh:64
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
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:954
Bounds consistent distinct propagator.
Definition: distinct.hh:125
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