nq.hpp
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, 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 <algorithm>
namespace Gecode { namespace Int { namespace Rel {
/*
* Disequality
*
*/
template<class V0, class V1>
forceinline
Nq<V0,V1>::Nq(Home home, V0 x0, V1 x1)
: MixBinaryPropagator<V0,PC_INT_VAL,V1,PC_INT_VAL>(home,x0,x1) {}
template<class V0, class V1>
ExecStatus
Nq<V0,V1>::post(Home home, V0 x0, V1 x1){
if (x0.assigned()) {
GECODE_ME_CHECK(x1.nq(home,x0.val()));
} else if (x1.assigned()) {
GECODE_ME_CHECK(x0.nq(home,x1.val()));
} else if (x0 == x1) {
return ES_FAILED;
} else {
(void) new (home) Nq<V0,V1>(home,x0,x1);
}
return ES_OK;
}
template<class V0, class V1>
forceinline
Nq<V0,V1>::Nq(Space& home, Nq<V0,V1>& p)
: MixBinaryPropagator<V0,PC_INT_VAL,V1,PC_INT_VAL>(home,p) {}
template<class V0, class V1>
Actor*
Nq<V0,V1>::copy(Space& home) {
return new (home) Nq<V0,V1>(home,*this);
}
template<class V0, class V1>
PropCost
Nq<V0,V1>::cost(const Space&, const ModEventDelta&) const {
return PropCost::unary(PropCost::LO);
}
template<class V0, class V1>
ExecStatus
Nq<V0,V1>::propagate(Space& home, const ModEventDelta&) {
if (x0.assigned()) {
GECODE_ME_CHECK(x1.nq(home,x0.val()));
} else {
GECODE_ME_CHECK(x0.nq(home,x1.val()));
}
return home.ES_SUBSUMED(*this);
}
/*
* Nary disequality propagator
*/
template<class View>
forceinline
NaryNq<View>::NaryNq(Home home, ViewArray<View>& x)
: NaryPropagator<View,PC_INT_VAL>(home,x) {}
template<class View>
PropCost
NaryNq<View>::cost(const Space&, const ModEventDelta&) const {
return PropCost::linear(PropCost::LO,x.size());
}
template<class View>
forceinline
NaryNq<View>::NaryNq(Space& home, NaryNq<View>& p)
: NaryPropagator<View,PC_INT_VAL>(home,p) {}
template<class View>
Actor*
NaryNq<View>::copy(Space& home) {
return new (home) NaryNq<View>(home,*this);
}
template<class View>
inline ExecStatus
NaryNq<View>::post(Home home, ViewArray<View>& x) {
x.unique();
// Try to find an assigned view
int n = x.size();
if (n <= 1)
return ES_FAILED;
for (int i=n; i--; )
if (x[i].assigned()) {
std::swap(x[0],x[i]);
break;
}
if (x[0].assigned()) {
int v = x[0].val();
// Eliminate all equal views and possibly find disequal view
for (int i=n-1; i>0; i--)
if (!x[i].in(v)) {
return ES_OK;
} else if (x[i].assigned()) {
assert(x[i].val() == v);
x[i]=x[--n];
}
x.size(n);
}
if (n == 1)
return ES_FAILED;
if (n == 2)
return Nq<View,View>::post(home,x[0],x[1]);
(void) new (home) NaryNq(home,x);
return ES_OK;
}
template<class View>
forceinline size_t
NaryNq<View>::dispose(Space& home) {
(void) NaryPropagator<View,PC_INT_VAL>::dispose(home);
return sizeof(*this);
}
template<class View>
ExecStatus
NaryNq<View>::propagate(Space& home, const ModEventDelta&) {
// Make sure that x[0] is assigned
if (!x[0].assigned()) {
// Note that there is at least one assigned view
for (int i=1; true; i++)
if (x[i].assigned()) {
std::swap(x[0],x[i]);
break;
}
}
int v = x[0].val();
int n = x.size();
for (int i=n-1; i>0; i--)
if (!x[i].in(v)) {
x.size(n);
return home.ES_SUBSUMED(*this);
} else if (x[i].assigned()) {
assert(x[i].val() == v);
x[i] = x[--n];
}
x.size(n);
if (n == 1)
return ES_FAILED;
if (n == 2) {
GECODE_ME_CHECK(x[1].nq(home,v));
return home.ES_SUBSUMED(*this);
}
return ES_FIX;
}
}}}
// STATISTICS: int-prop
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::Rel::NaryNq::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition nq.hpp:151
Gecode::Int::Rel::NaryNq::NaryNq
NaryNq(Home home, ViewArray< View > &x)
Constructor for posting.
Definition nq.hpp:96
Gecode::Int::Rel::NaryNq::post
static ExecStatus post(Home home, ViewArray< View > &x)
Post propagator .
Definition nq.hpp:118
Gecode::Int::Rel::NaryNq::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nq.hpp:112
Gecode::Int::Rel::NaryNq::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nq.hpp:158
Gecode::Int::Rel::NaryNq::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition nq.hpp:101
Gecode::Int::Rel::Nq::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary).
Definition nq.hpp:75
Gecode::Int::Rel::Nq::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nq.hpp:81
Gecode::Int::Rel::Nq::post
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition nq.hpp:49
Gecode::Int::Rel::Nq::Nq
Nq(Space &home, Nq< V0, V1 > &p)
Constructor for cloning p.
Definition nq.hpp:64
Gecode::Int::Rel::Nq::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition nq.hpp:69
Gecode::MixBinaryPropagator< V0, PC_INT_VAL, V1, PC_INT_VAL >::x1
V1 x1
Definition pattern.hpp:209
Gecode::MixBinaryPropagator< V0, PC_INT_VAL, V1, PC_INT_VAL >::MixBinaryPropagator
MixBinaryPropagator(Space &home, MixBinaryPropagator &p)
Definition pattern.hpp:597
Gecode::MixBinaryPropagator< V0, PC_INT_VAL, V1, PC_INT_VAL >::x0
V0 x0
Definition pattern.hpp:207
Gecode::NaryPropagator::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition pattern.hpp:513
Gecode::NaryPropagator< View, PC_INT_VAL >::x
ViewArray< View > x
Definition pattern.hpp:145
Gecode::NaryPropagator< View, PC_INT_VAL >::NaryPropagator
NaryPropagator(Space &home, NaryPropagator &p)
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
Gecode::PropCost::unary
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition core.hpp:4813
Gecode::PropCost::linear
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition core.hpp:4796
Gecode::PropCost::LO
@ LO
Cheap.
Definition core.hpp:513
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::View
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::Space::ES_SUBSUMED
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
GECODE_ME_CHECK
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
Gecode::Int::Rel
Simple relation propagators.
Gecode::Int
Finite domain integers.
Gecode::Int::PC_INT_VAL
const Gecode::PropCond PC_INT_VAL
Propagate when a view becomes assigned (single value).
Definition var-type.hpp:82
Gecode
Gecode toplevel namespace
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_FIX
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
forceinline
#define forceinline
Definition config.hpp:187