spacenode.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Guido Tack, 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 Gist {
forceinline void
SpaceNode::setFlag(int flag, bool value) {
if (value)
nstatus |= 1<<(flag-1);
else
nstatus &= ~(1<<(flag-1));
}
forceinline bool
SpaceNode::getFlag(int flag) const {
return (nstatus & (1<<(flag-1))) != 0;
}
forceinline void
SpaceNode::setHasOpenChildren(bool b) {
setFlag(HASOPENCHILDREN, b);
}
forceinline void
SpaceNode::setHasFailedChildren(bool b) {
setFlag(HASFAILEDCHILDREN, b);
}
forceinline void
SpaceNode::setHasSolvedChildren(bool b) {
setFlag(HASSOLVEDCHILDREN, b);
}
forceinline void
SpaceNode::setStatus(NodeStatus s) {
nstatus &= ~( STATUSMASK );
nstatus |= s << 20;
}
forceinline NodeStatus
SpaceNode::getStatus(void) const {
return static_cast<NodeStatus>((nstatus & STATUSMASK) >> 20);
}
forceinline void
SpaceNode::setDistance(unsigned int d) {
if (d > MAXDISTANCE)
d = MAXDISTANCE;
nstatus &= ~( DISTANCEMASK );
nstatus |= d;
}
forceinline unsigned int
SpaceNode::getDistance(void) const {
return nstatus & DISTANCEMASK;
}
forceinline
SpaceNode::SpaceNode(int p)
: Node(p), copy(NULL), nstatus(0) {
choice = NULL;
setStatus(UNDETERMINED);
setHasSolvedChildren(false);
setHasFailedChildren(false);
}
forceinline Space*
SpaceNode::getSpace(NodeAllocator& na,
BestNode* curBest, int c_d, int a_d) {
acquireSpace(na,curBest,c_d,a_d);
Space* ret;
if (Support::marked(copy)) {
ret = static_cast<Space*>(Support::unmark(copy));
copy = NULL;
} else {
ret = copy->clone();
}
return ret;
}
forceinline const Space*
SpaceNode::getWorkingSpace(void) const {
assert(copy != NULL);
if (Support::marked(copy))
return static_cast<Space*>(Support::unmark(copy));
return copy;
}
forceinline void
SpaceNode::purge(const NodeAllocator& na) {
if (!isRoot() && (getStatus() != SOLVED || !na.bab())) {
// only delete copies from solutions if we are not in BAB
if (Support::marked(copy))
delete static_cast<Space*>(Support::unmark(copy));
else
delete copy;
copy = NULL;
}
}
forceinline bool
SpaceNode::isCurrentBest(BestNode* curBest) {
return curBest != NULL && curBest->s == this;
}
forceinline bool
SpaceNode::isOpen(void) {
return ((getStatus() == UNDETERMINED) ||
getFlag(HASOPENCHILDREN));
}
forceinline bool
SpaceNode::hasFailedChildren(void) {
return getFlag(HASFAILEDCHILDREN);
}
forceinline bool
SpaceNode::hasSolvedChildren(void) {
return getFlag(HASSOLVEDCHILDREN);
}
forceinline bool
SpaceNode::hasOpenChildren(void) {
return getFlag(HASOPENCHILDREN);
}
forceinline bool
SpaceNode::hasCopy(void) {
return copy != NULL;
}
forceinline bool
SpaceNode::hasWorkingSpace(void) {
return copy != NULL && Support::marked(copy);
}
forceinline int
SpaceNode::getAlternative(const NodeAllocator& na) const {
SpaceNode* p = getParent(na);
if (p == NULL)
return -1;
for (int i=static_cast<int>(p->getNumberOfChildren()); i--;)
if (p->getChild(na,i) == this)
return i;
GECODE_NEVER;
return -1;
}
forceinline const Choice*
SpaceNode::getChoice(void) {
return choice;
}
}}
// STATISTICS: gist-any
Gecode::Choice
Choice for performing commit
Definition core.hpp:1412
Gecode::Gist::BestNode
Static reference to the currently best space.
Definition spacenode.hh:80
Gecode::Gist::BestNode::s
SpaceNode * s
The currently best node found, or NULL.
Definition spacenode.hh:83
Gecode::Gist::NodeAllocatorBase::bab
bool bab(void) const
Return branch-and-bound flag.
Definition node.hpp:114
Gecode::Gist::Node::NodeAllocator
NodeAllocatorBase< VisualNode > NodeAllocator
Definition node.hh:143
Gecode::Gist::Node::getNumberOfChildren
unsigned int getNumberOfChildren(void) const
Return the number of children.
Definition node.hpp:214
Gecode::Gist::Node::Node
Node(int p, bool failed=false)
Construct node with parent p.
Definition node.hpp:175
Gecode::Gist::Node::getParent
int getParent(void) const
Return the parent.
Definition node.hpp:182
Gecode::Gist::Node::getChild
int getChild(int n) const
Return index of child no n.
Definition node.hpp:195
Gecode::Gist::Node::isRoot
bool isRoot(void) const
Check if this node is the root of a tree.
Definition node.hpp:211
Gecode::Gist::SpaceNode::copy
Space * copy
A copy used for recomputation, or NULL.
Definition spacenode.hh:96
Gecode::Gist::SpaceNode::hasCopy
bool hasCopy(void)
Return whether the node has a copy.
Definition spacenode.hpp:159
Gecode::Gist::SpaceNode::choice
const Choice * choice
Definition spacenode.hh:98
Gecode::Gist::SpaceNode::hasWorkingSpace
bool hasWorkingSpace(void)
Return whether the node has a working space.
Definition spacenode.hpp:164
Gecode::Gist::SpaceNode::getChoice
const Choice * getChoice(void)
Return choice of this node.
Definition spacenode.hpp:181
Gecode::Gist::SpaceNode::HASFAILEDCHILDREN
@ HASFAILEDCHILDREN
Definition spacenode.hh:123
Gecode::Gist::SpaceNode::HASOPENCHILDREN
@ HASOPENCHILDREN
Definition spacenode.hh:122
Gecode::Gist::SpaceNode::HASSOLVEDCHILDREN
@ HASSOLVEDCHILDREN
Definition spacenode.hh:124
Gecode::Gist::SpaceNode::getWorkingSpace
const Space * getWorkingSpace(void) const
Return working space (if present).
Definition spacenode.hpp:112
Gecode::Gist::SpaceNode::isCurrentBest
bool isCurrentBest(BestNode *curBest)
Return whether this node is the currently best solution.
Definition spacenode.hpp:133
Gecode::Gist::SpaceNode::getDistance
unsigned int getDistance(void) const
Return distance from copy.
Definition spacenode.hpp:84
Gecode::Gist::SpaceNode::nstatus
unsigned int nstatus
Status of the node.
Definition spacenode.hh:106
Gecode::Gist::SpaceNode::setStatus
void setStatus(NodeStatus s)
Set status to s.
Definition spacenode.hpp:65
Gecode::Gist::SpaceNode::hasFailedChildren
bool hasFailedChildren(void)
Return whether the subtree of this node has any failed children.
Definition spacenode.hpp:144
Gecode::Gist::SpaceNode::setFlag
void setFlag(int flag, bool value)
Set status flag.
Definition spacenode.hpp:37
Gecode::Gist::SpaceNode::acquireSpace
void acquireSpace(NodeAllocator &na, BestNode *curBest, int c_d, int a_d)
Acquire working space, either from parent or by recomputation.
Definition spacenode.cpp:156
Gecode::Gist::SpaceNode::hasSolvedChildren
bool hasSolvedChildren(void)
Return whether the subtree of this node has any solved children.
Definition spacenode.hpp:149
Gecode::Gist::SpaceNode::getAlternative
int getAlternative(const NodeAllocator &na) const
Return alternative number of this node.
Definition spacenode.hpp:169
Gecode::Gist::SpaceNode::isOpen
bool isOpen(void)
Return whether this node still has open children.
Definition spacenode.hpp:138
Gecode::Gist::SpaceNode::getStatus
NodeStatus getStatus(void) const
Return current status of the node.
Definition spacenode.hpp:71
Gecode::Gist::SpaceNode::getSpace
Space * getSpace(NodeAllocator &na, BestNode *curBest, int c_d, int a_d)
Return working space. Receiver must delete the space.
Definition spacenode.hpp:98
Gecode::Gist::SpaceNode::SpaceNode
SpaceNode(int p)
Construct node with parent p.
Definition spacenode.hpp:89
Gecode::Gist::SpaceNode::purge
void purge(const NodeAllocator &na)
Clear working space and copy (if present and this is not the root).
Definition spacenode.hpp:120
Gecode::Gist::SpaceNode::getFlag
bool getFlag(int flag) const
Return status flag.
Definition spacenode.hpp:45
Gecode::Gist::SpaceNode::setDistance
void setDistance(unsigned int d)
Set distance from copy.
Definition spacenode.hpp:76
Gecode::Gist::SpaceNode::hasOpenChildren
bool hasOpenChildren(void)
Return whether the subtree of this node has any open children.
Definition spacenode.hpp:154
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Gist
The Gecode Interactive Search Tool.
Gecode::Gist::NodeStatus
NodeStatus
Status of nodes in the search tree.
Definition spacenode.hh:44
Gecode::Gist::UNDETERMINED
@ UNDETERMINED
Node that has not been explored yet.
Definition spacenode.hh:48
Gecode::Gist::SOLVED
@ SOLVED
Node representing a solution.
Definition spacenode.hh:45
Gecode::Support::unmark
void * unmark(void *p)
Return unmarked pointer for a marked pointer p.
Definition marked-pointer.hpp:63
Gecode::Support::marked
bool marked(void *p)
Check whether p is marked.
Definition marked-pointer.hpp:54
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56