Search in sources :

Example 51 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testMax.

@Test
public void testMax() {
    Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
    ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U then if V then 4 else 2 else 3"), context);
    ExpressionVariable u = new DefaultExpressionVariable(parse("U"));
    List<ExpressionVariable> variablesToMaxOut = new ArrayList<>();
    variablesToMaxOut.add(u);
    Factor maxFactor = factorUV.max(variablesToMaxOut);
    assertEquals("if V then 4 else 3", maxFactor.toString());
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ArrayList(java.util.ArrayList) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 52 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testMultiply3.

@Test
public void testMultiply3() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    Factor result = aFactor.multiply(ZERO_FACTOR);
    assertEquals(ZERO_FACTOR, result);
}
Also used : CommonTheory(com.sri.ai.grinder.application.CommonTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CommonTheory(com.sri.ai.grinder.application.CommonTheory) 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) Expression(com.sri.ai.expresso.api.Expression) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 53 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testAdd4.

@Test
public void testAdd4() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    ConstantFactor tenFactor = new ConstantFactor(10.);
    Factor result = aFactor.add(tenFactor);
    assertEquals("10 + a", result.toString());
}
Also used : CommonTheory(com.sri.ai.grinder.application.CommonTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CommonTheory(com.sri.ai.grinder.application.CommonTheory) 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) Expression(com.sri.ai.expresso.api.Expression) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 54 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class ExpressionFactorTest method testAdd3.

@Test
public void testAdd3() {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    Expression a = DefaultSymbol.createSymbol("a");
    ExpressionFactor aFactor = new DefaultExpressionFactor(a, context);
    Factor result = aFactor.add(ZERO_FACTOR);
    assertEquals(aFactor, result);
}
Also used : CommonTheory(com.sri.ai.grinder.application.CommonTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CommonTheory(com.sri.ai.grinder.application.CommonTheory) 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) Expression(com.sri.ai.expresso.api.Expression) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 55 with CommonTheory

use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.

the class UsefulExpressionOperations method main.

