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