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

projector.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  *
00006  *  Copyright:
00007  *     Guido Tack, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-27 18:06:13 +0100 (Wed, 27 Feb 2008) $ by $Author: tack $
00011  *     $Revision: 6332 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "gecode/set/projectors.hh"
00039 
00040 namespace Gecode {
00041 
00042   int
00043   Projector::arity(void) const {
00044     return _arity;
00045   }
00046 
00047   void codeScope(Support::DynamicArray<int>& s, const SetExprCode& c,
00048                  bool monotone) {
00049     int tmp = 0;
00050     for (int i=0; i<c.size(); i++) {
00051       switch (c[i]) {
00052       case SetExprCode::COMPLEMENT:
00053       case SetExprCode::INTER:
00054       case SetExprCode::UNION:
00055       case SetExprCode::EMPTY:
00056       case SetExprCode::UNIVERSE:
00057         break;
00058       case SetExprCode::GLB:
00059         if (s[tmp] == Set::PC_SET_ANY+1)
00060           s[tmp] = monotone ? Set::PC_SET_CGLB : Set::PC_SET_CLUB;
00061         else if (monotone && s[tmp] != Set::PC_SET_CGLB)
00062           s[tmp] = Set::PC_SET_ANY;
00063         else if (!monotone && s[tmp] != Set::PC_SET_CLUB)
00064           s[tmp] = Set::PC_SET_ANY;
00065         break;
00066       case SetExprCode::LUB:
00067         if (s[tmp] == Set::PC_SET_ANY+1)
00068           s[tmp] = monotone ? Set::PC_SET_CLUB : Set::PC_SET_CGLB;
00069         else if (monotone && s[tmp] != Set::PC_SET_CLUB)
00070           s[tmp] = Set::PC_SET_ANY;
00071         else if (!monotone && s[tmp] != Set::PC_SET_CGLB)
00072           s[tmp] = Set::PC_SET_ANY;
00073         break;
00074       default:
00075         tmp = c[i]-SetExprCode::LAST;
00076         break;
00077       }
00078     }
00079   }
00080 
00081   void
00082   Projector::scope(Support::DynamicArray<int>& s) const {
00083     codeScope(s, glb, false);
00084     codeScope(s, lub, true);
00085   }
00086 
00087   ExecStatus
00088   Projector::check(Space*, ViewArray<Set::SetView>& x) {
00089     {
00090       // Check if glb violates current upper bound of x[i]
00091       SetExprRanges glbranges(x,glb,false);
00092       Iter::Ranges::Size<SetExprRanges> g(glbranges);
00093       Set::LubRanges<Set::SetView> xir(x[i]);
00094       if (!Iter::Ranges::subset(g, xir))
00095         return ES_FAILED;
00096       while (g()) ++g;
00097       if (g.size() > x[i].cardMax()) {
00098         return ES_FAILED;
00099       }        
00100     }
00101     {
00102       // Check if lub violates current lower bound of x[i]
00103       SetExprRanges lubranges(x,lub,true);
00104       Iter::Ranges::Size<SetExprRanges> l(lubranges);
00105       Set::GlbRanges<Set::SetView> xir(x[i]);
00106       if (!Iter::Ranges::subset(xir, l))
00107         return ES_FAILED;
00108       while (l()) ++l;
00109       if (l.size() < x[i].cardMin()) {
00110         return ES_FAILED;
00111       }
00112     }
00113     {
00114       // Check if monotone interpretation of lower bound is
00115       // contained in current lower bound of x[i]. If not, the constraint
00116       // is not subsumed.
00117       SetExprRanges glbranges(x,glb,true);
00118       Set::GlbRanges<Set::SetView> xir(x[i]);
00119       if (!Iter::Ranges::subset(glbranges, xir)) {
00120         return ES_FIX;
00121       }
00122     }
00123     {
00124       // Check if current upper bound of x[i] if contained in
00125       // anti-monotone interpretation of upper bound. If not, the constraint
00126       // is not subsumed.
00127       SetExprRanges lubranges(x,lub,false);
00128       Set::LubRanges<Set::SetView> xir(x[i]);
00129       if (!Iter::Ranges::subset(xir, lubranges)) {
00130         return ES_FIX;
00131       }
00132     }
00133     // Both bounds, interpreted monotonically (glb) and anti-monotonically
00134     // (lub) are contained in the respective bounds of x[i]. This means
00135     // that the bounds are entailed.
00136     return __ES_SUBSUMED;
00137   }
00138 
00139 }
00140 
00141 std::ostream&
00142 operator<<(std::ostream& os, const Gecode::Projector& p) {
00143   return os << p.getGlb() << " <= x[" << p.getIdx() << "] <= " << p.getLub()
00144             << std::endl;
00145 }
00146 
00147 // STATISTICS: set-prop