this repo has no description
at develop 7.4 kB view raw
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, 2006 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 40 /// %Tests for channel constraints 41 namespace Channel { 42 43 /** 44 * \defgroup TaskTestIntChannel Channel constraints 45 * \ingroup TaskTestInt 46 */ 47 //@{ 48 /// Simple test for channel (testing all variables) 49 class ChannelFull : public Test { 50 private: 51 int xoff; //< Offset for the x variables 52 int yoff; //< Offset for the y variables 53 public: 54 /// Construct and register test 55 ChannelFull(int xoff0, int yoff0, Gecode::IntPropLevel ipl) 56 : Test("Channel::Full::"+str(xoff0)+"::"+str(yoff0)+"::"+str(ipl), 57 8,0,3,false,ipl), 58 xoff(xoff0), yoff(yoff0) { 59 contest = CTL_NONE; 60 } 61 /// Check whether \a x is solution 62 virtual bool solution(const Assignment& x) const { 63 for (int i=0; i<4; i++) 64 if (x[4+x[i]] != i) 65 return false; 66 return true; 67 } 68 /// Post constraint on \a x 69 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 70 using namespace Gecode; 71 IntVarArgs xa(4); IntVarArgs ya(4); 72 for (int i=4; i--; ) { 73 if (xoff != 0) { 74 IntVar xo(home, xoff, 3+xoff); 75 Gecode::rel(home, x[i] == xo-xoff); 76 xa[i] = xo; 77 } else { 78 xa[i] = x[i]; 79 } 80 if (yoff != 0) { 81 IntVar yo(home, yoff, 3+yoff); 82 Gecode::rel(home, x[4+i] == yo-yoff); 83 ya[i] = yo; 84 } else { 85 ya[i] = x[4+i]; 86 } 87 } 88 channel(home, xa, xoff, ya, yoff, ipl); 89 } 90 }; 91 92 /// Simple test for channel (testing single set of variables) 93 class ChannelHalf : public Test { 94 public: 95 /// Construct and register test 96 ChannelHalf(Gecode::IntPropLevel ipl) 97 : Test("Channel::Half::"+str(ipl),6,0,5,false,ipl) { 98 contest = CTL_NONE; 99 } 100 /// Check whether \a x is solution 101 virtual bool solution(const Assignment& x) const { 102 for (int i=0; i<6; i++) 103 for (int j=i+1; j<6; j++) 104 if (x[i] == x[j]) 105 return false; 106 return true; 107 } 108 /// Post constraint on \a x 109 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 110 using namespace Gecode; 111 Gecode::IntVarArgs y(home,6,dom); 112 for (int i=0; i<6; i++) 113 for (int j=0; j<6; j++) { 114 Gecode::BoolVar b(home,0,1); 115 rel(home, x[i], Gecode::IRT_EQ, j, b); 116 rel(home, y[j], Gecode::IRT_EQ, i, b); 117 } 118 channel(home, x, y, ipl); 119 } 120 }; 121 122 /// %Test channel with shared variables 123 class ChannelShared : public Test { 124 public: 125 /// Construct and register test 126 ChannelShared(Gecode::IntPropLevel ipl) 127 : Test("Channel::Shared::"+str(ipl),6,0,5,false,ipl) { 128 contest = CTL_NONE; 129 } 130 /// Check whether \a x is solution 131 virtual bool solution(const Assignment& x) const { 132 for (int i=0; i<6; i++) 133 if (x[x[i]] != i) 134 return false; 135 return true; 136 } 137 /// Post constraint on \a x 138 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 139 using namespace Gecode; 140 channel(home, x, x, ipl); 141 } 142 }; 143 144 /// %Test channel between integer and Boolean variable 145 class ChannelLinkSingle : public Test { 146 public: 147 /// Construct and register test 148 ChannelLinkSingle(void) 149 : Test("Channel::Bool::Single",2,-1,2) { 150 contest = CTL_NONE; 151 } 152 /// Check whether \a x is solution 153 virtual bool solution(const Assignment& x) const { 154 return ((x[0]==0) || (x[0]==1)) && (x[0]==x[1]); 155 } 156 /// Post constraint on \a x 157 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 158 using namespace Gecode; 159 Gecode::BoolVar b(home,0,1); 160 channel(home, x[0], b); 161 channel(home, x[1], b); 162 } 163 }; 164 165 /// %Test channel between integer variable and array of Boolean variables 166 class ChannelLinkMulti : public Test { 167 private: 168 int o; 169 public: 170 /// Construct and register test 171 ChannelLinkMulti(const std::string& s, int min, int max, int o0) 172 : Test("Channel::Bool::Multi::"+s,7,min,max), o(o0) { 173 } 174 /// Check whether \a x is solution 175 virtual bool solution(const Assignment& x) const { 176 int n = x.size()-1; 177 for (int i=n; i--; ) 178 if ((x[i] != 0) && (x[i] != 1)) 179 return false; 180 int k=x[n]-o; 181 if ((k<0) || (k>=n)) 182 return false; 183 for (int i=0; i<k; i++) 184 if (x[i] != 0) 185 return false; 186 for (int i=k+1; i<n; i++) 187 if (x[i] != 0) 188 return false; 189 return x[k] == 1; 190 } 191 /// Post constraint on \a x 192 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 193 using namespace Gecode; 194 int n=x.size()-1; 195 Gecode::BoolVarArgs b(n); 196 for (int i=n; i--; ) 197 b[i]=channel(home,x[i]); 198 channel(home, b, x[n], o); 199 } 200 }; 201 202 203 204 ChannelFull cfd(0,0,Gecode::IPL_DOM); 205 ChannelFull cfv(0,0,Gecode::IPL_VAL); 206 207 ChannelFull cfd11(1,1,Gecode::IPL_DOM); 208 ChannelFull cfv11(1,1,Gecode::IPL_VAL); 209 210 ChannelFull cfd35(3,5,Gecode::IPL_DOM); 211 ChannelFull cfv35(3,5,Gecode::IPL_VAL); 212 213 ChannelHalf chd(Gecode::IPL_DOM); 214 ChannelHalf chv(Gecode::IPL_VAL); 215 216 ChannelShared csd(Gecode::IPL_DOM); 217 ChannelShared csv(Gecode::IPL_VAL); 218 219 ChannelLinkSingle cls; 220 221 ChannelLinkMulti clma("A", 0, 5, 0); 222 ChannelLinkMulti clmb("B", 1, 6, 1); 223 ChannelLinkMulti clmc("C",-1, 4,-1); 224 //@} 225 226 } 227}} 228 229// STATISTICS: test-int 230