Search in sources :

Example 66 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class AbstractFunctionBasedGroup method makeProblemExpression.

public Expression makeProblemExpression(List<Expression> indices, List<Expression> indicesTypes, Expression constraint, Expression body) {
    List<Expression> indexExpressions = makeIndexExpressions(indices, indicesTypes);
    IndexExpressionsSet indexExpressionsSet = new ExtensionalIndexExpressionsSet(indexExpressions);
    DefaultIntensionalMultiSet set = new DefaultIntensionalMultiSet(indexExpressionsSet, body, constraint);
    Expression problem = apply(getFunctionString(), set);
    return problem;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) DefaultIntensionalMultiSet(com.sri.ai.expresso.core.DefaultIntensionalMultiSet)

Example 67 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class AbstractFunctionBasedGroup method getExpressionAndIndexExpressionsFromProblemExpression.

@Override
public Pair<Expression, IndexExpressionsSet> getExpressionAndIndexExpressionsFromProblemExpression(Expression expression, Context context) {
    String functorString = getFunctionString();
    Util.myAssert(() -> expression.hasFunctor(functorString), () -> "Expression expected to be application of " + functorString + " but is " + expression);
    IntensionalSet set = (IntensionalSet) expression.get(0);
    Expression body = IfThenElse.make(set.getCondition(), set.getHead(), additiveIdentityElement());
    Pair<Expression, IndexExpressionsSet> result = Pair.make(body, set.getIndexExpressions());
    return result;
}
Also used : IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 68 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class SampleCommonInterpreterTest method run.

private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
    SamplingCommonInterpreter interpreter = new SamplingCommonInterpreter(sampleSizeN, alwaysSample, random);
    Expression expression = parse(expressionString);
    if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
        IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
        IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(indexExpressions);
            // Ensure condition of correct type is created
            Type indexType = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
            SingleVariableConstraint singleVariableConstraint = null;
            if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            } else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
                singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
            }
            if (singleVariableConstraint != null) {
                singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
                expression = expression.set(0, intensionalSet);
            }
        }
    }
    Expression result = interpreter.apply(expression, context);
    System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
    return result;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) SamplingCommonInterpreter(com.sri.ai.grinder.interpreter.SamplingCommonInterpreter) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 69 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class Expressions method makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees.

private static Expression makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
    ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
    Expression indexExpressionsKleeneList = subTreeExpressions.get(0);
    IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(ensureListFromKleeneList(indexExpressionsKleeneList));
    Expression body = subTreeExpressions.get(1);
    Expression result = new DefaultUniversallyQuantifiedFormula(indexExpressions, body);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) DefaultUniversallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultUniversallyQuantifiedFormula) DefaultLambdaExpression(com.sri.ai.expresso.core.DefaultLambdaExpression) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 70 with IndexExpressionsSet

use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.

the class ExtensionalIndexExpressionsSet method replaceSymbol.

@Override
public IndexExpressionsSet replaceSymbol(Expression symbol, Expression newSymbol, Registry registry) {
    Function<Expression, Expression> renameSymbol = e -> IndexExpressions.renameSymbol(e, symbol, newSymbol, registry);
    List<Expression> newList = replaceElementsNonDestructively(getList(), renameSymbol);
    IndexExpressionsSet newIndexExpressions;
    if (newList != getList()) {
        newIndexExpressions = new ExtensionalIndexExpressionsSet(newList);
    } else {
        newIndexExpressions = this;
    }
    return newIndexExpressions;
}
Also used : GrinderUtil.makeIndexExpressionsFromSymbolsAndTypes(com.sri.ai.grinder.helper.GrinderUtil.makeIndexExpressionsFromSymbolsAndTypes) Arrays(java.util.Arrays) Function(com.google.common.base.Function) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) Expression(com.sri.ai.expresso.api.Expression) SyntaxTree(com.sri.ai.expresso.api.SyntaxTree) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) Util.replaceElementsNonDestructively(com.sri.ai.util.Util.replaceElementsNonDestructively) List(java.util.List) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) GrinderUtil.makeListOfSymbolsAndTypesExpressionsFromSymbolsAndTypesStrings(com.sri.ai.grinder.helper.GrinderUtil.makeListOfSymbolsAndTypesExpressionsFromSymbolsAndTypesStrings) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Registry(com.sri.ai.grinder.api.Registry) SyntaxTrees(com.sri.ai.expresso.helper.SyntaxTrees) Util(com.sri.ai.util.Util) Collections(java.util.Collections) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Aggregations

IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)74 Expression (com.sri.ai.expresso.api.Expression)73 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)48 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)24 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)23 ArrayList (java.util.ArrayList)20 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)17 Type (com.sri.ai.expresso.api.Type)15 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)12 Context (com.sri.ai.grinder.api.Context)12 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)9 Context (com.sri.ai.grinder.sgdpllt.api.Context)9 FunctionType (com.sri.ai.expresso.type.FunctionType)8 Util.mapIntoArrayList (com.sri.ai.util.Util.mapIntoArrayList)8 Rational (com.sri.ai.util.math.Rational)7 List (java.util.List)7 SyntaxTree (com.sri.ai.expresso.api.SyntaxTree)6 QuantifiedExpressionWithABody (com.sri.ai.expresso.api.QuantifiedExpressionWithABody)5 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)5 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)5