Search in sources :

Example 46 with Type

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

the class InversionSimplifier method getIndexAndFunctionType.

private static Pair<Expression, FunctionType> getIndexAndFunctionType(Expression functionOnIntensionalSet, Context context) {
    IndexExpressionsSet indexExpressionsSet = getIndexExpressions(functionOnIntensionalSet);
    List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
    if (indices.size() != 1) {
        throw new UnsupportedOperationException("Currently only support singular indices");
    }
    Expression index = indices.get(0);
    Context intensionalSetContext = context.extendWith(indexExpressionsSet);
    Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
    FunctionType functionType = null;
    if (type instanceof FunctionType) {
        functionType = (FunctionType) type;
    }
    Pair<Expression, FunctionType> result = new Pair<>(index, functionType);
    return result;
}
Also used : Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) FunctionType(com.sri.ai.expresso.type.FunctionType) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Pair(com.sri.ai.util.base.Pair)

Example 47 with Type

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

the class Derivative method derivativesOfFactor.

public static Set<Expression> derivativesOfFactor(Expression expression, Expression query, Context context) {
    Theory theory = context.getTheory();
    Set<Expression> variableInFactor = Expressions.freeVariables(expression, context);
    variableInFactor.remove(query);
    Set<Expression> ProbabilitiesFactor = new HashSet<Expression>();
    for (Expression variable : variableInFactor) {
        String str = "";
        Type type = context.getTypeOfRegisteredSymbol(variable);
        Iterator<Expression> valuesInType = type.iterator();
        List<Expression> probability = new ArrayList<Expression>();
        for (Expression values : in(valuesInType)) {
            String s = "prob" + variable.toString() + values.toString();
            probability.add(parse(s));
            context.extendWithSymbolsAndTypes(s, "0..1");
            str = str + "if " + variable + " = " + values.toString() + " then " + parse(s) + " else ";
        }
        str = str + " 0";
        ProbabilitiesFactor.add(parse(str));
    }
    Expression product = expression;
    for (Expression factor : ProbabilitiesFactor) {
        product = apply(TIMES, product, factor);
    }
    Expression evaluation = product;
    for (Expression variable : variableInFactor) {
        IndexExpressionsSet indices = getIndexExpressionsOfFreeVariablesIn(variable, context);
        Expression setOfFactorInstantiations = IntensionalSet.makeMultiSet(indices, // head
        evaluation, parse("true"));
        Expression sumOnPhi = apply(SUM, setOfFactorInstantiations);
        System.out.println(sumOnPhi);
        evaluation = theory.evaluate(sumOnPhi, context);
    }
    Set<Expression> result = new HashSet<Expression>();
    System.out.println(result);
    for (Expression variable : variableInFactor) {
        Type type = context.getTypeOfRegisteredSymbol(variable);
        Iterator<Expression> valuesInType = type.iterator();
        for (Expression values : in(valuesInType)) {
            String s = "prob" + variable.toString() + values.toString();
            result.add(Derivative.computeDerivative(evaluation, parse(s), context));
        }
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) HashSet(java.util.HashSet)

Example 48 with Type

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

the class ModelGenerator method generateProbability.

private static Expression generateProbability(Context context, int i, Expression... listOfVariables) {
    if (listOfVariables.length == 0) {
        return null;
    }
    // randomNumberGenerator();
    Expression result = makeSymbol(0);
    Expression variable = listOfVariables[i];
    // listOfVariables.remove(0);
    Type type = context.getTypeOfRegisteredSymbol(variable);
    Iterator<Expression> iteratorToValuesInType = type.iterator();
    if (listOfVariables.length == i + 1) {
        for (Expression value : in(iteratorToValuesInType)) {
            Expression varEqualsValue = apply(EQUAL, variable, value);
            Expression randProbability = randomNumberGenerator();
            result = apply(IF_THEN_ELSE, varEqualsValue, randProbability, result);
        }
    } else {
        for (Expression value : in(iteratorToValuesInType)) {
            Expression varEqualsValue = apply(EQUAL, variable, value);
            Expression randProbability = generateProbability(context, i + 1, listOfVariables);
            result = apply(IF_THEN_ELSE, varEqualsValue, randProbability, result);
        }
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression)

Example 49 with Type

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

the class SamplingIfNeededMultiQuantifierEliminator method domainIsContinuousOrDiscreteAndLargerThanSampleSize.

private boolean domainIsContinuousOrDiscreteAndLargerThanSampleSize(MeasurableMultiQuantifierEliminationProblem problem, Context context) {
    Type type = GrinderUtil.getTypeOfExpression(problem.getIndices().get(0), context);
    boolean result = type == null || !type.isDiscrete() || problem.getMeasure(context).compareTo(sampleSize) > 0;
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type)

Example 50 with Type

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

the class GrinderUtil method getCategoricalTypes.

/**
 * @param mapFromSymbolNameToTypeName
 * @param mapFromCategoricalTypeNameToSizeString
 * @param isUniquelyNamedConstantPredicate
 * @param registry
 * @return
 */
public static Collection<Type> getCategoricalTypes(Map<String, String> mapFromSymbolNameToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Predicate<Expression> isUniquelyNamedConstantPredicate, Registry registry) {
    Collection<Type> categoricalTypes = new LinkedList<Type>();
    for (Map.Entry<String, String> typeNameAndSizeString : mapFromCategoricalTypeNameToSizeString.entrySet()) {
        String typeExpressionString = typeNameAndSizeString.getKey();
        String sizeString = typeNameAndSizeString.getValue();
        // check if already present and, if not, make it
        Categorical type = (Categorical) registry.getType(typeExpressionString);
        if (type == null) {
            if (typeExpressionString.equals("Boolean")) {
                type = BOOLEAN_TYPE;
            } else {
                ArrayList<Expression> knownConstants = getKnownUniquelyNamedConstantsOf(typeExpressionString, mapFromSymbolNameToTypeName, isUniquelyNamedConstantPredicate, registry);
                type = new Categorical(typeExpressionString, parseInt(sizeString), knownConstants);
            }
        }
        categoricalTypes.add(type);
    }
    return categoricalTypes;
}
Also used : IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) TupleType(com.sri.ai.expresso.type.TupleType) Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) FunctionType(com.sri.ai.expresso.type.FunctionType) Expression(com.sri.ai.expresso.api.Expression) LambdaExpression(com.sri.ai.expresso.api.LambdaExpression) Categorical(com.sri.ai.expresso.type.Categorical) Map(java.util.Map) LinkedList(java.util.LinkedList)

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