use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class InversionSimplifier method getIndexAndFunctionType.
private static Pair<Expression, FunctionType> getIndexAndFunctionType(Expression functionOnIntensionalSet, Context context) {
IndexExpressionsSet indexExpressionsSet = getIndexExpressions(functionOnIntensionalSet);
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() != 1) {
throw new UnsupportedOperationException("Currently only support singular indices");
}
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressionsSet);
Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
FunctionType functionType = null;
if (type instanceof FunctionType) {
functionType = (FunctionType) type;
}
Pair<Expression, FunctionType> result = new Pair<>(index, functionType);
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class InversionSimplifier method isInvertible.
private static boolean isInvertible(Expression summationIndexedByFunction, Expression summationIndexFunctionName, FunctionType summationIndexFunctionType, List<Expression> originalQuantifierOrder, List<Expression> inversionQuantifierOrder, Context context) {
boolean result = false;
int indexOfSummationIndexedByFunction = inversionQuantifierOrder.indexOf(summationIndexedByFunction);
// NOTE: we will use the full nested context in order to derive primedUntilUnique variables to ensure
// we have globally unique variables in the expressions (simplifies reading/understanding).
Expression lastQuantifierHead = getHead(originalQuantifierOrder.get(originalQuantifierOrder.size() - 1));
Context lastQuantifierHeadContext = context;
for (Expression quantifier : originalQuantifierOrder) {
lastQuantifierHeadContext = lastQuantifierHeadContext.extendWith(getIndexExpressions(quantifier));
}
// Construct E correctly based on quantifiers after summation.
Expression E = lastQuantifierHead;
List<Expression> quantifiersAfter = inversionQuantifierOrder.subList(indexOfSummationIndexedByFunction + 1, inversionQuantifierOrder.size());
for (int i = quantifiersAfter.size() - 1; i >= 0; i--) {
E = quantifyE(E, quantifiersAfter.get(i));
}
// Now compute ocfE
Expression ocfE = SetOfArgumentTuplesForFunctionOccurringInExpression.compute(summationIndexFunctionName, summationIndexFunctionType, E);
// NOTE: only products will bubble up before the summation.
List<Expression> productsBefore = inversionQuantifierOrder.subList(0, indexOfSummationIndexedByFunction);
// Create the two sets of replacement quantifiers before summation indexed by function indices
// to ensure we have disjoint applications.
List<Expression> productsBeforeIndices = new ArrayList<>();
for (Expression productBefore : productsBefore) {
productsBeforeIndices.add(getIndexAndType(productBefore).first);
}
List<Expression> allBeforeIndices = new ArrayList<>(productsBeforeIndices);
List<Expression> productsBeforeIndicesPrime = new ArrayList<>();
List<Expression> productsBeforeIndices2Prime = new ArrayList<>();
for (int i = 0; i < productsBeforeIndices.size(); i++) {
Expression productBeforeIndex = productsBeforeIndices.get(i);
Expression allIndicesTuple = Expressions.makeTuple(allBeforeIndices);
Expression productBeforeIndexPrime = Expressions.primedUntilUnique(productBeforeIndex, allIndicesTuple, lastQuantifierHeadContext);
productsBeforeIndicesPrime.add(productBeforeIndexPrime);
allBeforeIndices.add(productBeforeIndexPrime);
allIndicesTuple = Expressions.makeTuple(allBeforeIndices);
Expression productBeforeIndex2Prime = Expressions.primedUntilUnique(productBeforeIndex, allIndicesTuple, lastQuantifierHeadContext);
productsBeforeIndices2Prime.add(productBeforeIndex2Prime);
allBeforeIndices.add(productBeforeIndex2Prime);
}
// Create the antecendant part of implication for condition testing inversion
// i.e.
// C_1[x_1/x'_1] and ... C_k[x_k/x'_k]
// and C_1[x_1/x''_1] and ... C_k[x_k/x''_k]
// and (x'_1,...,x'_k) != (x''_1,...,x''_k)
List<Expression> conjunctsPrime = new ArrayList<>();
List<Expression> conjuncts2Prime = new ArrayList<>();
for (int i = 0; i < productsBefore.size(); i++) {
Expression quantifierBefore = productsBefore.get(i);
Expression condition = getCondition(quantifierBefore);
// C_n[x_n/x'_n]
Expression conjunctPrime = replaceAll(condition, productsBeforeIndices, productsBeforeIndicesPrime, lastQuantifierHeadContext);
conjunctsPrime.add(conjunctPrime);
// C_n[x_n/x''_n]
Expression conjunct2Prime = replaceAll(condition, productsBeforeIndices, productsBeforeIndices2Prime, lastQuantifierHeadContext);
conjuncts2Prime.add(conjunct2Prime);
}
// (x'_1,...,x'_k) != (x''_1,...,x''_k)
Expression primesNotEqual = Disequality.make(Expressions.makeTuple(productsBeforeIndicesPrime), Expressions.makeTuple(productsBeforeIndices2Prime));
List<Expression> allConjuncts = new ArrayList<>();
allConjuncts.addAll(conjunctsPrime);
allConjuncts.addAll(conjuncts2Prime);
allConjuncts.add(primesNotEqual);
Expression conjunct = And.make(allConjuncts);
// Create the consequent part of implication for condition testing inversion
// i.e.:
// (oc_f[E][x_1/x'_1,....,x_k/x'_k]
// intersection
// oc_f[E][x_1/x''_1,....,x_k/x''_k])
// = {}
Expression ocfEPrime = replaceAll(ocfE, productsBeforeIndices, productsBeforeIndicesPrime, lastQuantifierHeadContext);
Expression ocfE2Prime = replaceAll(ocfE, productsBeforeIndices, productsBeforeIndices2Prime, lastQuantifierHeadContext);
Expression intersection = Sets.makeIntersection(ocfEPrime, ocfE2Prime);
Expression equality = Equality.make(intersection, Sets.EMPTY_SET);
Expression implication = Implication.make(conjunct, equality);
// Collect the index expressions for the universal quantifiers:
// i.e.
// for all x'_1 el. T_1 ... for all x'_k el. T_k for all x''_1 el. T_1 ... for all x''_k el. T_k
List<Expression> productsBeforeIndexExpressionSetsPrime = new ArrayList<>();
List<Expression> productsBeforeIndexExpressionSets2Prime = new ArrayList<>();
for (int i = 0; i < productsBefore.size(); i++) {
Expression productBefore = productsBefore.get(i);
Expression productBeforeIndexType = getIndexAndType(productBefore).second;
Expression indexExpressionPrime = IndexExpressions.makeIndexExpression(productsBeforeIndicesPrime.get(i), productBeforeIndexType);
productsBeforeIndexExpressionSetsPrime.add(indexExpressionPrime);
Expression indexExpression2Prime = IndexExpressions.makeIndexExpression(productsBeforeIndices2Prime.get(i), productBeforeIndexType);
productsBeforeIndexExpressionSets2Prime.add(indexExpression2Prime);
}
List<Expression> forAllIndexExpressionSets = new ArrayList<>();
forAllIndexExpressionSets.addAll(productsBeforeIndexExpressionSetsPrime);
forAllIndexExpressionSets.addAll(productsBeforeIndexExpressionSets2Prime);
// Construct the nested for all statement.
Expression forAll = implication;
for (int i = forAllIndexExpressionSets.size() - 1; i >= 0; i--) {
Expression forAllIndexExpressionSet = forAllIndexExpressionSets.get(i);
forAll = ForAll.make(forAllIndexExpressionSet, forAll);
}
Expression forAllEvaluated = context.getTheory().evaluate(forAll, context);
if (Expressions.TRUE.equals(forAllEvaluated)) {
result = true;
}
return result;
}
use of com.sri.ai.grinder.api.Context 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;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class PartitionTree method messageFromVariableToFactor.
public Bound messageFromVariableToFactor() {
Bound childrenProduct = this.childrenProduct();
Context context = this.model.getContext();
Theory theory = this.model.getTheory();
ArrayList<Expression> varToSum = this.getVarToSumInMessageFromVariableToFactor();
return childrenProduct.summingBound(varToSum, context, theory);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class Assignment method extendAssignments.
/**
* Sets the value assignment to a given expression in the binding mechanism stored in the context.
* @param newAssignment
* @param context
* @return
*/
public static Context extendAssignments(Map<Expression, Expression> newAssignments, Context context) {
@SuppressWarnings("unchecked") Map<Expression, Expression> assignments = (Map<Expression, Expression>) context.getGlobalObject(ContextAssignmentLookup.ASSIGNMENTS_GLOBAL_OBJECTS_KEY);
Map<Expression, Expression> extendedAssignments;
if (assignments == null) {
extendedAssignments = newAssignments;
} else {
extendedAssignments = new StackedHashMap<>(newAssignments, assignments);
}
Context result = ContextAssignmentLookup.setAssignments(context, extendedAssignments);
return result;
}
Aggregations