circuit.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>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2006
* Guido Tack, 2011
*
* 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/circuit.hh>
namespace Gecode {
void
circuit(Home home, int offset, const IntVarArgs& x, IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::circuit");
if (x.size() == 0)
throw Int::TooFewArguments("Int::circuit");
if (same(x))
throw Int::ArgumentSame("Int::circuit");
GECODE_POST;
ViewArray<Int::IntView> xv(home,x);
if (offset == 0) {
typedef Int::NoOffset<Int::IntView> NOV;
NOV no;
if (vbd(ipl) == IPL_DOM) {
GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV>
::post(home,xv,no)));
} else {
GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV>
::post(home,xv,no)));
}
} else {
typedef Int::Offset OV;
OV off(-offset);
if (vbd(ipl) == IPL_DOM) {
GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV>
::post(home,xv,off)));
} else {
GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV>
::post(home,xv,off)));
}
}
}
void
circuit(Home home, const IntVarArgs& x, IntPropLevel ipl) {
circuit(home,0,x,ipl);
}
void
circuit(Home home, const IntArgs& c, int offset,
const IntVarArgs& x, const IntVarArgs& y, IntVar z,
IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::circuit");
int n = x.size();
if (n == 0)
throw Int::TooFewArguments("Int::circuit");
if (same(x))
throw Int::ArgumentSame("Int::circuit");
if ((y.size() != n) || (c.size() != n*n))
throw Int::ArgumentSizeMismatch("Int::circuit");
circuit(home, offset, x, ipl);
GECODE_POST;
IntArgs cx(offset+n);
for (int i=0; i<offset; i++)
cx[i] = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++)
cx[offset+j] = c[i*n+j];
element(home, cx, x[i], y[i]);
}
linear(home, y, IRT_EQ, z);
}
void
circuit(Home home, const IntArgs& c,
const IntVarArgs& x, const IntVarArgs& y, IntVar z,
IntPropLevel ipl) {
circuit(home,c,0,x,y,z,ipl);
}
void
circuit(Home home, const IntArgs& c, int offset,
const IntVarArgs& x, IntVar z,
IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::circuit");
GECODE_POST;
IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max);
circuit(home, c, offset, x, y, z, ipl);
}
void
circuit(Home home, const IntArgs& c,
const IntVarArgs& x, IntVar z,
IntPropLevel ipl) {
circuit(home,c,0,x,z,ipl);
}
void
path(Home home, int offset, const IntVarArgs& x, IntVar s, IntVar e,
IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::path");
int n=x.size();
if (n == 0)
throw Int::TooFewArguments("Int::path");
if (same(x))
throw Int::ArgumentSame("Int::path");
GECODE_POST;
ViewArray<Int::IntView> xv(home,n+1);
for (int i=0; i<n; i++)
xv[i] = Int::IntView(x[i]);
xv[n] = s;
if (offset == 0) {
element(home, x, e, n);
typedef Int::NoOffset<Int::IntView> NOV;
NOV no;
if (vbd(ipl) == IPL_DOM) {
GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,NOV>
::post(home,xv,no)));
} else {
GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,NOV>
::post(home,xv,no)));
}
} else {
IntVarArgs ox(n+offset);
IntVar y(home, -1,-1);
for (int i=0; i<offset; i++)
ox[i] = y;
for (int i=0; i<n; i++)
ox[offset + i] = x[i];
element(home, ox, e, offset+n);
typedef Int::Offset OV;
OV off(-offset);
if (vbd(ipl) == IPL_DOM) {
GECODE_ES_FAIL((Int::Circuit::Dom<Int::IntView,OV>
::post(home,xv,off)));
} else {
GECODE_ES_FAIL((Int::Circuit::Val<Int::IntView,OV>
::post(home,xv,off)));
}
}
}
void
path(Home home, const IntVarArgs& x, IntVar s, IntVar e,
IntPropLevel ipl) {
path(home,0,x,s,e,ipl);
}
void
path(Home home, const IntArgs& c, int offset,
const IntVarArgs& x, IntVar s, IntVar e,
const IntVarArgs& y, IntVar z,
IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::path");
int n = x.size();
if (n == 0)
throw Int::TooFewArguments("Int::path");
if (same(x))
throw Int::ArgumentSame("Int::path");
if ((y.size() != n) || (c.size() != n*n))
throw Int::ArgumentSizeMismatch("Int::path");
GECODE_POST;
path(home, offset, x, s, e, ipl);
IntArgs cx(offset+n+1);
for (int i=0; i<offset; i++)
cx[i] = 0;
cx[offset+n] = 0;
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++)
cx[offset+j] = c[i*n+j];
element(home, cx, x[i], y[i]);
}
linear(home, y, IRT_EQ, z);
}
void
path(Home home, const IntArgs& c,
const IntVarArgs& x, IntVar s, IntVar e,
const IntVarArgs& y, IntVar z,
IntPropLevel ipl) {
path(home,c,0,x,s,e,y,z,ipl);
}
void
path(Home home, const IntArgs& c, int offset,
const IntVarArgs& x, IntVar s, IntVar e, IntVar z,
IntPropLevel ipl) {
Int::Limits::nonnegative(offset,"Int::path");
GECODE_POST;
IntVarArgs y(home, x.size(), Int::Limits::min, Int::Limits::max);
path(home, c, offset, x, s, e, y, z, ipl);
}
void
path(Home home, const IntArgs& c,
const IntVarArgs& x, IntVar s, IntVar e, IntVar z,
IntPropLevel ipl) {
path(home,c,0,x,s,e,z,ipl);
}
}
// STATISTICS: int-post
circuit.hh
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
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::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::Circuit::Dom
"Domain consistent" circuit propagator
Definition circuit.hh:121
Gecode::Int::Circuit::Val
"Value-consistent" circuit propagator
Definition circuit.hh:88
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::NoOffset
Converter without offsets.
Definition view.hpp:618
Gecode::Int::Offset
Converter with fixed offset.
Definition view.hpp:650
Gecode::Int::TooFewArguments
Exception: Too few arguments available in argument array
Definition exception.hpp:66
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::Int::OffsetView::offset
int offset(void) const
Integer-precision integer scale view.
Definition view.hpp:642
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::linear
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition linear.cpp:41
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::Int::Limits::nonnegative
void nonnegative(int n, const char *l)
Check whether n is in range and nonnegative, otherwise throw out of limits with information l.
Definition limits.hpp:68
Gecode::Int::Limits::min
const int min
Smallest allowed integer value.
Definition int.hh:118
Gecode::Int::Limits::max
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode
Gecode toplevel namespace
Gecode::vbd
IntPropLevel vbd(IntPropLevel ipl)
Extract value, bounds, or domain propagation from propagation level.
Definition ipl.hpp:37
Gecode::element
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Gecode::path
void path(Home home, const IntVarArgs &x, IntVar s, IntVar e, IntPropLevel ipl=IPL_DEF)
Post propagator such that x forms a Hamiltonian path.
Definition circuit.cpp:169
Gecode::same
bool same(VarArgArray< Var > x, VarArgArray< Var > y)
Definition array.hpp:1937
Gecode::post
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
Gecode::circuit
void circuit(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator such that x forms a circuit.
Definition circuit.cpp:73