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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations