Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

seq-u.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *     Christian Schulte <schulte@gecode.org>
00006  *
00007  *  Contributing authors:
00008  *     Gabor Szokoli <szokoli@gecode.org>
00009  *
00010  *  Copyright:
00011  *     Guido Tack, 2004
00012  *     Christian Schulte, 2004
00013  *     Gabor Szokoli, 2004
00014  *
00015  *  Last modified:
00016  *     $Date: 2008-02-06 18:48:22 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $
00017  *     $Revision: 6102 $
00018  *
00019  *  This file is part of Gecode, the generic constraint
00020  *  development environment:
00021  *     http://www.gecode.org
00022  *
00023  *  Permission is hereby granted, free of charge, to any person obtaining
00024  *  a copy of this software and associated documentation files (the
00025  *  "Software"), to deal in the Software without restriction, including
00026  *  without limitation the rights to use, copy, modify, merge, publish,
00027  *  distribute, sublicense, and/or sell copies of the Software, and to
00028  *  permit persons to whom the Software is furnished to do so, subject to
00029  *  the following conditions:
00030  *
00031  *  The above copyright notice and this permission notice shall be
00032  *  included in all copies or substantial portions of the Software.
00033  *
00034  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00035  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00036  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00037  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00038  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00039  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00040  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00041  *
00042  */
00043 
00044 #include "gecode/set/sequence.hh"
00045 
00046 namespace Gecode { namespace Set { namespace Sequence {
00047 
00048   /*
00049    * "Sequenced union" propagator
00050    *
00051    */
00052 
00053   Actor*
00054   SeqU::copy(Space* home, bool share) {
00055     return new (home) SeqU(home,share,*this);
00056   }
00057 
00058   Support::Symbol
00059   SeqU::ati(void) {
00060     return Support::Symbol("Gecode::Set::Sequence::Union");
00061   }
00062 
00063   Reflection::ActorSpec
00064   SeqU::spec(const Space* home, Reflection::VarMap& m) const {
00065     Reflection::ActorSpec s =
00066      NaryOnePropagator<SetView,PC_SET_ANY>::spec(home, m, ati());
00067     int count = 0;
00068     for (BndSetRanges uod(unionOfDets); uod(); ++uod)
00069       count++;
00070     Reflection::IntArrayArg* a = Reflection::Arg::newIntArray(count*2);
00071     count = 0;
00072     for (BndSetRanges uod(unionOfDets); uod(); ++uod) {
00073       (*a)[count++] = uod.min();
00074       (*a)[count++] = uod.max();
00075     }
00076     return s << a;
00077   }
00078 
00079   void
00080   SeqU::post(Space* home, Reflection::VarMap& vars,
00081              const Reflection::ActorSpec& spec) {
00082     spec.checkArity(3);
00083     ViewArray<SetView> x0(home, vars, spec[0]);
00084     SetView x1(home, vars, spec[1]);
00085     Reflection::IntArrayArgRanges r(spec[2]->toIntArray());
00086     SeqU* sequprop = new (home) SeqU(home, x0, x1);
00087     sequprop->unionOfDets.includeI(home, r);
00088   }
00089 
00090   ExecStatus
00091   SeqU::propagateSeqUnion(Space* home,
00092                           bool& modified, ViewArray<SetView>& x,
00093                           SetView& y) {
00094     GECODE_AUTOARRAY(GlbRanges<SetView>, XLBs,x.size());
00095     for (int i=x.size(); i--; ){
00096       GlbRanges<SetView> lb(x[i]);
00097       XLBs[i]=lb;
00098     }
00099     Iter::Ranges::NaryAppend<GlbRanges<SetView> > u(XLBs,x.size());
00100     GECODE_ME_CHECK_MODIFIED(modified, y.includeI(home,u));
00101 
00102     GLBndSet before(home);
00103     for (int i=0; i<x.size(); i++) {
00104       LubRanges<SetView> xiub(x[i]);
00105       before.includeI(home, xiub);
00106       BndSetRanges beforeR(before);
00107       GlbRanges<SetView> ylb(y);
00108       Iter::Ranges::Diff<GlbRanges<SetView>, BndSetRanges> diff(ylb, beforeR);
00109       if (diff()) {
00110         GECODE_ME_CHECK_MODIFIED(modified, x[i].exclude(home, diff.min(),
00111                                                         Limits::max));
00112       }
00113     }
00114     before.dispose(home);
00115 
00116     GLBndSet after(home);
00117     for (int i=x.size(); i--; ) {
00118       LubRanges<SetView> xiub(x[i]);
00119       after.includeI(home, xiub);
00120       BndSetRanges afterR(after);
00121       GlbRanges<SetView> ylb(y);
00122       Iter::Ranges::Diff<GlbRanges<SetView>, BndSetRanges> diff(ylb, afterR);
00123       if (diff()) {
00124         int max = diff.max();
00125         for (; diff(); ++diff)
00126           max = diff.max();
00127         GECODE_ME_CHECK_MODIFIED(modified, x[i].exclude(home,
00128                                                         Limits::min,
00129                                                         max));
00130       }
00131     }    
00132     after.dispose(home);
00133     
00134     return ES_FIX;
00135   }
00136 
00137   //Enforces sequentiality and ensures y contains union of Xi lower bounds.
00138   ExecStatus
00139   SeqU::propagate(Space* home, ModEventDelta med) {
00140     ModEvent me0 = SetView::me(med);
00141     bool ubevent = Rel::testSetEventUB(me0);
00142     bool anybevent = Rel::testSetEventAnyB(me0);
00143     bool cardevent = Rel::testSetEventCard(me0);
00144 
00145     bool modified = false;
00146     bool assigned=false;
00147     bool oldModified = false;
00148 
00149     do {
00150       oldModified = modified;
00151       modified = false;
00152 
00153       if (oldModified || modified || anybevent || cardevent)
00154         GECODE_ME_CHECK(propagateSeq(home,modified,assigned,x));
00155       if (oldModified || modified || anybevent)
00156         GECODE_ME_CHECK(propagateSeqUnion(home,modified,x,y));
00157       if (oldModified || modified || ubevent)
00158         GECODE_ME_CHECK(RelOp::unionNXiUB(home,modified,x,y,unionOfDets));
00159       if (oldModified || modified || ubevent)
00160         GECODE_ME_CHECK(RelOp::partitionNYUB(home,modified,x,y,unionOfDets));
00161       if (oldModified || modified || anybevent)
00162         GECODE_ME_CHECK(RelOp::partitionNXiLB(home,modified,x,y,unionOfDets));
00163       if (oldModified || modified || cardevent || ubevent)
00164         GECODE_ME_CHECK(RelOp::partitionNCard(home,modified,x,y,unionOfDets));
00165 
00166     } while (modified);
00167 
00168     for (int i=x.size(); i--;)
00169       if (!x[i].assigned())
00170         return ES_FIX;
00171     return ES_SUBSUMED(this,home);
00172   }
00173 
00174 }}}
00175 
00176 // STATISTICS: set-prop