use of com.sri.ai.expresso.api.Expression in project aic-praise by aic-sri-international.
the class HOGModelGrounding method createRandomVariableNameToTypeSizeAndUniqueConstantsMap.
//
// PRIVATE
//
private static Map<Expression, Triple<Expression, Integer, List<Expression>>> createRandomVariableNameToTypeSizeAndUniqueConstantsMap(FactorsAndTypes factorsAndTypes) {
Map<Expression, Triple<Expression, Integer, List<Expression>>> result = new LinkedHashMap<>();
factorsAndTypes.getMapFromRandomVariableNameToTypeName().entrySet().forEach(entry -> {
Expression randomVariableName = Expressions.parse(entry.getKey());
Expression type = Expressions.parse(entry.getValue());
int size = 0;
List<Expression> uniqueConstants = new ArrayList<>();
if (Expressions.hasFunctor(type, FunctorConstants.FUNCTION_TYPE)) {
throw new UnsupportedOperationException("Relational random variables, " + randomVariableName + ", are currently not supported.");
} else if (Expressions.hasFunctor(type, HOGMSortDeclaration.IN_BUILT_INTEGER.getName()) && type.numberOfArguments() == 2) {
size = (type.get(1).intValueExact() - type.get(0).intValueExact()) + 1;
} else if (type.hasFunctor(FunctorConstants.INTEGER_INTERVAL) && type.numberOfArguments() == 2) {
size = (type.get(1).intValueExact() - type.get(0).intValueExact()) + 1;
} else {
String sizeString = factorsAndTypes.getMapFromCategoricalTypeNameToSizeString().get(type);
if (sizeString == null) {
throw new IllegalArgumentException("Size of sort " + type + " is unknown");
}
size = Integer.parseInt(sizeString);
factorsAndTypes.getMapFromUniquelyNamedConstantNameToTypeName().entrySet().stream().filter(uniqueConstantAndTypeEntry -> uniqueConstantAndTypeEntry.getValue().equals(entry.getValue())).forEach(uniqueConstantAndTypeEntry -> uniqueConstants.add(Expressions.parse(uniqueConstantAndTypeEntry.getKey())));
}
result.put(randomVariableName, new Triple<>(type, size, uniqueConstants));
});
return result;
}
use of com.sri.ai.expresso.api.Expression in project aic-praise by aic-sri-international.
the class HOGModelGrounding method createTypeToValuesMap.
private static Map<Expression, List<Expression>> createTypeToValuesMap(FactorsAndTypes factorsAndTypes, Map<Expression, Triple<Expression, Integer, List<Expression>>> randomVariableNameToTypeExpressionTypeSizeAndUniqueConstants) {
Map<Expression, List<Expression>> typeToValuesMap = new LinkedHashMap<>();
randomVariableNameToTypeExpressionTypeSizeAndUniqueConstants.values().forEach(typeSizeAndUniqueConstants -> {
Expression type = typeSizeAndUniqueConstants.first;
Integer size = typeSizeAndUniqueConstants.second;
List<Expression> uniqueConstants = typeSizeAndUniqueConstants.third;
if (!typeToValuesMap.containsKey(type)) {
List<Expression> values = new ArrayList<>();
if (Expressions.hasFunctor(type, HOGMSortDeclaration.IN_BUILT_INTEGER.getName()) && type.numberOfArguments() == 2) {
int startInclusive = type.get(0).intValueExact();
int endInclusive = type.get(1).intValueExact();
IntStream.rangeClosed(startInclusive, endInclusive).sequential().forEach(value -> values.add(Expressions.makeSymbol(value)));
} else {
if (HOGMSortDeclaration.IN_BUILT_BOOLEAN.getName().equals(type)) {
values.addAll(HOGMSortDeclaration.IN_BUILT_BOOLEAN.getAssignedConstants());
} else if (type.hasFunctor(FunctorConstants.INTEGER_INTERVAL) && type.numberOfArguments() == 2) {
int firstValue;
int lastValue;
try {
firstValue = type.get(0).intValue();
lastValue = type.get(1).intValue();
for (int i = firstValue; i != lastValue + 1; i++) {
values.add(makeSymbol(i));
}
} catch (Error e) {
throw new Error("Integer interval can only be grounded if it has fixed bounds, but got " + type);
}
} else {
values.addAll(uniqueConstants);
for (int i = uniqueConstants.size() + 1; i <= size; i++) {
values.add(Expressions.makeSymbol(type.toString().toLowerCase() + "_" + i));
}
}
}
typeToValuesMap.put(type, values);
}
});
return typeToValuesMap;
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class AbstractEqualityConstraintTest method testCompleteSatisfiabilitySpecialCases.
@Test
public void testCompleteSatisfiabilitySpecialCases() {
// This test is to make sure that some more tricky cases are indeed tested,
// even though hopefully the large amount of generated random problems include them or their variants.
String conjunction;
Expression expected;
TheoryTestingSupport theoryTestingSupport = makeTheoryTestingSupport();
Map<String, Type> variableNamesAndTypes = new HashMap<>(theoryTestingSupport.getVariableNamesAndTypesForTesting());
variableNamesAndTypes.put("W", variableNamesAndTypes.get("X"));
theoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypes);
if (theoryTestingSupport.getTheory().singleVariableConstraintIsCompleteWithRespectToItsVariable()) {
conjunction = "X != a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
expected = null;
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport);
conjunction = "X = Y and X != a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
expected = null;
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport);
}
TheoryTestingSupport theoryTestingSupport2 = makeTheoryTestingSupport();
Categorical type = new Categorical("Type", 1, arrayList(parse("a")));
theoryTestingSupport2.setVariableNamesAndTypesForTesting(map("X", type, "Y", type, "Z", type, "W", type));
conjunction = "X != Y";
expected = null;
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport2);
TheoryTestingSupport theoryTestingSupport3 = makeTheoryTestingSupport();
type = new Categorical("Type", 2, arrayList(parse("a"), parse("b")));
theoryTestingSupport3.setVariableNamesAndTypesForTesting(map("X", type, "Y", type, "Z", type, "W", type));
conjunction = "X != Y and X != a";
expected = parse("Y != b and X != a and X != Y");
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport3);
conjunction = "X != a and X != b and X != c and X != sometype5 and X != Y";
expected = parse("Y != d and X != a and X != b and X != c and X != sometype5 and X != Y and X != Y");
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport);
conjunction = "X = a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
expected = parse("(W = d) and (Z = c) and (X = a) and (X != Z) and (X != W)");
runCompleteSatisfiabilityTest(conjunction, expected, theoryTestingSupport);
}
use of com.sri.ai.expresso.api.Expression in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method testMeasureEquivalentInterval.
@Test
public void testMeasureEquivalentInterval() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Expression variable;
String constraintString;
Expression expected;
variable = parse("X");
constraintString = "true";
expected = parse("[0;4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < 3";
expected = parse("[0;3[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X < 4";
expected = parse("]3; 4[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("{ 3.2 }");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < Y";
expected = parse("if Y > 0 then [0;Y[ else {}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
// keep these tests together
variable = parse("X");
constraintString = "X < Y + 1";
// see previous test for situation in which condition is always true
expected = parse("if Y > 3 then [0;4] else [0 ; Y + 1[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("{ 3.2 }");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.6";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > Z and X >= Y";
expected = parse("if Z < Y then if Y <= 3.4 then [Y;3.4] else {} else if Z < 3.4 then ]Z;3.4] else {}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
}
use of com.sri.ai.expresso.api.Expression in project aic-praise by aic-sri-international.
the class ConstantDeclaration method makeConstantDeclaration.
/**
* Make a constant declaration object. Will default the arity of the
* constant declaration to 0 and range to be of sort 'Boolean' if not
* specified in the expression passed in.
*
* @param expression
* an expression in the form of a constant declaration.
* @return a ConstantDeclaration object corresponding to the expression
* passed in.
*/
public static ConstantDeclaration makeConstantDeclaration(Expression expression) {
ConstantDeclaration declaration = null;
if (Expressions.hasFunctor(expression, FUNCTOR_CONSTANT_DECLARATION)) {
int numArgs = expression.numberOfArguments();
if (numArgs > 0) {
// Extract arguments
Expression name = expression.get(0);
Expression arity = Expressions.ZERO;
if (numArgs >= 2) {
arity = expression.get(1);
}
Expression[] parametersAndRange = new Expression[0];
if (numArgs > 2) {
parametersAndRange = new Expression[numArgs - 2];
for (int i = 2; i < numArgs; i++) {
parametersAndRange[i - 2] = expression.get(i);
}
} else {
parametersAndRange = new Expression[0];
}
declaration = new ConstantDeclaration(name, arity, parametersAndRange);
}
}
if (declaration == null) {
throw new IllegalArgumentException("Not a legal definition of a constant declartion:" + expression);
}
return declaration;
}
Aggregations