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

partition.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2003
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 
00022 #include "examples/support.hh"
00023 
00029 class Partition : public Example {
00030 protected:
00032   IntVarArray x;
00034   IntVarArray y;
00035 public:
00037   Partition(const Options& opt)
00038     : x(this,opt.size,1,2*opt.size),
00039       y(this,opt.size,1,2*opt.size) {
00040     const int n = opt.size;
00041     // Break symmetries by ordering numbers in each group
00042     for (int i = n-1; i--; ) {
00043       rel(this, x[i], IRT_LE, x[i+1]);
00044       rel(this, y[i], IRT_LE, y[i+1]);
00045     }
00046     rel(this, x[0], IRT_LE, y[0]);
00047 
00048     IntVarArgs xy(2*n);
00049     for (int i = n; i--; ) {
00050       xy[i] = x[i]; xy[n+i] = y[i];
00051     }
00052     distinct(this, xy, opt.icl);
00053     IntArgs c(2*n);
00054     for (int i = n; i--; ) {
00055       c[i] = 1; c[n+i] = -1;
00056     }
00057     linear(this, c, xy, IRT_EQ,0);
00058 
00059     // Array of products
00060     IntVarArray sxy(this,2*n,1,4*n*n);
00061     IntVarArgs sx(n);
00062     IntVarArgs sy(n);
00063 
00064     for (int i = n; i--; ) {
00065       mult(this,x[i],x[i],sxy[i]);   sx[i] = sxy[i];
00066       mult(this,y[i],y[i],sxy[n+i]); sy[i] = sxy[n+i];
00067     }
00068     linear(this, c,sxy,IRT_EQ,0);
00069 
00070     // Redundant
00071     linear(this, x, IRT_EQ, 2*n*(2*n+1)/4);
00072     linear(this, y, IRT_EQ, 2*n*(2*n+1)/4);
00073     linear(this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00074     linear(this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00075     branch(this, xy, BVAR_SIZE_MIN, BVAL_MIN);
00076   }
00077 
00079   Partition(bool share, Partition& s) : Example(share,s) {
00080     x.update(this, share, s.x);
00081     y.update(this, share, s.y);
00082   }
00084   virtual Space*
00085   copy(bool share) {
00086     return new Partition(share,*this);
00087   }
00089   virtual void
00090   print(void) {
00091     std::cout << "\t";
00092     int a, b;
00093     a = b = 0;
00094     for (int i = 0; i < x.size(); i++) {
00095       a += x[i].val();
00096       b += x[i].val()*x[i].val();
00097       std::cout << x[i] << ", ";
00098     }
00099     std::cout << " = " << a << ", " << b << std::endl << "\t";
00100     a = b = 0;
00101     for (int i = 0; i < y.size(); i++) {
00102       a += y[i].val();
00103       b += y[i].val()*y[i].val();
00104       std::cout << y[i] << ", ";
00105     }
00106     std::cout << " = " << a << ", " << b << std::endl;
00107   }
00108 };
00109 
00114 int
00115 main(int argc, char** argv) {
00116   Options o("Partition");
00117   o.size = 32;
00118   o.icl  = ICL_DOM;
00119   o.parse(argc,argv);
00120   Example::run<Partition,DFS>(o);
00121   return 0;
00122 }
00123 
00124 
00125 // STATISTICS: example-any
00126