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

order.icc

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/support/sort.hh"
00023 
00024 namespace Gecode { namespace Int { namespace Sortedness {
00025 
00033   template <class View, class Tuple, bool Perm>
00034   forceinline void
00035   sort_sigma(ViewArray<Tuple>& xz, bool fixed) {
00036     int xs = xz.size();
00037 
00038     // test for equal bounds
00039 
00040     if (Perm) {
00041       bool eqbnd = true;
00042       for (int i = xs - 1; i--; ) {
00043         eqbnd &= ( (xz[i][0].min() == xz[i + 1][0].min()) &&
00044                    (xz[i][0].max() == xz[i + 1][0].max()));
00045       }
00046 //       std::cout << "what sort\n";
00047 //       if (eqbnd || fixed) {
00048 //      std::cout << "eqbnd & fixed";
00049 //      TupleMinIncPerm<Tuple> min_inc;
00050 //      Support::quicksort<Tuple, TupleMinIncPerm<Tuple> >
00051 //        (&xz[0], xs, min_inc);
00052 //       } else {
00053 //      std::cout << "normal sort";
00054 //      TupleMinInc<Tuple> min_inc;
00055 //      Support::quicksort<Tuple, TupleMinInc<Tuple> >(&xz[0], xs, min_inc);
00056 //       }
00057 //       std::cout << "\n";
00058         TupleMinIncExt<Tuple> min_inc;
00059         Support::quicksort<Tuple, TupleMinIncExt<Tuple> >(&xz[0], xs, min_inc);
00060 
00061     } else {
00062       TupleMinInc<Tuple> min_inc;
00063       Support::quicksort<Tuple, TupleMinInc<Tuple> >(&xz[0], xs, min_inc);
00064     }
00065   }
00066 
00075   template <class View, class Tuple, bool Perm>
00076   forceinline void
00077   sort_tau(ViewArray<Tuple>& xz, int tau[]) {
00078     int xs = xz.size();
00079 
00080     if (Perm) {
00081 //       bool eqbnd = true;
00082 //       for (int i = xs - 1; i--;) {
00083 //      eqbnd &= ( (xz[i][0].min() == xz[i + 1][0].min()) &&
00084 //                 (xz[i][0].max() == xz[i + 1][0].max()));
00085 //       }
00086 
00087 //       if (eqbnd) {
00088 //      TupleMaxIncPerm<Tuple> max_inc;
00089 //      Support::quicksort<Tuple, TupleMaxIncPerm<Tuple> >
00090 //        (&xz[0], xs, max_inc);
00091 //       } else {
00092 //      TupleMaxInc<Tuple> ltmax(xz);
00093 //      Support::quicksort(&(*tau), xs, ltmax);
00094 //       }
00095         TupleMaxIncExt<Tuple> ltmax(xz);
00096         Support::quicksort(&(*tau), xs, ltmax);
00097 
00098     } else {
00099       TupleMaxInc<Tuple> ltmax(xz);
00100       Support::quicksort(&(*tau), xs, ltmax);
00101     }
00102   }
00103 
00111   template <class View, class Tuple>
00112   forceinline bool
00113   normalize(Space* home,
00114             ViewArray<View>& y,
00115             ViewArray<Tuple>& xz,
00116             bool& nofix) {
00117 
00118     int ys = y.size();
00119     for (int i = 1; i < ys; i++) {
00120       ModEvent me_lb = y[i].gq(home, y[i - 1].min());
00121       if (me_failed(me_lb)) {
00122         return false;
00123       }
00124       nofix |= (me_modified(me_lb) && y[i - 1].min() != y[i].min());
00125     }
00126 
00127     for (int i = ys - 1; i--; ) {
00128       ModEvent me_ub = y[i].lq(home, y[i + 1].max());
00129       if (me_failed(me_ub)) {
00130         return false;
00131       }
00132       nofix |= (me_modified(me_ub) && y[i + 1].max() != y[i].max());
00133     }
00134 
00135     int xs = xz.size();
00136     for (int i = xs; i--; ) {
00137       ModEvent me = xz[i][0].gq(home, y[0].min());
00138       if (me_failed(me)) {
00139         return false;
00140       }
00141       nofix |= (me_modified(me) && xz[i][0].min() != y[0].min());
00142 
00143       me = xz[i][0].lq(home, y[xs - 1].max());
00144       if (me_failed(me)) {
00145         return false;
00146       }
00147       nofix |= (me_modified(me) && xz[i][0].max() != y[xs - 1].max());
00148     }
00149     return true;
00150   }
00151 
00162   template<class View, class Tuple, bool Perm>
00163   forceinline bool
00164   perm_bc(Space* home, int tau[],
00165           SccComponent sinfo[], 
00166           int scclist[],
00167           ViewArray<Tuple>& xz,
00168           bool& crossingedge,
00169           bool& nofix) {
00170 
00171     int ps = xz.size();
00172 
00173     for (int i = 1; i < ps; i++) {
00174       // if there are "crossed edges"
00175       if (xz[i - 1][0].min() < xz[i][0].min()) {
00176         if (xz[i - 1][1].min() > xz[i][1].min()) {
00177           if (xz[i][1].min() != sinfo[scclist[i]].leftmost) {
00178             // edge does not take part in solution
00179             if (xz[i][1].assigned()) { // vital edge do not remove it
00180               if (xz[i - 1][0].max() < xz[i][0].min()) {
00181                 // invalid permutation
00182                 // the upper bound sorting cannot fix this
00183                 return false;
00184               }
00185             } else {
00186               crossingedge = true;
00187               // and the permutation can still be changed
00188               // fix the permutation, i.e. modify z
00189               ModEvent me_z = xz[i][1].gq(home, xz[i - 1][1].min());
00190               if (me_failed(me_z)) {
00191                 return false;
00192               }
00193               nofix |= ( me_modified(me_z) &&
00194                          xz[i - 1][1].min() != xz[i][1].min());
00195             }
00196           }
00197         }
00198       }
00199     }
00200 
00201     // the same check as above for the upper bounds
00202     for (int i = ps - 1; i--; ) {
00203       if (xz[tau[i]][0].max() < xz[tau[i + 1]][0].max()) {
00204         if (xz[tau[i]][1].max() > xz[tau[i + 1]][1].max()) {
00205           if (xz[tau[i]][1].max() != sinfo[scclist[tau[i]]].rightmost) {
00206             // edge does not take part in solution
00207             if (xz[tau[i]][1].assigned()) {
00208               if (xz[tau[i + 1]][0].min() > xz[tau[i]][0].max()) {
00209                 // invalid permutation
00210                 return false;
00211               }
00212             } else {
00213               crossingedge = true;
00214               ModEvent me_z = xz[tau[i]][1].lq(home, xz[tau[i + 1]][1].max());
00215               if (me_failed(me_z)) {
00216                 return false;
00217               }
00218               nofix |= (me_modified(me_z) &&
00219                         xz[tau[i + 1]][1].max() != xz[tau[i]][1].max());
00220             }
00221           }
00222         }
00223       }
00224     }
00225 
00226     return true;
00227   }
00228 
00229 }}}
00230 
00231 // STATISTICS: int-prop
00232