steiner.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>
*
* Copyright:
* Guido Tack, 2004
*
* 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 Steiner : public Script {
public:
enum {
MODEL_NONE,
MODEL_MATCHING,
MODEL_SEQ
};
int n;
int noOfTriples;
SetVarArray triples;
Steiner(const SizeOptions& opt)
: Script(opt), n(opt.size()), noOfTriples((n*(n-1))/6),
triples(*this, noOfTriples, IntSet::empty, 1, n, 3U, 3U) {
for (int i=0; i<noOfTriples; i++) {
for (int j=i+1; j<noOfTriples; j++) {
SetVar x = triples[i];
SetVar y = triples[j];
SetVar atmostOne(*this,IntSet::empty,1,n,0U,1U);
rel(*this, (x & y) == atmostOne);
IntVar x1(*this,1,n);
IntVar x2(*this,1,n);
IntVar x3(*this,1,n);
IntVar y1(*this,1,n);
IntVar y2(*this,1,n);
IntVar y3(*this,1,n);
if (opt.model() == MODEL_NONE) {
/* Naive alternative:
* just including the ints in the set
*/
rel(*this, singleton(x1) <= x);
rel(*this, singleton(x2) <= x);
rel(*this, singleton(x3) <= x);
rel(*this, singleton(y1) <= y);
rel(*this, singleton(y2) <= y);
rel(*this, singleton(y3) <= y);
} else if (opt.model() == MODEL_MATCHING) {
/* Smart alternative:
* Using matching constraints
*/
channelSorted(*this, {x1,x2,x3}, x);
channelSorted(*this, {y1,y2,y3}, y);
} else if (opt.model() == MODEL_SEQ) {
SetVar sx1 = expr(*this, singleton(x1));
SetVar sx2 = expr(*this, singleton(x2));
SetVar sx3 = expr(*this, singleton(x3));
SetVar sy1 = expr(*this, singleton(y1));
SetVar sy2 = expr(*this, singleton(y2));
SetVar sy3 = expr(*this, singleton(y3));
sequence(*this,{sx1,sx2,sx3},x);
sequence(*this,{sy1,sy2,sy3},y);
}
/* Breaking symmetries */
rel(*this, x1 < x2);
rel(*this, x2 < x3);
rel(*this, x1 < x3);
rel(*this, y1 < y2);
rel(*this, y2 < y3);
rel(*this, y1 < y3);
linear(*this,
{(n+1)*(n+1), n+1, 1, -(n+1)*(n+1), -(n+1), -1},
{x1, x2, x3, y1, y2, y3},
IRT_LE, 0);
}
}
branch(*this, triples, SET_VAR_NONE(), SET_VAL_MIN_INC());
}
virtual void
print(std::ostream& os) const {
for (int i=0; i<noOfTriples; i++) {
os << "\t[" << i << "] = " << triples[i] << std::endl;
}
}
Steiner(Steiner& s) : Script(s), n(s.n), noOfTriples(s.noOfTriples) {
triples.update(*this, s.triples);
}
virtual Space*
copy(void) {
return new Steiner(*this);
}
};
int
main(int argc, char* argv[]) {
SizeOptions opt("Steiner");
opt.model(Steiner::MODEL_NONE);
opt.model(Steiner::MODEL_NONE, "rel", "Use simple relation constraints");
opt.model(Steiner::MODEL_MATCHING, "matching", "Use matching constraints");
opt.model(Steiner::MODEL_SEQ, "sequence", "Use sequence constraints");
opt.size(9);
opt.iterations(20);
opt.parse(argc,argv);
Script::run<Steiner,DFS,SizeOptions>(opt);
return 0;
}
// STATISTICS: example-any
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntSet::empty
static const IntSet empty
Empty set.
Definition int.hh:283
Gecode::IntVar
Integer variables.
Definition int.hh:371
Gecode::SetVarArray
Set variable array
Definition set.hh:570
Gecode::SetVar
Set variables
Definition set.hh:127
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Steiner::main
int main(int argc, char *argv[])
Main-function.
Definition steiner.cpp:153
Steiner::print
virtual void print(std::ostream &os) const
Print solution.
Definition steiner.cpp:133
Steiner::Steiner
Steiner(const SizeOptions &opt)
Actual model.
Definition steiner.cpp:65
Steiner::copy
virtual Space * copy(void)
Copy during cloning.
Definition steiner.cpp:144
Steiner::MODEL_MATCHING
@ MODEL_MATCHING
Use matching constraints.
Definition steiner.cpp:53
Steiner::MODEL_SEQ
@ MODEL_SEQ
Use sequence constraints.
Definition steiner.cpp:54
Steiner::MODEL_NONE
@ MODEL_NONE
Use simple relation constraint.
Definition steiner.cpp:52
Steiner::Steiner
Steiner(Steiner &s)
Constructor for copying s.
Definition steiner.cpp:139
Steiner::n
int n
Order of the Steiner problem.
Definition steiner.cpp:57
Steiner::triples
SetVarArray triples
The steiner triples.
Definition steiner.cpp:62
Steiner::noOfTriples
int noOfTriples
Number of Steiner triples.
Definition steiner.cpp:59
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::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode
Gecode toplevel namespace
Gecode::atmostOne
void atmostOne(Home home, const SetVarArgs &x, unsigned int c)
Post propagator for and .
Definition distinct.cpp:41
Gecode::SET_VAR_NONE
SetVarBranch SET_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
Gecode::sequence
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition sequence.cpp:47
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::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