Generated on Thu Mar 22 10:39:41 2012 for Gecode by doxygen 1.6.3

unary.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2009
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-05-25 16:56:41 +0200 (Wed, 25 May 2011) $ by $Author: schulte $
00011  *     $Revision: 12022 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "test/int.hh"
00039 
00040 #include <gecode/minimodel.hh>
00041 
00042 namespace Test { namespace Int {
00043 
00045    namespace Unary {
00046 
00052 
00053      class ManFixPUnary : public Test {
00054      protected:
00056        Gecode::IntArgs p;
00058        static int st(const Gecode::IntArgs& p) {
00059          int t = 0;
00060          for (int i=p.size(); i--; )
00061            t += p[i];
00062          return t;
00063        }
00064      public:
00066        ManFixPUnary(const Gecode::IntArgs& p0, int o)
00067          : Test("Unary::Man::Fix::"+str(o)+"::"+str(p0),
00068                 p0.size(),o,o+st(p0)), 
00069            p(p0) {
00070          testsearch = false;
00071          contest = CTL_NONE;
00072        }
00074        virtual Assignment* assignment(void) const {
00075          return new RandomAssignment(arity,dom,500);
00076        }
00078        virtual bool solution(const Assignment& x) const {
00079          for (int i=0; i<x.size(); i++)
00080            for (int j=i+1; j<x.size(); j++)
00081              if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
00082                return false;
00083          return true;
00084        }
00086        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00087          Gecode::unary(home, x, p);
00088        }
00089      };
00090 
00092      class OptFixPUnary : public Test {
00093      protected:
00095        Gecode::IntArgs p;
00097        int l;
00099        static int st(const Gecode::IntArgs& p) {
00100          int t = 0;
00101          for (int i=p.size(); i--; )
00102            t += p[i];
00103          return t;
00104        }
00105      public:
00107        OptFixPUnary(const Gecode::IntArgs& p0, int o)
00108          : Test("Unary::Opt::Fix::"+str(o)+"::"+str(p0),
00109                 2*p0.size(),o,o+st(p0)), p(p0), l(o+st(p)/2) {
00110          testsearch = false;
00111          contest = CTL_NONE;
00112        }
00114        virtual Assignment* assignment(void) const {
00115          return new RandomAssignment(arity,dom,500);
00116        }
00118        virtual bool solution(const Assignment& x) const {
00119          int n = x.size() / 2;
00120          for (int i=0; i<n; i++)
00121            if (x[n+i] > l)
00122              for (int j=i+1; j<n; j++)
00123                if(x[n+j] > l)
00124                  if ((x[i]+p[i] > x[j]) && (x[j]+p[j] > x[i]))
00125                    return false;
00126          return true;
00127        }
00129        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00130          int n=x.size() / 2;
00131          Gecode::IntVarArgs s(n);
00132          Gecode::BoolVarArgs m(n);
00133          for (int i=0; i<n; i++) {
00134            s[i]=x[i];
00135            m[i]=Gecode::expr(home, (x[n+i] > l));
00136          }
00137          Gecode::unary(home, s, p, m);
00138        }
00139      };
00140 
00141 
00143      class ManFlexUnary : public Test {
00144      protected:
00146        int _minP;
00148        int _maxP;
00150        int off;
00151      public:
00153        ManFlexUnary(int n, int minP, int maxP, int o)
00154          : Test("Unary::Man::Flex::"+str(o)+"::"+str(n)+"::"
00155                 +str(minP)+"::"+str(maxP),
00156                 2*n,0,n*maxP), _minP(minP), _maxP(maxP), off(o) {
00157          testsearch = false;
00158          testfix = false;
00159          contest = CTL_NONE;
00160        }
00162        virtual Assignment* assignment(void) const {
00163          return new RandomMixAssignment(arity/2,dom,arity/2,
00164                                         Gecode::IntSet(_minP,_maxP),500);
00165        }
00167        virtual bool solution(const Assignment& x) const {
00168          int n = x.size()/2;
00169          for (int i=0; i<n; i++)
00170            for (int j=i+1; j<n; j++)
00171              if ((x[i]+x[n+i] > x[j]) && (x[j]+x[n+j] > x[i]))
00172                return false;
00173          return true;
00174        }
00176        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00177          Gecode::IntVarArgs s(x.size()/2);
00178          Gecode::IntVarArgs px(x.slice(x.size()/2));
00179          Gecode::IntVarArgs e(home,x.size()/2,
00180                               Gecode::Int::Limits::min,
00181                               Gecode::Int::Limits::max);
00182          for (int i=s.size(); i--;) {
00183            s[i] = expr(home, off+x[i]);
00184            rel(home, s[i]+px[i] == e[i]);
00185            rel(home, _minP <= px[i]);
00186            rel(home, _maxP >= px[i]);
00187          }
00188          Gecode::unary(home, s, px, e);
00189        }
00190      };
00191 
00193      class OptFlexUnary : public Test {
00194      protected:
00196        int _minP;
00198        int _maxP;
00200        int off;
00202        int l;
00204        static int st(const Gecode::IntArgs& p) {
00205          int t = 0;
00206          for (int i=p.size(); i--; )
00207            t += p[i];
00208          return t;
00209        }
00210      public:
00212        OptFlexUnary(int n, int minP, int maxP, int o)
00213          : Test("Unary::Opt::Flex::"+str(o)+"::"+str(n)+"::"
00214                 +str(minP)+"::"+str(maxP),
00215                 3*n,0,n*maxP), _minP(minP), _maxP(maxP), off(o),
00216                 l(n*maxP/2) {
00217          testsearch = false;
00218          testfix = false;
00219          contest = CTL_NONE;
00220        }
00222        virtual Assignment* assignment(void) const {
00223          return new RandomMixAssignment(2*(arity/3),dom,arity/3,
00224                                         Gecode::IntSet(_minP,_maxP),500);
00225        }
00227        virtual bool solution(const Assignment& x) const {
00228          int n = x.size() / 3;
00229          for (int i=0; i<n; i++)
00230            if (x[n+i] > l)
00231              for (int j=i+1; j<n; j++)
00232                if(x[n+j] > l)
00233                  if ((x[i]+x[2*n+i] > x[j]) && (x[j]+x[2*n+j] > x[i]))
00234                    return false;
00235          return true;
00236        }
00238        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00239          int n=x.size() / 3;
00240 
00241          Gecode::IntVarArgs s(n);
00242          Gecode::IntVarArgs px(n);
00243          Gecode::IntVarArgs e(home,n,
00244                               Gecode::Int::Limits::min,
00245                               Gecode::Int::Limits::max);
00246          for (int i=n; i--;) {
00247            s[i] = expr(home, off+x[i]);
00248            px[i] = x[2*n+i];
00249            rel(home, s[i]+px[i] == e[i]);
00250            rel(home, _minP <= px[i]);
00251            rel(home, _maxP >= px[i]);
00252          }
00253          Gecode::BoolVarArgs m(n);
00254          for (int i=0; i<n; i++)
00255            m[i]=Gecode::expr(home, (x[n+i] > l));
00256          Gecode::unary(home, s, px, e, m);
00257        }
00258      };
00259 
00260      Gecode::IntArgs p1(4, 2,2,2,2);
00261      ManFixPUnary mfu10(p1,0);
00262      ManFixPUnary mfu1i(p1,Gecode::Int::Limits::min);
00263      OptFixPUnary ofu10(p1,0);
00264      OptFixPUnary ofu1i(p1,Gecode::Int::Limits::min);
00265      ManFlexUnary mflu10(4,0,2,0);
00266      ManFlexUnary mflu1i(4,0,2,Gecode::Int::Limits::min);
00267      ManFlexUnary mflu101(4,1,3,0);
00268      ManFlexUnary mflu1i1(4,1,3,Gecode::Int::Limits::min);
00269      OptFlexUnary oflu10(4,0,2,0);
00270      OptFlexUnary oflu1i(4,0,2,Gecode::Int::Limits::min);
00271 
00272      Gecode::IntArgs p10(5, 2,2,0,2,2);
00273      ManFixPUnary mfu010(p10,0);
00274      ManFixPUnary mfu01i(p10,Gecode::Int::Limits::min);
00275      OptFixPUnary ofu010(p10,0);
00276      OptFixPUnary ofu01i(p10,Gecode::Int::Limits::min);
00277      ManFlexUnary mflu010(5,0,2,0);
00278      ManFlexUnary mflu01i(5,0,2,Gecode::Int::Limits::min);
00279      OptFlexUnary oflu010(5,0,2,0);
00280      OptFlexUnary oflu01i(5,0,2,Gecode::Int::Limits::min);
00281 
00282      Gecode::IntArgs p2(4, 4,3,3,5);
00283      ManFixPUnary mfu20(p2,0);
00284      ManFixPUnary mfu2i(p2,Gecode::Int::Limits::min);
00285      OptFixPUnary ofu20(p2,0);
00286      OptFixPUnary ofu2i(p2,Gecode::Int::Limits::min);
00287      ManFlexUnary mflu20(4,3,5,0);
00288      ManFlexUnary mflu2i(4,3,5,Gecode::Int::Limits::min);
00289      OptFlexUnary oflu20(4,3,5,0);
00290      OptFlexUnary oflu2i(4,3,5,Gecode::Int::Limits::min);
00291 
00292      Gecode::IntArgs p20(6, 4,0,3,3,0,5);
00293      ManFixPUnary mfu020(p20,0);
00294      ManFixPUnary mfu02i(p20,Gecode::Int::Limits::min);
00295      OptFixPUnary ofu020(p20,0);
00296      OptFixPUnary ofu02i(p20,Gecode::Int::Limits::min);
00297      ManFlexUnary mflu020(6,0,5,0);
00298      ManFlexUnary mflu02i(6,0,5,Gecode::Int::Limits::min);
00299      OptFlexUnary oflu020(6,0,5,0);
00300      OptFlexUnary oflu02i(6,0,5,Gecode::Int::Limits::min);
00301 
00302      Gecode::IntArgs p3(6, 4,2,9,3,7,5);
00303      ManFixPUnary mfu30(p3,0);
00304      ManFixPUnary mfu3i(p3,Gecode::Int::Limits::min);
00305      OptFixPUnary ofu30(p3,0);
00306      OptFixPUnary ofu3i(p3,Gecode::Int::Limits::min);
00307      ManFlexUnary mflu30(6,2,7,0);
00308      ManFlexUnary mflu3i(6,2,7,Gecode::Int::Limits::min);
00309      OptFlexUnary oflu30(6,2,7,0);
00310      OptFlexUnary oflu3i(6,2,7,Gecode::Int::Limits::min);
00311 
00312      Gecode::IntArgs p30(8, 4,0,2,9,3,7,5,0);
00313      ManFixPUnary mfu030(p30,0);
00314      ManFixPUnary mfu03i(p30,Gecode::Int::Limits::min);
00315      OptFixPUnary ofu030(p30,0);
00316      OptFixPUnary ofu03i(p30,Gecode::Int::Limits::min);
00317      ManFlexUnary mflu030(8,0,9,0);
00318      ManFlexUnary mflu03i(8,0,9,Gecode::Int::Limits::min);
00319      OptFlexUnary oflu030(8,0,9,0);
00320      OptFlexUnary oflu03i(8,0,9,Gecode::Int::Limits::min);
00321 
00323 
00324    }
00325 }}
00326 
00327 // STATISTICS: test-int