org.sat4j.core.VecInt Java Examples

The following examples show how to use org.sat4j.core.VecInt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SAT4JSolver.java    From opt4j with MIT License 6 votes vote down vote up
protected void addConstraintToSolver(Constraint constraint) {

		VecInt lits = toVecInt(constraint.getLiterals());

		IVec<BigInteger> coeffs = new Vec<>();

		for (Integer value : constraint.getCoefficients()) {
			coeffs.push(BigInteger.valueOf(value));
		}

		BigInteger d = BigInteger.valueOf(constraint.getRhs());
		Operator operator = constraint.getOperator();

		try {
			if (operator == LE || operator == EQ) {
				solver.addPseudoBoolean(lits, coeffs, false, d);
			}
			if (operator == GE || operator == EQ) {
				solver.addPseudoBoolean(lits, coeffs, true, d);
			}
		} catch (org.sat4j.specs.ContradictionException e) {
			solverValid = false;
			throw new ContradictionException(e);
		}
	}
 
Example #2
Source File: SAT4JSolver.java    From opt4j with MIT License 6 votes vote down vote up
protected VecInt toVecInt(Iterable<Literal> list) {
	VecInt vector = new VecInt();

	for (Literal literal : list) {
		Object var = literal.variable();
		if (!variables.containsKey(var)) {
			variables.put(var, nextVariable++);
			if (variables.size() > solver.nVars()) {
				setNVars(variables.size() * 2);
			}
		}
		boolean phase = literal.phase();

		vector.push(variables.get(var) * (phase ? 1 : -1));
	}

	return vector;
}
 
Example #3
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 6 votes vote down vote up
@Override
public boolean HasModel(Integer p, boolean[] model) {
	VecInt assumption = new VecInt();
	if (model.length != universe) {
		throw new IllegalArgumentException("Model size not equal to universe size");
	}
	for (int i = 0; i < model.length; i++) {
		if (model[i]) {
			assumption.push(i + 1);
		} else {
			assumption.push(-(i + 1));
		}
	}
	assumption.push(-p);
	return !unsafeIsSatisfiable(assumption);
}
 
