post.hpp
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Patrick Pekczynski <pekczynski@ps.uni-sb.de> 00005 * 00006 * Contributing authors: 00007 * Christian Schulte <schulte@gecode.org> 00008 * Guido Tack <tack@gecode.org> 00009 * 00010 * Copyright: 00011 * Patrick Pekczynski, 2004/2005 00012 * Christian Schulte, 2009 00013 * Guido Tack, 2009 00014 * 00015 * Last modified: 00016 * $Date: 2010-09-03 16:36:31 +0200 (Fri, 03 Sep 2010) $ by $Author: schulte $ 00017 * $Revision: 11390 $ 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/int/linear.hh> 00045 #include <gecode/int/distinct.hh> 00046 00047 namespace Gecode { namespace Int { namespace GCC { 00048 00049 template<class Card> 00051 class CardLess : public Support::Less<Card> { 00052 public: 00053 bool operator ()(const Card& x, const Card& y) { 00054 return x.card() < y.card(); 00055 } 00056 }; 00057 00062 template<class Card> 00063 ExecStatus 00064 postSideConstraints(Home home, ViewArray<IntView>& x, ViewArray<Card>& k) { 00065 CardLess<Card> cl; 00066 Support::quicksort(&k[0], k.size(), cl); 00067 Region r(home); 00068 00069 { 00070 int smin = 0; 00071 int smax = 0; 00072 for (int i = k.size(); i--; ) { 00073 GECODE_ME_CHECK((k[i].gq(home, 0))); 00074 GECODE_ME_CHECK((k[i].lq(home, x.size()))); 00075 smin += k[i].min(); 00076 smax += k[i].max(); 00077 } 00078 00079 // not enough variables to satisfy min req 00080 if ((x.size() < smin) || (smax < x.size())) 00081 return ES_FAILED; 00082 } 00083 00084 // Remove all values from the x that are not in v 00085 { 00086 int* v = r.alloc<int>(k.size()); 00087 for (int i=k.size(); i--;) 00088 v[i]=k[i].card(); 00089 Support::quicksort(v,k.size()); 00090 for (int i=x.size(); i--; ) { 00091 Iter::Values::Array iv(v,k.size()); 00092 GECODE_ME_CHECK(x[i].inter_v(home, iv, false)); 00093 } 00094 } 00095 00096 // Remove all values with 0 max occurrence 00097 // and remove corresponding occurrence variables from k 00098 { 00099 // The number of zeroes 00100 int n_z = 0; 00101 for (int i=k.size(); i--;) 00102 if (k[i].max() == 0) 00103 n_z++; 00104 00105 if (n_z > 0) { 00106 int* z = r.alloc<int>(n_z); 00107 n_z = 0; 00108 int n_k = 0; 00109 for (int i=0; i<k.size(); i++) 00110 if (k[i].max() == 0) { 00111 z[n_z++] = k[i].card(); 00112 } else { 00113 k[n_k++] = k[i]; 00114 } 00115 k.size(n_k); 00116 Support::quicksort(z,n_z); 00117 for (int i=x.size(); i--;) { 00118 Iter::Values::Array zi(z,n_z); 00119 GECODE_ME_CHECK(x[i].minus_v(home,zi,false)); 00120 } 00121 } 00122 } 00123 00124 if (Card::propagate) { 00125 Linear::Term<IntView>* t = r.alloc<Linear::Term<IntView> >(k.size()); 00126 for (int i = k.size(); i--; ) { 00127 t[i].a=1; t[i].x=k[i].base(); 00128 } 00129 Linear::post(home,t,k.size(),IRT_EQ,x.size(),ICL_BND); 00130 } 00131 00132 return ES_OK; 00133 } 00134 00135 00140 template<class Card> 00141 inline bool 00142 isDistinct(Home home, ViewArray<IntView>& x, ViewArray<Card>& k) { 00143 if (Card::propagate) { 00144 Region r(home); 00145 ViewRanges<IntView>* xrange = r.alloc<ViewRanges<IntView> >(x.size()); 00146 for (int i = x.size(); i--; ){ 00147 ViewRanges<IntView> iter(x[i]); 00148 xrange[i] = iter; 00149 } 00150 Iter::Ranges::NaryUnion<ViewRanges<IntView> > 00151 drl(r, &xrange[0], x.size()); 00152 if (static_cast<unsigned int>(k.size()) == Iter::Ranges::size(drl)) { 00153 for (int i=k.size(); i--;) 00154 if (k[i].min() != 1 || k[i].max() != 1) 00155 return false; 00156 return true; 00157 } else { 00158 return false; 00159 } 00160 } else { 00161 for (int i=k.size(); i--;) 00162 if (k[i].min() != 0 || k[i].max() != 1) 00163 return false; 00164 return true; 00165 } 00166 } 00167 00168 }}} 00169 00170 // STATISTICS: int-prop