Generated on Tue Oct 22 2013 00:48:58 for Gecode by doxygen 1.8.4
golf.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 
45 class GolfOptions : public Options {
46 protected:
47  Driver::IntOption _w; //< Number of weeks
48  Driver::IntOption _g; //< Number of groups
49  Driver::IntOption _s; //< Number of players per group
50 public:
53  : Options("Golf"),
54  _w("-w","number of weeks",9),
55  _g("-g","number of groups",8),
56  _s("-s","number of players per group",4) {
57  add(_w);
58  add(_g);
59  add(_s);
60  }
62  int w(void) const { return _w.value(); }
64  int g(void) const { return _g.value(); }
66  int s(void) const { return _s.value(); }
67 };
68 
80 class Golf : public Script {
81 public:
83  enum {
85  MODEL_SYMMETRY
86  };
87  int g;
88  int s;
89  int w;
90 
93 
95  Golf(const GolfOptions& opt) : g(opt.g()), s(opt.s()), w(opt.w()),
96  groups(*this,g*w,IntSet::empty,0,g*s-1,s,s) {
97  Matrix<SetVarArray> schedule(groups,g,w);
98 
99  // Groups in one week must be disjoint
100  SetVar allPlayers(*this, 0,g*s-1, 0,g*s-1);
101  for (int i=0; i<w; i++)
102  rel(*this, setdunion(schedule.row(i)) == allPlayers);
103 
104  // No two golfers play in the same group more than once
105  for (int i=0; i<groups.size()-1; i++)
106  for (int j=i+1; j<groups.size(); j++)
107  rel(*this, cardinality(groups[i] & groups[j]) <= 1);
108 
109  if (opt.model() == MODEL_SYMMETRY) {
110 
111  /*
112  * Redundant constraints and static symmetry breaking from
113  * "Solving Kirkman's Schoolgirl Problem in a Few Seconds"
114  * Nicolas Barnier, Pascal Brisset, Constraints, 10, 7-21, 2005
115  *
116  */
117 
118  // Redundant constraint:
119  // in each week, one player plays in only one group
120  for (int j=0; j<w; j++) {
121  for (int p=0; p < g*s; p++) {
122  BoolVarArgs b(g);
123  for (int i=0; i<g; i++)
124  b[i] = expr(*this, (singleton(p) <= schedule(i,j)));
125  linear(*this, b, IRT_EQ, 1);
126  }
127  }
128 
129  // Symmetry breaking: order groups
130  for (int j=0; j<w; j++) {
131  IntVarArgs m(g);
132  for (int i=0; i<g; i++)
133  m[i] = expr(*this, min(schedule(i,j)));
134  rel(*this, m, IRT_LE);
135  }
136 
137  // Symmetry breaking: order weeks
138  // minElem(group(w,0)\{0}) < minElem(group(w+1,0)\{0})
139  {
140  IntVarArgs m(w);
141  for (int i=0; i<w; i++)
142  m[i] = expr(*this, min(schedule(0,i)-IntSet(0,0)));
143  rel(*this, m, IRT_LE);
144  }
145 
146  // Symmetry breaking: value symmetry of player numbers
147  precede(*this, groups, IntArgs::create(groups.size(),0));
148  }
149 
150  branch(*this, groups, SET_VAR_MIN_MIN(), SET_VAL_MIN_INC());
151  }
152 
154  virtual void
155  print(std::ostream& os) const {
156  os << "Tournament plan" << std::endl;
157  Matrix<SetVarArray> schedule(groups,g,w);
158  for (int j=0; j<w; j++) {
159  os << "Week " << j << ": " << std::endl << " ";
160  os << schedule.row(j) << std::endl;
161  }
162  }
163 
165  Golf(bool share, Golf& s) : Script(share,s), g(s.g), s(s.s), w(s.w) {
166  groups.update(*this, share, s.groups);
167  }
169  virtual Space*
170  copy(bool share) {
171  return new Golf(share,*this);
172  }
173 };
174 
178 int
179 main(int argc, char* argv[]) {
182  opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
183  opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
184  opt.solutions(1);
185  opt.parse(argc,argv);
186  Script::run<Golf,DFS,GolfOptions>(opt);
187  return 0;
188 }
189 
190 // STATISTICS: example-any