use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-praise by aic-sri-international.
the class UAIUtil method convertGenericTableToInstance.
public static Expression convertGenericTableToInstance(FunctionTable functionTable, Expression genericFunctionTableExpr, List<Integer> instanceVarIdxs) {
Expression result = genericFunctionTableExpr;
Context context = new TrueContext();
for (int i = 0; i < functionTable.numberVariables(); i++) {
// Replace the generic variable name with the correct instance name
result = SyntacticSubstitute.replace(result, Expressions.makeSymbol(genericVariableName(i)), Expressions.makeSymbol(instanceVariableName(instanceVarIdxs.get(i))), context);
int varCardinality = functionTable.cardinality(i);
for (int c = 0; c < varCardinality; c++) {
// Replace the generic constants with constants for the variable index (if they differ)
Expression genericConstant = Expressions.makeSymbol(genericConstantValueForVariable(c, i, varCardinality));
Expression instanceConstant = Expressions.makeSymbol(instanceConstantValueForVariable(c, instanceVarIdxs.get(i), varCardinality));
if (!genericConstant.equals(instanceConstant)) {
result = SyntacticSubstitute.replace(result, genericConstant, instanceConstant, context);
}
}
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.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);
}
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) {
childrenMessage = theory.evaluate(childrenMessage, context);
String str = "sum({{ (on " + variableToSum + " in " + this.model.getValues(variableToSum) + " ) " + childrenMessage + " }})";
childrenMessage = parse(str);
}
return theory.evaluate(childrenMessage, context);
}
use of com.sri.ai.grinder.sgdpllt.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);
}
return theory.evaluate(childrenMessage, context);
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class IntensionalSetConditionSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Sets.isIntensionalSet(expression)) {
IntensionalSet intensionalSet = (IntensionalSet) expression;
Expression condition = intensionalSet.getCondition();
if (!(condition.equals(Expressions.TRUE) || condition.equals(Expressions.FALSE))) {
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
Context extendedContext = context.extendWith(indexExpressionsSet);
Expression evaluatedCondition = context.getTheory().evaluate(condition, extendedContext);
if (!evaluatedCondition.equals(condition)) {
result = IntensionalSet.make(Sets.isIntensionalMultiSet(expression) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, indexExpressionsSet, intensionalSet.getHead(), evaluatedCondition);
}
}
}
return result;
}
use of com.sri.ai.grinder.sgdpllt.api.Context in project aic-expresso by aic-sri-international.
the class Measure method get.
public static Rational get(Expression intensionalSetExpression, Context context) {
Rational result;
if (Sets.isIntensionalSet(intensionalSetExpression)) {
IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() == 1) {
Expression evaluatedResult;
Expression intensionalSetIndex = indices.get(0);
Expression intensionalSetHead = intensionalSet.getHead();
if (!intensionalSetHead.equals(intensionalSetIndex)) {
throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
}
Expression intensionalSetCondition = intensionalSet.getCondition();
Context intensionalSetContext = context.extendWith(indexExpressionsSet);
Type indexType = GrinderUtil.getType(intensionalSetIndex, intensionalSetContext);
if (intensionalSetCondition.equals(false)) {
// short circuit known empty sets up front.
evaluatedResult = Expressions.ZERO;
} else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
// NOTE : For Reals can always assume the condition is of this type.
SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
evaluatedResult = realSolver.solve(intensionalSetContext);
} else if (indexType instanceof FunctionType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
}
// measure(co-domain)^measure(domain)
FunctionType indexFunctionType = (FunctionType) indexType;
Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
Rational domainMeasure = Rational.ONE;
for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
domainMeasure = domainMeasure.multiply(argMeasure);
}
evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
} else if (indexType instanceof TupleType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
}
// (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
TupleType indexTupleType = (TupleType) indexType;
Rational elementMeasuresProduct = Rational.ONE;
for (Type elementType : indexTupleType.getElementTypes()) {
Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
}
evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
} else {
Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
evaluatedResult = context.getTheory().evaluate(countingFormula, context);
}
if (Expressions.isNumber(evaluatedResult)) {
result = evaluatedResult.rationalValue();
} else {
throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
}
} else {
throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
}
} else {
throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
}
return result;
}
Aggregations