Exemplo n.º 1
0
	IntElemDomain(IntView<U> _y, IntView<V> _x, vec<IntView<W> >& _a) :
		y(_y), x(_x), a(_a) {
		num_support = new Tint[y.getMax()-y.getMin()+1] - y.getMin();
		support = new int*[y.getMax()-y.getMin()+1] - y.getMin();
		temp_sup = new int[x.getMax()-x.getMin()+1];

		vec<int> temp;
		for (int v = y.getMin(); v <= y.getMax(); v++) {
			temp.clear();
			if (y.indomain(v)) {
				for (int i = x.getMin(); i <= x.getMax(); i++) {
					if (x.indomain(i) && a[i].indomain(v)) temp.push(i);
				}
			}
			num_support[v] = temp.size();
			support[v] = new int[temp.size()];
			for (int i = 0; i < temp.size(); i++) {
				support[v][i] = temp[i];
			}
		}

		for (int i = 0; i < a.size(); i++) a[i].attach(this, i, EVENT_C);
		y.attach(this, a.size(), EVENT_C);
		x.attach(this, a.size()+1, EVENT_C);
	}
Exemplo n.º 2
0
	void wakeup(int i, int c) {
		if (i == a.size()+1 && (c & EVENT_F)) {
			fixed_index = x.getVal();
			no_min_support = no_max_support = false;
			pushInQueue();
		}
		if (fixed_index >= 0) {
			if (i == a.size() || i == fixed_index) pushInQueue();
		} else {
			if (i < a.size()) {
				if (i == min_support && a[i].getMin() > y.getMin()) no_min_support = true;
				if (i == max_support && a[i].getMax() < y.getMax()) no_max_support = true;
				pushInQueue();
			} else if (i == a.size()+1) {
				if (!x.indomain(min_support)) { no_min_support = true; pushInQueue(); }
				if (!x.indomain(max_support)) { no_max_support = true; pushInQueue(); }
			} else pushInQueue();
		}
	}
Exemplo n.º 3
0
	bool propagate() {

		// propagate holes in y
		for (int v = y.getMin(); v <= y.getMax(); v++) {
			if (!y.indomain(v)) continue;
			assert(num_support[v] > 0);
			int *s = support[v];
			if (x.indomain(s[0]) && a[s[0]].indomain(v)) continue;
			int f = 0;
			while (!(x.indomain(s[f]) && a[s[f]].indomain(v)) && ++f < num_support[v]);
			if (f == num_support[v]) {
				// v has no support, remove from y
				Clause* r = NULL;
				if (so.lazy) {
					r = Reason_new(x.getMax() + 4 - x.getMin());
					(*r)[1] = x.getMinLit();
					(*r)[2] = x.getMaxLit();
					for (int i = x.getMin(); i <= x.getMax(); i++) {
						//(*r)[3 + i - x.getMin()] = x.indomain(i) ? ~a[i].getLit(v, 0) : ~x.getLit(i, 0);
						int reasonIndex = 3 + i - x.getMin();
						if(x.indomain(i))
						{
							Lit l = ~a[i].getLit(v, 0);
							(*r)[reasonIndex] = l;
						}
						else
						{
							Lit l = ~x.getLit(i, 0);
						 	(*r)[reasonIndex] = l;
						}
					}
				}
				if (!y.remVal(v, r)) return false;
			} else {
				// shift bad supports to back
				for (int i = 0; i < f; i++) temp_sup[i] = s[i];
				for (int i = f; i < num_support[v]; i++) {
					s[i-f] = s[i];
				}
				s += num_support[v] - f;
				for (int i = 0; i < f; i++) s[i] = temp_sup[i];
				num_support[v] -= f;
			}
		}

		// propagate holes in x
		// just ignore

		// propagate holes in a_i
		if (x.isFixed()) {
			int v = x.getVal();
			IntView<W>& f = a[v];
			setDom(y, setMin, f.getMin(), f.getMinLit(), x.getValLit());
			setDom(f, setMin, y.getMin(), y.getMinLit(), x.getValLit());
			setDom(y, setMax, f.getMax(), f.getMaxLit(), x.getValLit());
			setDom(f, setMax, y.getMax(), y.getMaxLit(), x.getValLit());
			for (typename IntView<W>::iterator i = a[v].begin(); i != a[v].end(); ) {
				int w = *i++;
				if (!y.indomain(w) && !a[v].remVal(w, so.lazy ? Reason(~y.getLit(w, 0), ~x.getLit(v, 1)) : Reason()))
					return false;
			}
		}

		return true;
	}
Exemplo n.º 4
0
	bool propagate() {

		// y = a[fixed_index]
		if (fixed_index >= 0) {
			assert(x.getVal() == fixed_index);
			IntView<W>& f = a[fixed_index];
			setDom(y, setMin, f.getMin(), f.getMinLit(), x.getValLit());
			setDom(f, setMin, y.getMin(), y.getMinLit(), x.getValLit());
			setDom(y, setMax, f.getMax(), f.getMaxLit(), x.getValLit());
			setDom(f, setMax, y.getMax(), y.getMaxLit(), x.getValLit());
			if (y.isFixed() && f.isFixed()) satisfied = true;
			return true;
		}

		for (int i = 0; i < a.size(); i++) {
			if (!x.indomain(i)) continue;
			if (y.getMax() < a[i].getMin()) setDom(x, remVal, i, y.getMaxLit(), a[i].getMinLit());
			if (y.getMin() > a[i].getMax()) setDom(x, remVal, i, y.getMinLit(), a[i].getMaxLit());
		}

		if (no_min_support) {
			int64_t old_m = y.getMin();
			int64_t new_m = INT64_MAX;
			int best = -1;
			for (int i = 0; i < a.size(); i++) {
				if (!x.indomain(i)) continue;
				int64_t cur_m = a[i].getMin();
				if (cur_m < new_m) {
					best = i;
					new_m = cur_m;
					if (cur_m <= old_m) break;
				}
			}
			min_support = best;
			if (y.setMinNotR(new_m)) {
				Clause *r = NULL;
				if (so.lazy) {
					r = Reason_new(a.size()+1);
					// Finesse lower bounds
					for (int i = 0; i < a.size(); i++) {
						(*r)[i+1] = x.indomain(i) ? a[i].getFMinLit(new_m) : x.getLit(i, 1);
					}
				}
				if (!y.setMin(new_m, r)) return false;
			}
			no_min_support = false;
		}

		if (no_max_support) {
			int64_t old_m = y.getMax();
			int64_t new_m = INT_MIN;
			int best = -1;
			for (int i = 0; i < a.size(); i++) {
				if (!x.indomain(i)) continue;
				int64_t cur_m = a[i].getMax();
				if (cur_m > new_m) {
					best = i;
					new_m = cur_m;
					if (cur_m >= old_m) break;
				}
			}
			max_support = best;
			if (y.setMaxNotR(new_m)) {
				Clause *r = NULL;
				if (so.lazy) {
					r = Reason_new(a.size()+1);
					// Finesse upper bounds
					for (int i = 0; i < a.size(); i++) {
						(*r)[i+1] = x.indomain(i) ? a[i].getFMaxLit(new_m) : x.getLit(i, 1);
					}
				}
				if (!y.setMax(new_m, r)) return false;
			}
			no_max_support = false;
		}

		return true;
	}