Search in sources :

Example 51 with Expression

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;
}
Also used : Triple(com.sri.ai.util.base.Triple) IntStream(java.util.stream.IntStream) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Triple(com.sri.ai.util.base.Triple) Expressions(com.sri.ai.expresso.helper.Expressions) Rational(com.sri.ai.util.math.Rational) Expression(com.sri.ai.expresso.api.Expression) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Symbol(com.sri.ai.expresso.api.Symbol) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) Map(java.util.Map) BigInteger(java.math.BigInteger) HOGMSortDeclaration(com.sri.ai.praise.model.v1.HOGMSortDeclaration) Function(com.google.common.base.Function) TernaryProcedure(com.sri.ai.util.base.TernaryProcedure) MixedRadixNumber(com.sri.ai.util.math.MixedRadixNumber) Util.list(com.sri.ai.util.Util.list) Context(com.sri.ai.grinder.sgdpllt.api.Context) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) List(java.util.List) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Util.myAssert(com.sri.ai.util.Util.myAssert) Expressions.isNumber(com.sri.ai.expresso.helper.Expressions.isNumber) Collections(java.util.Collections) FunctorConstants(com.sri.ai.grinder.sgdpllt.library.FunctorConstants) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Example 52 with Expression

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;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap)

Example 53 with Expression

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);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) HashMap(java.util.HashMap) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Categorical(com.sri.ai.expresso.type.Categorical) Test(org.junit.Test) AbstractTheoryIncludingEqualityTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryIncludingEqualityTest)

Example 54 with Expression

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);
}
Also used : Context(com.sri.ai.grinder.sgdpllt.api.Context) Expression(com.sri.ai.expresso.api.Expression) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) LinearRealArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.linearrealarithmetic.LinearRealArithmeticTheory) Test(org.junit.Test)

Example 55 with Expression

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;
}
Also used : Expression(com.sri.ai.expresso.api.Expression)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1392 Test (org.junit.Test)259 ArrayList (java.util.ArrayList)196 Context (com.sri.ai.grinder.api.Context)187 Type (com.sri.ai.expresso.api.Type)124 TrueContext (com.sri.ai.grinder.core.TrueContext)113 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)100 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)91 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)90 Context (com.sri.ai.grinder.sgdpllt.api.Context)87 Theory (com.sri.ai.grinder.api.Theory)78 Map (java.util.Map)78 LambdaExpression (com.sri.ai.expresso.api.LambdaExpression)71 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)68 List (java.util.List)68 DefaultLambdaExpression (com.sri.ai.expresso.core.DefaultLambdaExpression)63 CommonTheory (com.sri.ai.grinder.application.CommonTheory)55 LinkedHashMap (java.util.LinkedHashMap)55 LinkedHashSet (java.util.LinkedHashSet)54 Pair (com.sri.ai.util.base.Pair)52