Generated on Sat Feb 7 2015 02:01:25 for Gecode by doxygen 1.8.9.1
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: 2013-02-14 16:29:11 +0100 (Thu, 14 Feb 2013) $ by $Author: schulte $
13  * $Revision: 13292 $
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, IntConLevel icl) {
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  }
60  if (home.failed()) return;
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 (icl) {
71  case ICL_BND:
73  break;
74  case ICL_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]);
85  }
86  }
87 
88  void
89  unary(Home home, const TaskTypeArgs& t,
90  const IntVarArgs& flex, const IntArgs& fix, IntConLevel icl) {
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  if (home.failed()) return;
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, icl);
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]);
116  }
117  }
118 
119  void
120  unary(Home home, const IntVarArgs& s, const IntArgs& p,
121  const BoolVarArgs& m, IntConLevel icl) {
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,icl);
142  } else {
143  if (home.failed()) return;
144  TaskArray<OptFixPTask> t(home,s.size());
145  for (int i=s.size(); i--; )
146  t[i].init(s[i],p[i],m[i]);
148  }
149  }
150 
151  void
152  unary(Home home, const TaskTypeArgs& t,
153  const IntVarArgs& flex, const IntArgs& fix, const BoolVarArgs& m,
154  IntConLevel icl) {
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  if (home.failed()) return;
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,icl);
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]);
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]);
192  }
193  }
194  }
195 
196  void
197  unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
198  const IntVarArgs& e, IntConLevel icl) {
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  if (home.failed()) return;
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,icl);
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]);
224  }
225  }
226 
227  void
228  unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
229  const IntVarArgs& e, const BoolVarArgs& m, IntConLevel icl) {
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  if (home.failed()) return;
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,icl);
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]);
253  }
254  }
255 
256 }
257 
258 // STATISTICS: int-post
bool failed(void) const
Check whether corresponding space is failed.
Definition: core.hpp:3446
NodeType t
Type of node.
Definition: bool-expr.cpp:234
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
bool one(const Gecode::FloatValArgs &a)
Check whether has only one coefficients.
Definition: linear.cpp:50
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
Argument array for primtive types.
Definition: array.hpp:640
Scheduling propagator for unary resource with mandatory tasks
Definition: unary.hh:787
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:57
Domain consistent distinct propagator.
Definition: distinct.hh:251
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:167
Greater or equal ( )
Definition: int.hh:908
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:636
Passing integer arguments.
Definition: int.hh:607
Passing Boolean variables.
Definition: int.hh:690
Naive value distinct propagator.
Definition: distinct.hh:68
Scheduling propagator for unary resource with optional tasks
Definition: unary.hh:810
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
Bounds propagation or consistency.
Definition: int.hh:939
Bounds consistent distinct propagator.
Definition: distinct.hh:129
Gecode toplevel namespace
Finite domain integers.
Definition: abs.hpp:42
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:717
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:96
Domain propagation or consistency.
Definition: int.hh:940
bool same(const Space &home) const
Test whether array contains same variable multiply.
Definition: array.hpp:2085
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntConLevel icl)
Post propagators for scheduling tasks on unary resources.
Definition: unary.cpp:48