Search in sources :

Example 11 with EqualityTheory

use of com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory in project aic-praise by aic-sri-international.

the class RandomConditionalPotentialExpressionGenerator method newTheoryTestingSupport.

private TheoryTestingSupport newTheoryTestingSupport(Random random, RandomHOGMv1Generator.TheoryTypePropositionalArgs[] propositionTheoryArgs, RandomHOGMv1Generator.TheoryTypeEqualityArgs[] equalityTheoryArgs, RandomHOGMv1Generator.TheoryTypeInequalityArgs[] inequalityTheoryArgs) {
    List<Theory> theories = new ArrayList<>();
    if (propositionTheoryArgs.length > 0) {
        theories.add(new PropositionalTheory());
    }
    if (equalityTheoryArgs.length > 0) {
        EqualityTheory equalityTheory;
        if (inequalityTheoryArgs.length == 0) {
            // first flag is 'true' because all equalities are atoms in the final theory; there is no need to check arguments type
            equalityTheory = new EqualityTheory(true, true);
        } else {
            // 'false' because not all equalities are atoms in this final theory; need to check arguments type
            equalityTheory = new EqualityTheory(false, true);
        }
        theories.add(equalityTheory);
    }
    if (inequalityTheoryArgs.length > 0) {
        DifferenceArithmeticTheory differenceArithmeticTheory;
        if (equalityTheoryArgs.length == 0) {
            // first flag is 'true' because all equalities are atoms in the final theory; there is no need to check arguments type
            differenceArithmeticTheory = new DifferenceArithmeticTheory(true, true);
        } else {
            // 'false' because not all equalities are atoms in this final theory; need to check arguments type
            differenceArithmeticTheory = new DifferenceArithmeticTheory(false, true);
        }
        theories.add(differenceArithmeticTheory);
    }
    Theory finalTheory;
    if (theories.size() > 1) {
        finalTheory = new CompoundTheory(theories.toArray(new Theory[theories.size()]));
    } else {
        finalTheory = theories.get(0);
    }
    TheoryTestingSupport result = TheoryTestingSupport.make(random, finalTheory);
    return result;
}
Also used : EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) ArrayList(java.util.ArrayList) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)

Example 12 with EqualityTheory

use of com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithoutDifferenceArithmeticTest method runCompleteSatisfiabilityTest.

/**
	 * @param conjunction
	 * @param expected
	 */
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, Map<String, Type> variableNamesAndTypesForTesting) {
    TheoryTestingSupport equalityTheoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
    equalityTheoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), equalityTheoryTestingSupport, TheoryTestingSupport.make(makeRandom(), new PropositionalTheory()));
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    for (Expression literal : And.getConjuncts(parse(conjunction))) {
        constraint = constraint.conjoin(literal, context);
    }
    assertEquals(expected, constraint);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)

Example 13 with EqualityTheory

use of com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory 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.sgdpllt.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Test(org.junit.Test)

Example 14 with EqualityTheory

use of com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.

the class ExpressionStepSolverToLiteralSplitterStepSolverAdapterTest method testCompoundTheoryWithoutDifferenceArithmeticWithRandomDisjunctiveFormulas.

@Test
public void testCompoundTheoryWithoutDifferenceArithmeticWithRandomDisjunctiveFormulas() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    runRandomDisjunctiveFormulasTest(theoryTestingSupport);
}
Also used : EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Test(org.junit.Test)

Example 15 with EqualityTheory

use of com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory in project aic-expresso by aic-sri-international.

the class ExpressionStepSolverToLiteralSplitterStepSolverAdapterTest method testEqualityTheoryWithPropagationOfAllLiteralsWhenBoundWithRandomDisjunctiveFormulas.

@Test
public void testEqualityTheoryWithPropagationOfAllLiteralsWhenBoundWithRandomDisjunctiveFormulas() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
    extendTestingVaribles("X", theoryTestingSupport, "S", "T", "U", "V", "W");
    runRandomDisjunctiveFormulasTest(theoryTestingSupport);
}
Also used : EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Test(org.junit.Test)

Aggregations

EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)30 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)23 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)20 TheoryTestingSupport (com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport)20 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)20 Test (org.junit.Test)20 Expression (com.sri.ai.expresso.api.Expression)18 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)11 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)8 LinearRealArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory)8 Type (com.sri.ai.expresso.api.Type)7 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)6 LinkedHashMap (java.util.LinkedHashMap)6 Expressions (com.sri.ai.expresso.helper.Expressions)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 StepSolver (com.sri.ai.grinder.sgdpllt.api.StepSolver)4 AbstractTheoryTestingSupport (com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport)4 UnificationStepSolver (com.sri.ai.grinder.sgdpllt.theory.base.UnificationStepSolver)4 Beta (com.google.common.annotations.Beta)3