00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
00069
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
00080
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
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
00148