this repo has no description
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 { 39 /// %Tests for unary scheduling constraints 40 namespace Unary {} 41}} 42 43namespace Test { namespace Int { namespace Unary { 44 45 /** 46 * \defgroup TaskTestIntUnary Unary scheduling constraints 47 * \ingroup TaskTestInt 48 */ 49 //@{ 50 /// %Test for unary constraint 51 class ManFixPUnary : public Test { 52 protected: 53 /// The processing times 54 Gecode::IntArgs p; 55 /// Get a reasonable maximal start time 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: 63 /// Create and register test 64 ManFixPUnary(const Gecode::IntArgs& p0, int o, Gecode::IntPropLevel ipl0) 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 } 71 /// Create and register initial assignment 72 virtual Assignment* assignment(void) const { 73 return new RandomAssignment(arity, dom, 500, _rand); 74 } 75 /// %Test whether \a x is solution 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 /// Post constraint on \a x 84 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 85 Gecode::unary(home, x, p, ipl); 86 } 87 }; 88 89 /// %Test for unary constraint with optional tasks 90 class OptFixPUnary : public Test { 91 protected: 92 /// The processing times 93 Gecode::IntArgs p; 94 /// Threshold for taking a task as optional 95 int l; 96 /// Get a reasonable maximal start time 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: 104 /// Create and register test 105 OptFixPUnary(const Gecode::IntArgs& p0, int o, Gecode::IntPropLevel ipl0) 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 } 111 /// Create and register initial assignment 112 virtual Assignment* assignment(void) const { 113 return new RandomAssignment(arity, dom, 500, _rand); 114 } 115 /// %Test whether \a x is solution 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 /// Post constraint on \a x 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 139 /// %Test for unary constraint 140 class ManFlexUnary : public Test { 141 protected: 142 /// Minimum processing time 143 int _minP; 144 /// Maximum processing time 145 int _maxP; 146 /// Offset for start times 147 int off; 148 public: 149 /// Create and register test 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 } 158 /// Create and register initial assignment 159 virtual Assignment* assignment(void) const { 160 return new RandomMixAssignment(arity / 2, dom, arity / 2, 161 Gecode::IntSet(_minP, _maxP), 500, _rand); 162 } 163 /// %Test whether \a x is solution 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 /// Post constraint on \a x 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, 177 Gecode::Int::Limits::min, 178 Gecode::Int::Limits::max); 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 189 /// %Test for unary constraint with optional tasks 190 class OptFlexUnary : public Test { 191 protected: 192 /// Minimum processing time 193 int _minP; 194 /// Maximum processing time 195 int _maxP; 196 /// Offset for start times 197 int off; 198 /// Threshold for taking a task as optional 199 int l; 200 /// Get a reasonable maximal start time 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: 208 /// Create and register test 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 } 218 /// Create and register initial assignment 219 virtual Assignment* assignment(void) const { 220 return new RandomMixAssignment(2 * (arity / 3), dom, arity / 3, 221 Gecode::IntSet(_minP, _maxP), 500, _rand); 222 } 223 /// %Test whether \a x is solution 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 /// Post constraint on \a x 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, 241 Gecode::Int::Limits::min, 242 Gecode::Int::Limits::max); 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 257 /// Help class to create and register tests 258 class Create { 259 public: 260 /// Perform creation and registration 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 330 Create c; 331 //@} 332 333 334}}} 335 336// STATISTICS: test-int