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;
}
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;
}
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;
}
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;
}
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 + ";");
}
}
}
Aggregations