Generated on Thu Jan 31 2019 20:56:29 for Gecode by doxygen 1.8.15
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: 2017-03-08 14:09:33 +0100 (Wed, 08 Mar 2017) $ by $Author: schulte $
15  * $Revision: 15563 $
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,s,s) {
109  Matrix<SetVarArray> schedule(groups,g,w);
110 
111  // Groups in one week must be disjoint
112  SetVar allPlayers(*this, 0,g*s-1, 0,g*s-1);
113  for (int i=0; i<w; i++)
114  rel(*this, setdunion(schedule.row(i)) == allPlayers);
115 
116  // No two golfers play in the same group more than once
117  if (opt.propagation() == PROP_SET || opt.propagation() == PROP_MIXED) {
118  // Cardinality of intersection between two groups is at most one
119  for (int i=0; i<groups.size()-1; i++)
120  for (int j=i+1; j<groups.size(); j++)
121  rel(*this, cardinality(groups[i] & groups[j]) <= 1);
122  }
123  if (opt.propagation() == PROP_INT || opt.propagation() == PROP_MIXED) {
124  // Set up table mapping from pairs (p1,p2) (where p1<p2) of players to
125  // unique integer identifiers
126  int playerCount = g * s;
127  TupleSet ts;
128  int pairCount=0;
129  for (int p1=0; p1<playerCount-1; ++p1) {
130  for (int p2=p1+1; p2<playerCount; ++p2) {
131  ts.add(IntArgs(3, p1, p2, pairCount++));
132  }
133  }
134  ts.finalize();
135 
136  // Collect pairs of golfers into pairs
137  IntVarArgs pairs;
138  for (int i=0; i<groups.size()-1; i++) {
139  // Channel sorted will ensure that for i<j, group[i]<group[j],
140  // ensuring that the tuple set has a valid mapping.
141  IntVarArgs group(*this, s, 0, playerCount-1);
142  channelSorted(*this, group, groups[i]);
143  // Collect all pairs in current group
144  for (int p1=0; p1<group.size()-1; ++p1) {
145  for (int p2=p1+1; p2<group.size(); ++p2) {
146  IntVar pair(*this, 0, pairCount);
147 
148  IntVarArgs args;
149  args << group[p1] << group[p2] << pair;
150  extensional(*this, args, ts);
151 
152  pairs << pair;
153  }
154  }
155  }
156 
157  // All pairs of golfers (using the unique identifiers) must be different
158  distinct(*this, pairs, opt.ipl());
159  }
160 
161  if (opt.model() == MODEL_SYMMETRY) {
162 
163  /*
164  * Redundant constraints and static symmetry breaking from
165  * "Solving Kirkman's Schoolgirl Problem in a Few Seconds"
166  * Nicolas Barnier, Pascal Brisset, Constraints, 10, 7-21, 2005
167  *
168  */
169 
170  // Redundant constraint:
171  // in each week, one player plays in only one group
172  for (int j=0; j<w; j++) {
173  for (int p=0; p < g*s; p++) {
174  BoolVarArgs b(g);
175  for (int i=0; i<g; i++)
176  b[i] = expr(*this, (singleton(p) <= schedule(i,j)));
177  linear(*this, b, IRT_EQ, 1);
178  }
179  }
180 
181  // Symmetry breaking: order groups
182  for (int j=0; j<w; j++) {
183  IntVarArgs m(g);
184  for (int i=0; i<g; i++)
185  m[i] = expr(*this, min(schedule(i,j)));
186  rel(*this, m, IRT_LE);
187  }
188 
189  // Symmetry breaking: order weeks
190  // minElem(group(w,0)\{0}) < minElem(group(w+1,0)\{0})
191  {
192  IntVarArgs m(w);
193  for (int i=0; i<w; i++)
194  m[i] = expr(*this, min(schedule(0,i)-IntSet(0,0)));
195  rel(*this, m, IRT_LE);
196  }
197 
198  // Symmetry breaking: value symmetry of player numbers
199  precede(*this, groups, IntArgs::create(groups.size(),0));
200  }
201 
202  branch(*this, groups, SET_VAR_MIN_MIN(), SET_VAL_MIN_INC());
203  }
204 
206  virtual void
207  print(std::ostream& os) const {
208  os << "Tournament plan" << std::endl;
209  Matrix<SetVarArray> schedule(groups,g,w);
210  for (int j=0; j<w; j++) {
211  os << "Week " << j << ": " << std::endl << " ";
212  os << schedule.row(j) << std::endl;
213  }
214  }
215 
217  Golf(bool share, Golf& s) : Script(share,s), g(s.g), s(s.s), w(s.w) {
218  groups.update(*this, share, s.groups);
219  }
221  virtual Space*
222  copy(bool share) {
223  return new Golf(share,*this);
224  }
225 };
226 
230 int
231 main(int argc, char* argv[]) {
234  opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
235  opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
237  opt.propagation(Golf::PROP_SET, "set", "Use set intersection cardinality for pair play constraints");
238  opt.propagation(Golf::PROP_INT, "int", "Use integer distinct for pair play constraints");
239  opt.propagation(Golf::PROP_MIXED, "mixed", "Use set interesection cardinality and integer distinct for pair play constraints");
240  opt.ipl(IPL_DOM);
241  opt.solutions(1);
242  opt.parse(argc,argv);
243  Script::run<Golf,DFS,GolfOptions>(opt);
244  return 0;
245 }
246 
247 // 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
Propagation of pair play amount using both set and int variables.
Definition: golf.cpp:95
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:111
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1669
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:985
Options for Golf example
Definition: golf.cpp:49
void propagation(int v)
Set default propagation value.
Definition: options.hpp:207
Propagation of pair play amount using int variables and distinct.
Definition: golf.cpp:94
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
Golf(bool share, Golf &s)
Constructor for copying s.
Definition: golf.cpp:217
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:1748
Parametric base-class for scripts.
Definition: driver.hh:703
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 update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
void value(int v)
Set default value to v.
Definition: options.hpp:78
Propagation of pair play amount using set variables.
Definition: golf.cpp:93
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:907
Options opt
The options.
Definition: test.cpp:101
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntPropLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition: extensional.cpp:45
GolfOptions(void)
Constructor.
Definition: golf.cpp:56
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:50
Less ( )
Definition: int.hh:910
Integer sets.
Definition: int.hh:174
virtual Space * copy(bool share)
Copy during cloning.
Definition: golf.cpp:222
Passing integer variables.
Definition: int.hh:639
int w
Number of weeks.
Definition: golf.cpp:99
Passing integer arguments.
Definition: int.hh:610
Passing Boolean variables.
Definition: int.hh:693
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:510
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:2161
Model with symmetry breaking.
Definition: golf.cpp:89
Set variables
Definition: set.hh:131
virtual void print(std::ostream &os) const
Print solution.
Definition: golf.cpp:207
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:353
int g
Number of groups in a week.
Definition: golf.cpp:97
Driver::IntOption _s
Definition: golf.cpp:53
A simple model.
Definition: golf.cpp:88
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Domain propagation Preferences: prefer speed or memory.
Definition: int.hh:960
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:287
Matrix-interface for arrays.
Definition: minimodel.hh:1923
Set variable array
Definition: set.hh:572
int main(int argc, char *argv[])
Main-function.
Definition: golf.cpp:231
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
void add(const IntArgs &tuple)
Add tuple to tuple set.
Definition: tuple-set.hpp:98
Options for scripts
Definition: driver.hh:366
Integer option.
Definition: driver.hh:209