Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
38namespace Test { namespace Int {
40 namespace Unary {}
41}}
42
43namespace Test { namespace Int { namespace Unary {
44
50
51 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;
70 }
71
72 virtual Assignment* assignment(void) const {
73 return new RandomAssignment(arity,dom,500);
74 }
75
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 }
83
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;
110 }
111
112 virtual Assignment* assignment(void) const {
113 return new RandomAssignment(arity,dom,500);
114 }
115
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 }
126
127 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
128 int n=x.size() / 2;
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;
157 }
158
159 virtual Assignment* assignment(void) const {
160 return new RandomMixAssignment(arity/2,dom,arity/2,
162 }
163
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 }
172
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;
217 }
218
219 virtual Assignment* assignment(void) const {
220 return new RandomMixAssignment(2*(arity/3),dom,arity/3,
222 }
223
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 }
234
235 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
236 int n=x.size() / 3;
237
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 }
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 size(void) const
Return size of array (number of elements)
Definition array.hpp:1613
Passing Boolean variables.
Definition int.hh:721
Passing integer arguments.
Definition int.hh:634
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:662
Integer variable array.
Definition int.hh:772
Computation spaces.
Definition core.hpp:1744
Base class for assignments
Definition int.hh:59
Iterator for basic and advanced integer propagation levels.
Definition int.hh:350
Generate random selection of assignments.
Definition int.hh:96
Generate random selection of assignments.
Definition int.hh:116
bool testsearch
Whether to perform search test.
Definition int.hh:238
bool testfix
Whether to perform fixpoint test.
Definition int.hh:240
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
int arity
Number of variables.
Definition int.hh:226
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
Help class to create and register tests.
Definition unary.cpp:258
Create(void)
Perform creation and registration.
Definition unary.cpp:261
Test for unary constraint
Definition unary.cpp:51
Gecode::IntArgs p
The processing times.
Definition unary.cpp:54
ManFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:64
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:84
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:76
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:56
Test for unary constraint
Definition unary.cpp:140
int off
Offset for start times.
Definition unary.cpp:147
int _minP
Minimum processing time.
Definition unary.cpp:143
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:173
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:159
int _maxP
Maximum processing time.
Definition unary.cpp:145
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
Test for unary constraint with optional tasks
Definition unary.cpp:90
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:112
OptFixPUnary(const Gecode::IntArgs &p0, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:105
Gecode::IntArgs p
The processing times.
Definition unary.cpp:93
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:97
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:116
int l
Threshold for taking a task as optional.
Definition unary.cpp:95
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:127
Test for unary constraint with optional tasks
Definition unary.cpp:190
int _minP
Minimum processing time.
Definition unary.cpp:193
static int st(const Gecode::IntArgs &p)
Get a reasonable maximal start time.
Definition unary.cpp:201
int off
Offset for start times.
Definition unary.cpp:197
int l
Threshold for taking a task as optional.
Definition unary.cpp:199
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition unary.cpp:235
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition unary.cpp:219
OptFlexUnary(int n, int minP, int maxP, int o, Gecode::IntPropLevel ipl0)
Create and register test.
Definition unary.cpp:209
int _maxP
Maximum processing time.
Definition unary.cpp:195
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition unary.cpp:224
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:989
const int min
Smallest allowed integer value.
Definition int.hh:118
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode toplevel namespace
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl=IPL_DEF)
Post propagators for scheduling tasks on unary resources.
Definition unary.cpp:44
Tests for unary scheduling constraints
Definition unary.cpp:40
Testing finite domain integers.
Definition int.cpp:40
@ CTL_NONE
No consistency-test.
Definition int.hh:140
General test support.
Definition afc.cpp:39