Generated on Mon Aug 27 2012 17:15:35 for Gecode by doxygen 1.8.1.2
steiner.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Copyright:
7  * Guido Tack, 2004
8  *
9  * Last modified:
10  * $Date: 2011-02-22 20:26:48 +1100 (Tue, 22 Feb 2011) $ by $Author: tack $
11  * $Revision: 11761 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/set.hh>
41 
42 using namespace Gecode;
43 
52 class Steiner : public Script {
53 public:
55  enum {
58  MODEL_SEQ
59  };
61  int n;
64 
67 
70  : n(opt.size()), noOfTriples((n*(n-1))/6),
71  triples(*this, noOfTriples, IntSet::empty, 1, n, 3, 3) {
72 
73  for (int i=0; i<noOfTriples; i++) {
74  for (int j=i+1; j<noOfTriples; j++) {
75  SetVar x = triples[i];
76  SetVar y = triples[j];
77 
78  SetVar atmostOne(*this,IntSet::empty,1,n,0,1);
79  rel(*this, (x & y) == atmostOne);
80 
81  IntVar x1(*this,1,n);
82  IntVar x2(*this,1,n);
83  IntVar x3(*this,1,n);
84  IntVar y1(*this,1,n);
85  IntVar y2(*this,1,n);
86  IntVar y3(*this,1,n);
87 
88  if (opt.model() == MODEL_NONE) {
89  /* Naive alternative:
90  * just including the ints in the set
91  */
92  rel(*this, singleton(x1) <= x);
93  rel(*this, singleton(x2) <= x);
94  rel(*this, singleton(x3) <= x);
95  rel(*this, singleton(y1) <= y);
96  rel(*this, singleton(y2) <= y);
97  rel(*this, singleton(y3) <= y);
98 
99  } else if (opt.model() == MODEL_MATCHING) {
100  /* Smart alternative:
101  * Using matching constraints
102  */
103 
104  channelSorted(*this, IntVarArgs()<<x1<<x2<<x3, x);
105  channelSorted(*this, IntVarArgs()<<y1<<y2<<y3, y);
106  } else if (opt.model() == MODEL_SEQ) {
107  SetVar sx1 = expr(*this, singleton(x1));
108  SetVar sx2 = expr(*this, singleton(x2));
109  SetVar sx3 = expr(*this, singleton(x3));
110  SetVar sy1 = expr(*this, singleton(y1));
111  SetVar sy2 = expr(*this, singleton(y2));
112  SetVar sy3 = expr(*this, singleton(y3));
113  sequence(*this,SetVarArgs()<<sx1<<sx2<<sx3,x);
114  sequence(*this,SetVarArgs()<<sy1<<sy2<<sy3,y);
115  }
116 
117  /* Breaking symmetries */
118  rel(*this, x1 < x2);
119  rel(*this, x2 < x3);
120  rel(*this, x1 < x3);
121 
122  rel(*this, y1 < y2);
123  rel(*this, y2 < y3);
124  rel(*this, y1 < y3);
125 
126  linear(*this, IntArgs(6,(n+1)*(n+1),n+1,1,-(n+1)*(n+1),-(n+1),-1),
127  IntVarArgs()<<x1<<x2<<x3<<y1<<y2<<y3, IRT_LE, 0);
128  }
129  }
130 
131  branch(*this, triples, SET_VAR_NONE, SET_VAL_MIN_INC);
132  }
134  virtual void
135  print(std::ostream& os) const {
136  for (int i=0; i<noOfTriples; i++) {
137  os << "\t[" << i << "] = " << triples[i] << std::endl;
138  }
139  }
141  Steiner(bool share, Steiner& s) : Script(share,s), n(s.n), noOfTriples(s.noOfTriples) {
142  triples.update(*this, share, s.triples);
143  }
145  virtual Space*
146  copy(bool share) {
147  return new Steiner(share,*this);
148  }
149 };
150 
154 int
155 main(int argc, char* argv[]) {
156  SizeOptions opt("Steiner");
158  opt.model(Steiner::MODEL_NONE, "rel", "Use simple relation constraints");
159  opt.model(Steiner::MODEL_MATCHING, "matching", "Use matching constraints");
160  opt.model(Steiner::MODEL_SEQ, "sequence", "Use sequence constraints");
161  opt.size(9);
162  opt.iterations(20);
163  opt.parse(argc,argv);
164  Script::run<Steiner,DFS,SizeOptions>(opt);
165  return 0;
166 }
167 
168 
169 // STATISTICS: example-any
170