%Test linear relation over Boolean variables equal to integer variable
| 199 | |
| 200 | /// %Test linear relation over Boolean variables equal to integer variable |
| 201 | class BoolVar : public Test { |
| 202 | protected: |
| 203 | /// Coefficients |
| 204 | Gecode::IntArgs a; |
| 205 | /// Integer relation type to propagate |
| 206 | Gecode::IntRelType irt; |
| 207 | public: |
| 208 | /// Create and register test |
| 209 | BoolVar(const std::string& s, |
| 210 | int min, int max, const Gecode::IntArgs& a0, |
| 211 | Gecode::IntRelType irt0) |
| 212 | : Test("Linear::Bool::Var::"+str(irt0)+"::"+s,a0.size()+1, |
| 213 | min,max,true), |
| 214 | a(a0), irt(irt0) { |
| 215 | testfix=false; |
| 216 | } |
| 217 | /// %Test whether \a x is solution |
| 218 | virtual bool solution(const Assignment& x) const { |
| 219 | int n=x.size()-1; |
| 220 | for (int i=0; i<n; i++) |
| 221 | if ((x[i] < 0) || (x[i] > 1)) |
| 222 | return false; |
| 223 | double e = 0.0; |
| 224 | for (int i=0; i<n; i++) |
| 225 | e += a[i]*x[i]; |
| 226 | return cmp(e, irt, static_cast<double>(x[n])); |
| 227 | } |
| 228 | /// %Test whether \a x is to be ignored |
| 229 | virtual bool ignore(const Assignment& x) const { |
| 230 | for (int i=x.size()-1; i--; ) |
| 231 | if ((x[i] < 0) || (x[i] > 1)) |
| 232 | return true; |
| 233 | return false; |
| 234 | } |
| 235 | /// Post constraint on \a x |
| 236 | virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { |
| 237 | int n=x.size()-1; |
| 238 | Gecode::BoolVarArgs y(n); |
| 239 | for (int i=n; i--; ) |
| 240 | y[i]=Gecode::channel(home,x[i]); |
| 241 | if (one(a)) |
| 242 | Gecode::linear(home, y, irt, x[n]); |
| 243 | else |
| 244 | Gecode::linear(home, a, y, irt, x[n]); |
| 245 | } |
| 246 | /// Post reified constraint on \a x for \a r |
| 247 | virtual void post(Gecode::Space& home, Gecode::IntVarArray& x, |
| 248 | Gecode::Reify r) { |
| 249 | int n=x.size()-1; |
| 250 | Gecode::BoolVarArgs y(n); |
| 251 | for (int i=n; i--; ) |
| 252 | y[i]=Gecode::channel(home,x[i]); |
| 253 | if (one(a)) |
| 254 | Gecode::linear(home, y, irt, x[n], r); |
| 255 | else |
| 256 | Gecode::linear(home, a, y, irt, x[n], r); |
| 257 | } |
| 258 | }; |