Search in sources :

Example 36 with Theory

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

the class CompoundTheoryTestingSupport method getTheoryTestingSupport.

private TheoryTestingSupport getTheoryTestingSupport(String variable) {
    Type variableType = getTestingVariableType(variable);
    Theory subConstraintTheory = getTheory().getTheory(parse(variable), variableType);
    check(() -> subConstraintTheory != null, () -> "There is no sub-theory suitable for " + variable + ", which has type " + variableType);
    TheoryTestingSupport result = getTheoryToTestingSupport().get(subConstraintTheory);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.api.Theory) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport)

Example 37 with Theory

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

the class SymbolicShell method makeTheory.

private static Theory makeTheory() {
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory(), new BruteForceFunctionTheory());
    theory = new BruteForceFallbackTheory(theory);
    return theory;
}
Also used : BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory)

Example 38 with Theory

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

the class AbstractModelCountingWithPropagatedLiteralsImportedFromSatisfiabilityStepSolver method getPropagatedCNF.

/**
 * This is overridden to re-use satisfiability's propagated CNF.
 * It overrides caching done by overridden method, but that's fine since satisfiability should be doing that already.
 */
@Override
protected ArrayList<ArrayList<Expression>> getPropagatedCNF(Context context) {
    Theory theory = getConstraint().getTheory();
    ExpressionStepSolver satisfiability = theory.getSingleVariableConstraintSatisfiabilityStepSolver(getConstraint(), context);
    if (satisfiability == null) {
        throw new Error("No solver present for solving satisfiability of " + getConstraint().getVariable());
    }
    AbstractExpressionWithPropagatedLiteralsStepSolver satisfiabilityWithPropagatedLiterals;
    try {
        satisfiabilityWithPropagatedLiterals = (AbstractExpressionWithPropagatedLiteralsStepSolver) satisfiability;
    } catch (ClassCastException e) {
        throw new Error(this.getClass() + " can only be used with theories providing satisfiability context-dependent step solvers" + " that are extensions of " + AbstractExpressionWithPropagatedLiteralsStepSolver.class + ", but theory " + theory.getClass() + " provided instead an instance of" + satisfiability.getClass());
    }
    return satisfiabilityWithPropagatedLiterals.getPropagatedCNF(context);
}
Also used : Theory(com.sri.ai.grinder.api.Theory) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver)

Example 39 with Theory

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

the class CompilationTest method test.

