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

all-interval-sort.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, 2005
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:06:52 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3517 $
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 
00031 #include "examples/support.hh"
00032 #include "gecode/minimodel.hh"
00033 
00034 class AllInterval : public Example {
00035 private:
00036   int n;
00037   IntVarArray x;
00038   IntVarArray d;
00039 public:
00040 
00046   void adiff_sn(Space* home, IntVarArray& x) {
00047 
00048     // S_n = \{0, \dots, n - 1\}
00049     IntVarArray sn(home, n);
00050 
00051     for (int i = n; i--; ) {
00052       sn[i].init(home, i, i);
00053     }
00054 
00055     sortedness(home, x, sn);
00056   }
00057 
00063   void adiff_sn_star(Space* home, IntVarArray& x) {
00064     // S^*_n = S_n \setminus \{0\} = \{1, \dots, n - 1\}
00065     IntVarArray snstar(home, n - 1);
00066 
00067     for (int i = n - 1; i--; ) {
00068       snstar[i].init(home, i + 1, i + 1);
00069     }
00070     sortedness(home, x, snstar);
00071   }
00072 
00078   void difference(Space* home, IntVarArray& x, IntVarArray& d) {
00079 
00080     IntVarArray diff(this, n - 1);
00081     for (int i = 0; i < n - 1; i++) {
00082       diff[i].init(this, 1 - n, n - 1);
00083     }
00084 
00085     for (int i = 0; i < n - 1; i++) {
00086       post(this, x[i + 1] - x[i] == diff[i]);
00087       abs(this, diff[i], d[i]);
00088     }
00089   }
00090 
00102   /*
00103    * \brief Break negation symmetry
00104    *
00105    *  As we are intereseted in the absolute value of the difference of neighbored values in x
00106    *  the following holds:
00107    *  If the sequence \f$ \sigma = \langle s_0, \dots, s_{n - 1} \rangle \f$ is a solution to
00108    *  the problem so is the negated sequence
00109    *  \f$ \phi = \langle (n - 1 - s_0), \dots, (n - 1) - s_{n - 1}\f$. \n
00110    */
00111   void break_negation(Space* home, IntVarArray& x) {
00112     rel(home, x[0], IRT_LE, x[1]);
00113   }
00114 
00115   /*
00116    * \brief Break reveres symmetry
00117    *
00118    *  If the sequence \f$ \sigma = \langle s_0, \dots, s_{n - 1} \rangle \f$ is a solution
00119    *  so is the reverse sequence \f$\sigma^{-1} = \langle s_{n - 1}, \dots, s_0 \rangle \f$
00120    */
00121 
00122   void break_reversal(Space* home, IntVarArray& d) {
00123     rel(home, d[0], IRT_LE, d[n - 2]);
00124   }
00126 
00127   AllInterval(const Options& op) :
00128     n(op.size),
00129     x(this, n),
00130     d(this, n - 1) {
00131 
00132     IntSet dom_zn   (0, n - 1);
00133     IntSet dom_zns  (1, n - 1);
00134 
00135     // initialization of the problem variables
00136     for (int i = 0; i < n; i++) {
00137       x[i].init(this, dom_zn);
00138       if (i < n - 1)
00139         d[i].init(this, dom_zns);
00140     }
00141 
00142     difference(this, x, d);
00143 
00144     // breaks negation
00145     break_negation(this, x);
00146 
00147     // breaks reversal
00148     break_reversal(this, d);
00149 
00150 
00151     adiff_sn(this, x);
00152     adiff_sn_star(this, d);
00153 
00154     branch(this, x, BVAR_SIZE_MIN, BVAL_MIN);
00155 
00156   }
00157 
00158   AllInterval(bool share, AllInterval& a)
00159     : Example(share, a),
00160       n(a.n) {
00161     x.update(this, share, a.x);
00162     d.update(this, share, a.d);
00163   }
00164 
00165   virtual Space*
00166   copy(bool share) {
00167     return new AllInterval(share, *this);
00168   }
00169 
00170   virtual void print(void){
00171     std::cout << "Sol:\n";
00172     std::cout << "x: ";
00173     for (int i = 0; i < n; i++) {
00174       std::cout << x[i] << " ";
00175     }
00176     std::cout <<"\n";
00177 
00178     std::cout << "d: ";
00179     for (int i = 0; i < n - 1; i++) {
00180       std::cout << d[i] << " ";
00181     }
00182     std::cout <<"\n";
00183 
00184   }
00185 };
00186 
00187 
00188 int main(int argc, char** argv){
00189   Options opt("All-Interval Series ");
00190   opt.size = 12;
00191   opt.parse(argc, argv);
00192   if (opt.size < 2) {
00193     std::cerr << "n must be at least 2!" << std::endl;
00194     return -1;
00195   }
00196   Example::run<AllInterval, DFS>(opt);
00197   return 0;
00198 }
00199 
00200 // STATISTICS: example-any
00201