Generated on Mon Aug 25 11:35:33 2008 for Gecode by doxygen 1.5.6

partition.cc

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, 2003
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-07-11 10:10:41 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7311 $
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 "examples/support.hh"
00039 #include "gecode/minimodel.hh"
00040 
00046 class Partition : public Example {
00047 protected:
00049   IntVarArray x;
00051   IntVarArray y;
00052 public:
00054   Partition(const SizeOptions& opt)
00055     : x(this,opt.size(),1,2*opt.size()),
00056       y(this,opt.size(),1,2*opt.size()) {
00057     const int n = opt.size();
00058     // Break symmetries by ordering numbers in each group
00059     rel(this, x, IRT_LE);
00060     rel(this, y, IRT_LE);
00061 
00062     rel(this, x[0], IRT_LE, y[0]);
00063 
00064     IntVarArgs xy(2*n);
00065     for (int i = n; i--; ) {
00066       xy[i] = x[i]; xy[n+i] = y[i];
00067     }
00068     distinct(this, xy, opt.icl());
00069 
00070     IntArgs c(2*n);
00071     for (int i = n; i--; ) {
00072       c[i] = 1; c[n+i] = -1;
00073     }
00074     linear(this, c, xy, IRT_EQ, 0);
00075 
00076     // Array of products
00077     IntVarArgs sxy(2*n), sx(n), sy(n);
00078 
00079     for (int i = n; i--; ) {
00080       sx[i] = sxy[i] =   sqr(this, x[i]);
00081       sy[i] = sxy[n+i] = sqr(this, y[i]); 
00082     }
00083     linear(this, c, sxy, IRT_EQ, 0);
00084 
00085     // Redundant
00086     linear(this, x, IRT_EQ, 2*n*(2*n+1)/4);
00087     linear(this, y, IRT_EQ, 2*n*(2*n+1)/4);
00088     linear(this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00089     linear(this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00090     branch(this, xy, INT_VAR_SIZE_MIN, INT_VAL_MIN);
00091   }
00092 
00094   Partition(bool share, Partition& s) : Example(share,s) {
00095     x.update(this, share, s.x);
00096     y.update(this, share, s.y);
00097   }
00099   virtual Space*
00100   copy(bool share) {
00101     return new Partition(share,*this);
00102   }
00104   virtual void
00105   print(std::ostream& os) {
00106     os << "\t";
00107     int a, b;
00108     a = b = 0;
00109     for (int i = 0; i < x.size(); i++) {
00110       a += x[i].val();
00111       b += x[i].val()*x[i].val();
00112       os << x[i] << ", ";
00113     }
00114     os << " = " << a << ", " << b << std::endl << "\t";
00115     a = b = 0;
00116     for (int i = 0; i < y.size(); i++) {
00117       a += y[i].val();
00118       b += y[i].val()*y[i].val();
00119       os << y[i] << ", ";
00120     }
00121     os << " = " << a << ", " << b << std::endl;
00122   }
00123 };
00124 
00129 int
00130 main(int argc, char* argv[]) {
00131   SizeOptions opt("Partition");
00132   opt.size(32);
00133   opt.icl(ICL_BND);
00134   opt.parse(argc,argv);
00135   Example::run<Partition,DFS,SizeOptions>(opt);
00136   return 0;
00137 }
00138 
00139 
00140 // STATISTICS: example-any
00141