示例#1
0
void ModelEnumerator::addProjectVar(SharedContext& ctx, Var v, bool tag) {
	if (ctx.master()->value(v) == value_free && (!tag || !ctx.marked(posLit(v)))) {
		project_->push_back(v);
		ctx.setFrozen(v, true);
		ctx.setProject(v, true);
		if (tag) { ctx.mark(posLit(v)); ctx.mark(negLit(v)); }
	}
}
示例#2
0
	void testNLCUnsorted() {
		std::stringstream prg;
		prg << "* #variable= 4 #constraint= 2 #product= 2 sizeproduct= 8\n"
				<< "1 x1 +1 x2 x1 >=1;\n"
			  << "1 x1 +1 x2 x3 x4 ~x4 x2 x3 >=1;\n"
				;
		ObjectiveFunction obj;
		CPPUNIT_ASSERT(parseOPB(prg, ctx, obj));
		CPPUNIT_ASSERT(ctx.numVars() == 6);
		CPPUNIT_ASSERT(ctx.master()->isTrue(posLit(1)));
		CPPUNIT_ASSERT(ctx.master()->isFalse(posLit(6)));
	}
示例#3
0
	void testPBEqualityBug() {
		std::stringstream prg;
		prg << "* #variable= 4 #constraint= 2\n"
				<< "+1 x1 = 1;\n"
			  << "+1 x1 +1 x2 +1 x3 +1 x4 = 1;\n"
			  ;
		ObjectiveFunction obj;
		CPPUNIT_ASSERT(parseOPB(prg, ctx, obj));
		CPPUNIT_ASSERT(ctx.master()->isTrue(posLit(1)));
		CPPUNIT_ASSERT(ctx.master()->isFalse(posLit(2)));
		CPPUNIT_ASSERT(ctx.master()->isFalse(posLit(3)));
		CPPUNIT_ASSERT(ctx.master()->isFalse(posLit(4)));
	}