@Test
public void test() {
    Expression input;
    Expression expected;
    Theory theory = new CompoundTheory(new EqualityTheory(true, true), new PropositionalTheory());
    Map<String, String> mapFromCategoricalTypeNameToSizeString;
    Map<String, String> mapFromVariableNameToTypeName;
    Map<String, String> mapFromUniquelyNamedConstantToTypeName;
    input = Expressions.parse("if X = a then if X = b then 1 else 2 else 3");
    expected = parse("if X = a then 2 else 3");
    mapFromCategoricalTypeNameToSizeString = Util.map("Everything", "2");
    mapFromVariableNameToTypeName = Util.map("X", "Everything");
    mapFromUniquelyNamedConstantToTypeName = Util.map("a", "Everything", "b", "Everything");
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
    input = Expressions.parse("" + "if X = a and Y = a then 0.1 else " + "if X = a and Y = b then 0.1 else " + "if X = b and Y = a then 0.2 else " + // + "if X = b and Y = b then 0.2" // no need to test because it is the last case
    "0.2");
    expected = parse("if X = a then 0.1 else 0.2");
    mapFromCategoricalTypeNameToSizeString = Util.map("Everything", "2");
    mapFromVariableNameToTypeName = Util.map("X", "Everything", "Y", "Everything");
    mapFromUniquelyNamedConstantToTypeName = Util.map("a", "Everything", "b", "Everything");
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
    input = Expressions.parse("" + "if X = a and Y = a and Z = a then 0.1 else " + "if X = a and Y = a and Z = b then 0.1 else " + "if X = a and Y = a and Z = c then 0.1 else " + "if X = a and Y = b and Z = a then 0.1 else " + "if X = a and Y = b and Z = b then 0.1 else " + "if X = a and Y = b and Z = c then 0.1 else " + "if X = a and Y = c and Z = a then 0.1 else " + "if X = a and Y = c and Z = b then 0.1 else " + "if X = a and Y = c and Z = c then 0.1 else " + "if X = b and Y = a and Z = a then 0.2 else " + "if X = b and Y = a and Z = b then 0.2 else " + "if X = b and Y = a and Z = c then 0.2 else " + "if X = b and Y = b and Z = a then 0.2 else " + "if X = b and Y = b and Z = b then 0.2 else " + "if X = b and Y = b and Z = c then 0.2 else " + "if X = b and Y = c and Z = a then 0.2 else " + "if X = b and Y = c and Z = b then 0.2 else " + "if X = b and Y = c and Z = c then 0.2 else " + "if X = c and Y = a and Z = a then 0.3 else " + "if X = c and Y = a and Z = b then 0.3 else " + "if X = c and Y = a and Z = c then 0.3 else " + "if X = c and Y = b and Z = a then 0.3 else " + "if X = c and Y = b and Z = b then 0.3 else " + "if X = c and Y = b and Z = c then 0.3 else " + "if X = c and Y = c and Z = a then 0.3 else " + "if X = c and Y = c and Z = b then 0.3 else " + /* X = c and Y = c and Z = c ; no need as it is implied by domain definition */
    "0.3");
    expected = parse("if X = a then 0.1 else if X = b then 0.2 else 0.3");
    mapFromCategoricalTypeNameToSizeString = Util.map("Everything", "3");
    mapFromVariableNameToTypeName = Util.map("X", "Everything", "Y", "Everything", "Z", "Everything");
    mapFromUniquelyNamedConstantToTypeName = Util.map("a", "Everything", "b", "Everything", "c", "Everything");
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
    // Same thing, but with non-capitalized variables that should still be recognized as variables
    input = Expressions.parse("" + "if x = a and y = a and z = a then 0.1 else " + "if x = a and y = a and z = b then 0.1 else " + "if x = a and y = a and z = c then 0.1 else " + "if x = a and y = b and z = a then 0.1 else " + "if x = a and y = b and z = b then 0.1 else " + "if x = a and y = b and z = c then 0.1 else " + "if x = a and y = c and z = a then 0.1 else " + "if x = a and y = c and z = b then 0.1 else " + "if x = a and y = c and z = c then 0.1 else " + "if x = b and y = a and z = a then 0.2 else " + "if x = b and y = a and z = b then 0.2 else " + "if x = b and y = a and z = c then 0.2 else " + "if x = b and y = b and z = a then 0.2 else " + "if x = b and y = b and z = b then 0.2 else " + "if x = b and y = b and z = c then 0.2 else " + "if x = b and y = c and z = a then 0.2 else " + "if x = b and y = c and z = b then 0.2 else " + "if x = b and y = c and z = c then 0.2 else " + "if x = c and y = a and z = a then 0.3 else " + "if x = c and y = a and z = b then 0.3 else " + "if x = c and y = a and z = c then 0.3 else " + "if x = c and y = b and z = a then 0.3 else " + "if x = c and y = b and z = b then 0.3 else " + "if x = c and y = b and z = c then 0.3 else " + "if x = c and y = c and z = a then 0.3 else " + "if x = c and y = c and z = b then 0.3 else " + /* x = c and y = c and z = c ; no need as it is implied by domain definition */
    "0.3");
    expected = parse("if x = a then 0.1 else if x = b then 0.2 else 0.3");
    mapFromCategoricalTypeNameToSizeString = Util.map("Everything", "3");
    mapFromVariableNameToTypeName = Util.map("x", "Everything", "y", "Everything", "z", "Everything");
    mapFromUniquelyNamedConstantToTypeName = Util.map("a", "Everything", "b", "Everything", "c", "Everything");
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
    input = Expressions.parse("" + "if not g0 and (g1 = consg1_0)\r\n" + "then 0.0001\r\n" + "else if not g0 and (g1 = consg1_1)\r\n" + "     then 1\r\n" + "     else if not g0 and (g1 = consg1_2)\r\n" + "          then 0.0001\r\n" + "          else if not g0 and (g1 = consg1_3)\r\n" + "               then 1\r\n" + "               else if g0 and (g1 = consg1_0)\r\n" + "                    then 1\r\n" + "                    else if g0 and (g1 = consg1_1)\r\n" + "                         then 1\r\n" + "                         else if g0 and (g1 = consg1_2)\r\n" + "                              then 1\r\n" + "                              else 1\r\n" + "");
    expected = parse("if not g0 then if g1 = consg1_0 then 0.0001 else if g1 = consg1_1 then 1 else if g1 = consg1_2 then 0.0001 else 1 else 1");
    mapFromCategoricalTypeNameToSizeString = Util.map("G1Type", "4", "Boolean", "2");
    mapFromVariableNameToTypeName = Util.map("g0", "Boolean", "g1", "G1Type");
    mapFromUniquelyNamedConstantToTypeName = Util.map("consg1_0", "G1Type", "consg1_1", "G1Type", "consg1_2", "G1Type", "consg1_3", "G1Type");
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
    input = Expressions.parse("if not g0 then 1 else 1");
    expected = parse("1");
    mapFromCategoricalTypeNameToSizeString = Util.map("G1Type", "4", "Boolean", "2");
    mapFromVariableNameToTypeName = Util.map("g0", "Boolean", "g1", "G1Type");
    mapFromUniquelyNamedConstantToTypeName = Util.map();
    runTest(input, expected, theory, mapFromCategoricalTypeNameToSizeString, mapFromVariableNameToTypeName, mapFromUniquelyNamedConstantToTypeName);
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) Theory(com.sri.ai.grinder.api.Theory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) Test(org.junit.Test)

