partition.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2003
*
* 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/minimodel.hh>
using namespace Gecode;
class Partition : public Script {
protected:
IntVarArray x;
IntVarArray y;
public:
Partition(const SizeOptions& opt)
: Script(opt),
x(*this,opt.size(),1,2*opt.size()),
y(*this,opt.size(),1,2*opt.size()) {
const int n = opt.size();
// Break symmetries by ordering numbers in each group
rel(*this, x, IRT_LE);
rel(*this, y, IRT_LE);
rel(*this, x[0], IRT_LE, y[0]);
IntVarArgs xy(2*n);
for (int i = n; i--; ) {
xy[i] = x[i]; xy[n+i] = y[i];
}
distinct(*this, xy, opt.ipl());
IntArgs c(2*n);
for (int i = n; i--; ) {
c[i] = 1; c[n+i] = -1;
}
linear(*this, c, xy, IRT_EQ, 0);
// Array of products
IntVarArgs sxy(2*n), sx(n), sy(n);
for (int i = n; i--; ) {
sx[i] = sxy[i] = expr(*this, sqr(x[i]));
sy[i] = sxy[n+i] = expr(*this, sqr(y[i]));
}
linear(*this, c, sxy, IRT_EQ, 0);
// Redundant constraints
linear(*this, x, IRT_EQ, 2*n*(2*n+1)/4);
linear(*this, y, IRT_EQ, 2*n*(2*n+1)/4);
linear(*this, sx, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
linear(*this, sy, IRT_EQ, 2*n*(2*n+1)*(4*n+1)/12);
branch(*this, xy, INT_VAR_AFC_SIZE_MAX(opt.decay()), INT_VAL_MIN());
}
Partition(Partition& s) : Script(s) {
x.update(*this, s.x);
y.update(*this, s.y);
}
virtual Space*
copy(void) {
return new Partition(*this);
}
virtual void
print(std::ostream& os) const {
os << "\t";
int a, b;
a = b = 0;
for (int i = 0; i < x.size(); i++) {
a += x[i].val();
b += x[i].val()*x[i].val();
os << x[i] << ", ";
}
os << " = " << a << ", " << b << std::endl << "\t";
a = b = 0;
for (int i = 0; i < y.size(); i++) {
a += y[i].val();
b += y[i].val()*y[i].val();
os << y[i] << ", ";
}
os << " = " << a << ", " << b << std::endl;
}
};
int
main(int argc, char* argv[]) {
SizeOptions opt("Partition");
opt.size(32);
opt.ipl(IPL_BND);
opt.parse(argc,argv);
Script::run<Partition,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::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntVarArgs
Passing integer variables.
Definition int.hh:656
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Partition::main
int main(int argc, char *argv[])
Main-functiona.
Definition partition.cpp:132
Partition::print
virtual void print(std::ostream &os) const
Print solution.
Definition partition.cpp:107
Partition::Partition
Partition(Partition &s)
Constructor used during cloning s.
Definition partition.cpp:96
Partition::y
IntVarArray y
Second group of numbers.
Definition partition.cpp:50
Partition::Partition
Partition(const SizeOptions &opt)
Actual model.
Definition partition.cpp:53
Partition::copy
virtual Space * copy(void)
Copying during cloning.
Definition partition.cpp:102
Partition::x
IntVarArray x
First group of numbers.
Definition partition.cpp:48
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.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_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IRT_LE
@ IRT_LE
Less ( ).
Definition int.hh:929
Gecode::IPL_BND
@ IPL_BND
Bounds propagation.
Definition int.hh:978
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::INT_VAR_AFC_SIZE_MAX
IntVarBranch INT_VAR_AFC_SIZE_MAX(double d=1.0, BranchTbl tbl=nullptr)
Select variable with largest accumulated failure count divided by domain size with decay factor d.
Definition var.hpp:236
Gecode::sqr
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition arithmetic.cpp:95
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::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55