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, 2005 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 <gecode/minimodel.hh> 35 36#include "test/int.hh" 37 38namespace Test { namespace Int { 39 40 /// %Tests for domain constraints 41 namespace Dom { 42 43 /** 44 * \defgroup TaskTestIntDom Domain constraints 45 * \ingroup TaskTestInt 46 */ 47 //@{ 48 /// %Test for domain constraint (integer) 49 class DomInt : public Test { 50 public: 51 /// Create and register test 52 DomInt(int n) 53 : Test("Dom::Int::"+str(n),n,-4,4,n == 1, 54 Gecode::IPL_DOM) {} 55 /// %Test whether \a x is solution 56 virtual bool solution(const Assignment& x) const { 57 for (int i=x.size(); i--; ) 58 if (x[i] != -2) 59 return false; 60 return true; 61 } 62 /// Post constraint on \a x 63 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 64 if (x.size() == 1) 65 Gecode::dom(home, x[0], -2); 66 else 67 Gecode::dom(home, x, -2); 68 } 69 /// Post reified constraint on \a x for \a r 70 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, 71 Gecode::Reify r) { 72 assert(x.size() == 1); 73 if (_rand(2) != 0) { 74 Gecode::dom(home, x[0], -2, r); 75 } else { 76 switch (r.mode()) { 77 case Gecode::RM_EQV: 78 Gecode::rel(home, Gecode::dom(x[0], -2) == r.var()); break; 79 case Gecode::RM_IMP: 80 Gecode::rel(home, Gecode::dom(x[0], -2) << r.var()); break; 81 case Gecode::RM_PMI: 82 Gecode::rel(home, Gecode::dom(x[0], -2) >> r.var()); break; 83 default: GECODE_NEVER; 84 } 85 } 86 } 87 }; 88 89 90 /// %Test for domain constraint (range) 91 class DomRange : public Test { 92 public: 93 /// Create and register test 94 DomRange(int n) 95 : Test("Dom::Range::"+str(n),n,-4,4,n == 1, 96 Gecode::IPL_DOM) {} 97 /// %Test whether \a x is solution 98 virtual bool solution(const Assignment& x) const { 99 for (int i=x.size(); i--; ) 100 if ((x[i] < -2) || (x[i] > 2)) 101 return false; 102 return true; 103 } 104 /// Post constraint on \a x 105 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 106 if (x.size() == 1) 107 Gecode::dom(home, x[0], -2, 2); 108 else 109 Gecode::dom(home, x, -2, 2); 110 } 111 /// Post reified constraint on \a x for \a r 112 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, 113 Gecode::Reify r) { 114 assert(x.size() == 1); 115 if (_rand(2) != 0) { 116 Gecode::dom(home, x[0], -2, 2, r); 117 } else { 118 switch (r.mode()) { 119 case Gecode::RM_EQV: 120 Gecode::rel(home, Gecode::dom(x[0], -2, 2) == r.var()); break; 121 case Gecode::RM_IMP: 122 Gecode::rel(home, Gecode::dom(x[0], -2, 2) << r.var()); break; 123 case Gecode::RM_PMI: 124 Gecode::rel(home, Gecode::dom(x[0], -2, 2) >> r.var()); break; 125 default: GECODE_NEVER; 126 } 127 } 128 } 129 }; 130 131 /// %Test for domain constraint (empty range) 132 class DomRangeEmpty : public Test { 133 public: 134 /// Create and register test 135 DomRangeEmpty(void) : Test("Dom::Range::Empty",1,-4,4,true) {} 136 /// %Test whether \a x is solution 137 virtual bool solution(const Assignment&) const { 138 return false; 139 } 140 /// Post constraint on \a x 141 virtual void post(Gecode::Space& home, Gecode::IntVarArray&) { 142 home.fail(); 143 } 144 /// Post reified constraint on \a x for \a r 145 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, 146 Gecode::Reify r) { 147 Gecode::dom(home, x[0], 3, 2, r); 148 } 149 }; 150 151 152 const int r[4][2] = { 153 {-4,-3},{-1,-1},{1,1},{3,5} 154 }; 155 Gecode::IntSet d(r,4); 156 157 /// %Test for domain constraint (full integer set) 158 class DomDom : public Test { 159 public: 160 /// Create and register test 161 DomDom(int n) 162 : Test("Dom::Dom::"+str(n),n,-6,6,n == 1, 163 Gecode::IPL_DOM) {} 164 /// %Test whether \a x is solution 165 virtual bool solution(const Assignment& x) const { 166 for (int i=x.size(); i--; ) 167 if (!(((x[i] >= -4) && (x[i] <= -3)) || 168 ((x[i] >= -1) && (x[i] <= -1)) || 169 ((x[i] >= 1) && (x[i] <= 1)) || 170 ((x[i] >= 3) && (x[i] <= 5)))) 171 return false; 172 return true; 173 } 174 /// Post constraint on \a x 175 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 176 if (x.size() == 1) 177 Gecode::dom(home, x[0], d); 178 else 179 Gecode::dom(home, x, d); 180 } 181 /// Post reified constraint on \a x for \a r 182 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, 183 Gecode::Reify r) { 184 assert(x.size() == 1); 185 if (_rand(2) != 0) { 186 Gecode::dom(home, x[0], d, r); 187 } else { 188 switch (r.mode()) { 189 case Gecode::RM_EQV: 190 Gecode::rel(home, Gecode::dom(x[0], d) == r.var()); break; 191 case Gecode::RM_IMP: 192 Gecode::rel(home, Gecode::dom(x[0], d) << r.var()); break; 193 case Gecode::RM_PMI: 194 Gecode::rel(home, Gecode::dom(x[0], d) >> r.var()); break; 195 default: GECODE_NEVER; 196 } 197 } 198 } 199 }; 200 201 DomInt di1(1); 202 DomInt di3(3); 203 DomRange dr1(1); 204 DomRange dr3(3); 205 DomDom dd1(1); 206 DomDom dd3(3); 207 DomRangeEmpty dre; 208 //@} 209 210 } 211}} 212 213// STATISTICS: test-int 214