Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
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  * Last modified:
10  * $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
11  * $Revision: 14967 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include "test/int.hh"
39 
40 #include <gecode/minimodel.hh>
41 
42 namespace Test { namespace Int {
44  namespace Unary {}
45 }}
46 
47 namespace Test { namespace Int { namespace Unary {
48 
54  class ManFixPUnary : public Test {
56  protected:
60  static int st(const Gecode::IntArgs& p) {
61  int t = 0;
62  for (int i=p.size(); i--; )
63  t += p[i];
64  return t;
65  }
66  public:
69  : Test("Unary::Man::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
70  p0.size(),o,o+st(p0),false,ipl0),
71  p(p0) {
72  testsearch = false;
73  contest = CTL_NONE;
74  }
76  virtual Assignment* assignment(void) const {
77  return new RandomAssignment(arity,dom,500);
78  }
80  virtual bool solution(const Assignment& x) const {
81  for (int i=0; i<x.size(); i++)
82  for (int j=i+1; j<x.size(); j++)
83  if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
84  return false;
85  return true;
86  }
88  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
89  Gecode::unary(home, x, p, ipl);
90  }
91  };
92 
94  class OptFixPUnary : public Test {
95  protected:
99  int l;
101  static int st(const Gecode::IntArgs& p) {
102  int t = 0;
103  for (int i=p.size(); i--; )
104  t += p[i];
105  return t;
106  }
107  public:
110  : Test("Unary::Opt::Fix::"+str(o)+"::"+str(p0)+"::"+str(ipl0),
111  2*p0.size(),o,o+st(p0),false,ipl0), p(p0), l(o+st(p)/2) {
112  testsearch = false;
113  contest = CTL_NONE;
114  }
116  virtual Assignment* assignment(void) const {
117  return new RandomAssignment(arity,dom,500);
118  }
120  virtual bool solution(const Assignment& x) const {
121  int n = x.size() / 2;
122  for (int i=0; i<n; i++)
123  if (x[n+i] > l)
124  for (int j=i+1; j<n; j++)
125  if(x[n+j] > l)
126  if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
127  return false;
128  return true;
129  }
131  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
132  int n=x.size() / 2;
135  for (int i=0; i<n; i++) {
136  s[i]=x[i];
137  m[i]=Gecode::expr(home, (x[n+i] > l));
138  }
139  Gecode::unary(home, s, p, m, ipl);
140  }
141  };
142 
144  class ManFlexUnary : public Test {
145  protected:
147  int _minP;
149  int _maxP;
151  int off;
152  public:
154  ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
155  : Test("Unary::Man::Flex::"+str(o)+"::"+str(n)+"::"
156  +str(minP)+"::"+str(maxP)+"::"+str(ipl0),
157  2*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o) {
158  testsearch = false;
159  testfix = false;
160  contest = CTL_NONE;
161  }
163  virtual Assignment* assignment(void) const {
164  return new RandomMixAssignment(arity/2,dom,arity/2,
165  Gecode::IntSet(_minP,_maxP),500);
166  }
168  virtual bool solution(const Assignment& x) const {
169  int n = x.size()/2;
170  for (int i=0; i<n; i++)
171  for (int j=i+1; j<n; j++)
172  if ((x[i]+x[n+i] > x[j]) && (x[j]+x[n+j] > x[i]))
173  return false;
174  return true;
175  }
177  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
178  Gecode::IntVarArgs s(x.size()/2);
179  Gecode::IntVarArgs px(x.slice(x.size()/2));
180  Gecode::IntVarArgs e(home,x.size()/2,
183  for (int i=s.size(); i--;) {
184  s[i] = expr(home, off+x[i]);
185  rel(home, s[i]+px[i] == e[i]);
186  rel(home, _minP <= px[i]);
187  rel(home, _maxP >= px[i]);
188  }
189  Gecode::unary(home, s, px, e, ipl);
190  }
191  };
192 
194  class OptFlexUnary : public Test {
195  protected:
197  int _minP;
199  int _maxP;
201  int off;
203  int l;
205  static int st(const Gecode::IntArgs& p) {
206  int t = 0;
207  for (int i=p.size(); i--; )
208  t += p[i];
209  return t;
210  }
211  public:
213  OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
214  : Test("Unary::Opt::Flex::"+str(o)+"::"+str(n)+"::"
215  +str(minP)+"::"+str(maxP)+"::"+str(ipl0),
216  3*n,0,n*maxP,false,ipl0), _minP(minP), _maxP(maxP), off(o),
217  l(n*maxP/2) {
218  testsearch = false;
219  testfix = false;
220  contest = CTL_NONE;
221  }
223  virtual Assignment* assignment(void) const {
224  return new RandomMixAssignment(2*(arity/3),dom,arity/3,
225  Gecode::IntSet(_minP,_maxP),500);
226  }
228  virtual bool solution(const Assignment& x) const {
229  int n = x.size() / 3;
230  for (int i=0; i<n; i++)
231  if (x[n+i] > l)
232  for (int j=i+1; j<n; j++)
233  if(x[n+j] > l)
234  if ((x[i]+x[2*n+i] > x[j]) && (x[j]+x[2*n+j] > x[i]))
235  return false;
236  return true;
237  }
239  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
240  int n=x.size() / 3;
241 
243  Gecode::IntVarArgs px(n);
244  Gecode::IntVarArgs e(home,n,
247  for (int i=n; i--;) {
248  s[i] = expr(home, off+x[i]);
249  px[i] = x[2*n+i];
250  rel(home, s[i]+px[i] == e[i]);
251  rel(home, _minP <= px[i]);
252  rel(home, _maxP >= px[i]);
253  }
255  for (int i=0; i<n; i++)
256  m[i]=Gecode::expr(home, (x[n+i] > l));
257  Gecode::unary(home, s, px, e, m, ipl);
258  }
259  };
260 
262  class Create {
263  public:
265  Create(void) {
266  using namespace Gecode;
267  IntArgs p1(4, 2,2,2,2);
268  IntArgs p10(5, 2,2,0,2,2);
269  IntArgs p2(4, 4,3,3,5);
270  IntArgs p20(6, 4,0,3,3,0,5);
271  IntArgs p3(6, 4,2,9,3,7,5);
272  IntArgs p30(8, 4,0,2,9,3,7,5,0);
273 
274  for (IntPropBasicAdvanced ipba; ipba(); ++ipba) {
275  (void) new ManFixPUnary(p1,0,ipba.ipl());
276  (void) new ManFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
277  (void) new OptFixPUnary(p1,0,ipba.ipl());
278  (void) new OptFixPUnary(p1,Gecode::Int::Limits::min,ipba.ipl());
279  (void) new ManFlexUnary(4,0,2,0,ipba.ipl());
280  (void) new ManFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
281  (void) new ManFlexUnary(4,1,3,0,ipba.ipl());
282  (void) new ManFlexUnary(4,1,3,Gecode::Int::Limits::min,ipba.ipl());
283  (void) new OptFlexUnary(4,0,2,0,ipba.ipl());
284  (void) new OptFlexUnary(4,0,2,Gecode::Int::Limits::min,ipba.ipl());
285 
286  (void) new ManFixPUnary(p10,0,ipba.ipl());
287  (void) new ManFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
288  (void) new OptFixPUnary(p10,0,ipba.ipl());
289  (void) new OptFixPUnary(p10,Gecode::Int::Limits::min,ipba.ipl());
290  (void) new ManFlexUnary(5,0,2,0,ipba.ipl());
291  (void) new ManFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
292  (void) new OptFlexUnary(5,0,2,0,ipba.ipl());
293  (void) new OptFlexUnary(5,0,2,Gecode::Int::Limits::min,ipba.ipl());
294 
295  (void) new ManFixPUnary(p2,0,ipba.ipl());
296  (void) new ManFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
297  (void) new OptFixPUnary(p2,0,ipba.ipl());
298  (void) new OptFixPUnary(p2,Gecode::Int::Limits::min,ipba.ipl());
299  (void) new ManFlexUnary(4,3,5,0,ipba.ipl());
300  (void) new ManFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
301  (void) new OptFlexUnary(4,3,5,0,ipba.ipl());
302  (void) new OptFlexUnary(4,3,5,Gecode::Int::Limits::min,ipba.ipl());
303 
304  (void) new ManFixPUnary(p20,0,ipba.ipl());
305  (void) new ManFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
306  (void) new OptFixPUnary(p20,0,ipba.ipl());
307  (void) new OptFixPUnary(p20,Gecode::Int::Limits::min,ipba.ipl());
308  (void) new ManFlexUnary(6,0,5,0,ipba.ipl());
309  (void) new ManFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
310  (void) new OptFlexUnary(6,0,5,0,ipba.ipl());
311  (void) new OptFlexUnary(6,0,5,Gecode::Int::Limits::min,ipba.ipl());
312 
313  (void) new ManFixPUnary(p3,0,ipba.ipl());
314  (void) new ManFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
315  (void) new OptFixPUnary(p3,0,ipba.ipl());
316  (void) new OptFixPUnary(p3,Gecode::Int::Limits::min,ipba.ipl());
317  (void) new ManFlexUnary(6,2,7,0,ipba.ipl());
318  (void) new ManFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
319  (void) new OptFlexUnary(6,2,7,0,ipba.ipl());
320  (void) new OptFlexUnary(6,2,7,Gecode::Int::Limits::min,ipba.ipl());
321 
322  (void) new ManFixPUnary(p30,0,ipba.ipl());
323  (void) new ManFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
324  (void) new OptFixPUnary(p30,0,ipba.ipl());
325  (void) new OptFixPUnary(p30,Gecode::Int::Limits::min,ipba.ipl());
326  (void) new ManFlexUnary(8,0,9,0,ipba.ipl());
327  (void) new ManFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
328  (void) new OptFlexUnary(8,0,9,0,ipba.ipl());
329  (void) new OptFlexUnary(8,0,9,Gecode::Int::Limits::min,ipba.ipl());
330  }
331  }
332  };
333 
336 
337 
338 }}}
339 
340 // STATISTICS: test-int
bool testsearch
Whether to perform search test.
Definition: int.hh:242
ConTestLevel contest
Whether to test for certain consistency.
Definition: int.hh:240
int off
Offset for start times.
Definition: unary.cpp:151
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:239
Gecode::IntPropLevel ipl
Propagation level.
Definition: int.hh:238
Generate random selection of assignments.
Definition: int.hh:100
Help class to create and register tests.
Definition: unary.cpp:262
Passing integer variables.
Definition: int.hh:639
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::IntArgs p
The processing times.
Definition: unary.cpp:58
Create c
Definition: unary.cpp:334
@ CTL_NONE
No consistency-test.
Definition: int.hh:144
Gecode::IntArgs i(4, 1, 2, 3, 4)
NodeType t
Type of node.
Definition: bool-expr.cpp:234
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:116
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:955
Computation spaces.
Definition: core.hpp:1748
Test for unary constraint with optional tasks
Definition: unary.cpp:94
Integer variable array.
Definition: int.hh:744
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:223
int off
Offset for start times.
Definition: unary.cpp:201
int _minP
Minimum processing time.
Definition: unary.cpp:147
Gecode toplevel namespace
int _maxP
Maximum processing time.
Definition: unary.cpp:199
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:244
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:76
Integer sets.
Definition: int.hh:174
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
OptFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:109
Test for unary constraint
Definition: unary.cpp:55
Passing Boolean variables.
Definition: int.hh:693
int l
Threshold for taking a task as optional.
Definition: unary.cpp:203
Gecode::IntArgs p
The processing times.
Definition: unary.cpp:97
OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:213
Test for unary constraint with optional tasks
Definition: unary.cpp:194
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: unary.cpp:163
ManFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:154
const int max
Largest allowed integer value.
Definition: int.hh:116
Create(void)
Perform creation and registration.
Definition: unary.cpp:265
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:228
int arity
Number of variables.
Definition: int.hh:230
Base class for assignments
Definition: int.hh:63
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
ManFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: unary.cpp:68
Test for unary constraint
Definition: unary.cpp:144
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:120
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:80
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:60
General test support.
Definition: afc.cpp:43
const int min
Smallest allowed integer value.
Definition: int.hh:118
Generate random selection of assignments.
Definition: int.hh:120
int l
Threshold for taking a task as optional.
Definition: unary.cpp:99
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:205
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition: unary.cpp:101
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:177
int _maxP
Maximum processing time.
Definition: unary.cpp:149
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:131
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: unary.cpp:168
Passing integer arguments.
Definition: int.hh:610
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: unary.cpp:88
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl)
Post propagators for scheduling tasks on unary resources.
Definition: unary.cpp:48
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
int _minP
Minimum processing time.
Definition: unary.cpp:197
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:232
Iterator for basic and advanced integer propagation levels.
Definition: int.hh:352
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:212