Search in sources :

Example 26 with EqualityTheory

use of com.sri.ai.grinder.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.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 27 with EqualityTheory

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

the class ExpressionStepSolverToLiteralSplitterStepSolverAdapterTest method testCompoundTheoryWithDifferenceArithmeticWithRandomDisjunctiveFormulas.

@Test
public void testCompoundTheoryWithDifferenceArithmeticWithRandomDisjunctiveFormulas() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    // using different testing variables and types to test distribution of testing information
    // to sub constraint theories.
    Categorical booleanType = BOOLEAN_TYPE;
    Categorical dogsType = new Categorical("Dogs", 4, arrayList(parse("fido"), parse("rex")));
    IntegerInterval oneTwoThree = new IntegerInterval(1, 3);
    Map<String, Type> variablesAndTypes = map("F", booleanType, "G", booleanType, "R", dogsType, "S", dogsType, "T", oneTwoThree, "U", oneTwoThree);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    runRandomDisjunctiveFormulasTest(theoryTestingSupport);
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) Categorical(com.sri.ai.expresso.type.Categorical) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) Test(org.junit.Test)

Example 28 with EqualityTheory

use of com.sri.ai.grinder.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.theory.equality.EqualityTheory) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) Test(org.junit.Test)

Example 29 with EqualityTheory

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

the class Tests method main.

public static void main(String[] args) {
    // Theory initialization
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("A", "Boolean");
    Model m;
    String modelName;
    Triple<Set<Expression>, Context, Expression> a = IsingModel(4, 4, context, parse("Boolean"));
    println(a);
    m = new Model(a, theory, true);
    Expression b = ModelGenerator.lveCalculation(m);
    println(b);
    // testFunction(modelName, m,true);
    // modelName = "Line Model";
    // m = new Model(lineModel(10, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    // 
    // modelName = "Binary Tree Model";
    // m = new Model(nTreeModel(4, 2, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    // 
    // modelName = "Random Model";
    // m = new Model(ModelGenerator.randomModel(8, 10, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    modelName = "Ising Model";
    List<List<TupleOfData>> listOdModelsToPrintInFile = new ArrayList<>();
    // m = new Model(IsingModel(20, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel2X2 = testing("IsingModel",m,2,2);
    // listOdModelsToPrintInFile.add(IsingModel2X2);
    // println("ok");
    // 
    // m = new Model(IsingModel(3, 3, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel3X3 = testing("IsingModel",m,3,3);
    // listOdModelsToPrintInFile.add(IsingModel3X3);
    // println("ok");
    // 
    // m = new Model(IsingModel(3, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel3X4 = testing("IsingModel",m,3,4);
    // listOdModelsToPrintInFile.add(IsingModel3X4);
    // println("ok");
    // 
    // m = new Model(IsingModel(4, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel4X4 = testing("IsingModel",m,4,4);
    // listOdModelsToPrintInFile.add(IsingModel4X4);
    // println("ok");
    // 
    // //		m = new Model(IsingModel(4, 5, context, parse("Boolean")),theory, true);
    // //		List<InferenceResult> IsingModel4X5 = testing("IsingModel",m,4,5);
    // //		listOdModelsToPrintInFile.add(IsingModel4X5);
    // //		println("ok");
    // 
    // modelName = "Line Model";
    // m = new Model(lineModel(20, context, parse("Boolean")),theory, true);
    // List<InferenceResult> line10 = testing(modelName,m,4,5);
    // listOdModelsToPrintInFile.add(line10);
    // println("ok");
    modelName = "Binary Tree Model";
    m = new Model(IsingModel(4, 4, context, parse("Boolean")), theory, true);
    List<TupleOfData> btree = testing(modelName, m, 4, 5);
    listOdModelsToPrintInFile.add(btree);
    println("ok");
    testingAndWritingToFile(modelName + ".csv", listOdModelsToPrintInFile);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Set(java.util.Set) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) 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) ArrayList(java.util.ArrayList) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) IsingModel(anytimeExactBeliefPropagation.ModelGenerator.IsingModel) Model(anytimeExactBeliefPropagation.Model.Model) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with EqualityTheory

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

the class EvaluationTest method testEvaluationOfCardinalityExpressions.

@Test
public void testEvaluationOfCardinalityExpressions() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Map<String, Type> variablesAndTypes = new LinkedHashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
    Type booleanType = variablesAndTypes.get("P");
    variablesAndTypes.put("S", booleanType);
    variablesAndTypes.put("T", booleanType);
    variablesAndTypes.put("U", booleanType);
    theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    String expressionString;
    Expression expected;
    expressionString = "| {{ (on I in 1..10) 3 : I != 4 and P }} |";
    expected = parse("if P then 9 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10 : I != 4 and P |";
    expected = parse("if P then 9 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on ) 3 : I != 4 and P }} |";
    expected = parse("if I != 4 then if P then 1 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| : I != 4 and P |";
    expected = parse("if I != 4 then if P then 1 else 0 else 0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on ) 3 : P and not P }} |";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "| : P and not P |";
    expected = parse("0");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on I in 1..10, J in 1..2) 3 : I != 4 }} |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10, J in 1..2 : I != 4 |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| {{ (on I in 1..10, P in Boolean) 3 : I != 4 }} |";
    expected = parse("18");
    runTest(expressionString, expected, context);
    expressionString = "| I in 1..10, P in Boolean: I != 4 |";
    expected = parse("18");
    runTest(expressionString, expected, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)37 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)30 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)28 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)27 Context (com.sri.ai.grinder.api.Context)22 Expression (com.sri.ai.expresso.api.Expression)21 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)20 Test (org.junit.Test)18 TrueContext (com.sri.ai.grinder.core.TrueContext)15 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)15 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)11 Theory (com.sri.ai.grinder.api.Theory)10 Type (com.sri.ai.expresso.api.Type)8 LinkedHashMap (java.util.LinkedHashMap)7 Expressions (com.sri.ai.expresso.helper.Expressions)4 FunctionType (com.sri.ai.expresso.type.FunctionType)4 StepSolver (com.sri.ai.grinder.api.StepSolver)4 AbstractTheoryTestingSupport (com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport)4 UnificationStepSolver (com.sri.ai.grinder.theory.base.UnificationStepSolver)4 Before (org.junit.Before)4