val.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, 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.
*
*/
namespace Gecode { namespace Int { namespace Distinct {
/*
* Eliminating singleton variables
*
*/
template<class View, bool complete>
ExecStatus
prop_val(Space& home, ViewArray<View>& x) {
assert(x.size() > 1);
int n = x.size();
Region r;
int* stack = r.alloc<int>(n);
int* c_v = &stack[0];
// c_n is the current number of values on stack
int c_n = 0;
// Collect all assigned variables on stack
for (int i=n; i--; )
if (x[i].assigned()) {
c_v[c_n++]=x[i].val(); x[i]=x[--n];
}
// The number of trips
int t = 0;
do {
t++;
if (!complete && (t > 16)) {
// Give up after sixteen iterations, but the values must be
// propagated first
// Maybe we are lucky in that this iteration does the trick...
ExecStatus es = ES_FIX;
while (c_n > 0) {
int v = c_v[--c_n];
// Check whether value is on stack only once
for (int i=0; i<c_n; i++)
if (c_v[i] == v)
goto failed;
// Tell and do not collect new values
for (int i=0; i<n; i++) {
ModEvent me = x[i].nq(home,v);
if (me_failed(me))
goto failed;
if (me == ME_INT_VAL)
es = ES_NOFIX;
}
}
x.size(n);
return es;
}
if (c_n > 31) {
// Many values, use full domain operation
IntSet d(&c_v[0],c_n);
// If the size s of d is different from the number of values,
// a value must have appeared multiply: failure
if (d.size() != static_cast<unsigned int>(c_n))
goto failed;
// We do not need the values on the stack any longer, reset
c_n = 0;
// Tell and collect new values
for (int i = n; i--; )
if ((d.min() <= x[i].max()) && (d.max() >= x[i].min())) {
IntSetRanges dr(d);
ModEvent me = x[i].minus_r(home,dr,false);
if (me_failed(me))
goto failed;
if (me == ME_INT_VAL) {
c_v[c_n++]=x[i].val(); x[i]=x[--n];
}
}
} else {
// Values for next iteration
int* n_v = &c_v[c_n];
// Stack top for the next iteration
int n_n = 0;
while (c_n > 0) {
int v = c_v[--c_n];
// Check whether value is not on current stack
for (int i=0; i<c_n; i++)
if (c_v[i] == v)
goto failed;
// Check whether value is not on next stack
for (int i=0; i<n_n; i++)
if (n_v[i] == v)
goto failed;
// Tell and collect new values
for (int i = n; i--; ) {
ModEvent me = x[i].nq(home,v);
if (me_failed(me))
goto failed;
if (me == ME_INT_VAL) {
n_v[n_n++]=x[i].val(); x[i]=x[--n];
}
}
}
c_v = n_v; c_n = n_n;
}
} while (c_n > 0);
x.size(n);
return ES_FIX;
failed:
x.size(0);
return ES_FAILED;
}
/*
* The propagator proper
*
*/
template<class View>
forceinline
Val<View>::Val(Home home, ViewArray<View>& x)
: NaryPropagator<View,PC_INT_VAL>(home,x) {}
template<class View>
forceinline
Val<View>::Val(Space& home, Val<View>& p)
: NaryPropagator<View,PC_INT_VAL>(home,p) {}
#ifdef GECODE_HAS_CBS
template<class View>
void
Val<View>::solndistrib(Space& home, Propagator::SendMarginal send) const {
cbsdistinct(home,this->id(),x,send);
}
template<class View>
void
Val<View>::domainsizesum(Propagator::InDecision in, unsigned int& size,
unsigned int& size_b) const {
cbssize(x,in,size,size_b);
}
#endif
template<class View>
Actor*
Val<View>::copy(Space& home) {
return new (home) Val<View>(home,*this);
}
template<class View>
ExecStatus
Val<View>::propagate(Space& home, const ModEventDelta&) {
GECODE_ES_CHECK((prop_val<View,true>(home,x)));
return (x.size() < 2) ? home.ES_SUBSUMED(*this) : ES_FIX;
}
template<class View>
ExecStatus
Val<View>::post(Home home, ViewArray<View>& x) {
if (x.size() == 2)
return Rel::Nq<View,View>::post(home,x[0],x[1]);
if (x.size() > 2)
(void) new (home) Val<View>(home,x);
return ES_OK;
}
}}}
// 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::IntSetRanges
Range iterator for integer sets.
Definition int.hh:292
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntSet::min
int min(int i) const
Return minimum of range at position i.
Definition int-set-1.hpp:152
Gecode::IntSet::max
int max(int i) const
Return maximum of range at position i.
Definition int-set-1.hpp:158
Gecode::IntSet::size
unsigned int size(void) const
Return size (cardinality) of set.
Definition int-set-1.hpp:198
Gecode::Int::Distinct::Val
Naive value distinct propagator.
Definition distinct.hh:64
Gecode::Int::Distinct::Val::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition val.hpp:172
Gecode::Int::Distinct::Val::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition val.hpp:178
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::Distinct::Val::Val
Val(Home home, ViewArray< View > &x)
Constructor for posting.
Definition val.hpp:147
Gecode::Int::Rel::Nq::post
static ExecStatus post(Home home, V0 x0, V1 x1)
Post propagator .
Definition nq.hpp:49
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::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Val
Gecode::View
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1156
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_failed
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
GECODE_ES_CHECK
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
Gecode::Int::Distinct
Distinct propagators
Gecode::Int::Distinct::prop_val
ExecStatus prop_val(Space &home, ViewArray< View > &)
Eliminate singletons by naive value propagation.
Definition val.hpp:42
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::Int::ME_INT_VAL
const Gecode::ModEvent ME_INT_VAL
Domain operation has resulted in a value (assigned variable).
Definition var-type.hpp:56
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
Gecode::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
forceinline
#define forceinline
Definition config.hpp:187