示例#4
0
Literal Lookahead::heuristic(Solver& s) {
	if (s.value(score.best) != value_free) {
		// no candidate available
		return posLit(0);
	}
	ScoreLook& sc = score;
	Literal choice= Literal(sc.best, sc.score[sc.best].prefSign());
	if (!sc.deps.empty() && sc.mode == ScoreLook::score_max_min) {
		// compute heuristic values for candidates skipped during last lookahead
		uint32 min, max;
		sc.score[sc.best].score(max, min);
		sc.addDeps = false;
		bool ok    = true;
		LitVec::size_type i = 0;
		do {
			Var v        = sc.deps[i];
			VarScore& vs = sc.score[v];
			if (s.value(v) == value_free) {
				uint32 vMin, vMax;
				vs.score(vMax, vMin);
				if (vMin == 0 || vMin > min || (vMin == min && vMax > max)) {
					uint32 neg = vs.score(negLit(v)) > 0 ? vs.score(negLit(v)) : max+1;
					uint32 pos = vs.score(posLit(v)) > 0 ? vs.score(posLit(v)) : max+1;
					if (!vs.tested(negLit(v))) {
						ok  = ok && s.test(negLit(v), this);
						neg = vs.score(negLit(v));
					}
					if ((neg > min || (neg == min && pos > max)) && !vs.tested(posLit(v))) {
						ok  = ok && s.test(posLit(v), this);
					}
				}
				if (vs.testedBoth() && sc.greaterMaxMin(v, max, min)) {
					vs.score(max, min);
					choice = Literal(v, vs.prefSign());
				}
			}
		} while (++i != sc.deps.size() && ok);
		if (!ok) {
			// One of the candidates failed. Since none of them failed
			// during previous propagation, this indicates that
			// either some post propagator has wrong priority or
			// parallel solving is active and a stop conflict was set.
			// Since we can't resolve the problem here, we simply return the
			// literal that caused the conflict
			assert(s.hasConflict());
			return negLit(0);
		}
	}
	return choice;
}
示例#5
0
/////////////////////////////////////////////////////////////////////////////////////////
// Lookback selection strategies
/////////////////////////////////////////////////////////////////////////////////////////
uint32 momsScore(const Solver& s, Var v) {
	uint32 sc;
	if (s.numBinaryConstraints()) {
		uint32 s1 = s.estimateBCP(posLit(v), 0) - 1;
		uint32 s2 = s.estimateBCP(negLit(v), 0) - 1;
		sc = ((s1 * s2)<<10) + (s1 + s2);
	}
	else {
		// problem does not contain binary constraints - fall back to counting watches
		uint32 s1 = s.numWatches(posLit(v));
		uint32 s2 = s.numWatches(negLit(v));
		sc = ((s1 * s2)<<10) + (s1 + s2);
	}
	return sc;
}
示例#6
0
Enumerator::EnumeratorConstraint* CBConsequences::doInit(SharedContext& ctx, uint32 t, bool start) {
	delete current_;
	current_ = 0;
	if (start) {
		if (ctx.symTab().type() == SymbolTable::map_direct) {
			// create indirect from direct mapping
			Var end = ctx.symTab().size();
			ctx.symTab().startInit();
			char buf[1024];
			for (Var v = 1; v < end; ++v) {
				sprintf(buf, "%u", v);
				ctx.symTab().addUnique(v, buf).lit = posLit(v);
			}
			ctx.symTab().endInit();
		}
		const SymbolTable& index = ctx.symTab();
		for (SymbolTable::const_iterator it = index.curBegin(); it != index.end(); ++it) {
			if (!it->second.name.empty()) { 
				ctx.setFrozen(it->second.lit.var(), true);
				if (type_ == cautious_consequences) {
					it->second.lit.watch();  
				}
			}
		} 
		return 0;
	}
	else {
		current_ = new GlobalConstraint(t > 1);
	}
	return new LocalConstraint();
}
示例#7
0
bool Antecedent::checkPlatformAssumptions() {
	int32* i = new int32(22);
	uint64 p = (uint64)(uintp)i;
	bool convOk = ((int32*)(uintp)p) == i;
	bool alignmentOk = (p & 3) == 0;
	delete i;
	if ( !alignmentOk ) {
		throw PlatformError("Unsupported Pointer-Alignment!");
	}
	if ( !convOk ) {
		throw PlatformError("Can't convert between Pointer and Integer!");
	}
	p = ~uintp(1);
	store_set_bit(p, 0);
	if (!test_bit(p, 0)) {
		throw PlatformError("Can't set LSB in pointer!");
	}
	store_clear_bit(p, 0);
	if (p != (~uintp(1))) {
		throw PlatformError("Can't restore LSB in pointer!");
	}
	Literal max = posLit(varMax-1);
	Antecedent a(max);
	if (a.type() != Antecedent::binary_constraint || a.firstLiteral() != max) {
		throw PlatformError("Cast between 64- and 32-bit integer does not work as expected!");
	}
	Antecedent b(max, ~max);
	if (b.type() != Antecedent::ternary_constraint || b.firstLiteral() != max || b.secondLiteral() != ~max) {
		throw PlatformError("Cast between 64- and 32-bit integer does not work as expected!");
	}
	return true;
}
示例#8
0
Literal ClaspVmtf::getLiteral(const Solver& s, Var v) const {
	Literal r;
	if ( (r = savedLiteral(s, v)) == posLit(0) ) {
		r = score_[v].occ_== 0
			? s.preferredLiteralByType(v)
			: Literal(v, score_[v].occ_ < 0 );
	}
	return r;
}
示例#9
0
void BacktrackEnumerator::undoLevel(Solver& s) {
	while (!nogoods_.empty() && nogoods_.back().second >= s.decisionLevel()) {
		Clause* c = (Clause*)nogoods_.back().first;
		nogoods_.pop_back();
		*c->end() = posLit(0);
		c->removeWatches(s);
		c->destroy();
	}
}
示例#10
0
文件: clingo.cpp 项目: potassco/clasp
void ClingoPropagator::destroy(Solver* s, bool detach) {
	if (s && detach) {
		for (Var v = 1; v <= s->numVars(); ++v) {
			s->removeWatch(posLit(v), this);
			s->removeWatch(negLit(v), this);
		}
	}
	destroyDB(db_, s, detach);
	PostPropagator::destroy(s, detach);
}
示例#11
0
Literal ClaspBerkmin::selectLiteral(Solver& s, Var var, bool vsids) {
	Literal l;
	if ( (l = savedLiteral(s,var)) == posLit(0) ) {
		int32 w0 = vsids ? (int32)s.estimateBCP(posLit(var), 5) : order_.occ(var);
		int32 w1 = vsids ? (int32)s.estimateBCP(negLit(var), 5) : 0;
		if (w1 == 1 && w0 == w1) {
			// no binary bcp - use occurrences
			w0 = order_.occ(var);
			w1 = 0;
		}
		return w0 != w1 
			? Literal(var, (w0-w1)<0)
			: s.preferredLiteralByType(var);
	}
	else if (order_.huang && (order_.occ(var)*(-1+(2*l.sign()))) > 32) {
		l = ~l;
	}
	return l;
}
示例#12
0
bool Lookahead::checkImps(Solver& s, Literal p) {
	assert(!imps_.empty());
	bool ok = true;
	if (score.score[p.var()].testedBoth()) {
		for (LitVec::const_iterator it = imps_.begin(), end = imps_.end(); it != end && ok; ++it) {
			ok  = s.force(*it, posLit(0));
		}
	}
	imps_.clear();
	return ok && (s.queueSize() == 0 || s.propagateUntil(this));
}
示例#13
0
Literal ClaspVsids::doSelect(Solver& s) {
	Var var;
	while ( s.value(vars_.top()) != value_free ) {
		vars_.pop();
	}
	var = vars_.top();
	Literal r;
	if ( (r = savedLiteral(s, var)) == posLit(0) ) {
		r = score_[var].second == 0
			? s.preferredLiteralByType(var)
			: Literal( var, score_[var].second < 0 );
	}
	return r;
}
示例#14
0
Literal RestrictedUnit::doSelect(Solver& s) {
	s.addPost(look_);
	Literal choice = look_->propagateFixpoint(s) ? look_->heuristic(s) : negLit(0);
	s.removePost(look_);
	// if all fld-candidates are currently assigned, use
	// decorated heuristic to select next choice -
	// if all vars are assigned after fld, we return posLit(0)
	// as the "noop" choice.
	if (choice == posLit(0) && s.numFreeVars() > 0) {
		choice = default_->doSelect(s);
	}
	if (!isSentinel(choice) && --numChoices_ == 0) {
		destroy(s);
	}
	return choice;
}
示例#15
0
bool SatBuilder::addClause(LitVec& clause, wsum_t cw) {
	if (!ctx()->ok() || satisfied(clause)) { return ctx()->ok(); }
	CLASP_ASSERT_CONTRACT_MSG(cw >= 0 && (cw <= std::numeric_limits<weight_t>::max() || cw == hardWeight_), "Clause weight out of bounds!");
	if (cw == 0 && maxSat_) { cw = 1; }
	if (cw == hardWeight_ || clause.empty()) {
		return ClauseCreator::create(*ctx()->master(), clause, Constraint_t::static_constraint).ok() && markAssigned();
	}
	else {
		// Store weight, relaxtion var, and (optionally) clause
		softClauses_.push_back(Literal::fromRep((uint32)cw));
		if (clause.size() != 1) { softClauses_.push_back(posLit(++vars_)); softClauses_.insert(softClauses_.end(), clause.begin(), clause.end()); }
		else                    { softClauses_.push_back(~clause.back());  }
		softClauses_.back().watch(); // mark end of clause
	}
	return true;
}
示例#16
0
/////////////////////////////////////////////////////////////////////////////////////////
// Lookahead propagator
/////////////////////////////////////////////////////////////////////////////////////////
Lookahead::Lookahead(Type type, bool imps)
	: nodes_(2, LitNode(posLit(0)))
	, last_(head_id)    // circular list
	, pos_(head_id)     // lookahead start pos
	, top_(uint32(-2)) {
	head()->next = head_id;
	undo()->next = UINT32_MAX;
	if (type != hybrid_lookahead) {
		score.mode = ScoreLook::score_max_min;
		score.types= (type == body_lookahead)
			? Var_t::body_var
			: Var_t::atom_var;
	}
	else {
		score.mode = ScoreLook::score_max;
		score.types= Var_t::atom_body_var;
	}
	if (imps) { head()->lit.watch(); }
}
示例#17
0
Literal UnitHeuristic::doSelect(Solver& s) {
	Literal x = look_->heuristic(s);
	if (x != posLit(0) || s.numFreeVars() == 0) { return x; }
	// No candidates. This can happen if the problem
	// contains choice rules and lookahead is not atom-based.
	// Add remaining free vars to lookahead so that we can
	// make an informed decision.
	VarType types = look_->score.types;
	for (Var v = 1, end = s.numVars()+1; v != end; ++v) {
		if ((s.value(v) == value_free || s.level(v) > 0) && (s.sharedContext()->type(v) & types) == 0) {
			look_->append(s.sharedContext()->preferredLiteralByType(v), true);
		}
	}
	look_->score.clearDeps();
	look_->score.types = Var_t::atom_body_var;
	return look_->propagateFixpoint(s)
		? look_->heuristic(s)
		: negLit(0);
}
示例#18
0
ShortImplicationsGraph::Block::Block() {
	for (int i = 0; i != block_cap; ++i) { data[i] = posLit(0); }
	size_lock = 0;
	next      = 0;
}
示例#19
0
文件: clingo.cpp 项目: potassco/clasp
Potassco::Lit_t ClingoPropagator::Control::addVariable() {
	POTASSCO_REQUIRE(!assignment_.hasConflict(), "Invalid addVariable() on conflicting assignment");
	ScopedUnlock unlocked(lock(), ctx_);
	return encodeLit(posLit(solver().pushAuxVar()));
}