Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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  * Contributing authors:
7  * Mikael Lagerkvist <lagerkvist@gecode.org>
8  *
9  * Copyright:
10  * Guido Tack, 2004
11  * Mikael Lagerkivst, 2017
12  *
13  * Last modified:
14  * $Date$ by $Author$
15  * $Revision$
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 #include <gecode/driver.hh>
43 #include <gecode/int.hh>
44 #include <gecode/set.hh>
45 
46 using namespace Gecode;
47 
49 class GolfOptions : public Options {
50 protected:
51  Driver::IntOption _w; //< Number of weeks
52  Driver::IntOption _g; //< Number of groups
53  Driver::IntOption _s; //< Number of players per group
54 public:
57  : Options("Golf"),
58  _w("w","number of weeks",9),
59  _g("g","number of groups",8),
60  _s("s","number of players per group",4) {
61  add(_w);
62  add(_g);
63  add(_s);
64  }
66  int w(void) const { return _w.value(); }
68  int g(void) const { return _g.value(); }
70  int s(void) const { return _s.value(); }
71 };
72 
84 class Golf : public Script {
85 public:
87  enum {
89  MODEL_SYMMETRY
90  };
92  enum {
96  };
97  int g;
98  int s;
99  int w;
100 
103 
106  : Script(opt),
107  g(opt.g()), s(opt.s()), w(opt.w()),
108  groups(*this,g*w,IntSet::empty,0,g*s-1,
109  static_cast<unsigned int>(s),static_cast<unsigned int>(s)) {
110  Matrix<SetVarArray> schedule(groups,g,w);
111 
112  // Groups in one week must be disjoint
113  SetVar allPlayers(*this, 0,g*s-1, 0,g*s-1);
114  for (int i=0; i<w; i++)
115  rel(*this, setdunion(schedule.row(i)) == allPlayers);
116 
117  // No two golfers play in the same group more than once
118  if (opt.propagation() == PROP_SET || opt.propagation() == PROP_MIXED) {
119  // Cardinality of intersection between two groups is at most one
120  for (int i=0; i<groups.size()-1; i++)
121  for (int j=i+1; j<groups.size(); j++)
122  rel(*this, cardinality(groups[i] & groups[j]) <= 1);
123  }
124  if (opt.propagation() == PROP_INT || opt.propagation() == PROP_MIXED) {
125  // Set up table mapping from pairs (p1,p2) (where p1<p2) of players to
126  // unique integer identifiers
127  int playerCount = g * s;
128  TupleSet ts(3);
129  int pairCount=0;
130  for (int p1=0; p1<playerCount-1; p1++)
131  for (int p2=p1+1; p2<playerCount; p2++)
132  ts.add(p1, p2, pairCount++);
133  ts.finalize();
134 
135  // Collect pairs of golfers into pairs
136  IntVarArgs pairs;
137  for (int i=0; i<groups.size()-1; i++) {
138  // Channel sorted will ensure that for i<j, group[i]<group[j],
139  // ensuring that the tuple set has a valid mapping.
140  IntVarArgs group(*this, s, 0, playerCount-1);
141  channelSorted(*this, group, groups[i]);
142  // Collect all pairs in current group
143  for (int p1=0; p1<group.size()-1; ++p1) {
144  for (int p2=p1+1; p2<group.size(); ++p2) {
145  IntVar pair(*this, 0, pairCount);
146 
147  IntVarArgs args;
148  args << group[p1] << group[p2] << pair;
149  extensional(*this, args, ts);
150 
151  pairs << pair;
152  }
153  }
154  }
155 
156  // All pairs of golfers (using the unique identifiers) must be different
157  distinct(*this, pairs, opt.ipl());
158  }
159 
160  if (opt.model() == MODEL_SYMMETRY) {
161 
162  /*
163  * Redundant constraints and static symmetry breaking from
164  * "Solving Kirkman's Schoolgirl Problem in a Few Seconds"
165  * Nicolas Barnier, Pascal Brisset, Constraints, 10, 7-21, 2005
166  *
167  */
168 
169  // Redundant constraint:
170  // in each week, one player plays in only one group
171  for (int j=0; j<w; j++) {
172  for (int p=0; p < g*s; p++) {
173  BoolVarArgs b(g);
174  for (int i=0; i<g; i++)
175  b[i] = expr(*this, (singleton(p) <= schedule(i,j)));
176  linear(*this, b, IRT_EQ, 1);
177  }
178  }
179 
180  // Symmetry breaking: order groups
181  for (int j=0; j<w; j++) {
182  IntVarArgs m(g);
183  for (int i=0; i<g; i++)
184  m[i] = expr(*this, min(schedule(i,j)));
185  rel(*this, m, IRT_LE);
186  }
187 
188  // Symmetry breaking: order weeks
189  // minElem(group(w,0)\{0}) < minElem(group(w+1,0)\{0})
190  {
191  IntVarArgs m(w);
192  for (int i=0; i<w; i++)
193  m[i] = expr(*this, min(schedule(0,i)-IntSet(0,0)));
194  rel(*this, m, IRT_LE);
195  }
196 
197  // Symmetry breaking: value symmetry of player numbers
198  precede(*this, groups, IntArgs::create(groups.size(),0));
199  }
200 
201  branch(*this, groups, SET_VAR_MIN_MIN(), SET_VAL_MIN_INC());
202  }
203 
205  virtual void
206  print(std::ostream& os) const {
207  os << "Tournament plan" << std::endl;
208  Matrix<SetVarArray> schedule(groups,g,w);
209  for (int j=0; j<w; j++) {
210  os << "Week " << j << ": " << std::endl << " ";
211  os << schedule.row(j) << std::endl;
212  }
213  }
214 
216  Golf(Golf& s) : Script(s), g(s.g), s(s.s), w(s.w) {
217  groups.update(*this, s.groups);
218  }
220  virtual Space*
221  copy(void) {
222  return new Golf(*this);
223  }
224 };
225 
229 int
230 main(int argc, char* argv[]) {
233  opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
234  opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
236  opt.propagation(Golf::PROP_SET, "set", "Use set intersection cardinality for pair play constraints");
237  opt.propagation(Golf::PROP_INT, "int", "Use integer distinct for pair play constraints");
238  opt.propagation(Golf::PROP_MIXED, "mixed", "Use set interesection cardinality and integer distinct for pair play constraints");
239  opt.ipl(IPL_DOM);
240  opt.solutions(1);
241  opt.parse(argc,argv);
242  Script::run<Golf,DFS,GolfOptions>(opt);
243  return 0;
244 }
245 
246 // STATISTICS: example-any
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Definition: channel.cpp:49
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition: array.hpp:72
SetVarArray groups
The sets representing the groups.
Definition: golf.cpp:102
SetExpr singleton(const LinIntExpr &e)
Singleton expression.
Definition: set-expr.cpp:694
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:159
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1657
void 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
int w(void) const
Return number of weeks.
Definition: golf.cpp:66
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:973
Options for Golf example
Definition: golf.cpp:49
void propagation(int v)
Set default propagation value.
Definition: options.hpp:207
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1060
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:45
int s
Number of players in a group.
Definition: golf.cpp:98
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:220
int s(void) const
Return number of players per group.
Definition: golf.cpp:70
Computation spaces.
Definition: core.hpp:1668
Parametric base-class for scripts.
Definition: driver.hh:733
A simple model.
Definition: golf.cpp:88
Golf(const GolfOptions &opt)
Actual model.
Definition: golf.cpp:105
SetExpr setdunion(const SetVarArgs &x)
Disjoint union of set variables.
Definition: set-expr.cpp:717
int g(void) const
Return number of groups.
Definition: golf.cpp:68
void value(int v)
Set default value to v.
Definition: options.hpp:78
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Gecode::IntArgs i(4, 1, 2, 3, 4)
Equality ( )
Definition: int.hh:905
virtual Space * copy(void)
Copy during cloning.
Definition: golf.cpp:221
Options opt
The options.
Definition: test.cpp:101
Model with symmetry breaking.
Definition: golf.cpp:89
Propagation of pair play amount using both set and int variables.
Definition: golf.cpp:95
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntPropLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition: extensional.cpp:47
GolfOptions(void)
Constructor.
Definition: golf.cpp:56
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:50
Golf(Golf &s)
Constructor for copying s.
Definition: golf.cpp:216
Less ( )
Definition: int.hh:908
Integer sets.
Definition: int.hh:174
Passing integer variables.
Definition: int.hh:637
int w
Number of weeks.
Definition: golf.cpp:99
Passing Boolean variables.
Definition: int.hh:691
SetValBranch SET_VAL_MIN_INC(void)
Definition: val.hpp:59
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:818
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:544
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:71
Class represeting a set of tuples.
Definition: int.hh:2144
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
Set variables
Definition: set.hh:131
virtual void print(std::ostream &os) const
Print solution.
Definition: golf.cpp:206
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
void precede(Home home, const IntVarArgs &x, int s, int t, IntPropLevel)
Post propagator that s precedes t in x.
Definition: precede.cpp:47
Integer variables.
Definition: int.hh:351
int g
Number of groups in a week.
Definition: golf.cpp:97
Driver::IntOption _s
Definition: golf.cpp:53
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:958
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:287
Matrix-interface for arrays.
Definition: minimodel.hh:2052
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition: tuple-set.hpp:146
Set variable array
Definition: set.hh:572
int main(int argc, char *argv[])
Main-function.
Definition: golf.cpp:230
Driver::IntOption _g
Definition: golf.cpp:52
void model(int v)
Set default model value.
Definition: options.hpp:181
Gecode toplevel namespace
Driver::IntOption _w
Definition: golf.cpp:51
Example: Golf tournament
Definition: golf.cpp:84
SetVarBranch SET_VAR_MIN_MIN(BranchTbl tbl)
Definition: var.hpp:190
Propagation of pair play amount using int variables and distinct.
Definition: golf.cpp:94
Options for scripts
Definition: driver.hh:370
Integer option.
Definition: driver.hh:213
Propagation of pair play amount using set variables.
Definition: golf.cpp:93