use of com.sri.ai.expresso.api.Type 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 + ";");
}
}
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class TheoryTestingSupport method getTestingVariableType.
/**
* Get the type associated with the given testing variable.
*
* @param variable
* the testing variable whose type is to be returned.
* @return the type of the given testing variable.
*/
default Type getTestingVariableType(String variable) {
String variableName = getVariableName(variable);
Type result = getVariableNamesAndTypesForTesting().get(variableName);
// We need to check if the variable is a function application
// because if it is we need to use the codomain of its function type
// as the type of the testing variable.
Expression variableExpresison = parse(variable);
Expression functor = variableExpresison.getFunctor();
if (functor != null) {
result = ((FunctionType) result).getCodomain();
}
return result;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class CompoundTheoryTestingSupport method setVariableNamesAndTypesForTesting.
/**
* This is overridden so that given variables and types for testing are distributed to their
* respective theories according to {@link #isSuitableFor(Type)}.
*/
@Override
public void setVariableNamesAndTypesForTesting(Map<String, Type> variableNamesAndTypesForTesting) {
// First ensure the compound set of variables names and type information is setup correctly
super.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
// Then update the sub-theories so that they share appropriate subsets of this information
Map<Theory, Map<String, Type>> mapForSubTheory = map();
for (Theory subTheory : getTheory().getSubTheories()) {
mapForSubTheory.put(subTheory, map());
}
for (Map.Entry<String, Type> variableNameAndType : getVariableNamesAndTypesForTesting().entrySet()) {
String variableName = variableNameAndType.getKey();
Type type = variableNameAndType.getValue();
for (Theory subTheory : getTheory().getSubTheories()) {
if (subTheory.isSuitableFor(type) || (type instanceof FunctionType && subTheory.isSuitableFor(((FunctionType) type).getCodomain()))) {
mapForSubTheory.get(subTheory).put(variableName, type);
}
}
}
for (Map.Entry<Theory, TheoryTestingSupport> entry : getTheoryToTestingSupport().entrySet()) {
Map<String, Type> forThisSubTheory = mapForSubTheory.get(entry.getKey());
entry.getValue().setVariableNamesAndTypesForTesting(forThisSubTheory);
}
}
use of com.sri.ai.expresso.api.Type 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.api.Type in project aic-expresso by aic-sri-international.
the class DifferenceArithmeticTheoryTestingSupport method makeRandomAtomOn.
/**
* Makes a random atom on variable by summing or subtracting terms from two random atoms generated by super class implementation.
*/
@Override
public Expression makeRandomAtomOn(String mainVariable, Context context) {
String mainVariableName = getVariableName(mainVariable);
Type mainType = getTestingVariableType(mainVariable);
List<String> variableNamesThatAreSubtypesOf = getVariableNamesWhoseTypesAreSubtypesOf(mainType);
int maxNumberOfOtherVariablesInAtom = Math.min(variableNamesThatAreSubtypesOf.size(), 2);
// used to be 3, but if literal has more than two variables, it steps out of difference arithmetic and may lead
// to multiplied variables when literals are propagated.
// For example, X = Y + Z and X = -Y - Z + 3 imply 2Y + 2Z = 3
int numberOfOtherVariablesInAtom = getRandom().nextInt(maxNumberOfOtherVariablesInAtom);
// Note: that otherVariablesForAtom will contain only one or zero elements
ArrayList<String> otherVariablesForAtom = new ArrayList<>();
if (numberOfOtherVariablesInAtom > 0) {
otherVariablesForAtom.add(pickTestingVariableAtRandom(mainType, otherName -> !otherName.equals(mainVariableName)));
}
ArrayList<Expression> constants = new ArrayList<Expression>();
int numberOfConstants = getRandom().nextInt(3);
for (int i = 0; i != numberOfConstants; i++) {
// Note: We know we can safely sample from the Difference Arithmetic Theory Types.
Expression sampledConstant = mainType.sampleUniquelyNamedConstant(getRandom());
Expression constant;
if (getRandom().nextBoolean()) {
constant = sampledConstant;
} else {
constant = makeSymbol(-sampledConstant.intValue());
}
constants.add(constant);
}
ArrayList<Expression> leftHandSideArguments = new ArrayList<Expression>();
leftHandSideArguments.add(parse(mainVariable));
// needs to be difference, so it's added as negative
Util.mapIntoList(otherVariablesForAtom, otherVariable -> UnaryMinus.make(parse(otherVariable)), leftHandSideArguments);
leftHandSideArguments.addAll(constants);
int numberOfOtherVariablesToBeCanceled = getRandom().nextInt(otherVariablesForAtom.size() + 1);
ArrayList<String> otherVariablesToBeCanceled = Util.pickKElementsWithoutReplacement(otherVariablesForAtom, numberOfOtherVariablesToBeCanceled, getRandom());
// note that this term is positive, so it will cancel the previously negative term with the same "other variable"
Util.mapIntoList(otherVariablesToBeCanceled, v -> parse(v), leftHandSideArguments);
// Note: it may seem odd to generate an "other variable" and add another term that will cancel it later.
// However, this is useful for making sure canceling works properly.
Expression leftHandSide = Plus.make(leftHandSideArguments);
String functor = pickUniformly(getTheoryFunctors(), getRandom());
Expression unsimplifiedResult = apply(functor, leftHandSide, 0);
Expression result = getTheory().simplify(unsimplifiedResult, context);
return result;
}
Aggregations