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>
*
* Contributing authors:
* Mikael Lagerkvist <lagerkvist@gecode.org>
*
* Copyright:
* Christian Schulte, 2006
* Mikael Lagerkvist, 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.
*
*/
namespace Gecode { namespace Int { namespace Channel {
template<class View>
class ValInfo {
public:
View view;
bool a;
void init(View x, int n);
void update(Space& home, ValInfo<View>& vi);
bool doval(void) const;
bool dodom(void) const;
void assigned(void);
void removed(int i);
void done(void);
};
template<class View>
forceinline void
ValInfo<View>::init(View x, int) {
view = x; a = false;
}
template<class View>
forceinline void
ValInfo<View>::update(Space& home, ValInfo<View>& vi) {
view.update(home,vi.view); a = vi.a;
}
template<class View>
forceinline bool
ValInfo<View>::doval(void) const {
return !a && view.assigned();
}
template<class View>
forceinline bool
ValInfo<View>::dodom(void) const {
return false;
}
template<class View>
forceinline void
ValInfo<View>::assigned(void) {
a = true;
}
template<class View>
forceinline void
ValInfo<View>::removed(int) {}
template<class View>
forceinline void
ValInfo<View>::done(void) {}
// Propagate assigned views for x
template<class View, class Offset, class Info>
ExecStatus
doprop_val(Space& home, int n, Info* x, Offset& ox,
Info* y, Offset& oy,
int& n_na, ProcessStack& xa, ProcessStack& ya) {
do {
int i = xa.pop();
int j = ox(x[i].view).val();
// Assign the y variable to i (or test if already assigned!)
{
ModEvent me = oy(y[j].view).eq(home,i);
if (me_failed(me)) {
return ES_FAILED;
}
// Record that y[j] has been assigned and must be propagated
if (me_modified(me))
ya.push(j);
}
// Prune the value j from all x variables
for (int k=0; k<i; k++) {
ModEvent me = ox(x[k].view).nq(home,j);
if (me_failed(me)) {
return ES_FAILED;
}
if (me_modified(me)) {
if (me == ME_INT_VAL) {
// Record that x[k] has been assigned and must be propagated
xa.push(k);
} else {
// One value has been removed and this removal does not need
// to be propagated again: after all y[j] = i, so it holds
// that y[j] != k.
x[k].removed(j);
}
}
}
// The same for the other views
for (int k=i+1; k<n; k++) {
ModEvent me = ox(x[k].view).nq(home,j);
if (me_failed(me)) {
return ES_FAILED;
}
if (me_modified(me)) {
if (me == ME_INT_VAL) {
// Record that x[k] has been assigned and must be propagated
xa.push(k);
} else {
// One value has been removed and this removal does not need
// to be propagated again: after all y[j] = i, so it holds
// that y[j] != k.
x[k].removed(j);
}
}
}
x[i].assigned(); n_na--;
} while (!xa.empty());
return ES_OK;
}
// Just do a test whether a call to the routine is necessary
template<class View, class Offset, class Info>
forceinline ExecStatus
prop_val(Space& home, int n, Info* x, Offset& ox, Info* y, Offset& oy,
int& n_na, ProcessStack& xa, ProcessStack& ya) {
if (xa.empty())
return ES_OK;
return doprop_val<View,Offset,Info>(home,n,x,ox,y,oy,n_na,xa,ya);
}
/*
* The actual propagator
*
*/
template<class View, class Offset, bool shared>
forceinline
Val<View,Offset,shared>::Val(Home home, int n, ValInfo<View>* xy,
Offset& ox, Offset& oy)
: Base<ValInfo<View>,Offset,PC_INT_VAL>(home,n,xy,ox,oy) {}
template<class View, class Offset, bool shared>
forceinline
Val<View,Offset,shared>::Val(Space& home, Val<View,Offset,shared>& p)
: Base<ValInfo<View>,Offset,PC_INT_VAL>(home,p) {}
template<class View, class Offset, bool shared>
Actor*
Val<View,Offset,shared>::copy(Space& home) {
return new (home) Val<View,Offset,shared>(home,*this);
}
template<class View, class Offset, bool shared>
ExecStatus
Val<View,Offset,shared>::propagate(Space& home, const ModEventDelta&) {
Region r;
ProcessStack xa(r,n);
ProcessStack ya(r,n);
ValInfo<View>* x = xy;
ValInfo<View>* y = xy+n;
// Scan x and y for assigned but not yet propagated views
for (int i=0; i<n; i++) {
if (x[i].doval()) xa.push(i);
if (y[i].doval()) ya.push(i);
}
do {
// Propagate assigned views for x
GECODE_ES_CHECK((prop_val<View,Offset,ValInfo<View> >
(home,n,x,ox,y,oy,n_na,xa,ya)));
// Propagate assigned views for y
GECODE_ES_CHECK((prop_val<View,Offset,ValInfo<View> >
(home,n,y,oy,x,ox,n_na,ya,xa)));
assert(ya.empty());
} while (!xa.empty());
if (n_na == 0)
return home.ES_SUBSUMED(*this);
return shared ? ES_NOFIX : ES_FIX;
}
template<class View, class Offset, bool shared>
ExecStatus
Val<View,Offset,shared>::post(Home home, int n, ValInfo<View>* xy,
Offset& ox, Offset& oy) {
assert(n > 0);
if (n == 1) {
GECODE_ME_CHECK(ox(xy[0].view).eq(home,0));
GECODE_ME_CHECK(oy(xy[1].view).eq(home,0));
return ES_OK;
}
for (int i=0; i<n; i++) {
GECODE_ME_CHECK(ox(xy[i ].view).gq(home,0));
GECODE_ME_CHECK(ox(xy[i ].view).le(home,n));
GECODE_ME_CHECK(oy(xy[i+n].view).gq(home,0));
GECODE_ME_CHECK(oy(xy[i+n].view).le(home,n));
}
(void) new (home) Val<View,Offset,shared>(home,n,xy,ox,oy);
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::Int::Channel::Base
Base-class for channel propagators.
Definition channel.hh:55
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::Base
Base(Space &home, Base< ValInfo< View >, Offset, pc > &p)
Definition base.hpp:47
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::oy
Offset oy
Definition channel.hh:64
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::n
int n
Definition channel.hh:58
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::ox
Offset ox
Definition channel.hh:62
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::n_na
int n_na
Definition channel.hh:60
Gecode::Int::Channel::Base< ValInfo< View >, Offset, PC_INT_VAL >::xy
ValInfo< View > * xy
Definition channel.hh:66
Gecode::Int::Channel::ValInfo
Combine view with information for value propagation.
Definition val.hpp:45
Gecode::Int::Channel::ValInfo::done
void done(void)
Update the cardinality and bounds information after pruning.
Definition val.hpp:103
Gecode::Int::Channel::ValInfo::a
bool a
Whether it has been propagated that view is assigned.
Definition val.hpp:50
Gecode::Int::Channel::ValInfo::view
View view
The view.
Definition val.hpp:48
Gecode::Int::Channel::ValInfo::init
void init(View x, int n)
Initialize.
Definition val.hpp:69
Gecode::Int::Channel::ValInfo::assigned
void assigned(void)
Record that view got assigned.
Definition val.hpp:93
Gecode::Int::Channel::ValInfo::removed
void removed(int i)
Record that one value got removed.
Definition val.hpp:99
Gecode::Int::Channel::ValInfo::dodom
bool dodom(void) const
Check whether propagation for domain is to be done.
Definition val.hpp:87
Gecode::Int::Channel::ValInfo::doval
bool doval(void) const
Check whether propagation for assignment is to be done.
Definition val.hpp:81
Gecode::Int::Channel::ValInfo::update
void update(Space &home, ValInfo< View > &vi)
Update during cloning.
Definition val.hpp:75
Gecode::Int::Channel::Val
Naive channel propagator.
Definition channel.hh:97
Gecode::Int::Channel::Val::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition val.hpp:193
Gecode::Int::Channel::Val::post
static ExecStatus post(Home home, int n, ValInfo< View > *xy, Offset &ox, Offset &oy)
Post propagator for channeling.
Definition val.hpp:230
Gecode::Int::Channel::Val::Val
Val(Space &home, Val &p)
Constructor for cloning p.
Gecode::Int::Channel::Val::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition val.hpp:199
Gecode::Int::Offset
Converter with fixed offset.
Definition view.hpp:650
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::Support::StaticStack::push
void push(const T &x)
Push element x on top of stack.
Definition static-stack.hpp:137
Gecode::Support::StaticStack::pop
T pop(void)
Pop topmost element from stack and return it.
Definition static-stack.hpp:116
Gecode::Support::StaticStack::empty
bool empty(void) const
Test whether stack is empty.
Definition static-stack.hpp:98
Gecode::View
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::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::me_modified
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition modevent.hpp:59
Gecode::Int::Channel
Channel propagators
Gecode::Int::Channel::prop_val
ExecStatus prop_val(Space &home, int n, Info *x, Offset &ox, Info *y, Offset &oy, int &n_na, ProcessStack &xa, ProcessStack &ya)
Definition val.hpp:169
Gecode::Int::Channel::ProcessStack
Support::StaticStack< int, Region > ProcessStack
Processing stack.
Definition channel.hh:48
Gecode::Int::Channel::doprop_val
ExecStatus doprop_val(Space &home, int n, Info *x, Offset &ox, Info *y, Offset &oy, int &n_na, ProcessStack &xa, ProcessStack &ya)
Definition val.hpp:109
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::shared
bool shared(ViewArray< ViewX > x, ViewArray< ViewY > y)
Definition array.hpp:1466
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
forceinline
#define forceinline
Definition config.hpp:187