Generated on Wed Jan 1 2020 10:37:59 for Gecode by doxygen 1.8.16
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  * 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  };
88  enum {
92  };
93  int g;
94  int s;
95  int w;
96 
99 
102  : Script(opt),
103  g(opt.g()), s(opt.s()), w(opt.w()),
104  groups(*this,g*w,IntSet::empty,0,g*s-1,
105  static_cast<unsigned int>(s),static_cast<unsigned int>(s)) {
106  Matrix<SetVarArray> schedule(groups,g,w);
107 
108  // Groups in one week must be disjoint
109  SetVar allPlayers(*this, 0,g*s-1, 0,g*s-1);
110  for (int i=0; i<w; i++)
111  rel(*this, setdunion(schedule.row(i)) == allPlayers);
112 
113  // No two golfers play in the same group more than once
114  if (opt.propagation() == PROP_SET || opt.propagation() == PROP_MIXED) {
115  // Cardinality of intersection between two groups is at most one
116  for (int i=0; i<groups.size()-1; i++)
117  for (int j=i+1; j<groups.size(); j++)
118  rel(*this, cardinality(groups[i] & groups[j]) <= 1);
119  }
120  if (opt.propagation() == PROP_INT || opt.propagation() == PROP_MIXED) {
121  // Set up table mapping from pairs (p1,p2) (where p1<p2) of players to
122  // unique integer identifiers
123  int playerCount = g * s;
124  TupleSet ts(3);
125  int pairCount=0;
126  for (int p1=0; p1<playerCount-1; p1++)
127  for (int p2=p1+1; p2<playerCount; p2++)
128  ts.add({p1, p2, pairCount++});
129  ts.finalize();
130 
131  // Collect pairs of golfers into pairs
132  IntVarArgs pairs;
133  for (int i=0; i<groups.size()-1; i++) {
134  // Channel sorted will ensure that for i<j, group[i]<group[j],
135  // ensuring that the tuple set has a valid mapping.
136  IntVarArgs group(*this, s, 0, playerCount-1);
137  channelSorted(*this, group, groups[i]);
138  // Collect all pairs in current group
139  for (int p1=0; p1<group.size()-1; ++p1) {
140  for (int p2=p1+1; p2<group.size(); ++p2) {
141  IntVar pair(*this, 0, pairCount);
142 
143  IntVarArgs args;
144  args << group[p1] << group[p2] << pair;
145  extensional(*this, args, ts);
146 
147  pairs << pair;
148  }
149  }
150  }
151 
152  // All pairs of golfers (using the unique identifiers) must be different
153  distinct(*this, pairs, opt.ipl());
154  }
155 
156  if (opt.model() == MODEL_SYMMETRY) {
157 
158  /*
159  * Redundant constraints and static symmetry breaking from
160  * "Solving Kirkman's Schoolgirl Problem in a Few Seconds"
161  * Nicolas Barnier, Pascal Brisset, Constraints, 10, 7-21, 2005
162  *
163  */
164 
165  // Redundant constraint:
166  // in each week, one player plays in only one group
167  for (int j=0; j<w; j++) {
168  for (int p=0; p < g*s; p++) {
169  BoolVarArgs b(g);
170  for (int i=0; i<g; i++)
171  b[i] = expr(*this, (singleton(p) <= schedule(i,j)));
172  linear(*this, b, IRT_EQ, 1);
173  }
174  }
175 
176  // Symmetry breaking: order groups
177  for (int j=0; j<w; j++) {
178  IntVarArgs m(g);
179  for (int i=0; i<g; i++)
180  m[i] = expr(*this, min(schedule(i,j)));
181  rel(*this, m, IRT_LE);
182  }
183 
184  // Symmetry breaking: order weeks
185  // minElem(group(w,0)\{0}) < minElem(group(w+1,0)\{0})
186  {
187  IntVarArgs m(w);
188  for (int i=0; i<w; i++)
189  m[i] = expr(*this, min(schedule(0,i)-IntSet(0,0)));
190  rel(*this, m, IRT_LE);
191  }
192 
193  // Symmetry breaking: value symmetry of player numbers
194  precede(*this, groups, IntArgs::create(groups.size(),0));
195  }
196 
197  branch(*this, groups, SET_VAR_MIN_MIN(), SET_VAL_MIN_INC());
198  }
199 
201  virtual void
202  print(std::ostream& os) const {
203  os << "Tournament plan" << std::endl;
204  Matrix<SetVarArray> schedule(groups,g,w);
205  for (int j=0; j<w; j++) {
206  os << "Week " << j << ": " << std::endl << " ";
207  os << schedule.row(j) << std::endl;
208  }
209  }
210 
212  Golf(Golf& s) : Script(s), g(s.g), s(s.s), w(s.w) {
213  groups.update(*this, s.groups);
214  }
216  virtual Space*
217  copy(void) {
218  return new Golf(*this);
219  }
220 };
221 
225 int
226 main(int argc, char* argv[]) {
229  opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
230  opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
232  opt.propagation(Golf::PROP_SET, "set", "Use set intersection cardinality for pair play constraints");
233  opt.propagation(Golf::PROP_INT, "int", "Use integer distinct for pair play constraints");
234  opt.propagation(Golf::PROP_MIXED, "mixed", "Use set interesection cardinality and integer distinct for pair play constraints");
235  opt.ipl(IPL_DOM);
236  opt.solutions(1);
237  opt.parse(argc,argv);
238  Script::run<Golf,DFS,GolfOptions>(opt);
239  return 0;
240 }
241 
242 // STATISTICS: example-any
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition: tuple-set.hpp:142
void propagation(int v)
Set default propagation value.
Definition: options.hpp:203
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition: array.hpp:76
Propagation of pair play amount using set variables.
Definition: golf.cpp:89
Passing integer variables.
Definition: int.hh:656
Less ( )
Definition: int.hh:929
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:177
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Definition: channel.cpp:45
Propagation of pair play amount using int variables and distinct.
Definition: golf.cpp:90
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
void value(int v)
Set default value to v.
Definition: options.hpp:74
SetVarBranch SET_VAR_MIN_MIN(BranchTbl tbl)
Definition: var.hpp:186
Computation spaces.
Definition: core.hpp:1742
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:39
int w(void) const
Return number of weeks.
Definition: golf.cpp:62
Example: Golf tournament
Definition: golf.cpp:80
Model with symmetry breaking.
Definition: golf.cpp:85
SetExpr singleton(const LinIntExpr &e)
Singleton expression.
Definition: set-expr.cpp:691
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:926
Golf(Golf &s)
Constructor for copying s.
Definition: golf.cpp:212
int g
Number of groups in a week.
Definition: golf.cpp:93
int main(int argc, char *argv[])
Main-function.
Definition: golf.cpp:226
Gecode toplevel namespace
Integer sets.
Definition: int.hh:174
Options for Golf example
Definition: golf.cpp:45
Options opt
The options.
Definition: test.cpp:97
int s(void) const
Return number of players per group.
Definition: golf.cpp:66
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:155
BoolVar expr(Home home, const BoolExpr &e, const IntPropLevels &ipls)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:629
Passing Boolean variables.
Definition: int.hh:712
Options for scripts
Definition: driver.hh:366
Parametric base-class for scripts.
Definition: driver.hh:729
virtual Space * copy(void)
Copy during cloning.
Definition: golf.cpp:217
int s
Number of players in a group.
Definition: golf.cpp:94
int g(void) const
Return number of groups.
Definition: golf.cpp:64
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
SetValBranch SET_VAL_MIN_INC(void)
Definition: val.hpp:55
Set variables
Definition: set.hh:127
A simple model.
Definition: golf.cpp:84
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:817
Integer variables.
Definition: int.hh:371
Driver::IntOption _s
Definition: golf.cpp:49
Class represeting a set of tuples.
Definition: int.hh:2191
int w
Number of weeks.
Definition: golf.cpp:95
GolfOptions(void)
Constructor.
Definition: golf.cpp:52
Matrix-interface for arrays.
Definition: minimodel.hh:2087
virtual void print(std::ostream &os) const
Print solution.
Definition: golf.cpp:202
Driver::IntOption _w
Definition: golf.cpp:47
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:41
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1607
void precede(Home home, const IntVarArgs &x, int s, int t, IntPropLevel)
Post propagator that s precedes t in x.
Definition: precede.cpp:43
SetExpr setdunion(const SetVarArgs &x)
Disjoint union of set variables.
Definition: set-expr.cpp:714
Golf(const GolfOptions &opt)
Actual model.
Definition: golf.cpp:101
Propagation of pair play amount using both set and int variables.
Definition: golf.cpp:91
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntPropLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Equality ( )
Definition: int.hh:926
Integer option.
Definition: driver.hh:209
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:67
SetVarArray groups
The sets representing the groups.
Definition: golf.cpp:98
Set variable array
Definition: set.hh:570
void model(int v)
Set default model value.
Definition: options.hpp:177
Driver::IntOption _g
Definition: golf.cpp:48
Gecode::IntArgs i({1, 2, 3, 4})
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013