nogoods.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>
*
* Copyright:
* Christian Schulte, 2013
*
* 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/search/nogoods.hh>
namespace Gecode { namespace Search {
forceinline NGL*
disposenext(NGL* ngl, Space& home, Propagator& p, bool c) {
NGL* n = ngl->next();
if (c)
ngl->cancel(home,p);
home.rfree(ngl,ngl->dispose(home));
return n;
}
void
NoNGL::subscribe(Space&, Propagator&) {
GECODE_NEVER;
}
void
NoNGL::cancel(Space&, Propagator&) {
GECODE_NEVER;
}
void
NoNGL::reschedule(Space&, Propagator&) {
GECODE_NEVER;
}
NGL::Status
NoNGL::status(const Space&) const {
GECODE_NEVER;
return NGL::NONE;
}
ExecStatus
NoNGL::prune(Space&) {
GECODE_NEVER;
return ES_OK;
}
NGL*
NoNGL::copy(Space&) {
GECODE_NEVER;
return NULL;
}
Actor*
NoGoodsProp::copy(Space& home) {
return new (home) NoGoodsProp(home,*this);
}
PropCost
NoGoodsProp::cost(const Space&, const ModEventDelta&) const {
return PropCost::linear(PropCost::LO,n);
}
void
NoGoodsProp::reschedule(Space& home) {
root->reschedule(home,*this);
NGL* l = root->next();
while ((l != NULL) && l->leaf()) {
l->reschedule(home,*this);
l = l->next();
}
if (l != NULL)
l->reschedule(home,*this);
}
ExecStatus
NoGoodsProp::propagate(Space& home, const ModEventDelta&) {
restart:
// Start with checking the first literal
switch (root->status(home)) {
case NGL::FAILED:
// All no-goods are dead, let dispose() clean up
return home.ES_SUBSUMED(*this);
case NGL::SUBSUMED:
{
NGL* l = disposenext(root,home,*this,true); n--;
// Prune leaf-literals
while ((l != NULL) && l->leaf()) {
l->cancel(home,*this); n--;
GECODE_ES_CHECK(l->prune(home));
l = disposenext(l,home,*this,false);
}
root = l;
// Is there anything left?
if (l == NULL)
return home.ES_SUBSUMED(*this);
// Skip literal that already has a subscription
l = l->next();
// Create subscriptions for leafs
while ((l != NULL) && l->leaf()) {
l->subscribe(home,*this); n++;
l = l->next();
}
// Create subscription for possible non-leaf literal
if (l != NULL) {
l->subscribe(home,*this); n++;
}
goto restart;
}
case NGL::NONE:
break;
default: GECODE_NEVER;
}
{
NGL* p = root;
NGL* l = p->next();
// Check the leaves
while ((l != NULL) && l->leaf()) {
switch (l->status(home)) {
case NGL::SUBSUMED:
l = disposenext(l,home,*this,true); n--;
p->next(l);
GECODE_ES_CHECK(root->prune(home));
if (root->status(home) == NGL::FAILED)
return home.ES_SUBSUMED(*this);
break;
case NGL::FAILED:
l = disposenext(l,home,*this,true); n--;
p->next(l);
break;
case NGL::NONE:
p = l; l = l->next();
break;
default: GECODE_NEVER;
}
}
// Check the next subtree
if (l != NULL) {
switch (l->status(home)) {
case NGL::FAILED:
(void) disposenext(l,home,*this,true); n--;
// Prune entire subtree
p->next(NULL);
break;
case NGL::SUBSUMED:
{
// Unlink node
l = disposenext(l,home,*this,true); n--;
p->next(l);
// Create subscriptions
while ((l != NULL) && l->leaf()) {
l->subscribe(home,*this); n++;
l = l->next();
}
if (l != NULL) {
l->subscribe(home,*this); n++;
}
}
break;
case NGL::NONE:
break;
default: GECODE_NEVER;
}
}
}
return ES_NOFIX;
}
size_t
NoGoodsProp::dispose(Space& home) {
if (home.failed()) {
// This will be executed when one ngl returned true for notice()
NGL* l = root;
while (l != NULL) {
NGL* t = l->next();
(void) l->dispose(home);
l = t;
}
} else if (root != NULL) {
// This will be executed for subsumption
NGL* l = disposenext(root,home,*this,true);
while ((l != NULL) && l->leaf())
l = disposenext(l,home,*this,true);
if (l != NULL)
l = disposenext(l,home,*this,true);
while (l != NULL)
l = disposenext(l,home,*this,false);
}
home.ignore(*this,AP_DISPOSE,true);
(void) Propagator::dispose(home);
return sizeof(*this);
}
}}
// STATISTICS: search-other
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3252
Gecode::NGL
No-good literal recorded during search.
Definition core.hpp:1340
Gecode::NGL::leaf
bool leaf(void) const
Test whether literal is a leaf.
Definition core.hpp:3789
Gecode::NGL::prune
virtual ExecStatus prune(Space &home)=0
Propagate the negation of the no-good literal.
Gecode::NGL::cancel
virtual void cancel(Space &home, Propagator &p)=0
Cancel propagator p from all views of the no-good literal.
Gecode::NGL::subscribe
virtual void subscribe(Space &home, Propagator &p)=0
Subscribe propagator p to all views of the no-good literal.
Gecode::NGL::status
virtual NGL::Status status(const Space &home) const =0
Test the status of the no-good literal.
Gecode::NGL::Status
Status
The status of a no-good literal.
Definition core.hpp:1346
Gecode::NGL::SUBSUMED
@ SUBSUMED
The literal is subsumed.
Definition core.hpp:1348
Gecode::NGL::FAILED
@ FAILED
The literal is failed.
Definition core.hpp:1347
Gecode::NGL::NONE
@ NONE
The literal is neither failed nor subsumed.
Definition core.hpp:1349
Gecode::NGL::reschedule
virtual void reschedule(Space &home, Propagator &p)=0
Schedule propagator p for all views of the no-good literal.
Gecode::NGL::next
NGL * next(void) const
Return pointer to next literal.
Definition core.hpp:3793
Gecode::NGL::dispose
virtual size_t dispose(Space &home)
Dispose.
Definition core.hpp:3821
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
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
Base-class for propagators.
Definition core.hpp:1064
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Search::NoGoodsProp::n
unsigned int n
Number of no-good literals with subscriptions.
Definition nogoods.hh:70
Gecode::Search::NoGoodsProp::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition nogoods.cpp:196
Gecode::Search::NoGoodsProp::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition nogoods.cpp:100
Gecode::Search::NoGoodsProp::copy
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition nogoods.cpp:77
Gecode::Search::NoGoodsProp::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Const function (defined as low unary).
Definition nogoods.cpp:82
Gecode::Search::NoGoodsProp::NoGoodsProp
NoGoodsProp(Space &home, NGL *root)
Constructor for creation.
Definition nogoods.hpp:50
Gecode::Search::NoGoodsProp::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition nogoods.cpp:87
Gecode::Search::NoGoodsProp::root
NGL * root
Root of no-good literal tree.
Definition nogoods.hh:68
Gecode::Search::NoNGL::status
virtual NGL::Status status(const Space &home) const
Test the status of the no-good literal.
Definition nogoods.cpp:61
Gecode::Search::NoNGL::prune
virtual ExecStatus prune(Space &home)
Propagate the negation of the no-good literal.
Definition nogoods.cpp:66
Gecode::Search::NoNGL::copy
virtual NGL * copy(Space &home)
Create copy.
Definition nogoods.cpp:71
Gecode::Search::NoNGL::cancel
virtual void cancel(Space &home, Propagator &p)
Cancel propagator p from all views of the no-good literal.
Definition nogoods.cpp:53
Gecode::Search::NoNGL::reschedule
virtual void reschedule(Space &home, Propagator &p)
Schedule propagator p for all views of the no-good literal.
Definition nogoods.cpp:57
Gecode::Search::NoNGL::subscribe
virtual void subscribe(Space &home, Propagator &p)
Subscribe propagator p to all views of the no-good literal.
Definition nogoods.cpp:49
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::rfree
void rfree(void *p, size_t s)
Free memory previously allocated with alloc (might be reused later).
Definition core.hpp:2803
Gecode::Space::ES_SUBSUMED
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
Gecode::Space::failed
bool failed(void) const
Check whether space is failed.
Definition core.hpp:4044
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::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode::Search
Search engines
Gecode::Search::disposenext
NGL * disposenext(NGL *ngl, Space &home, Propagator &p, bool c)
Help function to cancel and dispose a no-good literal.
Definition nogoods.cpp:40
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_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
nogoods.hh
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56