/* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2003 * * Last modified: * $Date: 2006-10-23 16:16:09 +0200 (Mon, 23 Oct 2006) $ by $Author: schulte $ * $Revision: 3768 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ #include #include "gecode/support/dynamic-array.hh" #include "gecode/support/static-stack.hh" #include "gecode/iter.hh" namespace Gecode { namespace Int { namespace Distinct { /** * \brief Class for combining two pointers with a flag * * When one pointer is given, this other can be retrieved. * */ template class CombPtrFlag { private: /// Store pointer and flag ptrdiff_t cpf; public: /// Initialize with pointer \a p1 and \a p2 CombPtrFlag(T* p1, T* p2); /// Initialize with pointer \a p1 and \a p2 void init(T* p1, T* p2); /// Return the other pointer when \a p is given T* ptr(T* p) const; /// Check whether flag is set int is_set(void) const; /// Set flag void set(void); /// Clear flag void unset(void); }; /** * \brief Bidirectional links for both edges and anchors in nodes of view-value graph * */ class BiLink { private: BiLink* _prev; BiLink* _next; public: BiLink(void); BiLink* prev(void) const; void prev(BiLink*); BiLink* next(void) const; void next(BiLink*); void add(BiLink*); void unlink(void); void mark(void); bool marked(void) const; bool empty(void) const; }; template class Edge; /** * \brief Base-class for nodes (both view and value nodes) * * Note: the obvious ill-design to have also nodes and edges * parametric wrt View is because the right design (having template * function members) gets miscompiled (and actually not even compiled * with some C++ compilers). Duh! * */ template class Node : public BiLink { public: unsigned int pre, low, comp; Node(void); Edge* edge_fst(void) const; Edge* edge_lst(void) const; static void* operator new(size_t, void*); }; /** * \brief Value nodes in view-value graph * */ template class ValNode : public Node { protected: const int _val; Edge* _matching; public: ValNode(int); int val(void) const; void matching(Edge*); Edge* matching(void) const; }; /** * \brief View nodes in view-value graph * */ template class ViewNode : public Node { protected: Edge* _val_edges; View _view; public: ViewNode(View); Edge* val_edges(void) const; Edge** val_edges_ref(void); View view(void) const; }; /** * \brief Edges in view-value graph * */ template class Edge : public BiLink { protected: Edge* _next_edge; CombPtrFlag > sd; public: Edge(Node*, Node*); Node* dst(Node*) const; ViewNode* view(ValNode*) const; ValNode* val(ViewNode*) const; bool used(Node*) const; void use(void); void free(void); void revert(Node*); Edge* next_edge(void) const; Edge** next_edge_ref(void); Edge* next(void) const; static void* operator new(size_t, void*); }; }}} #include "gecode/int/distinct/combptr.icc" #include "gecode/int/distinct/bilink.icc" #include "gecode/int/distinct/node.icc" #include "gecode/int/distinct/edge.icc" namespace Gecode { namespace Int { namespace Distinct { /** * \brief View-value graph for propagating domain-consistent distinct * */ template class DomCtrl::ViewValGraph { protected: ViewNode** view; int n_view; ValNode** val; int n_val; char* mem; unsigned int count; unsigned int cnt0; unsigned int cnt1; Support::StaticStack*> n_s; public: ViewValGraph(int, View*, const int*, int, unsigned int); ViewValGraph(int, View*, int, unsigned int, int); ~ViewValGraph(void); size_t size; static ViewValGraph* init(int,View*); bool initial_match(void); void mark(void); void tell(Space*); bool overflow(void) const; bool sync(void); protected: bool search_match(ViewNode*); bool match(ViewNode*); void scc(Node*); public: // Memory management static void* operator new(size_t); static void operator delete(void*); }; template forceinline DomCtrl::ViewValGraph::ViewValGraph(int n_view0, View* x, int n_val0, unsigned int n_edges, int min) : n_view(n_view0), n_val(n_val0), count(0), cnt0(0), cnt1(0), n_s(n_view+n_val) { size_t edge_size = sizeof(Edge) * n_edges; size_t views_size = sizeof(ViewNode) * n_view; size_t view_size = sizeof(ViewNode*) * n_view; size_t vals_size = sizeof(ValNode) * n_val; size_t val_size = sizeof(ValNode*) * n_val; size_t size = edge_size + views_size + view_size + vals_size + val_size; mem = reinterpret_cast(Memory::malloc(size)); Edge* edges = reinterpret_cast*>(mem); ViewNode* view_nodes = reinterpret_cast*> (mem+edge_size); ValNode* val_nodes = reinterpret_cast*> (mem+edge_size+views_size); view = reinterpret_cast**> (mem+edge_size+views_size+vals_size); val = reinterpret_cast**> (mem+edge_size+views_size+vals_size+view_size); // Init value nodes { int v = min+n_val; for (int i = n_val; i--; ) val[i] = new (val_nodes + i) ValNode(--v); } // Init view nodes and edges for (int i = n_view; i--; ) { view[i] = new (view_nodes + i) ViewNode(x[i]); Edge** edge_p = view[i]->val_edges_ref(); ViewValues x_i(x[i]); while (x_i()) { *edge_p = new (edges++) Edge(val_nodes+x_i.val()-min, view_nodes+i); edge_p = (*edge_p)->next_edge_ref(); ++x_i; } *edge_p = NULL; } } template forceinline DomCtrl::ViewValGraph::ViewValGraph(int n_view0, View* x, const int* val_inf, int n_val0, unsigned int n_edges) : n_view(n_view0), n_val(n_val0), count(0), cnt0(0), cnt1(0), n_s(n_view+n_val) { size_t edge_size = sizeof(Edge) * n_edges; size_t views_size = sizeof(ViewNode) * n_view; size_t view_size = sizeof(ViewNode*) * n_view; size_t vals_size = sizeof(ValNode) * n_val; size_t val_size = sizeof(ValNode*) * n_val; size_t size = edge_size + views_size + view_size + vals_size + val_size; mem = reinterpret_cast(Memory::malloc(size)); Edge* edges = reinterpret_cast*>(mem); ViewNode* view_nodes = reinterpret_cast*> (mem+edge_size); ValNode* val_nodes = reinterpret_cast*> (mem+edge_size+views_size); view = reinterpret_cast**> (mem+edge_size+views_size+vals_size); val = reinterpret_cast**> (mem+edge_size+views_size+vals_size+view_size); // Init value nodes for (int i = n_val; i--; ) val[i] = new (val_nodes + i) ValNode(val_inf[i]); // Init view nodes for (int i = n_view; i--; ) { view[i] = new (view_nodes + i) ViewNode(x[i]); Edge** edge_p = view[i]->val_edges_ref(); ViewValues x_i(x[i]); int j = 0; while (x_i()) { while (val_inf[j] < x_i.val()) j++; *edge_p = new (edges++) Edge(val_nodes+j,view_nodes+i); edge_p = (*edge_p)->next_edge_ref(); ++x_i; } *edge_p = NULL; } } template typename DomCtrl::ViewValGraph* DomCtrl::ViewValGraph::init(int n_view, View* x) { // Find value information for construction of view value graph int min = x[n_view-1].min(); int max = x[n_view-1].max(); unsigned int size = x[n_view-1].size(); for (int i=n_view-1; i--; ) { size += x[i].size(); min = std::min(min,x[i].min()); max = std::max(max,x[i].max()); } unsigned int width = max-min+1; // Definitly not enough values if (width < static_cast(n_view)) return NULL; // Test whether the values are dense or sparse if (static_cast(n_view)*2 >= width) return new ViewValGraph(n_view,x,static_cast(width),size,min); Support::DynamicArray val_inf(64); // Compute set of values int n_val = 0; GECODE_AUTOARRAY(ViewRanges,x_r,n_view); for (int i = n_view; i--; ) x_r[i].init(x[i]); Iter::Ranges::NaryUnion > xu(x_r, n_view); // Of course, the union of non-empty iterators is non-empty assert(xu()); // Fill array of values do { for (int v = xu.min(); v <= xu.max(); v++) val_inf[n_val++] = v; ++xu; } while (xu()); if (n_val < n_view) return NULL; return new ViewValGraph(n_view,x,val_inf,n_val,size); } template bool DomCtrl::ViewValGraph::search_match(ViewNode* x) { x->comp = count; // Try to find matching edge cheaply: is there a free edge around? { Edge* e = x->val_edges(); // This holds true as domains are never empty assert(e != NULL); do { if (!e->val(x)->matching()) { e->revert(x); e->val(x)->matching(e); return true; } e = e->next_edge(); } while (e != NULL); } // No, find matching edge by augmenting path method { Edge* e = x->val_edges(); do { ValNode* n = e->val(x); ViewNode* y = n->matching()->view(n); if ((y->comp < count) && search_match(y)) { n->matching()->revert(n); e->revert(x); n->matching(e); return true; } e = e->next_edge(); } while (e != NULL); } return false; } template forceinline bool DomCtrl::ViewValGraph::match(ViewNode* x) { assert(x->edge_fst() == x->edge_lst()); count++; return search_match(x); } template bool DomCtrl::ViewValGraph::initial_match(void) { for (int i = n_view; i--; ) if (!match(view[i])) return false; return true; } template void DomCtrl::ViewValGraph::scc(Node* w) { w->pre = cnt0; w->low = cnt0; unsigned int min = cnt0++; n_s.push(w); for (Edge* e = w->edge_fst(); e != w->edge_lst(); e = e->next()) { Node* v = e->dst(w); if (v->pre < count) scc(v); if (v->low < min) min = v->low; } if (min < w->low) { w->low = min; return; } do { Node* v = n_s.pop(); v->comp = cnt1; v->low = UINT_MAX; } while (n_s.last() != w); cnt1++; } template forceinline void DomCtrl::ViewValGraph::mark(void) { // Marks all edges as used that are on simple paths in the graph // that start from a free (unmatched node) by depth-first-search n_s.reset(); // Insert all free nodes: they can be only value nodes as we // have a maximum matching covering all view nodes count++; for (int i = n_val; i--; ) if (!val[i]->matching()) // Is it orphaned? if (val[i]->empty()) { val[i] = val[--n_val]; } else { val[i]->comp = count; n_s.push(val[i]); } // Invariant: only value nodes are on the stack! while (!n_s.empty()) { ValNode* n = static_cast*>(n_s.pop()); for (Edge* e = n->edge_fst(); e != n->edge_lst(); e = e->next()) { // Get the value node e->use(); ViewNode* x = e->view(n); if (x->comp < count) { x->comp = count; assert(x->edge_fst()->next() == x->edge_lst()); ValNode* m = x->edge_fst()->val(x); x->edge_fst()->use(); if (m->comp < count) { m->comp = count; n_s.push(m); } } } } count++; cnt0 = count; cnt1 = count; n_s.reset(); for (int i = n_view; i--; ) if (view[i]->comp < count) scc(view[i]); count = cnt0+1; } template forceinline void DomCtrl::ViewValGraph::tell(Space* home) { // Tell constraints and also eliminate nodes and edges for (int i = n_view; i--; ) if (!view[i]->edge_fst()->used(view[i])) { view[i]->view().eq(home,view[i]->edge_fst()->val(view[i])->val()); view[i]->edge_fst()->val(view[i])->matching(NULL); view[i] = view[--n_view]; } else { for (Edge* e = view[i]->val_edges(); e!=NULL; e = e->next_edge()) if (!e->used(view[i])) { view[i]->view().nq(home,e->val(view[i])->val()); } } } template forceinline bool DomCtrl::ViewValGraph::overflow(void) const { return count > (UINT_MAX >> 1); } template bool DomCtrl::ViewValGraph::sync(void) { // Stack for view nodes to be rematched GECODE_AUTOARRAY(ViewNode*,re,n_view); int n_re = 0; // Synchronize nodes for (int i = n_view; i--; ) { ViewNode* x = view[i]; if (x->view().assigned()) { x->edge_fst()->val(x)->matching(NULL); for (Edge* e = x->val_edges(); e != NULL; e = e->next_edge()) e->unlink(); view[i] = view[--n_view]; } else { ViewRanges r(x->view()); Edge* m = x->edge_fst(); // Matching edge Edge** p = x->val_edges_ref(); Edge* e = *p; do { while (e->val(x)->val() < r.min()) { // Skip edge e->unlink(); e->mark(); e = e->next_edge(); *p = e; } assert(r.min() == e->val(x)->val()); // This edges must be kept for (unsigned int i=r.width(); i--; ) { e->free(); p = e->next_edge_ref(); e = e->next_edge(); } ++r; } while (r()); *p = NULL; while (e != NULL) { e->unlink(); e->mark(); e = e->next_edge(); } if (m->marked()) { // Matching has been deleted! m->val(x)->matching(NULL); re[n_re++] = x; } } } while (n_re--) if (!match(re[n_re])) return false; return true; } template forceinline DomCtrl::ViewValGraph::~ViewValGraph(void) { Memory::free(mem); } template forceinline void* DomCtrl::ViewValGraph::operator new(size_t s) { return Memory::malloc(s); } template forceinline void DomCtrl::ViewValGraph::operator delete(void* p) { Memory::free(p); } /* * The propagation controller * */ template forceinline DomCtrl::DomCtrl(void) : vvg(NULL) {} template forceinline bool DomCtrl::available(void) { if ((vvg != NULL) && vvg->overflow()) { delete vvg; vvg = NULL; } return vvg != NULL; } template forceinline ExecStatus DomCtrl::init(int n, View* x) { assert(vvg == NULL); vvg = ViewValGraph::init(n,x); if ((vvg == NULL) || !vvg->initial_match()) return ES_FAILED; return ES_OK; } template forceinline ExecStatus DomCtrl::sync(void) { return vvg->sync() ? ES_OK : ES_FAILED; } template forceinline void DomCtrl::propagate(Space* home) { vvg->mark(); vvg->tell(home); } template forceinline void DomCtrl::flush(void) { delete vvg; vvg = NULL; } template forceinline size_t DomCtrl::size(void) const { return (vvg != NULL) ? vvg->size : 0; } template forceinline void DomCtrl::dispose(void) { delete vvg; } /* * The propagator proper * */ template forceinline Dom::Dom(Space* home, ViewArray& x) : NaryPropagator(home,x,true) {} template ExecStatus Dom::post(Space* home, ViewArray& x) { if (x.size() == 2) return Rel::Nq::post(home,x[0],x[1]); if (x.size() == 3) return TerDom::post(home,x[0],x[1],x[2]); if (x.size() > 3) { // Do bounds propagation to make view-value graph smaller if (prop_bnd(home,x,x.size()) == ES_FAILED) return ES_FAILED; (void) new (home) Dom(home,x); } return ES_OK; } template forceinline Dom::Dom(Space* home, bool share, Dom& p) : NaryPropagator(home,share,p) {} template void Dom::flush(void) { dc.flush(); } template size_t Dom::size(void) const { return dc.size(); } template PropCost Dom::cost(void) const { return cost_lo(x.size(), (View::pme(this) == ME_INT_VAL) ? PC_LINEAR_LO : PC_CUBIC_LO); } template Actor* Dom::copy(Space* home, bool share) { return new (home) Dom(home,share,*this); } template ExecStatus Dom::propagate(Space* home) { if (View::pme(this) == ME_INT_VAL) { ExecStatus es = prop_val(home,x); if ((es == ES_FAILED) || (es == ES_SUBSUMED)) return es; if (es == ES_FIX) return this->ES_FIX_PARTIAL(View::pme(ME_INT_DOM)); if (prop_bnd(home,x,x.size()) == ES_FAILED) return ES_FAILED; es = prop_val(home,x); if ((es == ES_FAILED) || (es == ES_SUBSUMED)) return es; return this->ES_FIX_PARTIAL(View::pme(ME_INT_DOM)); } if (x.size() == 2) { GECODE_ES_CHECK(Rel::Nq::post(home,x[0],x[1])); return ES_SUBSUMED; } if (x.size() == 3) { GECODE_ES_CHECK(TerDom::post(home,x[0],x[1],x[2])); return ES_SUBSUMED; } if (dc.available()) { GECODE_ES_CHECK(dc.sync()); } else { GECODE_ES_CHECK(dc.init(x.size(),&x[0])); } dc.propagate(home); return ES_FIX; } template size_t Dom::dispose(Space* home) { dc.dispose(); (void) NaryPropagator::dispose(home); return sizeof(*this); } }}} // STATISTICS: int-prop