this repo has no description
at develop 4.2 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christopher Mears <Chris.Mears@monash.edu> 5 * 6 * Contributing authors: 7 * Christian Schulte <schulte@gecode.org> 8 * Guido Tack <tack@gecode.org> 9 * 10 * Copyright: 11 * Christopher Mears, 2011 12 * Christian Schulte, 2011 13 * Guido Tack, 2011 14 * 15 * This file is part of Gecode, the generic constraint 16 * development environment: 17 * http://www.gecode.org 18 * 19 * Permission is hereby granted, free of charge, to any person obtaining 20 * a copy of this software and associated documentation files (the 21 * "Software"), to deal in the Software without restriction, including 22 * without limitation the rights to use, copy, modify, merge, publish, 23 * distribute, sublicense, and/or sell copies of the Software, and to 24 * permit persons to whom the Software is furnished to do so, subject to 25 * the following conditions: 26 * 27 * The above copyright notice and this permission notice shall be 28 * included in all copies or substantial portions of the Software. 29 * 30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 31 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 32 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 33 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 34 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 35 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 36 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 37 * 38 */ 39 40#include "test/set.hh" 41 42using namespace Gecode; 43 44namespace Test { namespace Set { 45 46 /// %Tests for value precedence constraints 47 namespace Precede { 48 49 static IntSet ds(-1,3); 50 51 /// %Test for single value precedence constraint 52 class Single : public SetTest { 53 private: 54 /// The values for precedence 55 int s, t; 56 57 /// Check if \a i is a member of set \a x 58 bool in(int i, int x) const { 59 CountableSetRanges xr(ds,x); 60 Iter::Ranges::Singleton ir(i,i); 61 return Iter::Ranges::subset(ir,xr); 62 } 63 64 public: 65 /// Create and register test 66 Single(int s0, int t0) 67 : SetTest("Precede::Single::"+str(s0)+"<"+str(t0),4,ds,false), 68 s(s0), t(t0) {} 69 /// %Test whether \a x is solution 70 virtual bool solution(const SetAssignment& x) const { 71 int n = x.size(); 72 for (int i = 0 ; i < n ; i++) { 73 if (!in(s,x[i]) && in(t,x[i])) 74 return false; 75 if (in(s,x[i]) && !in(t,x[i])) 76 return true; 77 } 78 return true; 79 } 80 /// Post constraint on \a x 81 virtual void post(Gecode::Space& home, Gecode::SetVarArray& x, 82 Gecode::IntVarArray&) { 83 Gecode::precede(home, x, s, t); 84 } 85 }; 86 87 /// %Test for multiple value precedence constraint 88 class Multi : public SetTest { 89 private: 90 /// The values for precedence 91 Gecode::IntArgs c; 92 93 /// Check if \a i is a member of set \a x 94 bool in(int i, int x) const { 95 CountableSetRanges xr(ds,x); 96 Iter::Ranges::Singleton ir(i,i); 97 return Iter::Ranges::subset(ir,xr); 98 } 99 public: 100 /// Create and register test 101 Multi(const Gecode::IntArgs& c0) 102 : SetTest("Precede::Multi::"+str(c0),4,ds,false), c(c0) {} 103 /// %Test whether \a x is solution 104 virtual bool solution(const SetAssignment& x) const { 105 for (int j=0; j<c.size()-1; j++) 106 for (int i=0; i<x.size(); i++) { 107 if (!in(c[j],x[i]) && in(c[j+1],x[i])) 108 return false; 109 if (in(c[j],x[i]) && !in(c[j+1],x[i])) 110 break; 111 } 112 return true; 113 } 114 /// Post constraint on \a x 115 virtual void post(Gecode::Space& home, Gecode::SetVarArray& x, 116 Gecode::IntVarArray&) { 117 Gecode::precede(home, x, c); 118 } 119 }; 120 121 Single _a(2, 3); 122 Single _b(0, 3); 123 124 Multi _c(Gecode::IntArgs({1,2,3})); 125 Multi _d(Gecode::IntArgs({3,2,1})); 126 Multi _e(Gecode::IntArgs({4,2,3,1})); 127 128 } 129 130}} 131 132// STATISTICS: test-set