Search in sources :

Example 6 with Constraint

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);
    }
}
Also used : CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)

Example 7 with Constraint

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;
}
Also used : Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) DefaultMultiVariableConstraint(com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)

Example 8 with Constraint

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) ConstraintSplitting(com.sri.ai.grinder.sgdpllt.core.constraint.ConstraintSplitting)

Example 9 with Constraint

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;
}
Also used : AssociativeCommutativeSemiRing(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeSemiRing) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint)

Example 10 with Constraint

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;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint)

Aggregations

Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)25 Expression (com.sri.ai.expresso.api.Expression)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)15 CompleteMultiVariableContext (com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext)14 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)12 DefaultMultiVariableConstraint (com.sri.ai.grinder.sgdpllt.core.constraint.DefaultMultiVariableConstraint)10 Beta (com.google.common.annotations.Beta)9 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)9 Simplifier (com.sri.ai.grinder.sgdpllt.rewriter.api.Simplifier)9 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)8 Type (com.sri.ai.expresso.api.Type)8 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)8 And (com.sri.ai.grinder.sgdpllt.library.boole.And)8 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)7 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)7 Expressions.getVariableReferences (com.sri.ai.expresso.helper.Expressions.getVariableReferences)7 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)7 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)7 AssignmentsIterator (com.sri.ai.grinder.helper.AssignmentsIterator)7 GrinderUtil (com.sri.ai.grinder.helper.GrinderUtil)7