unary.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, 2009
* Guido Tack, 2010
*
* 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/unary.hh>
#include <gecode/int/distinct.hh>
#include <algorithm>
namespace Gecode {
void
unary(Home home, const IntVarArgs& s, const IntArgs& p, IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if (same(s))
throw Int::ArgumentSame("Int::unary");
if (s.size() != p.size())
throw Int::ArgumentSizeMismatch("Int::unary");
for (int i=0; i<p.size(); i++) {
Int::Limits::nonnegative(p[i],"Int::unary");
Int::Limits::check(static_cast<long long int>(s[i].max()) + p[i],
"Int::unary");
}
GECODE_POST;
bool allOne = true;
for (int i=0; i<p.size(); i++) {
if (p[i] != 1) {
allOne = false;
break;
}
}
if (allOne) {
ViewArray<IntView> xv(home,s);
switch (vbd(ipl)) {
case IPL_BND:
GECODE_ES_FAIL(Distinct::Bnd<IntView>::post(home,xv));
break;
case IPL_DOM:
GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,xv));
break;
default:
GECODE_ES_FAIL(Distinct::Val<IntView>::post(home,xv));
}
} else {
TaskArray<ManFixPTask> t(home,s.size());
for (int i=0; i<s.size(); i++)
t[i].init(s[i],p[i]);
GECODE_ES_FAIL(manpost(home,t,ipl));
}
}
void
unary(Home home, const TaskTypeArgs& t,
const IntVarArgs& flex, const IntArgs& fix, IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if ((flex.size() != fix.size()) || (flex.size() != t.size()))
throw Int::ArgumentSizeMismatch("Int::unary");
for (int i=0; i<fix.size(); i++) {
if (t[i] == TT_FIXP)
Int::Limits::nonnegative(fix[i],"Int::unary");
else
Int::Limits::check(fix[i],"Int::unary");
Int::Limits::check(static_cast<long long int>(flex[i].max()) + fix[i],
"Int::unary");
}
GECODE_POST;
bool fixp = true;
for (int i=0; i<t.size(); i++)
if (t[i] != TT_FIXP) {
fixp = false; break;
}
if (fixp) {
unary(home, flex, fix, ipl);
} else {
TaskArray<ManFixPSETask> tasks(home,flex.size());
for (int i=0; i<flex.size(); i++)
tasks[i].init(t[i],flex[i],fix[i]);
GECODE_ES_FAIL(manpost(home,tasks,ipl));
}
}
void
unary(Home home, const IntVarArgs& s, const IntArgs& p,
const BoolVarArgs& m, IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if (same(s))
throw Int::ArgumentSame("Int::unary");
if ((s.size() != p.size()) || (s.size() != m.size()))
throw Int::ArgumentSizeMismatch("Int::unary");
for (int i=0; i<p.size(); i++) {
Int::Limits::nonnegative(p[i],"Int::unary");
Int::Limits::check(static_cast<long long int>(s[i].max()) + p[i],
"Int::unary");
}
bool allMandatory = true;
for (int i=0; i<m.size(); i++) {
if (!m[i].one()) {
allMandatory = false;
break;
}
}
if (allMandatory) {
unary(home,s,p,ipl);
} else {
GECODE_POST;
TaskArray<OptFixPTask> t(home,s.size());
for (int i=0; i<s.size(); i++)
t[i].init(s[i],p[i],m[i]);
GECODE_ES_FAIL(optpost(home,t,ipl));
}
}
void
unary(Home home, const TaskTypeArgs& t,
const IntVarArgs& flex, const IntArgs& fix, const BoolVarArgs& m,
IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if ((flex.size() != fix.size()) || (flex.size() != t.size()) ||
(flex.size() != m.size()))
throw Int::ArgumentSizeMismatch("Int::unary");
bool fixp = true;
for (int i=0; i<fix.size(); i++) {
if (t[i] == TT_FIXP) {
Int::Limits::nonnegative(fix[i],"Int::unary");
} else {
fixp = false;
Int::Limits::check(fix[i],"Int::unary");
}
Int::Limits::check(static_cast<long long int>(flex[i].max()) + fix[i],
"Int::unary");
}
GECODE_POST;
bool allMandatory = true;
for (int i=0; i<m.size(); i++) {
if (!m[i].one()) {
allMandatory = false;
break;
}
}
if (allMandatory) {
unary(home,t,flex,fix,ipl);
} else {
if (fixp) {
TaskArray<OptFixPTask> tasks(home,flex.size());
for (int i=0; i<flex.size(); i++)
tasks[i].init(flex[i],fix[i],m[i]);
GECODE_ES_FAIL(optpost(home,tasks,ipl));
} else {
TaskArray<OptFixPSETask> tasks(home,flex.size());
for (int i=0; i<flex.size(); i++)
tasks[i].init(t[i],flex[i],fix[i],m[i]);
GECODE_ES_FAIL(optpost(home,tasks,ipl));
}
}
}
void
unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
const IntVarArgs& e, IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if ((s.size() != p.size()) || (s.size() != e.size()))
throw Int::ArgumentSizeMismatch("Int::unary");
GECODE_POST;
for (int i=0; i<p.size(); i++) {
IntView pi(p[i]);
GECODE_ME_FAIL(pi.gq(home,0));
}
bool fixP = true;
for (int i=0; i<p.size(); i++) {
if (!p[i].assigned()) {
fixP = false;
break;
}
}
if (fixP) {
IntArgs pp(p.size());
for (int i=0; i<p.size(); i++)
pp[i] = p[i].val();
unary(home,s,pp,ipl);
} else {
TaskArray<ManFlexTask> t(home,s.size());
for (int i=0; i<s.size(); i++)
t[i].init(s[i],p[i],e[i]);
GECODE_ES_FAIL(manpost(home,t,ipl));
}
}
void
unary(Home home, const IntVarArgs& s, const IntVarArgs& p,
const IntVarArgs& e, const BoolVarArgs& m, IntPropLevel ipl) {
using namespace Gecode::Int;
using namespace Gecode::Int::Unary;
if ((s.size() != p.size()) || (s.size() != m.size()) ||
(s.size() != e.size()))
throw Int::ArgumentSizeMismatch("Int::unary");
GECODE_POST;
for (int i=0; i<p.size(); i++) {
IntView pi(p[i]);
GECODE_ME_FAIL(pi.gq(home,0));
}
bool allMandatory = true;
for (int i=0; i<m.size(); i++) {
if (!m[i].one()) {
allMandatory = false;
break;
}
}
if (allMandatory) {
unary(home,s,p,e,ipl);
} else {
TaskArray<OptFlexTask> t(home,s.size());
for (int i=0; i<s.size(); i++)
t[i].init(s[i],p[i],e[i],m[i]);
GECODE_ES_FAIL(optpost(home,t,ipl));
}
}
}
// STATISTICS: int-post
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::BoolVarArgs
Passing Boolean variables.
Definition int.hh:712
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::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::Distinct::Bnd::post
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator for view array x.
Definition bnd.hpp:476
Gecode::Int::Distinct::Dom::post
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator for views x.
Definition dom.hpp:45
Gecode::Int::Distinct::Val::post
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator for view array x.
Definition val.hpp:185
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::TaskArray
Task array.
Definition task.hh:165
Gecode::ViewArray
View arrays.
Definition array.hpp:253
pi
const int * pi[]
Definition photo.cpp:14262
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_ME_FAIL
#define GECODE_ME_FAIL(me)
Check whether modification event me is failed, and fail space home.
Definition macros.hpp:77
Gecode::IntPropLevel
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
Gecode::TaskTypeArgs
ArgArray< TaskType > TaskTypeArgs
Argument arrays for passing task type arguments.
Definition int.hh:1015
Gecode::TT_FIXP
@ TT_FIXP
Definition int.hh:1005
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
distinct.hh
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::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::Unary
Int for unary resources
Gecode::Int::Unary::optpost
ExecStatus optpost(Home home, TaskArray< OptTask > &t, IntPropLevel ipl)
Post optional task propagator according to propagation level.
Definition post.hpp:53
Gecode::Int::Unary::manpost
ExecStatus manpost(Home home, TaskArray< ManTask > &t, IntPropLevel ipl)
Post mandatory task propagator according to propagation level.
Definition post.hpp:38
Gecode::Int
Finite domain integers.
Gecode
Gecode toplevel namespace
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::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Gecode::unary
void unary(Home home, const IntVarArgs &s, const IntArgs &p, IntPropLevel ipl=IPL_DEF)
Post propagators for scheduling tasks on unary resources.
Definition unary.cpp:44
unary.hh