/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2003 * * Last modified: * $Date: 2008-01-19 13:19:23 +0100 (Sat, 19 Jan 2008) $ by $Author: schulte $ * $Revision: 5916 $ * * 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 Search { /* * Node for recomputation * */ forceinline ReCoNode::ReCoNode(Space* s, Space* c) : _space(c), _alt(0), _desc(s->description()) {} forceinline Space* ReCoNode::space(void) const { return _space; } forceinline void ReCoNode::space(Space* s) { _space = s; } forceinline unsigned int ReCoNode::alt(void) const { return _alt; } forceinline bool ReCoNode::rightmost(void) const { return _alt+1 == _desc->alternatives(); } forceinline void ReCoNode::next(void) { _alt++; } forceinline const BranchingDesc* ReCoNode::desc(void) const { return _desc; } forceinline void ReCoNode::dispose(void) { delete _space; delete _desc; } /* * Depth-first stack with recomputation * */ forceinline ReCoStack::ReCoStack(unsigned int a_d0) : a_d(a_d0) {} forceinline const BranchingDesc* ReCoStack::push(Space* s, Space* c) { ReCoNode sn(s,c); ds.push(sn); return sn.desc(); } forceinline bool ReCoStack::next(EngineCtrl& stat) { // Generate path for next node and return whether node exists. while (!ds.empty()) if (ds.top().rightmost()) { stat.pop(ds.top().space(),ds.top().desc()); ds.pop().dispose(); } else { ds.top().next(); return true; } return false; } forceinline void ReCoStack::commit(Space* s, int i) const { const ReCoNode& n = ds[i]; s->commit(n.desc(),n.alt()); } forceinline int ReCoStack::lc(Space*& s) const { int l = ds.entries()-1; while (ds[l].space() == NULL) l--; s = ds[l].space(); return l; } forceinline int ReCoStack::entries(void) const { return ds.entries(); } forceinline size_t ReCoStack::stacksize(void) const { return ds.size(); } forceinline void ReCoStack::unwind(int l) { assert((ds[l].space() == NULL) || ds[l].space()->failed()); int n = ds.entries(); for (int i=l; i forceinline Space* ReCoStack::recompute(unsigned int& d, EngineCtrl& stat) { assert(!ds.empty()); // Recompute space according to path // Also say distance to copy (d == 0) requires immediate copying // Check for LAO if ((ds.top().space() != NULL) && ds.top().rightmost()) { Space* s = ds.top().space(); s->commit(ds.top().desc(),ds.top().alt()); ds.top().space(NULL); stat.lao(s); d = 0; stat.commit++; return s; } // General case for recomputation Space* s; // Last clone int l = lc(s); // Position of last clone int n = ds.entries(); // Number of stack entries d = static_cast(n - l); // New distance, if no adaptive recomputation if (constrained) { // The space on the stack could be failed now as an additional // constraint might have been added. if (s->status(stat.propagate) == SS_FAILED) { // s does not need deletion as it is on the stack (unwind does this) stat.fail++; unwind(l); return NULL; } // It is important to replace the space on the stack with the // copy: a copy might be much smaller due to flushed caches // of propagators Space* c = s->clone(); ds[l].space(c); stat.constrained(s,c); } else { s = s->clone(); } stat.clone++; if (d < a_d) { // No adaptive recomputation for (int i=l; i(d >> 1); // Middle between copy and top int i = l; // To iterate over all entries // Recompute up to middle for (; istatus(stat.propagate); // Again, the space might already propagate to failure if (constrained && (ss == SS_FAILED)) { // s must be deleted as it is not on the stack delete s; stat.commit += (i-l); stat.fail++; unwind(i); return NULL; } stat.clone++; ds[i].space(s->clone()); stat.adapt(ds[i].space()); d = static_cast(n-i); } // Finally do the remaining commits for (; i