Generated on Sat Oct 20 2018 12:43:45 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  *
6  * Copyright:
7  * Christian Schulte, 2009
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 "test/int.hh"
35 
36 #include <gecode/minimodel.hh>
37 
38 namespace Test { namespace Int {
40  namespace Unary {}
41 }}
42 
43 namespace Test { namespace Int { namespace Unary {
44 
50  class ManFixPUnary : public Test {
52  protected:
56  static int st(const Gecode::IntArgs& p) {
57  int t = 0;
58  for (int i=p.size(); i--; )
59  t += p[i];
60  return t;
61  }
62  public:
65  : Test("Unary::Man::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
66  p0.size(),o,o+st(p0),false,ipl0),
67  p(p0) {
68  testsearch = false;
69  contest = CTL_NONE;
70  }
72  virtual Assignment* assignment(void) const {
73  return new RandomAssignment(arity,dom,500);
74  }
76  virtual bool solution(const Assignment& x) const {
77  for (int i=0; i<x.size(); i++)
78  for (int j=i+1; j<x.size(); j++)
79  if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
80  return false;
81  return true;
82  }
84  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
85  Gecode::unary(home, x, p, ipl);
86  }
87  };
88 
90  class OptFixPUnary : public Test {
91  protected:
95  int l;
97  static int st(const Gecode::IntArgs& p) {
98  int t = 0;
99  for (int i=p.size(); i--; )
100  t += p[i];
101  return t;
102  }
103  public:
106  : Test("Unary::Opt::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
107  2*p0.size(),o,o+st(p0),false,ipl0), p(p0), l(o+st(p)/2) {
108  testsearch = false;
109  contest = CTL_NONE;
110  }
112  virtual Assignment* assignment(void) const {
113  return new RandomAssignment(arity,dom,500);
114  }
116  virtual bool solution(const Assignment& x) const {
117  int n = x.size() / 2;
118  for (int i=0; i<n; i++)
119  if (x[n+i] > l)
120  for (int j=i+1; j<n; j++)
121  if(x[n+j] > l)
122  if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
123  return false;
124  return true;
125  }
127  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
128  int n=x.size() / 2;
129  Gecode::IntVarArgs s(n);
130  Gecode::BoolVarArgs m(n);
131  for (int i=0; i<n; i++) {
132  s[i]=x[i];
133  m[i]=Gecode::expr(home, (x[n+i] > l));
134  }
135  Gecode::unary(home, s, p, m, ipl);
136  }
137  };
138 
140  class ManFlexUnary : public Test {
141  protected:
143  int _minP;
145  int _maxP;
147  int off;
148  public:
150  ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
151  : Test("Unary::Man::Flex::"+str(o)+"::"+str(n)+"::"
152  +str(minP)+"::"+str(maxP)+"::"+str(ipl0),
153  2*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o) {
154  testsearch = false;
155  testfix = false;
156  contest = CTL_NONE;
157  }
159  virtual Assignment* assignment(void) const {
160  return new RandomMixAssignment(arity/2,dom,arity/2,
161  Gecode::IntSet(_minP,_maxP),500);
162  }
164  virtual bool solution(const Assignment& x) const {
165  int n = x.size()/2;
166  for (int i=0; i<n; i++)
167  for (int j=i+1; j<n; j++)
168  if ((x[i]+x[n+i] > x[j]) && (x[j]+x[n+j] > x[i]))
169  return false;
170  return true;
171  }
173  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
174  Gecode::IntVarArgs s(x.size()/2);
175  Gecode::IntVarArgs px(x.slice(x.size()/2));
176  Gecode::IntVarArgs e(home,x.size()/2,
179  for (int i=s.size(); i--;) {
180  s[i] = expr(home, off+x[i]);
181  rel(home, s[i]+px[i] == e[i]);
182  rel(home, _minP <= px[i]);
183  rel(home, _maxP >= px[i]);
184  }
185  Gecode::unary(home, s, px, e, ipl);
186  }
187  };
188 
190  class OptFlexUnary : public Test {
191  protected:
193  int _minP;
195  int _maxP;
197  int off;
199  int l;
201  static int st(const Gecode::IntArgs& p) {
202  int t = 0;
203  for (int i=p.size(); i--; )
204  t += p[i];
205  return t;
206  }
207  public:
209  OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
210  : Test("Unary::Opt::Flex::"+str(o)+"::"+str(n)+"::"
211  +str(minP)+"::"+str(maxP)+"::"+str(ipl0),
212  3*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o),
213  l(n*maxP/2) {
214  testsearch = false;
215  testfix = false;
216  contest = CTL_NONE;
217  }
219  virtual Assignment* assignment(void) const {
220  return new RandomMixAssignment(2*(arity/3),dom,arity/3,
221  Gecode::IntSet(_minP,_maxP),500);
222  }
224  virtual bool solution(const Assignment& x) const {
225  int n = x.size() / 3;
226  for (int i=0; i<n; i++)
227  if (x[n+i] > l)
228  for (int j=i+1; j<n; j++)
229  if(x[n+j] > l)
230  if ((x[i]+x[2*n+i] > x[j]) && (x[j]+x[2*n+j] > x[i]))
231  return false;
232  return true;
233  }
235  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
236  int n=x.size() / 3;
237 
238  Gecode::IntVarArgs s(n);
239  Gecode::IntVarArgs px(n);
240  Gecode::IntVarArgs e(home,n,
243  for (int i=n; i--;) {
244  s[i] = expr(home, off+x[i]);
245  px[i] = x[2*n+i];
246  rel(home, s[i]+px[i] == e[i]);
247  rel(home, _minP <= px[i]);
248  rel(home, _maxP >= px[i]);
249  }
250  Gecode::BoolVarArgs m(n);
251  for (int i=0; i<n; i++)
252  m[i]=Gecode::expr(home, (x[n+i] > l));
253  Gecode::unary(home, s, px, e, m, ipl);
254  }
255  };
256 
258  class Create {
259  public:
261  Create(void) {
262  using namespace Gecode;
263  IntArgs p1({2,2,2,2});
264  IntArgs p10({2,2,0,2,2});
265  IntArgs p2({4,3,3,5});
266  IntArgs p20({4,0,3,3,0,5});
267  IntArgs p3({4,2,9,3,7,5});
268  IntArgs p30({4,0,2,9,3,7,5,0});
269 
270  for (IntPropBasicAdvanced ipba; ipba(); ++ipba) {
271  (void) new ManFixPUnary(p1,0,ipba.ipl());
272  (void) new ManFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
273  (void) new OptFixPUnary(p1,0,ipba.ipl());
274  (void) new OptFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
275  (void) new ManFlexUnary(4,0,2,0,ipba.ipl());
276  (void) new ManFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
277  (void) new ManFlexUnary(4,1,3,0,ipba.ipl());
278  (void) new ManFlexUnary(4,1,3,Gecode::Int::Limits::min,ipba.ipl());
279  (void) new OptFlexUnary(4,0,2,0,ipba.ipl());
280  (void) new OptFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
281 
282  (void) new ManFixPUnary(p10,0,ipba.ipl());
283  (void) new ManFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
284  (void) new OptFixPUnary(p10,0,ipba.ipl());
285  (void) new OptFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
286  (void) new ManFlexUnary(5,0,2,0,ipba.ipl());
287  (void) new ManFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
288  (void) new OptFlexUnary(5,0,2,0,ipba.ipl());
289  (void) new OptFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
290 
291  (void) new ManFixPUnary(p2,0,ipba.ipl());
292  (void) new ManFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
293  (void) new OptFixPUnary(p2,0,ipba.ipl());
294  (void) new OptFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
295  (void) new ManFlexUnary(4,3,5,0,ipba.ipl());
296  (void) new ManFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
297  (void) new OptFlexUnary(4,3,5,0,ipba.ipl());
298  (void) new OptFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
299 
300  (void) new ManFixPUnary(p20,0,ipba.ipl());
301  (void) new ManFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
302  (void) new OptFixPUnary(p20,0,ipba.ipl());
303  (void) new OptFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
304  (void) new ManFlexUnary(6,0,5,0,ipba.ipl());
305  (void) new ManFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
306  (void) new OptFlexUnary(6,0,5,0,ipba.ipl());
307  (void) new OptFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
308 
309  (void) new ManFixPUnary(p3,0,ipba.ipl());
310  (void) new ManFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
311  (void) new OptFixPUnary(p3,0,ipba.ipl());
312  (void) new OptFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
313  (void) new ManFlexUnary(6,2,7,0,ipba.ipl());
314  (void) new ManFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
315  (void) new OptFlexUnary(6,2,7,0,ipba.ipl());
316  (void) new OptFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
317 
318  (void) new ManFixPUnary(p30,0,ipba.ipl());
319  (void) new ManFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
320  (void) new OptFixPUnary(p30,0,ipba.ipl());
321  (void) new OptFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
322  (void) new ManFlexUnary(8,0,9,0,ipba.ipl());
323  (void) new ManFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
324  (void) new OptFlexUnary(8,0,9,0,ipba.ipl());
325  (void) new OptFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
326  }
327  }
328  };
329 
332 
333 
334 }}}
335 
336 // STATISTICS: test-int
int off
Offset for start times.
Definition: unary.cpp:147
int _minP
Minimum processing time.
Definition: unary.cpp:193
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
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:908
int size(void) const
Return number of variables.
Definition: int.hpp:46
Test for unary constraint
Definition: unary.cpp:51
int _maxP
Maximum processing time.
Definition: unary.cpp:145
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:116
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:40
Help class to create and register tests.
Definition: unary.cpp:258
Integer variable array.
Definition: int.hh:763
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:219
int l
Threshold for taking a task as optional.
Definition: unary.cpp:95
int _minP
Minimum processing time.
Definition: unary.cpp:143
int _maxP
Maximum processing time.
Definition: unary.cpp:195
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:159
const int max
Largest allowed integer value.
Definition: int.hh:116
Computation spaces.
Definition: core.hpp:1701
Generate random selection of assignments.
Definition: int.hh:96
const int min
Smallest allowed integer value.
Definition: int.hh:118
Iterator for basic and advanced integer propagation levels.
Definition: int.hh:350
OptFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:105
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:76
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:224
ManFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:64
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
Test for unary constraint
Definition: unary.cpp:140
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:84
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
No consistency-test.
Definition: int.hh:140
Generate random selection of assignments.
Definition: int.hh:116
Test for unary constraint with optional tasks
Definition: unary.cpp:90
int l
Threshold for taking a task as optional.
Definition: unary.cpp:199
unsigned int size(I &i)
Size of all ranges of range iterator i.
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:97
Integer sets.
Definition: int.hh:174
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:112
ArrayTraits< VarArgArray< Var > >::ArgsType slice(int start, int inc=1, int n=-1)
Definition: array.hpp:928
int off
Offset for start times.
Definition: unary.cpp:197
Gecode::IntArgs p
The processing times.
Definition: unary.cpp:54
OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:209
Passing integer variables.
Definition: int.hh:656
Passing integer arguments.
Definition: int.hh:628
Passing Boolean variables.
Definition: int.hh:712
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:627
General test support.
Definition: afc.cpp:39
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Test for unary constraint with optional tasks
Definition: unary.cpp:190
Base class for assignments
Definition: int.hh:59
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:235
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:201
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:127
Gecode::IntArgs p
The processing times.
Definition: unary.cpp:93
Gecode toplevel namespace
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:164
ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:150
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:56
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl)
Post propagators for scheduling tasks on unary resources.
Definition: unary.cpp:44
Create(void)
Perform creation and registration.
Definition: unary.cpp:261
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:72
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:173