Search in sources :

Example 21 with Theory

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

the class Derivative method derivativesOfFactor.

public static Set<Expression> derivativesOfFactor(Expression expression, Expression query, Context context) {
    Theory theory = context.getTheory();
    Set<Expression> variableInFactor = Expressions.freeVariables(expression, context);
    variableInFactor.remove(query);
    Set<Expression> ProbabilitiesFactor = new HashSet<Expression>();
    for (Expression variable : variableInFactor) {
        String str = "";
        Type type = context.getTypeOfRegisteredSymbol(variable);
        Iterator<Expression> valuesInType = type.iterator();
        List<Expression> probability = new ArrayList<Expression>();
        for (Expression values : in(valuesInType)) {
            String s = "prob" + variable.toString() + values.toString();
            probability.add(parse(s));
            context.extendWithSymbolsAndTypes(s, "0..1");
            str = str + "if " + variable + " = " + values.toString() + " then " + parse(s) + " else ";
        }
        str = str + " 0";
        ProbabilitiesFactor.add(parse(str));
    }
    Expression product = expression;
    for (Expression factor : ProbabilitiesFactor) {
        product = apply(TIMES, product, factor);
    }
    Expression evaluation = product;
    for (Expression variable : variableInFactor) {
        IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(variable, context);
        Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
        evaluation, parse("true"));
        Expression sumOnPhi = apply(SUM, setOfFactorInstantiations);
        System.out.println(sumOnPhi);
        evaluation = theory.evaluate(sumOnPhi, context);
    }
    Set<Expression> result = new HashSet<Expression>();
    System.out.println(result);
    for (Expression variable : variableInFactor) {
        Type type = context.getTypeOfRegisteredSymbol(variable);
        Iterator<Expression> valuesInType = type.iterator();
        for (Expression values : in(valuesInType)) {
            String s = "prob" + variable.toString() + values.toString();
            result.add(Derivative.computeDerivative(evaluation, parse(s), context));
        }
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) HashSet(java.util.HashSet)

Example 22 with Theory

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

the class Derivative method ifThenElseCase.

public static Expression ifThenElseCase(Expression expression, Expression variable, Context context) {
    Theory theory = context.getTheory();
    Expression condition = expression.getArguments().get(0);
    Expression trueBranch = expression.getArguments().get(1);
    Expression falseBranch = expression.getArguments().get(2);
    Set<Expression> conditionVariables = Expressions.freeVariables(condition, context);
    if (!conditionVariables.contains(variable)) {
        Expression toEvaluate = IfThenElse.make(condition, Derivative.computeDerivative(trueBranch, variable, context), Derivative.computeDerivative(falseBranch, variable, context));
        return theory.simplify(toEvaluate, context);
    } else {
        Expression functor = condition.getFunctor();
        if (functor == parse("=")) {
            Expression toEvaluate = IfThenElse.make(condition, parse("Undefined"), Derivative.computeDerivative(falseBranch, variable, context));
            return theory.simplify(toEvaluate, context);
        }
        if (functor == parse("!=")) {
            Expression toEvaluate = IfThenElse.make(condition, Derivative.computeDerivative(trueBranch, variable, context), parse("Undefined"));
            return theory.simplify(toEvaluate, context);
        }
        if (functor == parse("<=") || functor == parse(">=")) {
            Expression toEvaluate = IfThenElse.make(condition, IfThenElse.make(apply("=", condition.getArguments().get(0), condition.getArguments().get(1)), parse("Undefined"), Derivative.computeDerivative(trueBranch, variable, context)), Derivative.computeDerivative(falseBranch, variable, context));
            return theory.simplify(toEvaluate, context);
        }
        if (functor == parse("<") || functor == parse(">")) {
            Expression toEvaluate = IfThenElse.make(condition, Derivative.computeDerivative(trueBranch, variable, context), IfThenElse.make(apply("=", condition.getArguments().get(0), condition.getArguments().get(1)), parse("Undefined"), Derivative.computeDerivative(falseBranch, variable, context)));
            return theory.simplify(toEvaluate, context);
        }
    }
    return parse("UndefinedIfThenElseFunctor");
}
Also used : Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression)

Example 23 with Theory

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

the class Derivative method sumCase.

public static Expression sumCase(Expression expression, Expression variable, Context context) {
    Theory theory = context.getTheory();
    List<Expression> arguments = expression.getArguments();
    Expression sum = arguments.get(1);
    for (int i = 2; i < arguments.size(); i++) {
        sum = apply("+", sum, arguments.get(i));
    }
    Expression toEvaluate = apply("+", computeDerivative(arguments.get(0), variable, context), computeDerivative(sum, variable, context));
    return theory.simplify(toEvaluate, context);
}
Also used : Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression)

Example 24 with Theory

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

the class Derivative method puissCase.

public static Expression puissCase(Expression expression, Expression variable, Context context) {
    Theory theory = context.getTheory();
    List<Expression> arguments = expression.getArguments();
    Expression factor = arguments.get(1);
    for (int i = 2; i < arguments.size(); i++) {
        factor = apply("^", factor, arguments.get(i));
    }
    Expression toEvaluate = apply("*", Derivative.computeDerivative(apply("*", factor, apply("ln", arguments.get(0))), variable, context), expression);
    return theory.simplify(toEvaluate, context);
}
Also used : Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression)

Example 25 with Theory

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

the class PartitionTree method childrenProduct.

public Bound childrenProduct() {
    Theory theory = model.getTheory();
    Context context = model.getContext();
    Bound[] childrenArray = new Bound[children.size()];
    int i = 0;
    for (PartitionTree children : this.children) {
        if (children.node.getBound() == null) {
            return this.simplexOfNode();
        }
        childrenArray[i] = children.node.getBound();
        i++;
    }
    // Util.println(theory);
    // Util.println(context);
    // Util.println(childrenArray);
    // TODO to modify
    Bound childrenBound = Bounds.boundProduct(theory, context, true, childrenArray);
    return childrenBound;
}
Also used : Context(com.sri.ai.grinder.api.Context) Theory(com.sri.ai.grinder.api.Theory) Bound(com.sri.ai.grinder.library.bounds.Bound)

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