use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method runGroupProblemSolvingTesterOnEmptyConstraint.
/**
* Same as {@link #runGroupProblemSolvingTesterForSuccessiveConstraints(String, TestRunner, boolean, AssociativeCommutativeGroup, TheoryTestingSupport, long, int, boolean)},
* but running the tester on a single, empty constraint.
* @param problemName
* @param tester
* @param testAgainstBruteForce
* @param group
* @param theoryTestingSupport
* @param numberOfTests
* @param outputCount
* @throws Error
*/
private static void runGroupProblemSolvingTesterOnEmptyConstraint(String problemName, TestRunner tester, boolean testAgainstBruteForce, AssociativeCommutativeGroup group, TheoryTestingSupport theoryTestingSupport, long numberOfTests, boolean outputCount) throws Error {
Context context = theoryTestingSupport.makeContextWithTestingInformation();
SingleVariableConstraint emptyConstraint = theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), theoryTestingSupport.getTheory(), context);
for (int i = 1; i != numberOfTests + 1; i++) {
tester.runOneTest(list(), emptyConstraint, testAgainstBruteForce, theoryTestingSupport, context);
indicateCompletionOfTest(outputCount, problemName, i, testAgainstBruteForce, theoryTestingSupport);
}
}
use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class SGDPLLTTester method addLiteralToConstraintAndTest.
private static Constraint addLiteralToConstraintAndTest(TestRunner tester, Expression literal, Constraint constraint, Collection<Expression> literals, boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, Context context) throws Error {
output("Constraint is " + constraint);
output("Adding " + literal + " (literals added so far: " + join(literals, " and ") + ")");
literals.add(literal);
Constraint newConstraint = constraint.conjoin(literal, context);
output("New constraint is " + newConstraint);
tester.runOneTest(literals, newConstraint, testAgainstBruteForce, theoryTestingSupport, context);
return newConstraint;
}
use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractQuantifierEliminationStepSolver method resultIfLiteralContainsIndex.
protected Step resultIfLiteralContainsIndex(Expression literal, ExpressionLiteralSplitterStepSolver.Step bodyStep, Context contextForBody, Context context) {
// if the splitter contains the index, we must split the quantifier:
// Quant_x:C Body ---> (Quant_{x:C and L} Body) op (Quant_{x:C and not L} Body)
Step result;
Expression solutionValue;
// Here, we need to obtain the new index constraints, for the case in which the splitter literal is true and false,
// to create the corresponding sub-problems, solve them, and combine them.
// However, it is important to remember that bodyStep.getContextSplittingWhenSplitterIsLiteral()
// contains the splitting of contextForBody with the splitter literal,
// so the information on the new index constraints is already there in some form.
// TODO: We current don't have a Constraint-generic way to extract it, but expect to do it in the future.
// For now, we split the index constraint separately
ConstraintSplitting indexConstraintSplitting = new ConstraintSplitting(literal, getIndexConstraint(), context);
Constraint indexConstraintAndLiteral = indexConstraintSplitting.getConstraintAndLiteral();
Constraint indexConstraintAndLiteralNegation = indexConstraintSplitting.getConstraintAndLiteralNegation();
switch(indexConstraintSplitting.getResult()) {
case CONSTRAINT_IS_CONTRADICTORY:
solutionValue = null;
break;
case LITERAL_IS_UNDEFINED:
// (**) IF DELETING THIS MARKER, DELETE ALL THE REFERENCES TO IT IN THIS FILE
// This is where this step solver may return a Solution with literals in it:
// solveSubProblem uses an exhaustive solve.
solutionValue = solveSubProblems(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
break;
case LITERAL_IS_TRUE:
solutionValue = solveSubProblem(makeSubProblem(true, bodyStep, indexConstraintAndLiteral), context);
break;
case LITERAL_IS_FALSE:
solutionValue = solveSubProblem(makeSubProblem(false, bodyStep, indexConstraintAndLiteralNegation), context);
break;
default:
throw new Error("Unrecognized result for " + ConstraintSplitting.class + ": " + indexConstraintSplitting.getResult());
}
if (solutionValue == null) {
result = null;
} else {
result = new Solution(solutionValue);
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class AbstractSGVET method solve.
@Override
public Expression solve(AssociativeCommutativeGroup group, List<Expression> indices, Expression indicesConstraint, Expression body, Context context) {
checkInterrupted();
// Make sure body is simplified and quantifier-free.
body = context.getTheory().evaluate(body, context);
Expression result;
if (getDebug()) {
System.out.println("SGVE(T) input: " + body);
System.out.println("Width : " + width(body, context));
}
AssociativeCommutativeSemiRing semiRing = (AssociativeCommutativeSemiRing) group;
Partition partition;
if (indices.size() < 1) {
partition = null;
} else {
Expression factoredConditionalsExpression = factoredConditionalsWithAbsorbingElseClause(semiRing, body, context);
partition = pickPartition(semiRing, factoredConditionalsExpression, indices, context);
}
if (partition == null) {
if (basicOutput) {
System.out.println("No partition");
}
result = subSolver.solve(group, indices, indicesConstraint, body, context);
} else {
Expression indexSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.first, context);
if (basicOutput) {
System.out.println("Eliminating: " + getFirst(partition.index));
System.out.println("From : " + indexSubProblemExpression);
System.out.println("Width : " + width(indexSubProblemExpression, context) + " out of " + indices.size() + " indices");
}
// We now invoke the subsolver for summing the index out of the factors it is in.
// Ideally, we would reuse the current constraint, but the set of index has changed and the current constraint may
// use an internal representation that depends on its previous set of indices.
// In the future, we should try to re-use that internal representation and re-index it appropriately, but for now
// we rewrite the program in a way that the current constraint becomes a part of the input expression.
// This will be equivalent to using it as a constraint, but will cause the constraint to be re-built.
// BTW, the call to "project" below will also re-context the constraint for the same reason: re-indexing.
// In the future it should also re-use the representation.
// The following transformation is: sum_C E = sum_{true} if C then E else 0
Expression indexSubProblemExpressionWithConstraint = IfThenElse.make(indicesConstraint, indexSubProblemExpression, semiRing.multiplicativeAbsorbingElement());
Expression indexSubProblemSolution = subSolver.solve(group, partition.index, indexSubProblemExpressionWithConstraint, context);
if (basicOutput) {
System.out.println("Solution : " + indexSubProblemSolution + "\n");
}
partition.expressionsOnIndexAndNot.second.add(indexSubProblemSolution);
Expression remainingSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.second, context);
// the constraint is already represented in indexSubProblemSolution
Constraint constraintOnRemainingIndices = context;
result = solve(group, partition.remainingIndices, constraintOnRemainingIndices, remainingSubProblemExpression, context);
result = semiRing.multiply(result, context);
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Constraint in project aic-expresso by aic-sri-international.
the class DefaultMultiVariableConstraint method conjoin.
@Override
public Constraint conjoin(Expression formula, Context context) {
myAssert(() -> context.isLiteral(formula) || formula instanceof Constraint, () -> this.getClass() + " currently only supports conjoining with literals and constraints, but received " + formula);
Constraint result;
if (formula instanceof Constraint) {
result = conjoinWithConjunctiveClause(formula, context);
} else {
result = conjoinWithLiteral(formula, context);
}
return result;
}
Aggregations