Search in sources :

Example 21 with Context

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

the class BPTest method main.

public static void main(String[] args) {
    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);
    Model m = IsingModel(3, 4, theory, context, parse("Boolean"));
    // printModel(model);
    runTest(m);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) 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) 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)

Example 22 with Context

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

the class FactorComponent method calculate.

public Expression calculate() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    Expression childrenMessage = parse("{ 1 }");
    for (VariableComponent children : this.children) {
        childrenMessage = apply(TIMES, childrenMessage, children.calculate());
    }
    childrenMessage = apply(TIMES, childrenMessage, this.phi);
    for (Expression cutset : this.cutsetInsideSubModel) {
        // String str = "sum({{ (on " + cutset + " in Boolean ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        // childrenMessage = theory.evaluate(childrenMessage, context);
        Expression valuesTakenByVariableToSum = this.model.getValues(cutset);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, cutset, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, childrenMessage, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        childrenMessage = summation;
    // String str = "sum({{ (on " + variableToSum + " in " + this.model.getValues(variableToSum) +" ) " + childrenMessage + " }})";
    // childrenMessage = parse(str);
    }
    Set<Expression> toSum = model.getNeighbors(phi);
    for (Expression e : this.parent) {
        toSum.remove(e);
    }
    toSum.removeAll(this.cutsetOutsideSubModel);
    toSum.removeAll(this.cutsetInsideSubModel);
    for (Expression variableToSum : toSum) {
        Expression expressionToSum = theory.evaluate(childrenMessage, context);
        Expression valuesTakenByVariableToSum = this.model.getValues(variableToSum);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, variableToSum, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, expressionToSum, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        // String str = "sum({{ (on " + variableToSum + " in " + this.model.getValues(variableToSum) +" ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        childrenMessage = summation;
    }
    return theory.evaluate(childrenMessage, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 23 with Context

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

the class VariableComponent method calculateBound.

public void calculateBound() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    // Expression childrenBound = parse("{ 1 }");
    // for (FactorComponent children : this.children) {
    // childrenBound = Bounds.boundProduct(theory, context, childrenBound, children.bound);
    // }
    Bound[] childrenArray = new Bound[children.size()];
    int i = 0;
    for (FactorComponent children : this.children) {
        childrenArray[i] = children.bound;
        i++;
    }
    Bound childrenBound;
    if (childrenArray.length != 0) {
        childrenBound = Bounds.boundProduct(this.model.theory, this.model.context, isExtensionalBound, childrenArray);
    } else {
        childrenBound = Bounds.makeSingleElementBound(makeSymbol(1), isExtensionalBound);
    }
    Iterator<Expression> iteratorToVariables = this.cutsetInsideSubModel.iterator();
    ArrayList<Expression> variablesToBeSummedOut = new ArrayList<>(this.cutsetInsideSubModel.size());
    for (Expression var : Util.in(iteratorToVariables)) {
        variablesToBeSummedOut.add(var);
    }
    // We want sum other toSum of Phi*childrenBound
    DefaultExtensionalUniSet varToSum = new DefaultExtensionalUniSet(variablesToBeSummedOut);
    bound = childrenBound.summingBound(varToSum, 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)

Example 24 with Context

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

the class VariableComponent method calculate.

public Expression calculate() {
    Theory theory = this.model.theory;
    Context context = this.model.context;
    Expression childrenMessage = parse("1");
    for (FactorComponent children : this.children) {
        childrenMessage = apply(TIMES, childrenMessage, children.calculate());
        childrenMessage = theory.evaluate(childrenMessage, context);
    }
    for (Expression cutsetVariable : this.cutsetInsideSubModel) {
        // childrenMessage = theory.evaluate(childrenMessage, context);
        // String str = "sum({{ (on " + cutsetVariable + " in " + this.model.getValues(cutsetVariable) +" ) " + childrenMessage + " }})";
        // childrenMessage = parse(str);
        Expression valuesTakenByVariableToSum = this.model.getValues(cutsetVariable);
        IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, cutsetVariable, valuesTakenByVariableToSum));
        Expression intensionalMultiSet = IntensionalSet.makeMultiSet(indices, childrenMessage, parse("true"));
        Expression summation = apply(SUM, intensionalMultiSet);
        childrenMessage = summation;
    }
    return theory.evaluate(childrenMessage, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 25 with Context

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

the class ClearExampleEvaluation method main.

public static void main(String[] args) {
    // /// Evaluating expressions
    // The above code shows how to deal with the syntax of expressions.
    // Evaluating expressions requires knowing about the semantics, that is, to what functions each operator corresponds to ("+" to addition, etc).
    // This is provided by a theory, which for now it suffices to know is a collection of methods for evaluating expressions
    // according to an interpretation to some symbols.
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    // Because this evaluation is symbolic, evaluated expressions may involve free variables.
    // In this case, the result of the evaluation will be a simplified expression that
    // is equivalent to the original expression for all possible assignments to the free variables.
    // For example, X + 0*Y is evaluate to X because, for any assignment to (X,Y), X + 0*Y = X.
    // true context: all assignments to free variables are of interest
    Context context = new TrueContext(theory);
    // We will later see how we can use contexts that restrict the free variable assignments of interest.
    context = context.makeNewContextWithAddedType(BOOLEAN_TYPE);
    context = context.extendWithSymbolsAndTypes("B", "Integer");
    context = context.extendWithSymbolsAndTypes("J", "Integer");
    // Now that we have a theory and a context, we can evaluate expressions:
    println("1 + 0*X + 1  =  " + theory.evaluate(parse("1 + 1"), context));
    /*evaluate(new String[] {
				"sum({{ (on C in Boolean) (if C then if A then 50 else 50 else if A then 50 else 50) * (if C then if B then 60 else 40 else if B then 40 else 60) }})", "",
		}, theory, context);
		*/
    Expression test = theory.evaluate(parse("sum({{ (on C in Boolean) (if C then if A then 50 else 50 else if A then 50 else 50) * (if C then if B then 60 else 40 else if B then 40 else 60) }})"), context);
    println(test);
    String str = "sum({{ (on I in 1..10) I : I != J }})";
    Expression expr = parse(str);
    Expression test2 = theory.evaluate(expr, context);
    println(test2);
    // Here's how to do it from scratch, but see next the way we typically actually do it.
    Expression p = makeSymbol("P");
    context = context.extendWithSymbolsAndTypes("P", "Integer");
    IndexExpressionsSet indices = new ExtensionalIndexExpressionsSet(apply(IN, p, parse("1..4")));
    println("plop");
    // The "extensional" in ExtensionalIndexExpressionsSet means that the list/set of indices is extensionally defined,
    // even though they will be the indices of an intensionally defined set.
    Expression intensionalUniSet = // IntensionalSet.intensionalUniSet, or simply intensionalUniSet, also works
    IntensionalSet.makeMultiSet(indices, parse("5"), parse("true"));
    // Note that Equality.make(p, "Rodrigo") is the same as apply(FunctorConstants.EQUAL, p, "Rodrigo").
    // We often have 'make' methods for many operators: And.make, Or.make and so on.
    // packages in com.sri.ai.expresso.grinder.sgdpllt.library have many such operator-specific classes.
    println(intensionalUniSet);
    Expression sum = apply(SUM, intensionalUniSet);
    println(sum);
    Expression resultat = theory.evaluate(sum, context);
    println(resultat);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) 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) TrueContext(com.sri.ai.grinder.core.TrueContext) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14