Search in sources :

Example 6 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class CompoundTheoryWithoutDifferenceArithmeticTest 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.
    // These are copied from the equality theory test,
    // so it is really just to check whether things hold up
    // if equality theory is embedded in a compound theory.
    String conjunction;
    Expression expected;
    Categorical someType = AbstractTheoryTestingSupport.getDefaultTestingType();
    // need W besides the other defaults -- somehow not doing this in equality theory alone does not cause a problem, probably because the type for W is never needed when we have only equality theory
    Map<String, Type> variableNamesAndTypesForTesting = map("X", someType, "Y", someType, "Z", someType, "W", someType);
    conjunction = "X != a and X != b and X != sometype5 and X != Z and X != W and Z = c and W = d";
    expected = FALSE;
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
    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 = FALSE;
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
    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)");
    runCompleteSatisfiabilityTest(conjunction, expected, variableNamesAndTypesForTesting);
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) Categorical(com.sri.ai.expresso.type.Categorical) AbstractTheoryTest(com.sri.ai.test.grinder.sgdpllt.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Example 7 with Type

use of com.sri.ai.expresso.api.Type 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 8 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class Compilation method compile.

/**
	 * Compiles an expression to a normalized (decision-tree-like) expression.
	 * @param inputExpression
	 * @param mapFromVariableNameToTypeName
	 * @param mapFromCategoricalTypeNameToSizeString
	 * @param additionalTypes
	 * @param solverListener if not null, invoked on solver used for compilation, before and after compilation starts; returned solver on 'before' invocation is used (it may be the same one used as argument, of course).
	 * @return
	 */
public static Expression compile(Expression inputExpression, Theory theory, Map<String, String> mapFromVariableNameToTypeName, Map<String, String> mapFromUniquelyNamedConstantToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes, Function<MultiIndexQuantifierEliminator, MultiIndexQuantifierEliminator> solverListener) {
    // the group actually does not matter, because we are not going to have any indices.
    AssociativeCommutativeGroup group = new Max();
    // The solver for the parameters above.
    MultiIndexQuantifierEliminator solver = new SGDPLLT();
    if (solverListener != null) {
        solver = solverListener.apply(solver);
    }
    // We use the Prolog convention of small-letter initials for constants, but we need an exception for the random variables.
    Predicate<Expression> isPrologConstant = new PrologConstantPredicate();
    Predicate<Expression> isUniquelyNamedConstantPredicate = e -> isPrologConstant.apply(e) && !mapFromVariableNameToTypeName.containsKey(e);
    Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>(mapFromVariableNameToTypeName);
    mapFromSymbolNameToTypeName.putAll(mapFromUniquelyNamedConstantToTypeName);
    // Solve the problem.
    // no indices; we want to keep all variables
    List<Expression> indices = Util.list();
    Expression result = solver.solve(group, inputExpression, indices, mapFromSymbolNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isUniquelyNamedConstantPredicate, theory);
    if (solverListener != null) {
        solverListener.apply(null);
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) MultiIndexQuantifierEliminator(com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator) SGDPLLT(com.sri.ai.grinder.sgdpllt.core.solver.SGDPLLT) Collection(java.util.Collection) Expression(com.sri.ai.expresso.api.Expression) Function(java.util.function.Function) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Predicate(com.google.common.base.Predicate) Max(com.sri.ai.grinder.sgdpllt.group.Max) Map(java.util.Map) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) MultiIndexQuantifierEliminator(com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator) Max(com.sri.ai.grinder.sgdpllt.group.Max) SGDPLLT(com.sri.ai.grinder.sgdpllt.core.solver.SGDPLLT) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) Expression(com.sri.ai.expresso.api.Expression) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) LinkedHashMap(java.util.LinkedHashMap)

Example 9 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class Context method extendWith.

/**
	 * Extends context with index expressions, taking into account that new contextual variables may collide with existing ones.
	 * In this case, it renames the incoming variables to unique identifiers and replaces them in the types of remaining
	 * index expressions. It also renames the variables in a given expressions supposed to be in their scope (for example,
	 * the head and condition of an intensionally defined set).
	 * Returns the new context and the index expressions and expression in scope after the renaming.
	 * @param indexExpressions
	 * @param expressionInScope
	 * @return the new context and the index expressions and expression in scope after the renaming
	 */
