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 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 6 * 7 * Copyright: 8 * Christian Schulte, 2005 9 * Vincent Barichard, 2012 10 * 11 * This file is part of Gecode, the generic constraint 12 * development environment: 13 * http://www.gecode.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files (the 17 * "Software"), to deal in the Software without restriction, including 18 * without limitation the rights to use, copy, modify, merge, publish, 19 * distribute, sublicense, and/or sell copies of the Software, and to 20 * permit persons to whom the Software is furnished to do so, subject to 21 * the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 * 34 */ 35 36#include "test/float.hh" 37 38#include <gecode/minimodel.hh> 39 40namespace Test { namespace Float { 41 42 /// %Tests for relation constraints 43 namespace Rel { 44 45 /** 46 * \defgroup TaskTestFloatRel Relation constraints 47 * \ingroup TaskTestFloat 48 */ 49 //@{ 50 /// %Test for simple relation involving float variables 51 class FloatVarXY : public Test { 52 protected: 53 /// Float relation type to propagate 54 Gecode::FloatRelType frt; 55 public: 56 /// Create and register test 57 FloatVarXY(Gecode::FloatRelType frt0, int n, Gecode::FloatNum st) 58 : Test("Rel::Float::Var::XY::"+str(frt0)+"::"+str(n), 59 n+1,-3,3,st,CPLT_ASSIGNMENT,n==1), 60 frt(frt0) { 61 testsubsumed = false; 62 } 63 /// %Test whether \a x is solution 64 virtual MaybeType solution(const Assignment& x) const { 65 if (x.size() == 2) { 66 return cmp(x[0],frt,x[1]); 67 } else { 68 MaybeType r1 = cmp(x[0],frt,x[2]); 69 MaybeType r2 = cmp(x[1],frt,x[2]); 70 if ((r1 == MT_TRUE) && (r2 == MT_TRUE)) return MT_TRUE; 71 else if ((r1 == MT_FALSE) || (r2 == MT_FALSE)) return MT_FALSE; 72 else return MT_MAYBE; 73 } 74 } 75 /// Post constraint on \a x 76 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 77 using namespace Gecode; 78 if (x.size() == 2) { 79 rel(home, x[0], frt, x[1]); 80 } else { 81 FloatVarArgs y(2); 82 y[0]=x[0]; y[1]=x[1]; 83 rel(home, y, frt, x[2]); 84 } 85 } 86 /// Post reified constraint on \a x for \a r 87 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x, 88 Gecode::Reify r) { 89 assert(x.size() == 2); 90 Gecode::rel(home, x[0], frt, x[1], r); 91 } 92 }; 93 94 /// %Test for simple relation involving shared float variables 95 class FloatVarXX : public Test { 96 protected: 97 /// Float relation type to propagate 98 Gecode::FloatRelType frt; 99 public: 100 /// Create and register test 101 FloatVarXX(Gecode::FloatRelType frt0, Gecode::FloatNum st) 102 : Test("Rel::Float::Var::XX::"+str(frt0), 103 1,-3,3,st,CPLT_ASSIGNMENT,true), 104 frt(frt0) { 105 testsubsumed = false; 106 } 107 /// %Test whether \a x is solution 108 virtual MaybeType solution(const Assignment& x) const { 109 return cmp(x[0],frt,x[0]); 110 } 111 /// Post constraint on \a x 112 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 113 Gecode::rel(home, x[0], frt, x[0]); 114 } 115 /// Post reified constraint on \a x for \a r 116 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x, 117 Gecode::Reify r) { 118 Gecode::rel(home, x[0], frt, x[0], r); 119 } 120 }; 121 122 /// %Test for simple relation involving float variable and float constant 123 class FloatFloat : public Test { 124 protected: 125 /// Float relation type to propagate 126 Gecode::FloatRelType frt; 127 /// Float constant 128 Gecode::FloatVal c; 129 public: 130 /// Create and register test 131 FloatFloat(Gecode::FloatRelType frt0, int n, Gecode::FloatNum c0, 132 Gecode::FloatNum st) 133 : Test("Rel::Float::Float::"+str(frt0)+"::"+str(n)+"::"+str(c0), 134 n,-3,3,st,CPLT_ASSIGNMENT,n==1), 135 frt(frt0), c(c0) { 136 testsubsumed = false; 137 } 138 /// %Test whether \a x is solution 139 virtual MaybeType solution(const Assignment& x) const { 140 if (x.size() == 1) { 141 return cmp(x[0],frt,c); 142 } else { 143 return cmp(x[0],frt,c) & cmp(x[1],frt,c); 144 } 145 } 146 /// Post constraint on \a x 147 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 148 using namespace Gecode; 149 if (x.size() == 1) 150 rel(home, x[0], frt, c); 151 else 152 rel(home, x, frt, c); 153 } 154 /// Post reified constraint on \a x for \a r 155 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x, 156 Gecode::Reify r) { 157 assert(x.size() == 1); 158 Gecode::rel(home, x[0], frt, c, r); 159 } 160 }; 161 162 /// Help class to create and register tests 163 class Create { 164 public: 165 /// Perform creation and registration 166 Create(void) { 167 using namespace Gecode; 168 Gecode::FloatNum step = 0.7; 169 for (FloatRelTypes frts; frts(); ++frts) { 170 (void) new FloatVarXY(frts.frt(),1,step); 171 (void) new FloatVarXY(frts.frt(),2,step); 172 (void) new FloatVarXX(frts.frt(),step); 173 for (int c=-4; c<=4; c++) { 174 (void) new FloatFloat(frts.frt(),1,c,step); 175 (void) new FloatFloat(frts.frt(),2,c,step); 176 } 177 } 178 } 179 }; 180 181 Create c; 182 //@} 183 184 } 185 186}} 187 188// STATISTICS: test-float