public static void main(String[] args) {
    Theory theory = new CommonTheory();
    Context context = new TrueContext(theory);
    // Only one child and one parent, 2 parameters (Param1 and Param2)
    Expression child = parse("Child");
    Expression parent = parse("Parent");
    Expression param1 = parse("Param1");
    Expression param2 = parse("Param2");
    Expression param3 = parse("Param3");
    context = context.extendWithSymbolsAndTypes("Child", "1..5", "Parent", "1..5", "Param1", "Real", "Param2", "Real", "Param3", "Real", "A", "Boolean");
    // Old way: making parameters become constants
    // Predicate<Expression> isUniquelyNamedConstantPredicate = context.getIsUniquelyNamedConstantPredicate();
    // Predicate<Expression> newIsUniquelyNamedConstantPredicate = s -> s.equals(param1) || s.equals(param2) || s.equals(param3) || isUniquelyNamedConstantPredicate.apply(s);
    // context = context.setIsUniquelyNamedConstantPredicate(newIsUniquelyNamedConstantPredicate);
    println("My context:");
    println(context.getSymbolsAndTypes());
    // Expression E = parse("if Child < 5 then Param1 else Param2");
    Expression E = parse("if Parent != 5 then Param1 else Param2");
    // Expression E = parse("if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3");
    // Expression E = parse("if Parent != 5 then if Child < Parent then Param1 else Param2 else Param3");
    println("\nE = " + E + "\n");
    // Gives you <Child in 1..5>
    childIndexExpressionsSet = getIndexExpressionsForIndicesInListAndTypesInRegistry(list(child), context);
    parametersIndexExpressionsSet = getIndexExpressionsForIndicesInListAndTypesInRegistry(list(param1, param2, param3), context);
    Expression F1 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, forAllParametersValues(Equality.make(E, param1)));
    println("F1 = " + F1);
    println(context.evaluate(F1) + "\n");
    // Old way of computing families (taking the parameters as constants):
    // Expression F1 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, Equality.make(E, param1));
    // println("F1 = " + F1);
    // println(context.evaluate(F1) + "\n");
    Expression F2 = new DefaultExistentiallyQuantifiedFormula(childIndexExpressionsSet, forAllParametersValues(Equality.make(E, param2)));
    println("F2 = " + F2);
    println(context.evaluate(F2) + "\n");
    Expression F1intersectsF2 = verifyEquivalenceAndGetIntersectionCondition(F1, F2, context);
    println("F1intersectsF2 = " + F1intersectsF2);
    println(context.evaluate(F1intersectsF2));
    // Normalization for Parame1_1
    Expression multiset = new DefaultIntensionalMultiSet(childIndexExpressionsSet, child, forAllParametersValues(Equality.make(E, param1)));
    Expression cardinality = apply(CARDINALITY, multiset);
    println("\nCardinality = " + cardinality);
    Expression cardinalityResult = context.evaluate(cardinality);
    println("N for normalizing Param1_1: " + cardinalityResult);
    // Draft for the Java syntax when shattering the families
    LinkedList<Integer> initialFamilies = Util.list(1, 2, 3, 4, 5);
    List<Integer> finalFamilies = Util.list();
    while (!initialFamilies.isEmpty()) {
        int family1 = initialFamilies.removeFirst();
        for (Iterator<Integer> it = initialFamilies.iterator(); it.hasNext(); ) {
            int family2 = it.next();
            if (family2 == 4) {
                it.remove();
            }
        }
        if (family1 == 3)
            initialFamilies.add(7);
        finalFamilies.add(family1);
    }
    println("\n" + finalFamilies);
    // Testing syntax for context (used for method convertToAnExpressionBasedModelAfterLearning in ExpressionBayesianModel)
    ExpressionVariable expressionVariable = new DefaultExpressionVariable(parse("A"));
    println("\nexpressionVariable.toString() = " + expressionVariable.toString());
    Type type = context.getTypeOfRegisteredSymbol(expressionVariable);
    println("type.toString() = " + type.toString());
    println("child.toStringe() = " + child.toString());
    type = context.getTypeOfRegisteredSymbol(child);
    println("type.toString() = " + type.toString());
    // TODO: Below I let 2 comments about Expresso edge case problems, to be studied and fixed later:
    // 1]
    // Comparison that we would like to be false, Expresso problem with constants - to be seen later (TODO)
    // context.evaluate(Equality.make(parse("Param1"), parse("1-Param1"))); // we would like to have "false" as result here but it gives error, that is why we have to use OneMinusParam1 as other parameter
    // 2]
    // Two Expressions that are equals, equality result should be true (from ExpressionBayesianModelTest, testChildParentModel4), but error with parent been canceled out, problem with Expresso - to be seen later
    // also, TODO: see why it is not simplifying 1/Parent to 1/5 in learnedChild below
    Expression expectedChild = parse("if Parent = 5 then 0.2 else if Child > Parent then (((5 - Parent) + 0) / (5 + 0)) / (5 - Parent) else ((Parent + 0) / (5 + 0)) / Parent");
    Expression learnedChild = parse("if Parent < 5 then if Child > Parent then ((-Parent + 5) / ((-Parent + 5) + Parent)) / (-Parent + 5) else (Parent / ((-Parent + 5) + Parent)) / Parent else 1 / Parent");
    // println(context.evaluate(Equality.make(expectedChild, learnedChild)));
    // Problem when using "for all parameters in parametersValues" instead of handling them as constants - the problem was when adding "and (Parent != 5)" (family.condition) at the end of expression bellow
    Expression expression = parse("(if Parent != 5 then if Child < 5 then Param1 else Param2 else Param3)");
    println("\nexpression = " + expression);
    multiset = new DefaultIntensionalMultiSet(childIndexExpressionsSet, child, forAllParametersValues(Equality.make(expression, param1)));
    cardinality = apply(CARDINALITY, multiset);
    Expression numberOfChildValues = cardinality;
    println("numberOfChildValues = " + numberOfChildValues);
    println(context.evaluate(numberOfChildValues));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Theory(com.sri.ai.grinder.api.Theory) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) TrueContext(com.sri.ai.grinder.core.TrueContext) DefaultIntensionalMultiSet(com.sri.ai.expresso.core.DefaultIntensionalMultiSet) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)

Aggregations

Context (com.sri.ai.grinder.api.Context)58 CommonTheory (com.sri.ai.grinder.application.CommonTheory)58 TrueContext (com.sri.ai.grinder.core.TrueContext)58 Expression (com.sri.ai.expresso.api.Expression)55 Test (org.junit.Test)47 Theory (com.sri.ai.grinder.api.Theory)36 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)13 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)13 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)12 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)11 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)9 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)9 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)9 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)9 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)9 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)7 ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)5 ExpressionFactorNetwork (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork)4 ArrayList (java.util.ArrayList)3