gcc.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Patrick Pekczynski <pekczynski@ps.uni-sb.de>
* Guido Tack <tack@gecode.org>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Patrick Pekczynski, 2004
* Christian Schulte, 2009
* Guido Tack, 2006
*
* 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/int/gcc.hh>
namespace Gecode {
namespace {
template<class X>
struct LessP {
bool operator ()(const std::pair<X,int>& lhs,
const std::pair<X,int>& rhs) {
return lhs.second < rhs.second;
}
};
IntVar unify(Home home, IntVar x, IntVar y) {
rel(home, x, IRT_EQ, y);
return x;
}
IntSet unify(Home, const IntSet& x, const IntSet& y) {
IntSetRanges xr(x);
IntSetRanges yr(y);
Iter::Ranges::Inter<IntSetRanges,IntSetRanges> i(xr,yr);
IntSet z(i);
return z;
}
template<class A>
void removeDuplicates(Home home, A& c, IntArgs& v) {
typedef typename A::value_type S;
typedef std::pair<S,int> P;
Region re;
P* a = re.alloc<P>(c.size());
for (int i=0; i<c.size(); i++)
a[i] = P(c[i],v[i]);
LessP<S> l;
Support::quicksort(a,c.size(),l);
A cc;
IntArgs vv;
int cur = a[0].second-1;
for (int i=0; i<c.size(); i++) {
if (a[i].second==cur) {
cc[cc.size()-1] = unify(home, cc[cc.size()-1], a[i].first);
} else {
cc << a[i].first;
vv << a[i].second;
cur = a[i].second;
}
}
re.free<P>(a,c.size());
c = cc;
v = vv;
}
}
void count(Home home, const IntVarArgs& x,
const IntVarArgs& _c, const IntArgs& _v,
IntPropLevel ipl) {
using namespace Int;
IntVarArgs c(_c);
IntArgs v(_v);
if (v.size() != c.size())
throw ArgumentSizeMismatch("Int::count");
if (same(x))
throw ArgumentSame("Int::count");
GECODE_POST;
removeDuplicates(home,c,v);
ViewArray<IntView> xv(home, x);
ViewArray<GCC::CardView> cv(home, c.size());
// set the cardinality
for (int i=0; i<v.size(); i++)
cv[i].init(c[i],v[i]);
switch (vbd(ipl)) {
case IPL_BND:
GECODE_ES_FAIL(
(GCC::Bnd<GCC::CardView>::post(home,xv,cv)));
break;
case IPL_DOM:
GECODE_ES_FAIL(
(GCC::Dom<GCC::CardView>::post(home,xv,cv)));
break;
default:
GECODE_ES_FAIL(
(GCC::Val<GCC::CardView>::post(home,xv,cv)));
}
}
// domain is 0..|cards|- 1
void count(Home home, const IntVarArgs& x, const IntVarArgs& c,
IntPropLevel ipl) {
IntArgs values(c.size());
for (int i=0; i<c.size(); i++)
values[i] = i;
count(home, x, c, values, ipl);
}
// constant cards
void count(Home home, const IntVarArgs& x,
const IntSetArgs& _c, const IntArgs& _v,
IntPropLevel ipl) {
using namespace Int;
IntSetArgs c(_c);
IntArgs v(_v);
if (v.size() != c.size())
throw ArgumentSizeMismatch("Int::count");
if (same(x))
throw ArgumentSame("Int::count");
for (int i=0; i<c.size(); i++) {
Limits::check(v[i],"Int::count");
Limits::check(c[i].min(),"Int::count");
Limits::check(c[i].max(),"Int::count");
}
GECODE_POST;
removeDuplicates(home,c,v);
ViewArray<IntView> xv(home, x);
for (int i=0; i<v.size(); i++) {
if (c[i].ranges() > 1) {
// Found hole, so create temporary variables
ViewArray<GCC::CardView> cv(home, v.size());
for (int j=0; j<v.size(); j++)
cv[j].init(home,c[j],v[j]);
switch (vbd(ipl)) {
case IPL_BND:
GECODE_ES_FAIL(
(GCC::Bnd<GCC::CardView>::post(home, xv, cv)));
break;
case IPL_DOM:
GECODE_ES_FAIL(
(GCC::Dom<GCC::CardView>::post(home, xv, cv)));
break;
default:
GECODE_ES_FAIL(
(GCC::Val<GCC::CardView>::post(home, xv, cv)));
}
return;
}
}
// No holes: create CardConsts
ViewArray<GCC::CardConst> cv(home, c.size());
for (int i=0; i<c.size(); i++)
cv[i].init(home,c[i].min(),c[i].max(),v[i]);
switch (vbd(ipl)) {
case IPL_BND:
GECODE_ES_FAIL(
(GCC::Bnd<GCC::CardConst>::post(home, xv, cv)));
break;
case IPL_DOM:
GECODE_ES_FAIL(
(GCC::Dom<GCC::CardConst>::post(home, xv, cv)));
break;
default:
GECODE_ES_FAIL(
(GCC::Val<GCC::CardConst>::post(home, xv, cv)));
}
}
// domain is 0..|cards|- 1
void count(Home home, const IntVarArgs& x, const IntSetArgs& c,
IntPropLevel ipl) {
IntArgs values(c.size());
for (int i=0; i<c.size(); i++)
values[i] = i;
count(home, x, c, values, ipl);
}
void count(Home home, const IntVarArgs& x,
const IntSet& c, const IntArgs& v,
IntPropLevel ipl) {
IntSetArgs cards(v.size());
for (int i=0; i<v.size(); i++)
cards[i] = c;
count(home, x, cards, v, ipl);
}
}
// STATISTICS: int-post
Gecode::FloatVal::size
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Definition val.hpp:78
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSetRanges
Range iterator for integer sets.
Definition int.hh:292
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::Int::ArgumentSame
Exception: Arguments contain same variable multiply
Definition exception.hpp:80
Gecode::Int::ArgumentSizeMismatch
Exception: Arguments are of different size
Definition exception.hpp:73
Gecode::Int::GCC::Bnd::post
static ExecStatus post(Home home, ViewArray< IntView > &x, ViewArray< Card > &k)
Post propagator for views x and cardinalities k.
Definition bnd.hpp:808
Gecode::Int::GCC::Dom::post
static ExecStatus post(Home home, ViewArray< IntView > &x, ViewArray< Card > &k)
Post propagator for views x and cardinalities k.
Definition dom.hpp:299
Gecode::Int::GCC::Val::post
static ExecStatus post(Home home, ViewArray< IntView > &x, ViewArray< Card > &k)
Post propagator for views x and cardinalities k.
Definition val.hpp:287
Gecode::Iter::Ranges::Inter
Range iterator for computing intersection (binary).
Definition ranges-inter.hpp:44
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::ViewArray
View arrays.
Definition array.hpp:253
gcc.hh
Gecode::Int::OffsetView::operator()
View & operator()(View &x)
Integer-precision integer scale view.
Definition view.hpp:632
GECODE_POST
#define GECODE_POST
Check for failure in a constraint post function.
Definition macros.hpp:40
GECODE_ES_FAIL
#define GECODE_ES_FAIL(es)
Check whether execution status es is failed, and fail space home.
Definition macros.hpp:103
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
Gecode::IRT_EQ
@ IRT_EQ
Equality ( ).
Definition int.hh:926
Gecode::IPL_DOM
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode::IPL_BND
@ IPL_BND
Bounds propagation.
Definition int.hh:978
Gecode::Int::Limits::check
void check(int n, const char *l)
Check whether n is in range, otherwise throw out of limits with information l.
Definition limits.hpp:46
Gecode::Int
Finite domain integers.
Gecode::Support::quicksort
void quicksort(Type *l, Type *r, Less &less)
Standard quick sort.
Definition sort.hpp:130
Gecode
Gecode toplevel namespace
Gecode::IntSetArgs
ArgArray< IntSet > IntSetArgs
Passing set arguments.
Definition int.hh:619
Gecode::count
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::vbd
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
Gecode::same
bool same(VarArgArray< Var > x, VarArgArray< Var > y)
Definition array.hpp:1937
Gecode::values
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:143
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Test::Float::Arithmetic::c
Gecode::FloatVal c(-8, 8)
Test::Float::Arithmetic::a
Gecode::FloatVal a(-8, 5)
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
Test::Int::Distinct::v
const int v[7]
Definition distinct.cpp:259