Generated on Tue Apr 18 10:22:01 2017 for Gecode by doxygen 1.6.3

int.cpp

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  *  Copyright:
00008  *     Guido Tack, 2004
00009  *     Christian Schulte, 2004
00010  *
00011  *  Last modified:
00012  *     $Date: 2017-03-10 10:28:56 +0100 (Fri, 10 Mar 2017) $ by $Author: schulte $
00013  *     $Revision: 15567 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include <gecode/set.hh>
00041 
00042 #include <gecode/set/int.hh>
00043 #include <gecode/set/rel.hh>
00044 
00045 namespace Gecode {
00046 
00047   void
00048   rel(Home home, SetVar s, IntRelType rt, IntVar x) {
00049     GECODE_POST;
00050     switch (rt) {
00051     case IRT_EQ:
00052       {
00053         Gecode::Int::IntView xv(x);
00054         Set::SingletonView xsingle(xv);
00055         GECODE_ES_FAIL(
00056                        (Set::Rel::Eq<Set::SetView,Set::SingletonView>
00057                         ::post(home,s,xsingle)));
00058 
00059       }
00060       break;
00061     case IRT_NQ:
00062       {
00063         Gecode::Set::SetView sv(s);
00064         GECODE_ME_FAIL( sv.cardMin(home, 1));
00065         Gecode::Int::IntView xv(x);
00066         Set::SingletonView xsingle(xv);
00067         GECODE_ES_FAIL(
00068                        (Set::Rel::NoSubset<Set::SingletonView,Set::SetView>
00069                         ::post(home,xsingle,sv)));
00070 
00071       }
00072       break;
00073     case IRT_LQ:
00074       {
00075         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00076         rel(home, tmp, IRT_LQ, x);
00077         GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp));
00078       }
00079       break;
00080     case IRT_LE:
00081       {
00082         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00083         rel(home, tmp, IRT_LE, x);
00084         GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,tmp));
00085       }
00086       break;
00087     case IRT_GQ:
00088       {
00089         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00090         rel(home, tmp, IRT_GQ, x);
00091         GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp));
00092       }
00093       break;
00094     case IRT_GR:
00095       {
00096         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00097         rel(home, tmp, IRT_GR, x);
00098         GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,tmp));
00099       }
00100       break;
00101     default:
00102       throw Int::UnknownRelation("Set::rel");
00103     }
00104 
00105   }
00106 
00107 }
00108 
00109 namespace Gecode { namespace Set { namespace Int {
00110 
00112   void remin(Home home, SetVar s, IntVar m, Reify r) {
00113     IntVar c(home, 0, static_cast<int>(Set::Limits::card));
00114     cardinality(home, s, c);
00115     // Whether set is not empty
00116     BoolVar ne(home, 0, 1);
00117     rel(home, c, IRT_GR, 0, ne);
00118     if (r.mode() != RM_PMI)
00119       rel(home, r.var(), BOT_IMP, ne, 1);
00120     min(home, s, m, ne);
00121   }
00122 
00124   void remax(Home home, SetVar s, IntVar m, Reify r) {
00125     IntVar c(home, 0, static_cast<int>(Set::Limits::card));
00126     cardinality(home, s, c);
00127     // Whether set is not empty
00128     BoolVar ne(home, 0, 1);
00129     rel(home, c, IRT_GR, 0, ne);
00130     if (r.mode() != RM_PMI)
00131       rel(home, r.var(), BOT_IMP, ne, 1);
00132     max(home, s, m, ne);
00133   }
00134 
00135 }}}
00136 
00137 namespace Gecode {
00138 
00139   void
00140   rel(Home home, SetVar s, IntRelType rt, IntVar x, Reify r) {
00141     GECODE_POST;
00142     switch (rt) {
00143     case IRT_EQ:
00144       {
00145         Gecode::Int::IntView xv(x);
00146         Set::SingletonView xs(xv);
00147         switch (r.mode()) {
00148         case RM_EQV:
00149           GECODE_ES_FAIL((Set::Rel::ReEq<Set::SetView,Set::SingletonView,
00150                           Gecode::Int::BoolView,RM_EQV>
00151                           ::post(home,s,xs,r.var())));
00152           break;
00153         case RM_IMP:
00154           GECODE_ES_FAIL((Set::Rel::ReEq<Set::SetView,Set::SingletonView,
00155                           Gecode::Int::BoolView,RM_IMP>
00156                           ::post(home,s,xs,r.var())));
00157           break;
00158         case RM_PMI:
00159           GECODE_ES_FAIL((Set::Rel::ReEq<Set::SetView,Set::SingletonView,
00160                           Gecode::Int::BoolView,RM_PMI>
00161                           ::post(home,s,xs,r.var())));
00162           break;
00163         default:
00164           throw Gecode::Int::UnknownReifyMode("Set::rel");
00165         }
00166       }
00167       break;
00168     case IRT_NQ:
00169       {
00170         IntVar c(home, 0, static_cast<int>(Set::Limits::card));
00171         cardinality(home, s, c);
00172         // Whether set is not empty
00173         BoolVar ne(home, 0 , 1);
00174         rel(home, c, IRT_GR, 0, ne);
00175         // Whether {x} is a subset of s
00176         BoolVar ss(home, 0 , 1);
00177         rel(home, x, SRT_SUB, s, ss);
00178         BoolVar b;
00179         switch (r.mode()) {
00180         case RM_EQV:
00181           b=r.var();
00182           break;
00183         case RM_IMP:
00184           b=BoolVar(home, 0, 1);
00185           rel(home, r.var(), BOT_IMP, b, 1);
00186           break;
00187         case RM_PMI:
00188           b=BoolVar(home, 0, 1);
00189           rel(home, b, BOT_IMP, r.var(), 1);
00190           break;
00191         default:
00192           throw Gecode::Int::UnknownReifyMode("Set::rel");
00193         }
00194         BoolVarArgs p(1); p[0]=ne;
00195         BoolVarArgs n(1); n[0]=ss;
00196         clause(home, BOT_AND, p, n, b);
00197       }
00198       break;
00199     case IRT_LQ:
00200       {
00201         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00202         rel(home, tmp, IRT_LQ, x, r);
00203         Gecode::Set::Int::remax(home, s, tmp, r);
00204       }
00205       break;
00206     case IRT_LE:
00207       {
00208         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00209         rel(home, tmp, IRT_LE, x, r);
00210         Gecode::Set::Int::remax(home, s, tmp, r);
00211       }
00212       break;
00213     case IRT_GQ:
00214       {
00215         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00216         rel(home, tmp, IRT_GQ, x, r);
00217         Gecode::Set::Int::remin(home, s, tmp, r);
00218       }
00219       break;
00220     case IRT_GR:
00221       {
00222         IntVar tmp(home, Int::Limits::min, Int::Limits::max);
00223         rel(home, tmp, IRT_GR, x, r);
00224         Gecode::Set::Int::remin(home, s, tmp, r);
00225       }
00226       break;
00227     default:
00228       throw Int::UnknownRelation("Set::rel");
00229     }
00230   }
00231 
00232   void
00233   min(Home home, SetVar s, IntVar x){
00234     GECODE_POST;
00235     GECODE_ES_FAIL(Set::Int::MinElement<Set::SetView>::post(home,s,x));
00236   }
00237 
00238   void
00239   notMin(Home home, SetVar s, IntVar x){
00240     GECODE_POST;
00241     GECODE_ES_FAIL(Set::Int::NotMinElement<Set::SetView>::post(home,s,x));
00242   }
00243 
00244   void
00245   min(Home home, SetVar s, IntVar x, Reify r){
00246     GECODE_POST;
00247     switch (r.mode()) {
00248     case RM_EQV:
00249       GECODE_ES_FAIL((Set::Int::ReMinElement<Set::SetView,RM_EQV>
00250                      ::post(home,s,x,r.var())));
00251       break;
00252     case RM_IMP:
00253       GECODE_ES_FAIL((Set::Int::ReMinElement<Set::SetView,RM_IMP>
00254                      ::post(home,s,x,r.var())));
00255       break;
00256     case RM_PMI:
00257       GECODE_ES_FAIL((Set::Int::ReMinElement<Set::SetView,RM_PMI>
00258                      ::post(home,s,x,r.var())));
00259       break;
00260     default: throw Gecode::Int::UnknownReifyMode("Set::min");
00261     }
00262   }
00263 
00264   void
00265   max(Home home, SetVar s, IntVar x){
00266     GECODE_POST;
00267     GECODE_ES_FAIL(Set::Int::MaxElement<Set::SetView>::post(home,s,x));
00268   }
00269 
00270   void
00271   notMax(Home home, SetVar s, IntVar x){
00272     GECODE_POST;
00273     GECODE_ES_FAIL(Set::Int::NotMaxElement<Set::SetView>::post(home,s,x));
00274   }
00275 
00276   void
00277   max(Home home, SetVar s, IntVar x, Reify r){
00278     GECODE_POST;
00279     switch (r.mode()) {
00280     case RM_EQV:
00281       GECODE_ES_FAIL((Set::Int::ReMaxElement<Set::SetView,RM_EQV>
00282                      ::post(home,s,x,r.var())));
00283       break;
00284     case RM_IMP:
00285       GECODE_ES_FAIL((Set::Int::ReMaxElement<Set::SetView,RM_IMP>
00286                      ::post(home,s,x,r.var())));
00287       break;
00288     case RM_PMI:
00289       GECODE_ES_FAIL((Set::Int::ReMaxElement<Set::SetView,RM_PMI>
00290                      ::post(home,s,x,r.var())));
00291       break;
00292     default: throw Gecode::Int::UnknownReifyMode("Set::max");
00293     }
00294   }
00295 
00296   void weights(Home home, IntSharedArray elements, IntSharedArray weights,
00297                SetVar x, IntVar y) {
00298     GECODE_POST;
00299     GECODE_ES_FAIL(Set::Int::Weights<Set::SetView>::post(home,elements,
00300                                                               weights,x,y));
00301   }
00302 
00303 }
00304 
00305 // STATISTICS: set-post