Generated on Sat Feb 7 2015 02:01:13 for Gecode by doxygen 1.8.9.1
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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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 
Steiner(const SizeOptions &opt)
Actual model.
Definition: steiner.cpp:69
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
SetExpr singleton(const LinIntExpr &e)
Singleton expression.
Definition: set-expr.cpp:691
Options for scripts with additional size parameter
Definition: driver.hh:567
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Post propagator for and .
Definition: channel.cpp:49
Use sequence constraints.
Definition: steiner.cpp:58
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
Use simple relation constraint.
Definition: steiner.cpp:56
SetVarBranch SET_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:91
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
SetValBranch SET_VAL_MIN_INC(void)
Include smallest element.
Definition: val.hpp:59
int n
Order of the Steiner problem.
Definition: steiner.cpp:61
int main(int argc, char *argv[])
Main-function.
Definition: steiner.cpp:155
void atmostOne(Home home, const SetVarArgs &xa, unsigned int c)
Post propagator for and .
Definition: distinct.cpp:45
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntConLevel)
Post propagator for .
Definition: sequence.cpp:51
Use matching constraints.
Definition: steiner.cpp:57
Gecode::IntArgs i(4, 1, 2, 3, 4)
Options opt
The options.
Definition: test.cpp:101
SetVarArray triples
The steiner triples.
Definition: steiner.cpp:66
unsigned int size(I &i)
Size of all ranges of range iterator i.
Steiner(bool share, Steiner &s)
Constructor for copying s.
Definition: steiner.cpp:141
Less ( )
Definition: int.hh:907
virtual void print(std::ostream &os) const
Print solution.
Definition: steiner.cpp:135
Integer sets.
Definition: int.hh:171
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
static const IntSet empty
Empty set.
Definition: int.hh:262
Passing set variables.
Definition: set.hh:490
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Set variables
Definition: set.hh:129
Integer variables.
Definition: int.hh:350
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
int noOfTriples
Number of Steiner triples.
Definition: steiner.cpp:63
Set variable array
Definition: set.hh:571
void model(int v)
Set default model value.
Definition: options.hpp:155
Gecode toplevel namespace
virtual Space * copy(bool share)
Copy during cloning.
Definition: steiner.cpp:146
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
Example: Steiner triples
Definition: steiner.cpp:52