Generated on Wed Nov 1 15:04:39 2006 for Gecode by doxygen 1.4.5

sortedness.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00004  *
00005  *  Copyright:
00006  *     Patrick Pekczynski, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:03:26 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3512 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */     
00021 
00022 #include "gecode/int/sortedness.hh"
00023 namespace Gecode {
00024 
00025   using namespace Int;
00026   void sortedness(Space* home,
00027                   const IntVarArgs& x,
00028                   const IntVarArgs& y,
00029                   IntConLevel) {
00030 
00031     if (home->failed()) {
00032       return;
00033     }
00034 
00035     int n  = x.size();
00036     int n2 = 2*n;
00037 
00038     // construct single tuple for propagation without permutation variables
00039     ViewArray<ViewTuple<IntView,1> > x0(home, n);
00040     for (int i = n; i--; ) {
00041       x0[i][0] = x[i];
00042     }
00043     ViewArray<IntView> y0(home, y);
00044 
00045     ViewArray<IntView> xy(home, n2);
00046     for (int i = 0; i < n; i++) {
00047       xy[i] = x0[i][0];
00048     }
00049     for (int i = n; i < n2; i++) {
00050       xy[i] = y0[i - n];
00051     }
00052     if (xy.shared()) {
00053       throw ArgumentSame("Int::Sortedness");
00054     }
00055     if (n != y.size()) {
00056       throw ArgumentSizeMismatch("Int::Sortedness");
00057     }
00058 
00059 
00060     GECODE_ES_FAIL(home,
00061                    (Sortedness::
00062                     Sortedness<IntView, ViewTuple<IntView,1>, false>::
00063                     post(home, x0, y0)));
00064   }
00065 
00066   void sortedness(Space* home,
00067                   const IntVarArgs& x,
00068                   const IntVarArgs& y,
00069                   const IntVarArgs& z,
00070                   IntConLevel) {
00071     int n = x.size();
00072     int n2 = 2*n;
00073     int n3 = 3*n;
00074 
00075     if ((n != y.size()) || (n != z.size())) {
00076       throw ArgumentSizeMismatch("Int::sortedness");
00077     }
00078     if (home->failed()) {
00079       return;
00080     }
00081 
00082     ViewArray<ViewTuple<IntView, 2> > xz0(home, n);
00083 
00084     // assert that permutation variables encode a permutation
00085     ViewArray<IntView> pz0(home, n);
00086     ViewArray<IntView> y0(home, y);
00087     ViewArray<IntView> xyz(home, n3);
00088 
00089 
00090 
00091     for (int i = n; i--; ) {
00092       xz0[i][0] = x[i];
00093       xz0[i][1] = z[i];
00094       pz0[i]    = z[i];
00095       // Constrain z_i to a valid index
00096       GECODE_ME_FAIL(home,xz0[i][1].gq(home,0));
00097       GECODE_ME_FAIL(home,xz0[i][1].lq(home,n - 1));
00098     }
00099 
00100     // assert permutation
00101     distinct(home, z, ICL_BND);
00102 
00103     for (int i = 0; i < n; i++) {
00104       xyz[i] = xz0[i][0];
00105     }
00106     for (int i = n; i < n2; i++) {
00107       xyz[i] = y0[i - n];
00108     }
00109     for (int i = n2; i < n3; i++) {
00110       xyz[i] = xz0[i - n2][1];
00111     }
00112 
00113     if (xyz.shared()) {
00114       throw ArgumentSame("Int::sortedness");
00115     }
00116 
00117     GECODE_ES_FAIL(home,
00118                    (Sortedness::
00119                     Sortedness<IntView, ViewTuple<IntView,2>, true>::
00120                     post(home, xz0, y0)));
00121   }
00122 }
00123 
00124 // STATISTICS: int-post