use of com.sri.ai.expresso.api.Symbol in project aic-expresso by aic-sri-international.
the class SamplingProceduralAttachmentSingleQuantifierEliminator method makeAndRegisterProceduralAttachment.
private Symbol makeAndRegisterProceduralAttachment(int arity, Context context) {
Integer counter = context.updateInplaceGlobalObject(COUNTER_KEY, () -> 0, c -> c.intValue() + 1);
Simplifier samplerRewriter = (Simplifier) (e, c) -> {
sampler.setContext(c);
Expression result = sampler.next();
return result;
};
Symbol samplerFunctor = makeSymbol("sampler" + counter);
registerProceduralAttachment(samplerFunctor, arity, samplerRewriter, context);
return samplerFunctor;
}
use of com.sri.ai.expresso.api.Symbol in project aic-expresso by aic-sri-international.
the class SamplingAdderLazyIterator method multiplyByMeasure.
private Expression multiplyByMeasure(Expression average) {
Symbol measureExpression = makeSymbol(problem.getMeasure(sumOfSamplesIterator.context));
Expression result = problem.getGroup().addNTimes(average, measureExpression, sumOfSamplesIterator.context);
return result;
}
use of com.sri.ai.expresso.api.Symbol 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.api.Symbol 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.api.Symbol in project aic-expresso by aic-sri-international.
the class AbstractSingleVariableConstraintWithBinaryAtoms method getEquivalentSignAndNormalFunctor.
/**
* Determine which normal functor to use, possibly negating functor if non-normal
* @param sign
* @param atom
* @return
*/
private Pair<Boolean, Expression> getEquivalentSignAndNormalFunctor(boolean sign, Expression atom) {
Expression normalFunctor;
Expression functor = atom.getFunctor();
String functorString = functor.toString();
if (getNormalFunctors().contains(functorString)) {
normalFunctor = functor;
} else {
Symbol negatedFunctor = makeSymbol(getNegationFunctor(functorString));
myAssert(() -> getNormalFunctors().contains(negatedFunctor), () -> getClass().getSimpleName() + ": requires negation of non-normal functors to be normal functors, but got non-normal '" + functor + "' which has non-normal negation '" + negatedFunctor + "'");
normalFunctor = negatedFunctor;
// flip sign since we negated the functor
sign = !sign;
}
Pair<Boolean, Expression> signAndNormalFunctor = Pair.make(sign, normalFunctor);
return signAndNormalFunctor;
}
Aggregations