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

steiner.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-03-05 16:11:06 +0100 (Wed, 05 Mar 2008) $ by $Author: tack $
00011  *     $Revision: 6428 $
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/set.hh"
00039 #include "examples/support.hh"
00040 
00049 class Steiner : public Example {
00050 public:
00051 
00053   enum {
00054     MODEL_NONE,   
00055     MODEL_MATCHING, 
00056     MODEL_SEQ 
00057   };
00058 
00060   int n;
00062   int noOfTriples;
00063 
00065   SetVarArray triples;
00066 
00068   Steiner(const SizeOptions& opt)
00069     : n(opt.size()), noOfTriples((n*(n-1))/6),
00070       triples(this, noOfTriples, IntSet::empty, 1, n, 3, 3) {
00071 
00072     for (int i=0; i<noOfTriples; i++) {
00073       for (int j=i+1; j<noOfTriples; j++) {
00074         SetVar x = triples[i];
00075         SetVar y = triples[j];
00076 
00077         SetVar atmostOne(this,IntSet::empty,1,n,0,1);
00078         cardinality(this, atmostOne,0,1);
00079         rel(this, x, SOT_INTER, y, SRT_EQ, atmostOne);
00080 
00081         IntVar x1(this,1,n);
00082         IntVar x2(this,1,n);
00083         IntVar x3(this,1,n);
00084         IntVar y1(this,1,n);
00085         IntVar y2(this,1,n);
00086         IntVar y3(this,1,n);
00087 
00088         if (opt.model() == MODEL_NONE) {
00089           /* Naive alternative:
00090            * just including the ints in the set
00091            */
00092           rel(this, x, SRT_SUP, x1);
00093           rel(this, x, SRT_SUP, x2);
00094           rel(this, x, SRT_SUP, x3);
00095           rel(this, y, SRT_SUP, y1);
00096           rel(this, y, SRT_SUP, y2);
00097           rel(this, y, SRT_SUP, y3);
00098                     
00099         } else  if (opt.model() == MODEL_MATCHING) {
00100           /* Smart alternative:
00101            * Using matching constraints
00102            */
00103         
00104           IntVarArgs xargs(3);
00105           xargs[0] = x1; xargs[1] = x2; xargs[2] = x3;
00106           match(this, x,xargs);
00107         
00108           IntVarArgs yargs(3);
00109           yargs[0] = y1; yargs[1] = y2; yargs[2] = y3;
00110           match(this, y,yargs);
00111         } else if (opt.model() == MODEL_SEQ) {
00112           SetVar tmp20(this,IntSet::empty,1,n);
00113           SetVar tmp21(this,IntSet::empty,1,n);
00114           SetVar tmp22(this,IntSet::empty,1,n);
00115           SetVar tmp23(this,IntSet::empty,1,n);
00116           SetVar tmp24(this,IntSet::empty,1,n);
00117           SetVar tmp25(this,IntSet::empty,1,n);
00118           rel(this, tmp20, SRT_EQ, x1);
00119           rel(this, tmp21, SRT_EQ, x2);
00120           rel(this, tmp22, SRT_EQ, x3);
00121           rel(this, tmp23, SRT_EQ, y1);
00122           rel(this, tmp24, SRT_EQ, y2);
00123           rel(this, tmp25, SRT_EQ, y3);
00124           SetVarArgs xargs(3);
00125           xargs[0] = tmp20; xargs[1] = tmp21; xargs[2] = tmp22;
00126           SetVarArgs yargs(3);
00127           yargs[0] = tmp23; yargs[1] = tmp24; yargs[2] = tmp25;
00128           sequentialUnion(this,xargs,x);
00129           sequentialUnion(this,yargs,y);          
00130         }
00131         
00132         /* Breaking symmetries */
00133         
00134         rel(this, x1,IRT_LE,x2);
00135         rel(this, x2,IRT_LE,x3);
00136         rel(this, x1,IRT_LE,x3);
00137         
00138         rel(this, y1,IRT_LE,y2);
00139         rel(this, y2,IRT_LE,y3);
00140         rel(this, y1,IRT_LE,y3);
00141         
00142         IntArgs ia(6,(n+1)*(n+1),n+1,1,-(n+1)*(n+1),-(n+1),-1);
00143         IntVarArgs iva(6);
00144         iva[0]=x1; iva[1]=x2; iva[2]=x3;
00145         iva[3]=y1; iva[4]=y2; iva[5]=y3;
00146         linear(this, ia, iva, IRT_LE, 0);
00147       }
00148     }
00149 
00150     branch(this, triples, SET_VAR_NONE, SET_VAL_MIN);
00151   }
00152 
00154   virtual void
00155   print(std::ostream& os) {
00156     for (int i=0; i<noOfTriples; i++) {
00157       os << "\t[" << i << "] = " << triples[i] << std::endl;
00158     }
00159   }
00160 
00162   Steiner(bool share, Steiner& s) : Example(share,s), n(s.n), noOfTriples(s.noOfTriples) {
00163     triples.update(this, share, s.triples);
00164   }
00166   virtual Space*
00167   copy(bool share) {
00168     return new Steiner(share,*this);
00169   }
00170 
00171 };
00172 
00176 int
00177 main(int argc, char* argv[]) {
00178   SizeOptions opt("Steiner");
00179   opt.model(Steiner::MODEL_NONE);
00180   opt.model(Steiner::MODEL_NONE, "rel", "Use simple relation constraints");
00181   opt.model(Steiner::MODEL_MATCHING, "matching", "Use matching constraints");
00182   opt.model(Steiner::MODEL_SEQ, "sequence", "Use sequence constraints");
00183   opt.size(9);
00184   opt.iterations(20);
00185   opt.parse(argc,argv);
00186   Example::run<Steiner,DFS,SizeOptions>(opt);
00187   return 0;
00188 }
00189 
00190 
00191 // STATISTICS: example-any
00192