golf.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Contributing authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Copyright:
* Guido Tack, 2004
* Mikael Lagerkivst, 2017
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/set.hh>
using namespace Gecode;
class GolfOptions : public Options {
protected:
Driver::IntOption _w; //< Number of weeks
Driver::IntOption _g; //< Number of groups
Driver::IntOption _s; //< Number of players per group
public:
GolfOptions(void)
: Options("Golf"),
_w("w","number of weeks",9),
_g("g","number of groups",8),
_s("s","number of players per group",4) {
add(_w);
add(_g);
add(_s);
}
int w(void) const { return _w.value(); }
int g(void) const { return _g.value(); }
int s(void) const { return _s.value(); }
};
class Golf : public Script {
public:
enum {
MODEL_PLAIN,
MODEL_SYMMETRY
};
enum {
PROP_SET,
PROP_INT,
PROP_MIXED,
};
int g;
int s;
int w;
SetVarArray groups;
Golf(const GolfOptions& opt)
: Script(opt),
g(opt.g()), s(opt.s()), w(opt.w()),
groups(*this,g*w,IntSet::empty,0,g*s-1,
static_cast<unsigned int>(s),static_cast<unsigned int>(s)) {
Matrix<SetVarArray> schedule(groups,g,w);
// Groups in one week must be disjoint
SetVar allPlayers(*this, 0,g*s-1, 0,g*s-1);
for (int i=0; i<w; i++)
rel(*this, setdunion(schedule.row(i)) == allPlayers);
// No two golfers play in the same group more than once
if (opt.propagation() == PROP_SET || opt.propagation() == PROP_MIXED) {
// Cardinality of intersection between two groups is at most one
for (int i=0; i<groups.size()-1; i++)
for (int j=i+1; j<groups.size(); j++)
rel(*this, cardinality(groups[i] & groups[j]) <= 1);
}
if (opt.propagation() == PROP_INT || opt.propagation() == PROP_MIXED) {
// Set up table mapping from pairs (p1,p2) (where p1<p2) of players to
// unique integer identifiers
int playerCount = g * s;
TupleSet ts(3);
int pairCount=0;
for (int p1=0; p1<playerCount-1; p1++)
for (int p2=p1+1; p2<playerCount; p2++)
ts.add({p1, p2, pairCount++});
ts.finalize();
// Collect pairs of golfers into pairs
IntVarArgs pairs;
for (int i=0; i<groups.size()-1; i++) {
// Channel sorted will ensure that for i<j, group[i]<group[j],
// ensuring that the tuple set has a valid mapping.
IntVarArgs group(*this, s, 0, playerCount-1);
channelSorted(*this, group, groups[i]);
// Collect all pairs in current group
for (int p1=0; p1<group.size()-1; ++p1) {
for (int p2=p1+1; p2<group.size(); ++p2) {
IntVar pair(*this, 0, pairCount);
IntVarArgs args;
args << group[p1] << group[p2] << pair;
extensional(*this, args, ts);
pairs << pair;
}
}
}
// All pairs of golfers (using the unique identifiers) must be different
distinct(*this, pairs, opt.ipl());
}
if (opt.model() == MODEL_SYMMETRY) {
/*
* Redundant constraints and static symmetry breaking from
* "Solving Kirkman's Schoolgirl Problem in a Few Seconds"
* Nicolas Barnier, Pascal Brisset, Constraints, 10, 7-21, 2005
*
*/
// Redundant constraint:
// in each week, one player plays in only one group
for (int j=0; j<w; j++) {
for (int p=0; p < g*s; p++) {
BoolVarArgs b(g);
for (int i=0; i<g; i++)
b[i] = expr(*this, (singleton(p) <= schedule(i,j)));
linear(*this, b, IRT_EQ, 1);
}
}
// Symmetry breaking: order groups
for (int j=0; j<w; j++) {
IntVarArgs m(g);
for (int i=0; i<g; i++)
m[i] = expr(*this, min(schedule(i,j)));
rel(*this, m, IRT_LE);
}
// Symmetry breaking: order weeks
// minElem(group(w,0)\{0}) < minElem(group(w+1,0)\{0})
{
IntVarArgs m(w);
for (int i=0; i<w; i++)
m[i] = expr(*this, min(schedule(0,i)-IntSet(0,0)));
rel(*this, m, IRT_LE);
}
// Symmetry breaking: value symmetry of player numbers
precede(*this, groups, IntArgs::create(groups.size(),0));
}
branch(*this, groups, SET_VAR_MIN_MIN(), SET_VAL_MIN_INC());
}
virtual void
print(std::ostream& os) const {
os << "Tournament plan" << std::endl;
Matrix<SetVarArray> schedule(groups,g,w);
for (int j=0; j<w; j++) {
os << "Week " << j << ": " << std::endl << " ";
os << schedule.row(j) << std::endl;
}
}
Golf(Golf& s) : Script(s), g(s.g), s(s.s), w(s.w) {
groups.update(*this, s.groups);
}
virtual Space*
copy(void) {
return new Golf(*this);
}
};
int
main(int argc, char* argv[]) {
GolfOptions opt;
opt.model(Golf::MODEL_PLAIN);
opt.model(Golf::MODEL_PLAIN, "none", "no symmetry breaking");
opt.model(Golf::MODEL_SYMMETRY, "symmetry", "static symmetry breaking");
opt.propagation(Golf::PROP_SET);
opt.propagation(Golf::PROP_SET, "set", "Use set intersection cardinality for pair play constraints");
opt.propagation(Golf::PROP_INT, "int", "Use integer distinct for pair play constraints");
opt.propagation(Golf::PROP_MIXED, "mixed", "Use set interesection cardinality and integer distinct for pair play constraints");
opt.ipl(IPL_DOM);
opt.solutions(1);
opt.parse(argc,argv);
Script::run<Golf,DFS,GolfOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::BaseOptions::add
void add(Driver::BaseOption &o)
Add new option o.
Definition options.cpp:474
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
Gecode::Driver::IntOption
Integer option.
Definition driver.hh:209
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntArgs::create
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition array.hpp:76
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::Matrix
Matrix-interface for arrays.
Definition minimodel.hh:2161
Gecode::Matrix::row
Slice< A > row(int r) const
Access row r.
Definition matrix.hpp:177
Gecode::Options::Options
Options(const char *s)
Initialize options for script with name s.
Definition options.cpp:576
Gecode::SetVarArray
Set variable array
Definition set.hh:570
Gecode::SetVar
Set variables
Definition set.hh:127
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::TupleSet
Class represeting a set of tuples.
Definition int.hh:2191
Gecode::TupleSet::add
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition tuple-set.hpp:142
Gecode::TupleSet::finalize
void finalize(void)
Finalize tuple set.
Definition tuple-set.hpp:155
GolfOptions
Options for Golf example
Definition golf.cpp:45
GolfOptions::_w
Driver::IntOption _w
Definition golf.cpp:47
GolfOptions::_s
Driver::IntOption _s
Definition golf.cpp:49
GolfOptions::g
int g(void) const
Return number of groups.
Definition golf.cpp:64
GolfOptions::s
int s(void) const
Return number of players per group.
Definition golf.cpp:66
GolfOptions::GolfOptions
GolfOptions(void)
Constructor.
Definition golf.cpp:52
GolfOptions::w
int w(void) const
Return number of weeks.
Definition golf.cpp:62
GolfOptions::_g
Driver::IntOption _g
Definition golf.cpp:48
Golf::main
int main(int argc, char *argv[])
Main-function.
Definition golf.cpp:226
Golf::MODEL_PLAIN
@ MODEL_PLAIN
A simple model.
Definition golf.cpp:84
Golf::MODEL_SYMMETRY
@ MODEL_SYMMETRY
Model with symmetry breaking.
Definition golf.cpp:85
Golf::print
virtual void print(std::ostream &os) const
Print solution.
Definition golf.cpp:202
Golf::g
int g
Number of groups in a week.
Definition golf.cpp:93
Golf::copy
virtual Space * copy(void)
Copy during cloning.
Definition golf.cpp:217
Golf::s
int s
Number of players in a group.
Definition golf.cpp:94
Golf::groups
SetVarArray groups
The sets representing the groups.
Definition golf.cpp:98
Golf::PROP_MIXED
@ PROP_MIXED
Propagation of pair play amount using both set and int variables.
Definition golf.cpp:91
Golf::PROP_SET
@ PROP_SET
Propagation of pair play amount using set variables.
Definition golf.cpp:89
Golf::PROP_INT
@ PROP_INT
Propagation of pair play amount using int variables and distinct.
Definition golf.cpp:90
Golf::w
int w
Number of weeks.
Definition golf.cpp:95
Golf::Golf
Golf(const GolfOptions &opt)
Actual model.
Definition golf.cpp:101
Golf::Golf
Golf(Golf &s)
Constructor for copying s.
Definition golf.cpp:212
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
set.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
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
Gecode::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::extensional
void extensional(Home home, const IntVarArgs &x, DFA d, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition extensional-regular.cpp:39
Gecode::precede
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
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode
Gecode toplevel namespace
Gecode::channelSorted
void channelSorted(Home home, const IntVarArgs &x, SetVar y)
Post propagator for and .
Definition channel.cpp:45
Gecode::expr
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::SET_VAR_MIN_MIN
SetVarBranch SET_VAR_MIN_MIN(BranchTbl tbl=nullptr)
Select variable with smallest minimum unknown element.
Definition var.hpp:186
Gecode::setdunion
SetExpr setdunion(const SetVarArgs &)
Disjoint union of set variables.
Definition set-expr.cpp:714
Gecode::singleton
SetExpr singleton(const LinIntExpr &)
Singleton expression.
Definition set-expr.cpp:691
Gecode::SET_VAL_MIN_INC
SetValBranch SET_VAL_MIN_INC(void)
Include smallest element.
Definition val.hpp:55
Gecode::cardinality
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.
Definition set-expr.cpp:817