Search in sources :

Example 21 with Type

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 + ";");
        }
    }
}
Also used : Context(com.sri.ai.grinder.api.Context) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Type(com.sri.ai.expresso.api.Type) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Map(java.util.Map)

Example 22 with Type

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

Example 23 with Type

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);
    }
}
Also used : Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Theory(com.sri.ai.grinder.api.Theory) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) FunctionType(com.sri.ai.expresso.type.FunctionType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 24 with Type

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

Example 25 with Type

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;
}
Also used : Plus(com.sri.ai.grinder.library.number.Plus) Type(com.sri.ai.expresso.api.Type) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) Util.pickUniformly(com.sri.ai.util.Util.pickUniformly) Util.map(com.sri.ai.util.Util.map) List(java.util.List) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Context(com.sri.ai.grinder.api.Context) Util(com.sri.ai.util.Util) AbstractTheoryWithBinaryAtomsTestingSupport(com.sri.ai.grinder.theory.base.AbstractTheoryWithBinaryAtomsTestingSupport) UnaryMinus(com.sri.ai.grinder.library.number.UnaryMinus) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

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