Search in sources :

Example 1 with SingleVariableLinearRealArithmeticConstraint

use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.

the class Measure method constructComponentIntensionalSet.

private static Expression constructComponentIntensionalSet(Type indexType, IntensionalSet intensionalSet, Expression additiveIdentityElement, Context intensionalSetContext) {
    Expression conditionedBody = IfThenElse.make(intensionalSet.getCondition(), intensionalSet.getHead(), additiveIdentityElement);
    Expression componentIndex = Expressions.makeUniqueVariable("C", conditionedBody, intensionalSetContext);
    Expression indexExpression = IndexExpressions.makeIndexExpression(componentIndex, Expressions.parse(indexType.getName()));
    Expression intensionalCondition = Expressions.TRUE;
    // NOTE: handle the REAL cases where an SingleVariableLinearRealArithmeticConstraint is expected.
    if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
        SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(componentIndex, true, intensionalSetContext.getTheory());
        intensionalCondition = svlraConstraint;
    }
    Expression result = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, new ExtensionalIndexExpressionsSet(Arrays.asList(indexExpression)), componentIndex, intensionalCondition);
    return result;
}
Also used : ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 2 with SingleVariableLinearRealArithmeticConstraint

use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.

the class MeasureTest method measure.

private Rational measure(String testIntensionalSetString) {
    Expression testIntensionalSetExpression = parse(testIntensionalSetString);
    Expression properlyConditionedIntensionalSetExpression = testIntensionalSetExpression;
    if (Sets.isIntensionalSet(testIntensionalSetExpression)) {
        IntensionalSet intensionalSet = (IntensionalSet) testIntensionalSetExpression;
        List<Expression> indices = IndexExpressions.getIndices(intensionalSet.getIndexExpressions());
        if (indices.size() == 1) {
            Expression index = indices.get(0);
            Context intensionalSetContext = context.extendWith(intensionalSet.getIndexExpressions());
            Type type = GrinderUtil.getType(index, intensionalSetContext);
            if (type instanceof RealExpressoType || type instanceof RealInterval) {
                SingleVariableLinearRealArithmeticConstraint singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
                singleVariableConstraint = (SingleVariableLinearRealArithmeticConstraint) singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
                properlyConditionedIntensionalSetExpression = IntensionalSet.make(Sets.isMultiSet(intensionalSet) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, intensionalSet.getIndexExpressions(), intensionalSet.getHead(), singleVariableConstraint);
            }
        }
    }
    Rational result = Measure.get(properlyConditionedIntensionalSetExpression, context);
    return result;
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) TrueContext(com.sri.ai.grinder.sgdpllt.core.TrueContext) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval)

Example 3 with SingleVariableLinearRealArithmeticConstraint

use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.

the class AssignmentsSamplingIteratorTest method newSamplingIterator.

private Iterator<Map<Expression, Expression>> newSamplingIterator(String indexString, int sampleSize, String conditionString) {
    Expression index = parse(indexString);
    Expression condition = parse(conditionString);
    // Ensure condition of correct type is created
    Type indexType = GrinderUtil.getType(index, context);
    if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
        SingleVariableLinearRealArithmeticConstraint svlraConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, context.getTheory());
        svlraConstraint = (SingleVariableLinearRealArithmeticConstraint) svlraConstraint.conjoin(condition, context);
        condition = svlraConstraint;
    } else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
        SingleVariableDifferenceArithmeticConstraint svdaConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, context.getTheory());
        svdaConstraint = (SingleVariableDifferenceArithmeticConstraint) svdaConstraint.conjoin(condition, context);
        condition = svdaConstraint;
    }
    Iterator<Map<Expression, Expression>> result = new AssignmentsSamplingIterator(Arrays.asList(index), sampleSize, condition, conditionRewriter, random, context);
    return result;
}
Also used : TupleType(com.sri.ai.expresso.type.TupleType) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) AssignmentsSamplingIterator(com.sri.ai.grinder.helper.AssignmentsSamplingIterator) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) RealInterval(com.sri.ai.expresso.type.RealInterval) Map(java.util.Map)

Example 4 with SingleVariableLinearRealArithmeticConstraint

use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint in project aic-expresso by aic-sri-international.

the class LinearRealArithmeticTheoryTest method runTest.

/**
	 * @param variable
	 * @param constraintString
	 * @param expected
	 * @param computedFunction
	 * @param stepSolverMaker
	 * @param context
	 */
private void runTest(Expression variable, String constraintString, Expression expected, String computedFunction, Function<Constraint, ExpressionStepSolver> stepSolverMaker, Context context) {
    System.out.println("Solving " + computedFunction + " for " + variable + " in " + constraintString);
    Constraint constraint = new SingleVariableLinearRealArithmeticConstraint(variable, true, context.getTheory());
    constraint = constraint.conjoin(parse(constraintString), context);
    ExpressionStepSolver stepSolver = stepSolverMaker.apply(constraint);
    Expression actual = stepSolver.solve(context);
    System.out.println("Variable " + variable + "\nhas " + computedFunction + ":\n" + actual + "\nfor constraint:\n" + constraintString + "\n");
    assertEquals(expected, actual);
}
Also used : Constraint(com.sri.ai.grinder.sgdpllt.api.Constraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint) Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 5 with SingleVariableLinearRealArithmeticConstraint

use of com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint 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)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)6 SingleVariableLinearRealArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)6 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)5 RealInterval (com.sri.ai.expresso.type.RealInterval)5 Type (com.sri.ai.expresso.api.Type)4 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)3 Context (com.sri.ai.grinder.sgdpllt.api.Context)3 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)2 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)2 FunctionType (com.sri.ai.expresso.type.FunctionType)2 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)2 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)2 TupleType (com.sri.ai.expresso.type.TupleType)2 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)2 SingleVariableDifferenceArithmeticConstraint (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)2 Rational (com.sri.ai.util.math.Rational)2 DefaultCountingFormula (com.sri.ai.expresso.core.DefaultCountingFormula)1 AssignmentsSamplingIterator (com.sri.ai.grinder.helper.AssignmentsSamplingIterator)1 Constraint (com.sri.ai.grinder.sgdpllt.api.Constraint)1 ExpressionStepSolver (com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver)1