Search in sources :

Example 26 with IntegerInterval

use of com.sri.ai.expresso.type.IntegerInterval in project aic-expresso by aic-sri-international.

the class AssignmentsSamplingIterator method getTypeToSampleFrom.

public static Type getTypeToSampleFrom(Expression variable, Expression condition, Context context) {
    Type result = GrinderUtil.getTypeOfExpression(variable, context);
    if (result instanceof FunctionType) {
        FunctionType functionType = (FunctionType) result;
        result = new LazySampledFunctionType(functionType.getCodomain(), functionType.getArgumentTypes().toArray(new Type[functionType.getArity()]));
    } else {
        if (condition.equals(false)) {
            result = null;
        } else if (condition.equals(true)) {
        // we leave as is.
        } else if (result instanceof RealExpressoType || result instanceof RealInterval) {
            LinearRealArithmeticTheory theory = new LinearRealArithmeticTheory(true, true);
            SingleVariableLinearRealArithmeticConstraint constraint = (SingleVariableLinearRealArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableLinearRealArithmeticConstraint) constraint.conjoin(condition, context);
            IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver solver = new IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(constraint);
            Expression realInterval = solver.solve(context);
            if (Sets.isEmptySet(realInterval)) {
                // used to indicate an empty set.
                result = null;
            } else if (ExtensionalSets.isExtensionalSet(realInterval) && ExtensionalSets.isSingleton(realInterval)) {
                String singletonValue = realInterval.get(0).toString();
                result = new RealInterval("[" + singletonValue + ";" + singletonValue + "]");
            } else {
                result = new RealInterval(realInterval.toString());
            }
        } else if (result instanceof IntegerExpressoType || result instanceof IntegerInterval) {
            DifferenceArithmeticTheory theory = new DifferenceArithmeticTheory(true, true);
            SingleVariableDifferenceArithmeticConstraint constraint = (SingleVariableDifferenceArithmeticConstraint) theory.makeSingleVariableConstraint(variable, context);
            constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(condition, context);
            ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver solver = new ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(constraint);
            // NOTE: the exceptions set returned here is implicit in the condition so no need to use it here.
            RangeAndExceptionsSet rangeAndExceptionsSet = (RangeAndExceptionsSet) solver.solve(context);
            if (rangeAndExceptionsSet.isEmpty()) {
                // used to indicate an empty set.
                result = null;
            } else if (rangeAndExceptionsSet.isSingleton()) {
                result = new IntegerInterval(rangeAndExceptionsSet.getSingleValue().intValueExact(), rangeAndExceptionsSet.getSingleValue().intValueExact());
            } else {
                result = new IntegerInterval(rangeAndExceptionsSet.getStrictLowerBound().intValueExact() + 1, rangeAndExceptionsSet.getNonStrictUpperBound().intValueExact());
            }
        }
    }
    if (result != null && !result.isSampleUniquelyNamedConstantSupported()) {
        throw new IllegalArgumentException("Unable to sample " + variable + " from " + result);
    }
    return result;
}
Also used : RangeAndExceptionsSet(com.sri.ai.grinder.theory.differencearithmetic.RangeAndExceptionsSet) FunctionType(com.sri.ai.expresso.type.FunctionType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.linearrealarithmetic.IntervalWithMeasureEquivalentToSingleVariableLinearRealArithmeticConstraintStepSolver) 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) FunctionType(com.sri.ai.expresso.type.FunctionType) ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver(com.sri.ai.grinder.theory.differencearithmetic.ValuesOfSingleVariableDifferenceArithmeticConstraintStepSolver) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint) SingleVariableLinearRealArithmeticConstraint(com.sri.ai.grinder.theory.linearrealarithmetic.SingleVariableLinearRealArithmeticConstraint)

Example 27 with IntegerInterval

use of com.sri.ai.expresso.type.IntegerInterval in project aic-expresso by aic-sri-international.

the class DifferenceArithmeticTheory method symbolIsIntegerTyped.

private static boolean symbolIsIntegerTyped(Expression variable, Context context) {
    Type variableType = context.getTypeOfRegisteredSymbol(variable);
    boolean variableIsInteger = variableType instanceof IntegerExpressoType || variableType instanceof IntegerInterval;
    return variableIsInteger;
}
Also used : Type(com.sri.ai.expresso.api.Type) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval)

