Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.0
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
42using namespace Gecode;
43
45class GolfOptions : public Options {
46protected:
47 Driver::IntOption _w; //< Number of weeks
48 Driver::IntOption _g; //< Number of groups
49 Driver::IntOption _s; //< Number of players per group
50public:
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 }
61
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
80class Golf : public Script {
81public:
83 enum {
86 };
88 enum {
92 };
93 int g;
94 int s;
95 int w;
96
99
101 Golf(const GolfOptions& opt)
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
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 }
215
216 virtual Space*
217 copy(void) {
218 return new Golf(*this);
219 }
220};
221
225int
226main(int argc, char* argv[]) {
227 GolfOptions opt;
228 opt.model(Golf::MODEL_PLAIN);
229 opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
230 opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
231 opt.propagation(Golf::PROP_SET);
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);
239 return 0;
240}
241
242// STATISTICS: example-any
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1613
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
Passing Boolean variables.
Definition int.hh:721
Integer option.
Definition driver.hh:209
static void run(const Options &opt, Script *s=NULL)
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition array.hpp:76
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:662
Integer variables.
Definition int.hh:371
Matrix-interface for arrays.
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Set variable array
Definition set.hh:573
Set variables
Definition set.hh:127
Computation spaces.
Definition core.hpp:1744
Class represeting a set of tuples.
Definition int.hh:2206
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
void finalize(void)
Finalize tuple set.
Options for Golf example
Definition golf.cpp:45
Driver::IntOption _w
Definition golf.cpp:47
Driver::IntOption _s
Definition golf.cpp:49
int g(void) const
Return number of groups.
Definition golf.cpp:64
int s(void) const
Return number of players per group.
Definition golf.cpp:66
GolfOptions(void)
Constructor.
Definition golf.cpp:52
int w(void) const
Return number of weeks.
Definition golf.cpp:62
Driver::IntOption _g
Definition golf.cpp:48
int main(int argc, char *argv[])
Main-function.
Definition golf.cpp:226
@ MODEL_PLAIN
A simple model.
Definition golf.cpp:84
@ MODEL_SYMMETRY
Model with symmetry breaking.
Definition golf.cpp:85
virtual void print(std::ostream &os) const
Print solution.
Definition golf.cpp:202
int g
Number of groups in a week.
Definition golf.cpp:93
virtual Space * copy(void)
Copy during cloning.
Definition golf.cpp:217
int s
Number of players in a group.
Definition golf.cpp:94
SetVarArray groups
The sets representing the groups.
Definition golf.cpp:98
@ PROP_MIXED
Propagation of pair play amount using both set and int variables.
Definition golf.cpp:91
@ PROP_SET
Propagation of pair play amount using set variables.
Definition golf.cpp:89
@ PROP_INT
Propagation of pair play amount using int variables and distinct.
Definition golf.cpp:90
int w
Number of weeks.
Definition golf.cpp:95
Golf(const GolfOptions &opt)
Actual model.
Definition golf.cpp:101
Golf(Golf &s)
Constructor for copying s.
Definition golf.cpp:212
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
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, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
void extensional(Home home, const IntVarArgs &x, DFA d, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for extensional constraint described by a DFA.
void precede(Home home, const IntVarArgs &x, int s, int t, IntPropLevel=IPL_DEF)
Post propagator that s precedes t in x.
Definition precede.cpp:43
@ IRT_EQ
Equality ( )
Definition int.hh:941
@ IRT_LE
Less ( )
Definition int.hh:944
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
Gecode toplevel namespace
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Definition channel.cpp:45
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Select variable with smallest minimum unknown element SetVarBranch SET_VAR_MIN_MIN(BranchTbl tbl=nullptr)
Definition var.hpp:186
SetExpr setdunion(const SetVarArgs &)
Disjoint union of set variables.
Definition set-expr.cpp:714
SetExpr singleton(const LinIntExpr &)
Singleton expression.
Definition set-expr.cpp:691
Include smallest element SetValBranch SET_VAL_MIN_INC(void)
Definition val.hpp:55
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817