Esempio n. 1
0
bool SatBuilder::doEndProgram() {
	bool ok = ctx()->ok();
	if (!softClauses_.empty() && ok) {
		ctx()->setPreserveModels(true);
		ctx()->resizeVars(vars_+1);
		ctx()->startAddConstraints();
		LitVec cc; 
		for (LitVec::const_iterator it = softClauses_.begin(), end = softClauses_.end(); it != end && ok; ++it) {
			weight_t w     = (weight_t)it->asUint();
			Literal  relax = *++it;
			if (!relax.watched()) { 
				cc.assign(1, relax);
				do { cc.push_back(*++it); } while (!cc.back().watched());
				cc.back().clearWatch();
				ok = ClauseCreator::create(*ctx()->master(), cc, Constraint_t::static_constraint).ok();
			}
			relax.clearWatch();
			addMinLit(WeightLiteral(relax, w));
		}
		LitVec().swap(softClauses_);
	}
	if (ok && !ctx()->preserveModels()) {
		uint32 p    = 12;
		for (Var v  = 1; v != (Var)varState_.size() && ok; ++v) {
			uint32 m  = varState_[v];
			if ( (m & p) != p ) { ok = ctx()->addUnary(Literal(v, ((m>>2) & 1u) != 1)); }
		}
Esempio n. 2
0
	// <sum>::= <weightedterm> | <weightedterm> <sum>
	// <weightedterm>::= <integer> <oneOrMoreSpace> <term> <oneOrMoreSpace>
	void OPBParser::parseSum() {
		active_.lits.clear();
		while (!match(*input(), ';', true)) {
			int coeff = input()->parseInt(INT_MIN + 1, INT_MAX, "Coefficient expected!");
			parseTerm();
			Literal x = term_.size() == 1 ? term_[0] : builder_->addProduct(term_);
			active_.lits.push_back(WeightLiteral(x, coeff));
			if (**input() == '>' || **input() == '=') break;
			input()->skipWhite();
		}
		input()->skipWhite();
	}