00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00090
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
00101
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
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
00192