Example #4
Source File: Sat4J.java    From SJS with Apache License 2.0 5 votes vote down vote up
@Override
public void addClause(int... literals) {
    int key = allocVars(1);
    try {
        int[] c2 = new int[literals.length + 1];
        System.arraycopy(literals, 0, c2, 1, literals.length);
        c2[0] = -key;
        solver.addClause(new VecInt(c2));
        clausesByKey.put(key, literals);
    } catch (ContradictionException e) {
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
public SATFactory(ISolver s) {
	solver = s;
	andCache = new HashMap<>();
	orCache = new HashMap<>();
	VecInt trueClause = new VecInt();
	trueClause.push(1);
	unsafeAddClause(trueClause);
}
 
Example #6
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
private void unsafeAddClause(VecInt clause) {
	try {
		solver.addClause(clause);
		//System.out.println("Add clause: " + clause.toString());
	} catch (ContradictionException ex) {
		// should never happen
		ex.printStackTrace();
		System.err.println("Contradiction when adding clause: " + clause.toString());
		System.exit(-1);
	}
}
 
Example #7
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
public Integer MkAnd(List<Integer> cube) {
	if (cube.isEmpty()) {
		throw new IllegalArgumentException("mkAnd requires at least one literal");
	} else if (cube.size() == 1) {
		return cube.get(0);
	} else if (andCache.containsKey(cube)) {
		return andCache.get(cube);
	} else {
		int cubeName = fresh();
		//System.out.println(cubeName + " = And " + cube.toString());
		VecInt cubeImpliesCubeName = new VecInt();
	
		cubeImpliesCubeName.push(cubeName);
		for (Integer literal : cube) {
			// cubeName => literal
			VecInt cubeNameImpliesLit = new VecInt();
			cubeNameImpliesLit.push(-cubeName);
			cubeNameImpliesLit.push(literal);
			unsafeAddClause(cubeNameImpliesLit);

			cubeImpliesCubeName.push(-literal);
		}
		// cube => cubeName
		unsafeAddClause(cubeImpliesCubeName);
		andCache.put(cube, cubeName);
		return cubeName;
	}
}
 
Example #8
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
public Integer MkOr(List<Integer> clause) {
	if (clause.isEmpty()) {
		throw new IllegalArgumentException("mkOr requires at least one literal");
	} else if (clause.size() == 1) {
		return clause.get(0);
	} else if (orCache.containsKey(clause)) {
		return orCache.get(clause);
	} else {
		int clauseName = fresh();
		//System.out.println(clauseName + " = Or " + clause.toString());
		// clauseName => clause
		VecInt clauseNameImpliesClause = new VecInt();
		clauseNameImpliesClause.push(-clauseName);
		for (Integer literal : clause) {
			// literal => cubeName
			VecInt litImpliesClauseName = new VecInt();

			clauseNameImpliesClause.push(literal);
			litImpliesClauseName.push(clauseName);
			litImpliesClauseName.push(-literal);
			unsafeAddClause(litImpliesClauseName);
		}
		unsafeAddClause(clauseNameImpliesClause);
		orCache.put(clause, clauseName);
		return clauseName;
	}
}
 
Example #9
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
public boolean add(BooleanExpression p, BooleanExpression q) throws TimeoutException {
	VecInt pair = new VecInt();
	pair.push(mkIff(p, q));
	try {
		factory.solver.addClause(pair);
		return true;
	} catch (ContradictionException e) {
		return false;
	}
}
 
Example #10
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
public SATBooleanAlgebra(ISolver s, int universeSize) {
	if (universeSize < 0) {
		throw new IllegalArgumentException("Universe size must be >= 0");
	}
	solver = s;
	universe = universeSize;
	andCache = new HashMap<>();
	orCache = new HashMap<>();
	reverseCache = new HashMap<>();
	maxid = universeSize + 2;
	VecInt trueClause = new VecInt();
	trueClause.push(universe + 1);
	reverseCache.put(universe + 1, new Pair<>(true, new TreeSet<Integer>()));
	unsafeAddClause(trueClause);
}
 
Example #11
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
private void unsafeAddClause(VecInt clause) {
	try {
		solver.addClause(clause);
		// System.out.println("Add clause: " + clause.toString());
	} catch (ContradictionException ex) {
		// should never happen
		ex.printStackTrace();
		System.err.println("Contradiction when adding clause: " + clause.toString());
		System.exit(-1);
	}
}
 
Example #12
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 5 votes vote down vote up
private boolean unsafeIsSatisfiable(VecInt assumptions) {
	try {
		return solver.isSatisfiable(assumptions, false);
	} catch (TimeoutException ex) {
		ex.printStackTrace();
		System.exit(-1);
		return false;
	}
}
 
Example #13
Source File: SATRelation.java    From symbolicautomata with Apache License 2.0 4 votes vote down vote up
public boolean isMember(BooleanExpression p, BooleanExpression q) throws TimeoutException {
	VecInt mem = new VecInt();
	mem.push(-mkIff(p, q));
	return !factory.getSolver().isSatisfiable(mem, false);
}
 
Example #14
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 4 votes vote down vote up
@Override
public boolean AreEquivalent(Integer p, Integer q) {
	VecInt equiv = new VecInt();
	equiv.push(-MkOr(MkAnd(p, q), MkAnd(-p, -q)));
	return !unsafeIsSatisfiable(equiv);
}
 
Example #15
Source File: SATBooleanAlgebra.java    From symbolicautomata with Apache License 2.0 4 votes vote down vote up
@Override
public boolean IsSatisfiable(Integer p) {
	VecInt pclause = new VecInt();
	pclause.push(p);
	return unsafeIsSatisfiable(pclause);
}