Search in sources :

Example 11 with Symbol

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier)

Example 12 with Symbol

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol)

Example 13 with Symbol

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;
}
Also used : Arrays(java.util.Arrays) Model(com.sri.ai.grinder.anytime.Model) Expressions(com.sri.ai.expresso.helper.Expressions) PairOf(com.sri.ai.util.base.PairOf) SUM(com.sri.ai.grinder.library.FunctorConstants.SUM) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) EQUAL(com.sri.ai.grinder.library.FunctorConstants.EQUAL) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) IN(com.sri.ai.grinder.library.FunctorConstants.IN) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) AND(com.sri.ai.grinder.library.FunctorConstants.AND) Context(com.sri.ai.grinder.api.Context) DefaultExtensionalUniSet(com.sri.ai.expresso.core.DefaultExtensionalUniSet) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) IF_THEN_ELSE(com.sri.ai.grinder.library.FunctorConstants.IF_THEN_ELSE) List(java.util.List) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Predicate(com.google.common.base.Predicate) GrinderUtil.getIndexExpressionsOfFreeVariablesIn(com.sri.ai.grinder.helper.GrinderUtil.getIndexExpressionsOfFreeVariablesIn) Util(com.sri.ai.util.Util) TIMES(com.sri.ai.grinder.library.FunctorConstants.TIMES) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) ArrayList(java.util.ArrayList) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 14 with Symbol

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;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) ArrayList(java.util.ArrayList)

Example 15 with Symbol

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Symbol(com.sri.ai.expresso.api.Symbol)

Aggregations

Symbol (com.sri.ai.expresso.api.Symbol)22 Expression (com.sri.ai.expresso.api.Expression)15 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)13 Type (com.sri.ai.expresso.api.Type)6 DefaultSymbol (com.sri.ai.expresso.core.DefaultSymbol)6 Registry (com.sri.ai.grinder.api.Registry)6 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)5 DefaultRegistry (com.sri.ai.grinder.core.DefaultRegistry)5 ArrayList (java.util.ArrayList)5 Predicate (com.google.common.base.Predicate)4 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)4 Util (com.sri.ai.util.Util)4 PairOf (com.sri.ai.util.base.PairOf)4 Rational (com.sri.ai.util.math.Rational)4 Set (java.util.Set)4 Beta (com.google.common.annotations.Beta)3 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)3 DefaultExtensionalUniSet (com.sri.ai.expresso.core.DefaultExtensionalUniSet)3 DefaultTuple (com.sri.ai.expresso.core.DefaultTuple)3 TIMES (com.sri.ai.grinder.library.FunctorConstants.TIMES)3