Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
8.5 kB · 302 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- *//* * Main authors: * Patrick Pekczynski <pekczynski@ps.uni-sb.de> * * Contributing authors: * Christian Schulte <schulte@gecode.org> * Guido Tack <tack@gecode.org> * * Copyright: * Patrick Pekczynski, 2005 * Christian Schulte, 2009 * Guido Tack, 2009 * * 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 GCC {
template<class Card> forceinline Val<Card>::Val(Home home, ViewArray<IntView>& x0, ViewArray<Card>& k0) : Propagator(home), x(x0), k(k0){ x.subscribe(home, *this, PC_INT_VAL); k.subscribe(home, *this, PC_INT_VAL); }
template<class Card> forceinline Val<Card>::Val(Space& home, Val<Card>& p) : Propagator(home,p) { x.update(home, p.x); k.update(home, p.k); }
template<class Card> forceinline size_t Val<Card>::dispose(Space& home) { x.cancel(home,*this, PC_INT_VAL); k.cancel(home,*this, PC_INT_VAL); (void) Propagator::dispose(home); return sizeof(*this); }
template<class Card> Actor* Val<Card>::copy(Space& home) { return new (home) Val<Card>(home,*this); }
template<class Card> PropCost Val<Card>::cost(const Space&, const ModEventDelta&) const { /* * Complexity depends on the time needed for value lookup in \a k * which is O(n log n). */ return PropCost::linear(PropCost::HI,x.size()); }
template<class Card> void Val<Card>::reschedule(Space& home) { x.reschedule(home, *this, PC_INT_VAL); k.reschedule(home, *this, PC_INT_VAL); }
template<class Card> ExecStatus prop_val(Space& home, Propagator& p, ViewArray<IntView>& x, ViewArray<Card>& k) { assert(x.size() > 0);
Region r; // count[i] denotes how often value k[i].card() occurs in x int* count = r.alloc<int>(k.size());
// initialization int sum_min = 0; int removed = 0; for (int i=0; i<k.size(); i++) { removed += k[i].counter(); sum_min += k[i].min(); count[i] = 0; }
// less than or equal than the total number of free variables // to satisfy the required occurences for (int i=0; i<k.size(); i++) GECODE_ME_CHECK(k[i].lq(home, x.size()+removed-(sum_min - k[i].min())));
// number of unassigned views int non = x.size();
for (int i=0; i<x.size(); i++) if (x[i].assigned()) { int idx; if (!lookupValue(k,x[i].val(),idx)) { return ES_FAILED; } count[idx]++; non--; }
// check for subsumption if (non == 0) { for (int i=0; i<k.size(); i++) GECODE_ME_CHECK((k[i].eq(home, count[i] + k[i].counter()))); return home.ES_SUBSUMED(p); }
// total number of unsatisfied miminum occurences int req = 0; // number of values whose min requirements are not yet met int n_r = 0; // if only one value is unsatisified single holds the index of that value int single = -1; // total number of assigned views wrt. the original probem size int t_noa = 0;
for (int i = k.size(); i--; ) { int ci = count[i] + k[i].counter(); t_noa += ci; if (ci == 0) { // this works req += k[i].min(); n_r++; single = i; }
// number of unassigned views cannot satisfy // the required minimum occurence if (req > non) { return ES_FAILED; } }
// if only one unsatisfied occurences is left if ((req == non) && (n_r == 1)) { // This works as the x are not shared! for (int i = x.size(); i--; ) { // try to assign it if (!x[i].assigned()) { GECODE_ME_CHECK(x[i].eq(home, k[single].card())); assert((single >= 0) && (single < k.size())); count[single]++; } } assert((single >= 0) && (single < k.size()));
for (int i = k.size(); i--; ) GECODE_ME_CHECK(k[i].eq(home, count[i] + k[i].counter())); return home.ES_SUBSUMED(p); }
// Bitset for indexes that can be removed Support::BitSet<Region> rem(r,static_cast<unsigned int>(k.size()));
for (int i = k.size(); i--; ) { int ci = count[i] + k[i].counter(); if (ci == k[i].max()) { assert(!rem.get(i)); rem.set(static_cast<unsigned int>(i)); k[i].counter(ci); // the solution contains ci occurences of value k[i].card(); GECODE_ME_CHECK(k[i].eq(home, ci)); } else { if (ci > k[i].max()) { return ES_FAILED; }
// in case of variable cardinalities if (Card::propagate) { GECODE_ME_CHECK(k[i].gq(home, ci)); int occupied = t_noa - ci; GECODE_ME_CHECK(k[i].lq(home, x.size() + removed - occupied)); } } // reset counter count[i] = 0; }
// reduce the problem size { int n_x = x.size(); for (int i = n_x; i--; ) { if (x[i].assigned()) { int idx; if (!lookupValue(k,x[i].val(),idx)) { return ES_FAILED; } if (rem.get(static_cast<unsigned int>(idx))) x[i]=x[--n_x]; } } x.size(n_x); }
// remove values { // Values to prune int* pr = r.alloc<int>(k.size()); // Number of values to prune int n_pr = 0; for (Iter::Values::BitSet<Support::BitSet<Region> > i(rem); i(); ++i) pr[n_pr++] = k[i.val()].card(); Support::quicksort(pr,n_pr); for (int i = x.size(); i--;) { Iter::Values::Array pi(pr,n_pr); GECODE_ME_CHECK(x[i].minus_v(home, pi, false)); } }
{ bool all_assigned = true;
for (int i = x.size(); i--; ) { if (x[i].assigned()) { int idx; if (!lookupValue(k,x[i].val(),idx)) { return ES_FAILED; } count[idx]++; } else { all_assigned = false; } }
if (all_assigned) { for (int i = k.size(); i--; ) GECODE_ME_CHECK((k[i].eq(home, count[i] + k[i].counter()))); return home.ES_SUBSUMED(p); } }
if (Card::propagate) { // check again consistency of cardinalities int reqmin = 0; int allmax = 0; for (int i = k.size(); i--; ) { if (k[i].counter() > k[i].max()) { return ES_FAILED; } allmax += k[i].max() - k[i].counter(); if (k[i].counter() < k[i].min()) reqmin += k[i].min() - k[i].counter(); GECODE_ME_CHECK((k[i].lq(home, x.size()+k[i].counter()))); }
if ((x.size() < reqmin) || (allmax < x.size())) { return ES_FAILED; } }
return ES_NOFIX; }
template<class Card> ExecStatus Val<Card>::propagate(Space& home, const ModEventDelta&) { return prop_val<Card>(home, *this, x, k); }
template<class Card> ExecStatus Val<Card>::post(Home home, ViewArray<IntView>& x, ViewArray<Card>& k) { GECODE_ES_CHECK((postSideConstraints<Card>(home,x,k)));
if (isDistinct<Card>(x,k)) return Distinct::Val<IntView>::post(home,x);
(void) new (home) Val<Card>(home,x,k); return ES_OK; }
}}}
// STATISTICS: int-prop