Search in sources :

Example 51 with IndexExpressionsSet

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

the class SetOfArgumentTuplesForFunctionOccurringInExpression method computeUnionArgs.

private static void computeUnionArgs(List<Expression> unionArgs, Predicate<Expression> isF, FunctionType fType, Predicate<Expression> isFApplication, Expression e) {
    // if E does not contain &fnof;, oc<sub>&fnof;</sub>[E] is &empty;
    if (!containsF(e, isF)) {
        unionArgs.add(Sets.EMPTY_SET);
    } else // if E is &fnof;(t) for t a tuple, oc<sub>&fnof;</sub>[E] is {t}
    if (isFApplication.apply(e)) {
        Expression tupleT = Expressions.makeTuple(e.getArguments());
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is &fnof;, oc<sub>&fnof;</sub>[E] is &Alpha;
    if (isF.apply(e)) {
        List<Expression> domainTypes = new ArrayList<>();
        for (Type domainType : fType.getArgumentTypes()) {
            domainTypes.add(Expressions.parse(domainType.getName()));
        }
        Expression tupleT = Expressions.makeTuple(domainTypes);
        Expression setT = ExtensionalSets.makeUniSet(tupleT);
        unionArgs.add(setT);
    } else // if E is g(E&prime;) for g a function symbol distinct from &fnof; and t a k-tuple of expressions
    if (Expressions.isFunctionApplicationWithArguments(e)) {
        // if g(E&prime;) is if C the E<sub>1</sub> else E<sub>2</sub> and C does not contain &fnof;
        if (IfThenElse.isIfThenElse(e) && !containsF(IfThenElse.condition(e), isF)) {
            // then oc<sub>&fnof;</sub>[E] is<br>
            // if C then oc<sub>&fnof;</sub>[E<sub>1</sub>] else oc<sub>&fnof;</sub>[E<sub>2</sub>]				
            Expression e1 = IfThenElse.thenBranch(e);
            List<Expression> thenUnionArgs = new ArrayList<>();
            computeUnionArgs(thenUnionArgs, isF, fType, isFApplication, e1);
            Expression thenBranch = makeUnion(thenUnionArgs);
            Expression e2 = IfThenElse.elseBranch(e);
            List<Expression> elseUnionArgs = new ArrayList<>();
            computeUnionArgs(elseUnionArgs, isF, fType, isFApplication, e2);
            Expression elseBranch = makeUnion(elseUnionArgs);
            Expression ifThenElse = IfThenElse.make(IfThenElse.condition(e), thenBranch, elseBranch);
            unionArgs.add(ifThenElse);
        } else {
            // oc<sub>&fnof;</sub>[t<sub>1</sub>] &cup; &hellip;	&cup; oc<sub>&fnof;</sub>[t<sub>k</sub>]
            for (Expression t : e.getArguments()) {
                computeUnionArgs(unionArgs, isF, fType, isFApplication, t);
            }
        }
    } else // quantified expression, 
    if (isArbitraryQuantifier(e) && !containsF(getQuantifierCondition(e), isF)) {
        // then oc<sub>&fnof;</sub>[E] is<br> 
        // &bigcup;<sub>x:C</sub>oc<sub>&fnof;</sub>[E&prime;]
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        Expression ocfOfEPrime = makeUnion(ePrimeUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, ocfOfEPrime, c);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else // quantified expression,
    if (isArbitraryQuantifier(e)) {
        // then oc<sub>&fnof;</sub>[E] is<br> 
        // &bigcup;<sub>x</sub>(oc<sub>&fnof;</sub>[C] &cup; oc<sub>&fnof;</sub>[E&prime;])
        QuantifiedExpression q = (QuantifiedExpression) e;
        IndexExpressionsSet x = q.getIndexExpressions();
        Expression c = getQuantifierCondition(e);
        Expression ePrime = getQuantifiedExpression(q);
        List<Expression> conditionUnionArgs = new ArrayList<>();
        computeUnionArgs(conditionUnionArgs, isF, fType, isFApplication, c);
        List<Expression> ePrimeUnionArgs = new ArrayList<>();
        computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
        List<Expression> combinedUnionArgs = new ArrayList<>();
        combinedUnionArgs.addAll(conditionUnionArgs);
        combinedUnionArgs.addAll(ePrimeUnionArgs);
        Expression combinedUnion = makeUnion(combinedUnionArgs);
        Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, combinedUnion, Expressions.TRUE);
        Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
        unionArgs.add(intensionalUnion);
    } else {
        throw new UnsupportedOperationException("Do not have logic for handling expression of the form: " + e);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 52 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) {
    SampleCommonInterpreter interpreter = new SampleCommonInterpreter(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.getType(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 : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) SampleCommonInterpreter(com.sri.ai.grinder.sgdpllt.interpreter.SampleCommonInterpreter) 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) 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.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 53 with IndexExpressionsSet

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

the class IntensionalSetConditionSimplifier method simplify.

public static Expression simplify(Expression expression, Context context) {
    Expression result = expression;
    if (Sets.isIntensionalSet(expression)) {
        IntensionalSet intensionalSet = (IntensionalSet) expression;
        Expression condition = intensionalSet.getCondition();
        if (!(condition.equals(Expressions.TRUE) || condition.equals(Expressions.FALSE))) {
            IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
            Context extendedContext = context.extendWith(indexExpressionsSet);
            Expression evaluatedCondition = context.getTheory().evaluate(condition, extendedContext);
            if (!evaluatedCondition.equals(condition)) {
                result = IntensionalSet.make(Sets.isIntensionalMultiSet(expression) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, indexExpressionsSet, intensionalSet.getHead(), evaluatedCondition);
            }
        }
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet)

Example 54 with IndexExpressionsSet

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

the class Measure method get.

public static Rational get(Expression intensionalSetExpression, Context context) {
    Rational result;
    if (Sets.isIntensionalSet(intensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
        IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
        List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
        if (indices.size() == 1) {
            Expression evaluatedResult;
            Expression intensionalSetIndex = indices.get(0);
            Expression intensionalSetHead = intensionalSet.getHead();
            if (!intensionalSetHead.equals(intensionalSetIndex)) {
                throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
            }
            Expression intensionalSetCondition = intensionalSet.getCondition();
            Context intensionalSetContext = context.extendWith(indexExpressionsSet);
            Type indexType = GrinderUtil.getType(intensionalSetIndex, intensionalSetContext);
            if (intensionalSetCondition.equals(false)) {
                // short circuit known empty sets up front.
                evaluatedResult = Expressions.ZERO;
            } else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
                // NOTE : For Reals can always assume the condition is of this type.				
                SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
                MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
                evaluatedResult = realSolver.solve(intensionalSetContext);
            } else if (indexType instanceof FunctionType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
                }
                // measure(co-domain)^measure(domain)
                FunctionType indexFunctionType = (FunctionType) indexType;
                Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
                Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
                Rational domainMeasure = Rational.ONE;
                for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
                    Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
                    Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
                    domainMeasure = domainMeasure.multiply(argMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
            } else if (indexType instanceof TupleType) {
                if (!intensionalSetCondition.equals(true)) {
                    throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
                }
                // (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
                TupleType indexTupleType = (TupleType) indexType;
                Rational elementMeasuresProduct = Rational.ONE;
                for (Type elementType : indexTupleType.getElementTypes()) {
                    Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
                    Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
                    elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
                }
                evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
            } else {
                Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
                evaluatedResult = context.getTheory().evaluate(countingFormula, context);
            }
            if (Expressions.isNumber(evaluatedResult)) {
                result = evaluatedResult.rationalValue();
            } else {
                throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
            }
        } else {
            throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
        }
    } else {
        throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
    }
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Rational(com.sri.ai.util.math.Rational) FunctionType(com.sri.ai.expresso.type.FunctionType) DefaultCountingFormula(com.sri.ai.expresso.core.DefaultCountingFormula) RealInterval(com.sri.ai.expresso.type.RealInterval) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) TupleType(com.sri.ai.expresso.type.TupleType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver)

Example 55 with IndexExpressionsSet

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

the class Bounds method isExtremePoint.

/**
	 * Checks if \phi is a convex combination of the elements in bound
	 * @param phi
	 * 			factor
	 * @param bound
	 * @return
	 */
public static boolean isExtremePoint(Expression phi, int indexPhi, Expression bound, Theory theory, Context context) {
    //TODO
    //caro pq recopia a lista toda
    Expression boundWithoutPhi = removeNonDestructively(bound, indexPhi);
    List<Expression> listOfB = getElements(boundWithoutPhi);
    int n = listOfB.size();
    Expression[] c = new Expression[n];
    for (int i = 0; i < n; i++) {
        c[i] = makeSymbol("c" + i);
        context = context.extendWithSymbolsAndTypes("c" + i, "Real");
    }
    // 0<=ci<=1
    ArrayList<Expression> listOfC = new ArrayList<>(listOfB.size());
    for (int i = 0; i < n; i++) {
        Expression cibetwen0And1 = apply(AND, apply(GREATER_THAN_OR_EQUAL_TO, 1, c[i]), apply(GREATER_THAN_OR_EQUAL_TO, c[i], 0));
        listOfC.add(cibetwen0And1);
    }
    Expression allcibetwen0And1 = apply(AND, listOfC);
    //sum over ci =1
    listOfC = new ArrayList<>(Arrays.asList(c));
    Expression sumOverCiEqualsOne = apply(EQUAL, 1, apply(PLUS, listOfC));
    //sum of ci*phi1 = phi
    ArrayList<Expression> prodciphii = new ArrayList<>(listOfB.size());
    int i = 0;
    for (Expression phii : listOfB) {
        prodciphii.add(apply(TIMES, phii, c[i]));
        i++;
    }
    Expression convexSum = apply(EQUAL, phi, apply(PLUS, prodciphii));
    ArrayList<Expression> listOfCiInReal = new ArrayList<>(listOfB.size());
    for (i = 0; i < n; i++) {
        listOfCiInReal.add(apply(IN, c[i], "Real"));
    }
    IndexExpressionsSet thereExistsCiInReal = new ExtensionalIndexExpressionsSet(listOfCiInReal);
    Expression body = apply(AND, allcibetwen0And1, sumOverCiEqualsOne, convexSum);
    Expression isExtreme = new DefaultExistentiallyQuantifiedFormula(thereExistsCiInReal, body);
    if (debug)
        println(isExtreme);
    //Expression result = theory.evaluate(isExtreme, context);
    return true;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) DefaultExistentiallyQuantifiedFormula(com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula) Util.mapIntoArrayList(com.sri.ai.util.Util.mapIntoArrayList) ArrayList(java.util.ArrayList) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) 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