default default Triple<Context, ExtensionalIndexExpressionsSet, Expression> extendWith(ExtensionalIndexExpressionsSet indexExpressions, Expression expressionInScope) {
    Triple<Context, ExtensionalIndexExpressionsSet, Expression> result;
    if (thereExists(getIndices(indexExpressions), index -> this.containsSymbol(index))) {
        // OPTIMIZATION: only kick in this entire procedure when extending with symbol in the context (previous ones could have been dealt with normally).
        // the objects to be returned in the triple:
        Context newContext = this;
        ArrayList<Expression> newIndexExpressionsList = new ArrayList<>(indexExpressions.getList());
        Expression newExpressionInScope = expressionInScope;
        // Collects all existing symbols to be able to create unique symbols
        Set<Expression> alreadyDefined = Util.set();
        alreadyDefined.addAll(this.getSymbols());
        alreadyDefined.addAll(Expressions.freeSymbols(new DefaultTuple(newIndexExpressionsList), this));
        alreadyDefined.addAll(Expressions.freeSymbols(expressionInScope, this));
        Predicate<Expression> isAlreadyDefined = e -> alreadyDefined.contains(e);
        for (int i = 0; i != newIndexExpressionsList.size(); i++) {
            Expression indexExpression = newIndexExpressionsList.get(i);
            Symbol index = (Symbol) indexExpression.get(0);
            Expression type = indexExpression.get(1);
            PairOf<Expression> newIndexAndNewExpressionInScope = Expressions.standardizeApart(index, isAlreadyDefined, newExpressionInScope);
            Expression newIndex = newIndexAndNewExpressionInScope.first;
            newExpressionInScope = newIndexAndNewExpressionInScope.second;
            // type should not contain the index
            Expression newIndexExpression = apply(IN, newIndex, type);
            newIndexExpressionsList.set(i, newIndexExpression);
            alreadyDefined.add(newIndex);
            for (int j = i + 1; j != newIndexExpressionsList.size(); j++) {
                Expression anotherIndexExpression = newIndexExpressionsList.get(j);
                Expression anotherIndex = anotherIndexExpression.get(0);
                Expression anotherType = anotherIndexExpression.get(1);
                Expression newAnotherType = anotherType.replaceSymbol(index, newIndex, this);
                // anotherIndex is a symbols and does not contain index
                Expression newAnotherIndexExpression = apply(IN, anotherIndex, newAnotherType);
                newIndexExpressionsList.set(j, newAnotherIndexExpression);
            }
        }
        ExtensionalIndexExpressionsSet newIndexExpressions = new ExtensionalIndexExpressionsSet(newIndexExpressionsList);
        newContext = newContext.extendWith(newIndexExpressions);
        result = triple(newContext, newIndexExpressions, newExpressionInScope);
    } else {
        // no collision; usual extension and the expressions do not change.
        result = triple(extendWith(indexExpressions), indexExpressions, expressionInScope);
    }
    return result;
}
Also used : IN(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.IN) Type(com.sri.ai.expresso.api.Type) Triple(com.sri.ai.util.base.Triple) Collection(java.util.Collection) Expressions(com.sri.ai.expresso.helper.Expressions) Set(java.util.Set) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) Triple.triple(com.sri.ai.util.base.Triple.triple) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Symbol(com.sri.ai.expresso.api.Symbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Predicate(com.google.common.base.Predicate) Map(java.util.Map) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Registry(com.sri.ai.grinder.api.Registry) Util(com.sri.ai.util.Util) IndexExpressions.getIndices(com.sri.ai.grinder.sgdpllt.library.indexexpression.IndexExpressions.getIndices) Util.thereExists(com.sri.ai.util.Util.thereExists) Symbol(com.sri.ai.expresso.api.Symbol) ArrayList(java.util.ArrayList) DefaultTuple(com.sri.ai.expresso.core.DefaultTuple) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression)

Example 10 with Type

use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.

the class TrueContext method add.

@Override
public TrueContext add(Type type) {
    TrueContext result = clone();
    String name = type.getName();
    Expression typeExpression = parse(name);
    LinkedHashMap<Expression, Type> additionalTypeMap = map(typeExpression, type);
    result.fromTypeExpressionToType = new StackedHashMap<>(additionalTypeMap, fromTypeExpressionToType);
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression)

Aggregations

Type (com.sri.ai.expresso.api.Type)123 Expression (com.sri.ai.expresso.api.Expression)93 FunctionType (com.sri.ai.expresso.type.FunctionType)28 Test (org.junit.Test)25 Context (com.sri.ai.grinder.api.Context)24 LinkedHashMap (java.util.LinkedHashMap)22 Categorical (com.sri.ai.expresso.type.Categorical)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)20 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)19 TupleType (com.sri.ai.expresso.type.TupleType)19 ArrayList (java.util.ArrayList)19 Map (java.util.Map)19 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)18 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)18 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 Beta (com.google.common.annotations.Beta)13 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)12 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)12 List (java.util.List)12