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

steiner.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Guido Tack <tack@gecode.org>
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2004
00008  *     Christian Schulte, 2004
00009  *
00010  *  Last modified:
00011  *     $Date: 2006-08-04 16:06:52 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00012  *     $Revision: 3517 $
00013  *
00014  *  This file is part of Gecode, the generic constraint
00015  *  development environment:
00016  *     http://www.gecode.org
00017  *
00018  *  See the file "LICENSE" for information on usage and
00019  *  redistribution of this file, and for a
00020  *     DISCLAIMER OF ALL WARRANTIES.
00021  *
00022  */
00023 
00024 #include "gecode/set.hh"
00025 #include "examples/support.hh"
00026 
00035 class Steiner : public Example {
00036 public:
00037 
00038   int n;
00039   int n1;
00040   int n1n1;
00041   int len;
00042 
00043   SetVarArray root;
00044 
00045   Steiner(const Options& o)
00046     : n(o.size),
00047       n1(n+1), n1n1(n1*n1), len((n*(n-1))/6),
00048       root(this,len, IntSet::empty, 1, n, 3, 3) {
00049 
00050     for (int i=0; i<len; i++) {
00051       for (int j=i+1; j<len; j++) {
00052         SetVar x = root[i];
00053         SetVar y = root[j];
00054 
00055         SetVar atmostOne(this,IntSet::empty,1,n,0,1);
00056         cardinality(this, atmostOne,0,1);
00057         rel(this, x, SOT_INTER, y, SRT_EQ, atmostOne);
00058 
00059         IntVar x1(this,1,n);
00060         IntVar x2(this,1,n);
00061         IntVar x3(this,1,n);
00062         IntVar y1(this,1,n);
00063         IntVar y2(this,1,n);
00064         IntVar y3(this,1,n);
00065 
00066         if (o.naive) {
00067         
00068           /* Naive alternative:
00069            * just including the ints in the set
00070            */
00071           rel(this, x, SRT_SUP, x1);
00072           rel(this, x, SRT_SUP, x2);
00073           rel(this, x, SRT_SUP, x3);
00074           rel(this, y, SRT_SUP, y1);
00075           rel(this, y, SRT_SUP, y2);
00076           rel(this, y, SRT_SUP, y3);
00077         } else {
00078 
00079           /* Smart alternative:
00080            * Using matching constraints
00081            */
00082         
00083           IntVarArgs xargs(3);
00084           xargs[0] = x1; xargs[1] = x2; xargs[2] = x3;
00085           match(this, x,xargs);
00086         
00087           IntVarArgs yargs(3);
00088           yargs[0] = y1; yargs[1] = y2; yargs[2] = y3;
00089           match(this, y,yargs);
00090         
00091         }
00092         
00093         /* Breaking symmetries */
00094         
00095         rel(this, x1,IRT_LE,x2);
00096         rel(this, x2,IRT_LE,x3);
00097         rel(this, x1,IRT_LE,x3);
00098         
00099         rel(this, y1,IRT_LE,y2);
00100         rel(this, y2,IRT_LE,y3);
00101         rel(this, y1,IRT_LE,y3);
00102         
00103         IntArgs ia(6,n1n1,n1,1,-n1n1,-n1,-1);
00104         IntVarArgs iva(6);
00105         iva[0]=x1; iva[1]=x2; iva[2]=x3;
00106         iva[3]=y1; iva[4]=y2; iva[5]=y3;
00107         linear(this, ia, iva, IRT_LE, 0);
00108       }
00109     }
00110 
00111 
00112     branch(this, root, SETBVAR_NONE, SETBVAL_MIN);
00113   }
00114 
00115   Steiner(bool share, Steiner& s) : Example(share,s),
00116                                     n(s.n), n1(s.n1), n1n1(s.n1n1), len(s.len)
00117   {
00118     root.update(this, share, s.root);
00119   }
00120 
00121   virtual Space*
00122   copy(bool share) {
00123     return new Steiner(share,*this);
00124   }
00125 
00126   virtual void
00127   print(void) {
00128     for (int i=0; i<len; i++) {
00129       std::cout << "\t[" << i << "]" << root[i] << std::endl;
00130     }
00131   }
00132 
00133 };
00134 
00135 int
00136 main(int argc, char** argv) {
00137   Options o("Steiner");
00138   o.size = 9;
00139   o.naive = true;
00140   o.iterations = 20;
00141   o.parse(argc,argv);
00142   Example::run<Steiner,DFS>(o);
00143   return 0;
00144 }
00145 
00146 
00147 // STATISTICS: example-any
00148