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;
}
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");
}
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);
}
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);
}
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;
}
Aggregations