use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method simplex.
public static DefaultIntensionalBound simplex(List<Expression> variables, Theory theory, Context context) {
if (variables.size() == 0) {
return new DefaultIntensionalBound();
}
Expression one = makeSymbol("1");
Expression zero = makeSymbol("0");
List<Expression> indexExpressionsList = new ArrayList<>(variables.size());
// ExtensionalIndexExpressionsSet indexExpressions = GrinderUtil.makeIndexExpressionsForIndicesInListAndTypesInRegistry(Variables, context);
Expression head = one;
for (Expression var : variables) {
Expression index = Expressions.primedUntilUnique(var, var, context);
Expression type = context.getTypeExpressionOfRegisteredSymbol(var);
Expression indexExpression = IndexExpressions.makeIndexExpression(index, type);
indexExpressionsList.add(indexExpression);
head = apply(IF_THEN_ELSE, apply(EQUAL, var, index), head, zero);
}
Expression noCondition = makeSymbol(true);
ExtensionalIndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(indexExpressionsList);
DefaultIntensionalBound simplex = new DefaultIntensionalBound(indexExpressions, head, noCondition);
return simplex;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method summingPhiTimesBound.
private DefaultIntensionalBound summingPhiTimesBound(Expression variablesToBeSummedOut, Expression phi, Bound bound, Context context, Theory theory) {
if (!bound.isIntensionalBound()) {
return null;
}
DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
for (Expression indexExpression : indexExpressions.getList()) {
Expression index = indexExpression.get(0);
Expression type = indexExpression.get(1);
context = context.extendWithSymbolsAndTypes(index, type);
}
Expression x = makeSymbol("l");
Expression f = apply(TIMES, x, phi);
DefaultIntensionalBound fOfBound = applyFunctionToBound(f, x, bound, theory, context);
DefaultIntensionalBound result = summingBound(variablesToBeSummedOut, fOfBound, context, theory);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method normalize.
private DefaultIntensionalBound normalize(Bound bound, Theory theory, Context context) {
if (!bound.isIntensionalBound()) {
return null;
}
DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
Expression Head = intensionalBound.getHead();
Expression condition = intensionalBound.getCondition();
Set<Expression> hashSetOfindexVariables = Util.set();
ExtensionalIndexExpressionsSet freeVariablesOfTheHead = (ExtensionalIndexExpressionsSet) getIndexExpressionsOfFreeVariablesIn(Head, context);
for (Expression indexExpression : indexExpressions.getList()) {
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
context = context.extendWithSymbolsAndTypes(index, type);
hashSetOfindexVariables.add(index);
}
ArrayList<Expression> variablesToSumOutList = new ArrayList<>();
for (Expression indexExpression : freeVariablesOfTheHead.getList()) {
// Gambiarra!
Expression index = indexExpression.getFunctorOrSymbol();
if (!hashSetOfindexVariables.contains(index)) {
variablesToSumOutList.add(indexExpression);
}
}
ExtensionalIndexExpressionsSet variablesToSumOut = new ExtensionalIndexExpressionsSet(variablesToSumOutList);
Expression setOfInstantiationsOfTheHead = IntensionalSet.makeMultiSet(variablesToSumOut, // head
Head, // No Condition
makeSymbol(true));
Expression sumOnPhi = apply(SUM, setOfInstantiationsOfTheHead);
// sumOnPhi = theory.evaluate(sumOnPhi, context);
Expression normalizedHead = apply("/", Head, sumOnPhi);
Expression evaluation = theory.evaluate(normalizedHead, context);
DefaultIntensionalBound normalizedIntensionalSet = new DefaultIntensionalBound(indexExpressions, evaluation, condition);
return normalizedIntensionalSet;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method boundProduct.
public static DefaultIntensionalBound boundProduct(Theory theory, Context context, Bound... listOfBounds) {
if (listOfBounds.length == 0) {
DefaultIntensionalBound result = new DefaultIntensionalBound();
return result;
}
Set<Expression> alreadyDefined = Util.set();
alreadyDefined.addAll(context.getSymbols());
Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
ArrayList<Expression> productIndexExpressionList = new ArrayList<>();
Object[] productHeadArray = new Expression[listOfBounds.length];
Object[] productConditionArray = new Expression[listOfBounds.length];
int k = 0;
for (Bound bound : Arrays.asList(listOfBounds)) {
if (!bound.isIntensionalBound()) {
return null;
}
DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
Expression Head = intensionalBound.getHead();
Expression condition = intensionalBound.getCondition();
ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
for (int i = 0; i != newIndexExpressionsList.size(); i++) {
Expression indexExpression = newIndexExpressionsList.get(i);
Symbol index = (Symbol) indexExpression.get(0);
Expression type = indexExpression.get(1);
PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, Head);
Expression newIndex = newIndexAndNewExpressionInScope.first;
Head = newIndexAndNewExpressionInScope.second;
// type should not contain the index
Expression newIndexExpression = apply(IN, newIndex, type);
context = context.extendWithSymbolsAndTypes(newIndex, type);
newIndexExpressionsList.set(i, newIndexExpression);
alreadyDefined.add(newIndex);
for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
Expression anotherIndexExpression = newIndexExpressionsList.get(j);
Expression anotherIndex = anotherIndexExpression.get(0);
Expression anotherType = anotherIndexExpression.get(1);
Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, context);
// anotherIndex is a symbols and does not contain index
Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
newIndexExpressionsList.set(j, newAnotherIndexExpression);
}
}
productIndexExpressionList.addAll(newIndexExpressionsList);
productHeadArray[k] = Head;
productConditionArray[k] = condition;
k++;
}
Expression productCondition = apply(AND, productConditionArray);
productCondition = theory.evaluate(productCondition, context);
Expression productHead = apply(TIMES, productHeadArray);
productHead = theory.evaluate(productHead, context);
DefaultIntensionalBound result = new DefaultIntensionalBound(productIndexExpressionList, productHead, productCondition);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultIntensionalBound method summingBound.
private DefaultIntensionalBound summingBound(Expression variablesToBeSummedOut, Bound bound, Context context, Theory theory) {
if (!bound.isIntensionalBound()) {
return null;
}
DefaultIntensionalBound intensionalBound = (DefaultIntensionalBound) bound;
ExtensionalIndexExpressionsSet indexExpressions = (ExtensionalIndexExpressionsSet) intensionalBound.getIndexExpressions();
for (Expression indexExpression : indexExpressions.getList()) {
Expression index = indexExpression.get(0);
Expression type = indexExpression.get(1);
context = context.extendWithSymbolsAndTypes(index, type);
}
Expression x = makeSymbol("variableX");
IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(variablesToBeSummedOut, context);
Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
x, // No Condition
makeSymbol(true));
Expression f = apply(SUM, setOfFactorInstantiations);
DefaultIntensionalBound result = applyFunctionToBound(f, x, bound, theory, context);
result = normalize(result, theory, context);
return result;
}
Aggregations