this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christopher Mears <Chris.Mears@monash.edu> 5 * 6 * Contributing authors: 7 * Christian Schulte <schulte@gecode.org> 8 * 9 * Copyright: 10 * Christopher Mears, 2011 11 * Christian Schulte, 2011 12 * 13 * This file is part of Gecode, the generic constraint 14 * development environment: 15 * http://www.gecode.org 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining 18 * a copy of this software and associated documentation files (the 19 * "Software"), to deal in the Software without restriction, including 20 * without limitation the rights to use, copy, modify, merge, publish, 21 * distribute, sublicense, and/or sell copies of the Software, and to 22 * permit persons to whom the Software is furnished to do so, subject to 23 * the following conditions: 24 * 25 * The above copyright notice and this permission notice shall be 26 * included in all copies or substantial portions of the Software. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 * 36 */ 37 38namespace Gecode { namespace Int { namespace Precede { 39 40 /// Whether \a x is assigned to value \a v 41 template<class View> 42 forceinline bool 43 assigned(View x, int v) { 44 return x.assigned() && (x.val() == v); 45 } 46 47 template<class View> 48 forceinline 49 Single<View>::Index::Index(Space& home, Propagator& p, 50 Council<Index>& c, int i0) 51 : Advisor(home,p,c), i(i0) {} 52 53 template<class View> 54 forceinline 55 Single<View>::Index::Index(Space& home, Index& a) 56 : Advisor(home,a), i(a.i) {} 57 58 59 template<class View> 60 forceinline ExecStatus 61 Single<View>::updateAlpha(Space& home) { 62 int n = x.size(); 63 while ((alpha < n) && !x[alpha].in(s)) 64 GECODE_ME_CHECK(x[alpha++].nq(home, t)); 65 if (alpha < n) 66 GECODE_ME_CHECK(x[alpha].nq(home, t)); 67 return ES_OK; 68 } 69 70 template<class View> 71 forceinline ExecStatus 72 Single<View>::updateBeta(Space& home) { 73 int n = x.size(); 74 do { 75 beta++; 76 } while ((beta < n) && !x[beta].in(s)); 77 if (beta > gamma) 78 GECODE_ME_CHECK(x[alpha].eq(home, s)); 79 return ES_OK; 80 } 81 82 template<class View> 83 forceinline 84 Single<View>::Single(Home home, ViewArray<View>& x0, 85 int s0, int t0, int b, int g) 86 : NaryPropagator<View, PC_INT_NONE>(home,x0), 87 c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) { 88 for (int i=0; i<x.size(); i++) 89 if (!x[i].assigned()) 90 x[i].subscribe(home,*new (home) Index(home,*this,c,i)); 91 View::schedule(home, *this, ME_INT_BND); 92 } 93 94 template<class View> 95 inline ExecStatus 96 Single<View>::post(Home home, ViewArray<View>& x, int s, int t) { 97 { 98 int alpha = 0; 99 while ((alpha < x.size()) && !x[alpha].in(s)) 100 GECODE_ME_CHECK(x[alpha++].nq(home,t)); 101 x.drop_fst(alpha); 102 if (x.size() == 0) 103 return ES_OK; 104 } 105 // alpha has been normalized to 0 106 int beta = 0, gamma = 0; 107 GECODE_ME_CHECK(x[0].nq(home,t)); 108 do { 109 gamma++; 110 } while ((gamma < x.size()) && !assigned(x[gamma],t)); 111 do { 112 beta++; 113 } while ((beta < x.size()) && !x[beta].in(s)); 114 if (beta > gamma) { 115 GECODE_ME_CHECK(x[0].eq(home, s)); 116 return ES_OK; 117 } 118 if (gamma < x.size()) 119 x.drop_lst(gamma); 120 (void) new (home) Single<View>(home, x, s, t, beta, gamma); 121 return ES_OK; 122 } 123 124 125 126 template<class View> 127 forceinline 128 Single<View>::Single(Space& home, Single& p) 129 : NaryPropagator<View,PC_INT_NONE>(home, p), 130 s(p.s), t(p.t), 131 alpha(p.alpha), beta(p.beta), gamma(p.gamma) { 132 c.update(home, p.c); 133 } 134 template<class View> 135 Propagator* 136 Single<View>::copy(Space& home) { 137 // Try to eliminate assigned views at the beginning 138 if (alpha > 0) { 139 int i = 0; 140 while ((i < alpha) && x[i].assigned()) 141 i++; 142 x.drop_fst(i); 143 for (Advisors<Index> as(c); as(); ++as) 144 as.advisor().i -= i; 145 alpha -= i; beta -= i; gamma -= i; 146 } 147 // Try to eliminate assigned views at the end 148 if (gamma < x.size()) { 149 int i = x.size()-1; 150 while ((i > gamma) && x[i].assigned()) 151 i--; 152 x.drop_lst(i); 153 } 154 return new (home) Single<View>(home, *this); 155 } 156 157 158 template<class View> 159 inline size_t 160 Single<View>::dispose(Space& home) { 161 // Cancel remaining advisors 162 for (Advisors<Index> as(c); as(); ++as) 163 x[as.advisor().i].cancel(home,as.advisor()); 164 c.dispose(home); 165 (void) NaryPropagator<View,PC_INT_NONE>::dispose(home); 166 return sizeof(*this); 167 } 168 169 template<class View> 170 PropCost 171 Single<View>::cost(const Space&, const ModEventDelta&) const { 172 return PropCost::linear(PropCost::LO, x.size()); 173 } 174 175 template<class View> 176 void 177 Single<View>::reschedule(Space& home) { 178 View::schedule(home, *this, ME_INT_BND); 179 } 180 181 template<class View> 182 ExecStatus 183 Single<View>::advise(Space& home, Advisor& a0, const Delta& d) { 184 Index& a(static_cast<Index&>(a0)); 185 int i = a.i; 186 // Check for gamma 187 if ((beta <= gamma) && (i < gamma) && assigned(x[i],t)) 188 gamma = i; 189 if (x[i].assigned()) { 190 a.dispose(home,c); 191 if (c.empty()) 192 return ES_NOFIX; 193 } else if ((i < alpha) || (i > gamma)) { 194 x[i].cancel(home,a); 195 a.dispose(home,c); 196 return (c.empty()) ? ES_NOFIX : ES_FIX; 197 } 198 if (beta > gamma) 199 return ES_NOFIX; 200 if ((alpha == i) || (beta == i)) { 201 if (x[i].any(d) && !x[i].in(s)) 202 return ES_NOFIX; 203 if ((x[i].min(d) <= s) && (s <= x[i].max(d))) 204 return ES_NOFIX; 205 } 206 return ES_FIX; 207 } 208 209 template<class View> 210 ExecStatus 211 Single<View>::propagate(Space& home, const ModEventDelta&) { 212 int n = x.size(); 213 if (beta > gamma) { 214 GECODE_ME_CHECK(x[alpha].eq(home, s)); 215 return home.ES_SUBSUMED(*this); 216 } 217 if ((alpha < n) && !x[alpha].in(s)) { 218 alpha++; 219 while (alpha < beta) 220 GECODE_ME_CHECK(x[alpha++].nq(home, t)); 221 GECODE_ES_CHECK(updateAlpha(home)); 222 beta = alpha; 223 if (alpha < n) 224 GECODE_ES_CHECK(updateBeta(home)); 225 } else if ((beta < n) && !x[beta].in(s)) { 226 GECODE_ES_CHECK(updateBeta(home)); 227 } 228 229 return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX; 230 } 231 232}}} 233 234// STATISTICS: int-prop