Generated on Tue Apr 18 10:21:30 2017 for Gecode by doxygen 1.6.3

partition.cpp

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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
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 <gecode/driver.hh>
00039 #include <gecode/int.hh>
00040 #include <gecode/minimodel.hh>
00041 
00042 using namespace Gecode;
00043 
00049 class Partition : public Script {
00050 protected:
00052   IntVarArray x;
00054   IntVarArray y;
00055 public:
00057   Partition(const SizeOptions& opt)
00058     : Script(opt),
00059       x(*this,opt.size(),1,2*opt.size()),
00060       y(*this,opt.size(),1,2*opt.size()) {
00061     const int n = opt.size();
00062 
00063     // Break symmetries by ordering numbers in each group
00064     rel(*this, x, IRT_LE);
00065     rel(*this, y, IRT_LE);
00066 
00067     rel(*this, x[0], IRT_LE, y[0]);
00068 
00069     IntVarArgs xy(2*n);
00070     for (int i = n; i--; ) {
00071       xy[i] = x[i]; xy[n+i] = y[i];
00072     }
00073     distinct(*this, xy, opt.ipl());
00074 
00075     IntArgs c(2*n);
00076     for (int i = n; i--; ) {
00077       c[i] = 1; c[n+i] = -1;
00078     }
00079     linear(*this, c, xy, IRT_EQ, 0);
00080 
00081     // Array of products
00082     IntVarArgs sxy(2*n), sx(n), sy(n);
00083 
00084     for (int i = n; i--; ) {
00085       sx[i] = sxy[i] =   expr(*this, sqr(x[i]));
00086       sy[i] = sxy[n+i] = expr(*this, sqr(y[i]));
00087     }
00088     linear(*this, c, sxy, IRT_EQ, 0);
00089 
00090     // Redundant constraints
00091     linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4);
00092     linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4);
00093     linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00094     linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
00095 
00096     branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
00097   }
00098 
00100   Partition(bool share, Partition& s) : Script(share,s) {
00101     x.update(*this, share, s.x);
00102     y.update(*this, share, s.y);
00103   }
00105   virtual Space*
00106   copy(bool share) {
00107     return new Partition(share,*this);
00108   }
00110   virtual void
00111   print(std::ostream& os) const {
00112     os << "\t";
00113     int a, b;
00114     a = b = 0;
00115     for (int i = 0; i < x.size(); i++) {
00116       a += x[i].val();
00117       b += x[i].val()*x[i].val();
00118       os << x[i] << ", ";
00119     }
00120     os << " = " << a << ", " << b << std::endl << "\t";
00121     a = b = 0;
00122     for (int i = 0; i < y.size(); i++) {
00123       a += y[i].val();
00124       b += y[i].val()*y[i].val();
00125       os << y[i] << ", ";
00126     }
00127     os << " = " << a << ", " << b << std::endl;
00128   }
00129 };
00130 
00135 int
00136 main(int argc, char* argv[]) {
00137   SizeOptions opt("Partition");
00138   opt.size(32);
00139   opt.ipl(IPL_BND);
00140   opt.parse(argc,argv);
00141   Script::run<Partition,DFS,SizeOptions>(opt);
00142   return 0;
00143 }
00144 
00145 
00146 // STATISTICS: example-any
00147