Example 40 with Theory

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

the class FactorComponent method calculateBound.

public void calculateBound() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    // Expression childrenBound = parse("{1}");
    // for (VariableComponent children : this.children) {
    // childrenBound = Bounds.boundProduct(this.model.theory, this.model.context, childrenBound, children.bound);
    // }
    Bound[] cildrenArray = new Bound[children.size()];
    int i = 0;
    for (VariableComponent children : this.children) {
        cildrenArray[i] = children.bound;
        i++;
    }
    Bound childrenBound = Bounds.boundProduct(this.model.theory, this.model.context, isExtensionalBound, cildrenArray);
    Set<Expression> toSum = model.getNeighbors(phi);
    for (Expression e : this.parent) {
        toSum.remove(e);
    }
    for (Expression e : this.cutsetOutsideSubModel) {
        toSum.remove(e);
    }
    toSum.addAll(this.cutsetInsideSubModel);
    ArrayList<Expression> variablesToBeSummedOut = new ArrayList<>(toSum.size());
    variablesToBeSummedOut.addAll(toSum);
    // We want sum other toSum of Phi*childrenBound
    DefaultExtensionalUniSet varToSum = new DefaultExtensionalUniSet(variablesToBeSummedOut);
    bound = childrenBound.summingPhiTimesBound(varToSum, phi, context, theory);
}
Also used : Context(com.sri.ai.grinder.api.Context) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) Bound(com.sri.ai.grinder.library.bounds.Bound) ArrayList(java.util.ArrayList) DefaultExtensionalUniSet(com.sri.ai.expresso.core.DefaultExtensionalUniSet)

Aggregations

Theory (com.sri.ai.grinder.api.Theory)46 Expression (com.sri.ai.expresso.api.Expression)32 Context (com.sri.ai.grinder.api.Context)23 Type (com.sri.ai.expresso.api.Type)14 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)7 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)7 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)7 TrueContext (com.sri.ai.grinder.core.TrueContext)7 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)7 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)7 BruteForceCommonInterpreter (com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter)7 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)7 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)7 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)7 Beta (com.google.common.annotations.Beta)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)6 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)6 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)6 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)6