Example 28 with IntegerInterval

use of com.sri.ai.expresso.type.IntegerInterval in project aic-expresso by aic-sri-international.

the class SingleVariableDifferenceArithmeticConstraint method getType.

/**
 * Returns the {@link IntegerInterval} type of the constraint's variable.
 * @param context
 * @return
 */
public IntegerInterval getType(Context context) {
    if (cachedType == null) {
        Expression variableTypeExpression = getVariableTypeExpression(context);
        Type type = context.getTypeFromTypeExpression(variableTypeExpression);
        if (type instanceof IntegerExpressoType) {
            cachedType = new IntegerInterval("-infinity..infinity");
        // represents Integer as integer interval for uniformity
        } else {
            cachedType = (IntegerInterval) type;
        }
    }
    return cachedType;
}
Also used : Type(com.sri.ai.expresso.api.Type) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval)

Example 29 with IntegerInterval

use of com.sri.ai.expresso.type.IntegerInterval in project aic-expresso by aic-sri-international.

the class ContextSplittingTester method createContextBasedOnGlobalParameters.

// / CONTEXT CONSTRUCTION METHODS ////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////////////////////////////
private Context createContextBasedOnGlobalParameters() {
    Context context = new TrueContext(theory);
    Expression typeExpression = new IntegerInterval(1, cardinalityOfVariables).toExpression();
    List<Expression> variableSymbols = new ArrayList<Expression>(numberOfVariables);
    for (int i = 1; i <= numberOfVariables; i++) {
        variableSymbols.add(createSymbol("X" + i));
        context = context.extendWithSymbolsAndTypes(variableSymbols.get(i - 1), typeExpression);
    }
    return context;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) ArrayList(java.util.ArrayList) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 30 with IntegerInterval

use of com.sri.ai.expresso.type.IntegerInterval in project aic-expresso by aic-sri-international.

the class RandomConditionalExpressionGenerator method main.

public static void main(String[] args) {
    Random random = new Random();
    TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(new Random(), new DifferenceArithmeticTheory(true, true));
    for (int numberOfVariables = 3; numberOfVariables != 5; numberOfVariables++) {
        Map<String, Type> variableNamesAndTypes = map();
        Type integerInterval = new IntegerInterval(0, 100);
        for (int v = 0; v != numberOfVariables; v++) {
            variableNamesAndTypes.put("v" + v, integerInterval);
        }
        theoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypes);
        Context context = theoryTestingSupport.makeContextWithTestingInformation();
        RandomConditionalExpressionGenerator generator = new RandomConditionalExpressionGenerator(theoryTestingSupport, 4, () -> makeSymbol(random.nextDouble()), context);
        System.out.println();
        System.out.println();
        for (Map.Entry<String, Type> variableNameAndType : variableNamesAndTypes.entrySet()) {
            Type type = variableNameAndType.getValue();
            IntegerInterval interval = (IntegerInterval) type;
            System.out.println("random " + variableNameAndType.getKey() + ": " + interval.getNonStrictLowerBound() + ".." + interval.getNonStrictUpperBound() + ";");
        }
        for (int i = 0; i != 5; i++) {
            Expression output = generator.apply();
            System.out.println(output + ";");
        }
    }
}
Also used : Context(com.sri.ai.grinder.api.Context) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Type(com.sri.ai.expresso.api.Type) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Map(java.util.Map)

Aggregations

IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)62 Expression (com.sri.ai.expresso.api.Expression)24 Before (org.junit.Before)20 FunctionType (com.sri.ai.expresso.type.FunctionType)18 Test (org.junit.Test)18 Type (com.sri.ai.expresso.api.Type)16 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)15 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 TrueContext (com.sri.ai.grinder.core.TrueContext)14 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)13 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)13 Categorical (com.sri.ai.expresso.type.Categorical)12 TupleType (com.sri.ai.expresso.type.TupleType)12 TrueContext (com.sri.ai.grinder.sgdpllt.core.TrueContext)12 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)12 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)10 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)10 TupleTheory (com.sri.ai.grinder.sgdpllt.theory.tuple.TupleTheory)8 Context (com.sri.ai.grinder.api.Context)7