Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
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
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:92
SetExpr singleton(const LinIntExpr &e)
Singleton expression.
Definition: set-expr.cpp:691
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatNum c)
Post propagator for .
Definition: linear.cpp:45
Options for Golf example
Definition: golf.cpp:45
virtual void print(std::ostream &os) const
Print solution.
Definition: golf.cpp:155
SetValBranch SET_VAL_MIN_INC(void)
Include smallest element.
Definition: val.hpp:59
int s
Number of players in a group.
Definition: golf.cpp:88
Golf(bool share, Golf &s)
Constructor for copying s.
Definition: golf.cpp:165
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:622
Golf(const GolfOptions &opt)
Actual model.
Definition: golf.cpp:95
SetExpr setdunion(const SetVarArgs &x)
Disjoint union of set variables.
Definition: set-expr.cpp:714
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
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:904
Options opt
The options.
Definition: test.cpp:101
int s(void) const
Return number of players per group.
Definition: golf.cpp:66
GolfOptions(void)
Constructor.
Definition: golf.cpp:52
Slice< A > row(int r) const
Access row r.
Definition: matrix.hpp:181
void precede(Home home, const IntVarArgs &x, int s, int t, IntConLevel)
Post propagator that s precedes t in x.
Definition: precede.cpp:47
Less ( )
Definition: int.hh:907
Integer sets.
Definition: int.hh:171
virtual Space * copy(bool share)
Copy during cloning.
Definition: golf.cpp:170
Passing integer variables.
Definition: int.hh:636
int w
Number of weeks.
Definition: golf.cpp:89
A simple model.
Definition: golf.cpp:84
Passing Boolean variables.
Definition: int.hh:690
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:815
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:331
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Set variables
Definition: set.hh:129
int g
Number of groups in a week.
Definition: golf.cpp:87
Driver::IntOption _s
Definition: golf.cpp:49
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:243
int g(void) const
Return number of groups.
Definition: golf.cpp:64
Matrix-interface for arrays.
Definition: minimodel.hh:1924
Set variable array
Definition: set.hh:571
int main(int argc, char *argv[])
Main-function.
Definition: golf.cpp:179
Driver::IntOption _g
Definition: golf.cpp:48
void model(int v)
Set default model value.
Definition: options.hpp:155
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Driver::IntOption _w
Definition: golf.cpp:47
Model with symmetry breaking.
Definition: golf.cpp:85
Example: Golf tournament
Definition: golf.cpp:80
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
SetVarBranch SET_VAR_MIN_MIN(BranchTbl tbl)
Select variable with smallest minimum unknown element.
Definition: var.hpp:163
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
int w(void) const
Return number of weeks.
Definition: golf.cpp:62
Options for scripts
Definition: driver.hh:326
Integer option.
Definition: driver.hh:209