Search in sources :

Example 16 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class IncompleteMultiVariableConstraint 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.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 17 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method testModelCountingForSingleVariableConstraints.

/**
 * Given a theory and a number <code>n</code> of single-variable constraint tests,
 * generates <code>n</code> formulas in the theory
 * and see if the model counting solver works (checked by brute force).
 * Throws an {@link Error} with the failure description if a test fails.
 * @param theoryTestingSupport
 * @param numberOfTests
 * @param maxNumberOfLiterals
 * @param outputCount
 */
public static void testModelCountingForSingleVariableConstraints(boolean testAgainstBruteForce, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Expression variable = parse(theoryTestingSupport.pickTestingVariableAtRandom());
    NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(variable, context);
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
    TestRunner tester = (ls, c, tB, cT, p) -> runModelCountingTestForSingleVariableConstraint(variable, ls, c, tB, cT.getTheory(), p);
    runTesterGivenOnSuccessiveConjunctionsOfLiterals("model counting", tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 18 with Constraint

use of com.sri.ai.grinder.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 : IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 19 with Constraint

use of com.sri.ai.grinder.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()), context);
    for (int i = 1; i != numberOfTests + 1; i++) {
        tester.runOneTest(list(), emptyConstraint, testAgainstBruteForce, theoryTestingSupport, context);
        indicateCompletionOfTest(outputCount, problemName, i, testAgainstBruteForce, theoryTestingSupport);
    }
}
Also used : Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) IncompleteMultiVariableConstraint(com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint)

Example 20 with Constraint

use of com.sri.ai.grinder.api.Constraint in project aic-expresso by aic-sri-international.

the class AbstractSingleVariableConstraintWithBinaryAtomsIncludingEquality method conjoinNonTrivialNormalizedEqualityToConstraintWithNonBoundVariable.

protected AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjoinNonTrivialNormalizedEqualityToConstraintWithNonBoundVariable(boolean sign, Expression normalizedAtom, Context context) {
    Constraint result;
    // first, use super's implementation to detect inconsistencies
    AbstractSingleVariableConstraintWithDependentNormalizedAtoms conjunctionWithSignAndNormalizedAtom = super.conjoinNonTrivialSignAndNormalizedAtom(sign, normalizedAtom, context);
    if (conjunctionWithSignAndNormalizedAtom == null) {
        result = makeContradiction();
    } else {
        Expression binding = normalizedAtom;
        Expression valueVariableIsBoundTo = binding.get(1);
        // create a fresh constraint with the binding only and external literals
        result = makeSimplification(arrayList(binding), arrayList(), getExternalLiterals());
        // convert all other normalized atoms to external literals with valueVariableIsBoundTo standing for constraint's variable
        result = conjoinWithSignAndNormalizedAtomsOnValueVariableIsBoundTo(result, true, in(getPositiveNormalizedAtomsIncludingImplicitOnes(context)), valueVariableIsBoundTo, context);
        if (result != null) {
            result = conjoinWithSignAndNormalizedAtomsOnValueVariableIsBoundTo(result, false, in(getNegativeNormalizedAtomsIncludingImplicitOnes(context)), valueVariableIsBoundTo, context);
        }
    }
    return (AbstractSingleVariableConstraintWithDependentNormalizedAtoms) result;
}
Also used : Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractSingleVariableConstraintWithDependentNormalizedAtoms(com.sri.ai.grinder.core.constraint.AbstractSingleVariableConstraintWithDependentNormalizedAtoms)

Aggregations

Constraint (com.sri.ai.grinder.api.Constraint)22 Expression (com.sri.ai.expresso.api.Expression)16 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)15 Context (com.sri.ai.grinder.api.Context)11 IncompleteMultiVariableConstraint (com.sri.ai.grinder.core.constraint.IncompleteMultiVariableConstraint)10 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)9 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)9 LinkedHashSet (java.util.LinkedHashSet)9 Beta (com.google.common.annotations.Beta)8 Type (com.sri.ai.expresso.api.Type)8 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)8 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)8 Expressions.getVariablesBeingReferenced (com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced)8 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)8 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)8 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)8 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)8 Theory (com.sri.ai.grinder.api.Theory)8 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)8 AssignmentMapsIterator (com.sri.ai.grinder.helper.AssignmentMapsIterator)8