sequence.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * David Rijsman <David.Rijsman@quintiq.com> 00005 * 00006 * Contributing authors: 00007 * Christian Schulte <schulte@gecode.org> 00008 * 00009 * Copyright: 00010 * David Rijsman, 2009 00011 * Christian Schulte, 2009 00012 * 00013 * Last modified: 00014 * $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ 00015 * $Revision: 10684 $ 00016 * 00017 * This file is part of Gecode, the generic constraint 00018 * development environment: 00019 * http://www.gecode.org 00020 * 00021 * Permission is hereby granted, free of charge, to any person obtaining 00022 * a copy of this software and associated documentation files (the 00023 * "Software"), to deal in the Software without restriction, including 00024 * without limitation the rights to use, copy, modify, merge, publish, 00025 * distribute, sublicense, and/or sell copies of the Software, and to 00026 * permit persons to whom the Software is furnished to do so, subject to 00027 * the following conditions: 00028 * 00029 * The above copyright notice and this permission notice shall be 00030 * included in all copies or substantial portions of the Software. 00031 * 00032 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00033 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00035 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00036 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00037 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00038 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00039 * 00040 */ 00041 00042 #include "test/int.hh" 00043 00044 #include <gecode/minimodel.hh> 00045 #include <climits> 00046 00047 namespace Test { namespace Int { 00048 00050 namespace Sequence { 00051 00057 00058 class SequenceTest : public Test { 00059 protected: 00060 Gecode::IntSet s; 00061 int q,l,u; 00062 public: 00064 SequenceTest(const std::string& s, 00065 const Gecode::IntSet& s0, int q0, int l0, int u0, 00066 int size, int min, int max) 00067 : Test("Sequence::"+s,size,min,max), s(s0), q(q0), l(l0), u(u0) { 00068 } 00070 virtual bool solution(const Assignment& x) const { 00071 for (int i=0; i< (x.size() - q + 1); i++ ) { 00072 int total = 0; 00073 for (int j=i; j < i + q; j++ ) { 00074 if (s.in(x[j])) 00075 total++; 00076 if (total > u) 00077 return false; 00078 } 00079 if ( total < l ) 00080 return false; 00081 } 00082 return true; 00083 } 00084 }; 00085 00086 00088 class SequenceBoolTest : public SequenceTest { 00089 public: 00091 SequenceBoolTest(const std::string& s, const Gecode::IntSet& s0, 00092 int q0, int l0, int u0, int size) 00093 : SequenceTest("Bool::"+s,s0,q0,l0,u0,size,0,1) { 00094 } 00095 00097 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 00098 Gecode::BoolVarArgs c(x.size()); 00099 00100 for (int i=0; i<x.size(); i++) { 00101 c[i]=Gecode::channel(home,x[i]); 00102 } 00103 00104 Gecode::sequence(home,c,s,q,l,u); 00105 } 00106 }; 00107 00109 class SequenceIntTest : public SequenceTest { 00110 public: 00112 SequenceIntTest(const std::string& s, const Gecode::IntSet& s0, 00113 int q0, int l0, int u0, int size, int min, int max) 00114 : SequenceTest("Int::"+s,s0,q0,l0,u0,size,min,max) { 00115 } 00116 00118 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 00119 Gecode::sequence(home,x,s,q,l,u); 00120 } 00121 }; 00122 00124 class Create { 00125 public: 00126 00128 Create(void) { 00129 using namespace Gecode; 00130 00131 IntSet a(0,0); 00132 IntSet b(1,1); 00133 IntSet c(2,2); 00134 IntSet d(0,1); 00135 IntArgs ie(2, 0,2); 00136 IntSet e(ie); 00137 00138 (void) new SequenceBoolTest("A",a,3,2,2,6); 00139 (void) new SequenceBoolTest("B",b,3,2,2,6); 00140 (void) new SequenceBoolTest("C",b,6,2,2,6); 00141 (void) new SequenceBoolTest("D",b,6,0,0,6); 00142 (void) new SequenceBoolTest("E",b,6,6,6,6); 00143 00144 00145 (void) new SequenceIntTest ("A",c,3,2,2,6,2,3); 00146 (void) new SequenceIntTest ("B",c,3,2,2,6,2,4); 00147 (void) new SequenceIntTest ("C",b,3,2,2,6,1,3); 00148 (void) new SequenceIntTest ("D",c,3,0,0,3,1,3); 00149 (void) new SequenceIntTest ("E",c,3,3,3,3,1,3); 00150 (void) new SequenceIntTest ("F",c,3,2,2,10,2,3); 00151 00152 (void) new SequenceIntTest ("G",d,3,2,2,6,0,3); 00153 (void) new SequenceIntTest ("H",d,3,2,2,6,0,4); 00154 (void) new SequenceIntTest ("I",d,3,2,2,6,1,3); 00155 (void) new SequenceIntTest ("J",e,3,0,0,6,0,3); 00156 (void) new SequenceIntTest ("K",e,3,3,3,6,0,3); 00157 (void) new SequenceIntTest ("L",e,3,2,2,6,0,3); 00158 00159 } 00160 }; 00161 00162 Create c; 00164 00165 } 00166 }} 00167 00168 // STATISTICS: test-int