Search in sources :

Example 6 with Expression

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method getValueAtGivenPoint.

private Expression getValueAtGivenPoint(Expression literalFreeBody, Expression variable, Expression value, Theory theory, Context context) {
    Expression newBody = literalFreeBody.replaceAllOccurrences(variable, value, context);
    Expression valueAtPoint = theory.simplify(newBody, context);
    return valueAtPoint;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Example 7 with Expression

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

the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method computeSummationGivenValues.

private Expression computeSummationGivenValues(Expression variable, Expression literalFreeBody, RangeAndExceptionsSet values, Context context) {
    Expression result;
    if (values.equals(RangeAndExceptionsSet.EMPTY)) {
        result = ZERO;
    } else {
        Theory theory = context.getTheory();
        if (values instanceof RangeAndExceptionsSet.Singleton) {
            Expression value = ((RangeAndExceptionsSet.Singleton) values).getSingleValue();
            Expression valueAtPoint = DefaultPolynomial.make(getValueAtGivenPoint(literalFreeBody, variable, value, theory, context));
            result = valueAtPoint;
        } else {
            Expression interval;
            List<Expression> disequals;
            if (values.hasFunctor(MINUS)) {
                interval = values.get(0);
                disequals = values.get(1).getArguments();
            } else {
                interval = values;
                disequals = list();
            }
            Expression strictLowerBound = interval.get(0);
            Expression nonStrictUpperBound = interval.get(1);
            Polynomial bodyPolynomial = DefaultPolynomial.make(literalFreeBody);
            Expression intervalSummation = PolynomialSummation.sum(variable, strictLowerBound, nonStrictUpperBound, bodyPolynomial);
            ArrayList<Expression> argumentsForSubtraction = new ArrayList<>(1 + disequals.size());
            argumentsForSubtraction.add(intervalSummation);
            for (Expression disequal : disequals) {
                Expression valueAtDisequal = getValueAtGivenPoint(literalFreeBody, variable, disequal, theory, context);
                argumentsForSubtraction.add(apply(MINUS, valueAtDisequal));
            }
            Expression intervalSummationMinusValuesAtDisequals = apply(PLUS, argumentsForSubtraction);
            result = DefaultPolynomial.make(theory.simplify(intervalSummationMinusValuesAtDisequals, context));
        }
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) Polynomial.makeRandomPolynomial(com.sri.ai.grinder.polynomial.api.Polynomial.makeRandomPolynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) ArrayList(java.util.ArrayList)

Example 8 with Expression

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

the class Monomial method orderedUnionOfNonNumericConstantFactors.

/**
	 * Create an ordered union (no duplicates) of the non-numeric constant
	 * factors contained in two Monomials.
	 * 
	 * @param m1
	 *            the first monomial.
	 * @param m2
	 *            the second monomial.
	 * @return the ordered union (no duplicates) of the non-numeric constant
	 *         factors contained in two Monomials.
	 */
public static List<Expression> orderedUnionOfNonNumericConstantFactors(Monomial m1, Monomial m2) {
    List<Expression> m1Factors = m1.getOrderedNonNumericFactors();
    List<Expression> m2Factors = m2.getOrderedNonNumericFactors();
    // For efficiency ensure we have enough capacity in the union up front.
    List<Expression> result = new ArrayList<>(m1Factors.size() + m2Factors.size());
    // NOTE: we know m1 and m2's non-numeric constant factors are
    // ordered, which we can take advantage of to perform the
    // ordered union more efficiently.
    int m1FactorsSize = m1Factors.size();
    int m2FactorsSize = m2Factors.size();
    Expression m1Factor, m2Factor;
    for (int i1 = 0, i2 = 0; i1 < m1FactorsSize || i2 < m2FactorsSize; ) {
        if (i1 < m1FactorsSize && i2 < m2FactorsSize) {
            m1Factor = m1Factors.get(i1);
            m2Factor = m2Factors.get(i2);
            int m1FactorComparisonToM2Factor = m1Factor.compareTo(m2Factor);
            if (m1FactorComparisonToM2Factor == 0) {
                // are considered equal
                // just add one of them (as considered same) as do not want
                // duplicates in the returned union.
                result.add(m1Factor);
                i1++;
                i2++;
            } else if (m1FactorComparisonToM2Factor < 0) {
                // i.e. m1Factor
                // is before
                // m2Factor
                result.add(m1Factor);
                i1++;
            } else {
                // i.e. m2Factor is before m1Factor
                result.add(m2Factor);
                i2++;
            }
        } else if (i1 < m1FactorsSize) {
            // implies i2 is exhausted
            result.add(m1Factors.get(i1));
            i1++;
        } else {
            // implies i2 < m2FactorsSize and i1 is exhausted
            result.add(m2Factors.get(i2));
            i2++;
        }
    }
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 9 with Expression

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

the class AssignmentsIterator method makeMapFromVariablesToIteratorMakersFrom.

private static Map<Expression, NullaryFunction<Iterator<Expression>>> makeMapFromVariablesToIteratorMakersFrom(IndexExpressionsSet indexExpressionsSet, Registry registry) {
    Map<Expression, NullaryFunction<Iterator<Expression>>> fromVariableToIteratorMaker = map();
    ExtensionalIndexExpressionsSet extensionalIndexExpressionsSet;
    try {
        extensionalIndexExpressionsSet = (ExtensionalIndexExpressionsSet) indexExpressionsSet;
    } catch (ClassCastException e) {
        throw new Error("AssignmentsIterator defined for extensional index expressions sets only.");
    }
    for (Expression indexExpression : extensionalIndexExpressionsSet.getList()) {
        Expression variable = IndexExpressions.getIndex(indexExpression);
        Expression typeDescription = IndexExpressions.getType(indexExpression);
        if (typeDescription == null) {
            typeDescription = GrinderUtil.getTypeExpression(variable, registry);
        }
        putVariableAndIteratorMakerIn(fromVariableToIteratorMaker, variable, typeDescription, registry);
    }
    return fromVariableToIteratorMaker;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) NullaryFunction(com.sri.ai.util.base.NullaryFunction)

Example 10 with Expression

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

the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.

@Test
public void basicTests() {
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
    Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
    constraint = constraint.conjoin(condition, context);
    Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
    assertEquals(expected, constraint);
    // nested indices
    Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
    context = new TrueContext(theoryTestingSupport.getTheory());
    expected = parse("536");
    Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
    println(actual);
    assertEquals(expected, actual);
}
Also used : CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) CompleteMultiVariableContext(com.sri.ai.grinder.sgdpllt.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) AbstractTheoryTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52