use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class ModelGenerator method randomModel.
/**
* Generates a random model with a given number of variables
*
* @param nVariables
* @param nFactors
* @param context
* @param possibleValues
* @return
*/
public static Triple<Set<Expression>, Context, Expression> randomModel(int nVariables, int nFactors, Context context, Expression possibleValues) {
Set<Expression> factorsInModel = new HashSet<Expression>();
Expression[] a = new Expression[nVariables];
for (int i = 0; i < nVariables; i++) {
a[i] = makeSymbol("A_" + i);
context = context.extendWithSymbolsAndTypes(a[i], possibleValues);
}
Random rand = new Random();
for (int i = 0; i < nFactors; i++) {
int n = rand.nextInt(min(15, nVariables));
Expression[] varOfF = new Expression[n];
for (int j = 0; j < varOfF.length; j++) {
varOfF[j] = a[rand.nextInt(nVariables)];
}
Expression f = generateProbability(context, varOfF);
factorsInModel.add(f);
}
Triple<Set<Expression>, Context, Expression> result = new Triple<>(factorsInModel, context, a[0]);
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class ModelGenerator method nTreeModel.
/**
* creates a tree with n children per node
* @param depth
* @param numberOfChildren
* @param context
* @param possibleValues
* @return
*/
public static Triple<Set<Expression>, Context, Expression> nTreeModel(int depth, int numberOfChildren, Context context, Expression possibleValues) {
Set<Expression> factorsInModel = new HashSet<Expression>();
int nCols = 1;
for (int i = 0; i < depth; i++) {
nCols *= numberOfChildren;
}
Expression[][] a = new Expression[depth][nCols];
for (int i = 0; i < depth; i++) {
for (int j = 0; j < nCols; j++) {
a[i][j] = makeSymbol("A_" + i + "_" + j);
context = context.extendWithSymbolsAndTypes(a[i][j], possibleValues);
}
}
int colMax = 1;
for (int i = 0; i < depth - 1; i++) {
for (int j = 0; j < colMax; j++) {
for (int k = j * numberOfChildren; k < (j + 1) * numberOfChildren; k++) {
Expression factor = generateProbability(context, a[i][j], a[i + 1][k]);
factorsInModel.add(factor);
}
}
colMax *= numberOfChildren;
}
Triple<Set<Expression>, Context, Expression> result = new Triple<>(factorsInModel, context, a[0][0]);
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class TupleValuedFreeVariablesSimplifier method extendContextWithComponentVariables.
private static Context extendContextWithComponentVariables(Context context, Map<Expression, TupleType> freeVariablesOfTupleType, Map<Expression, List<Pair<Expression, Integer>>> freeVariableComponentsMap) {
Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>();
Set<Type> componentTypes = new LinkedHashSet<>();
for (Map.Entry<Expression, TupleType> freeVariableOfTupleType : freeVariablesOfTupleType.entrySet()) {
Expression freeVariable = freeVariableOfTupleType.getKey();
TupleType freeVariableTupleType = freeVariableOfTupleType.getValue();
componentTypes.addAll(freeVariableTupleType.getElementTypes());
List<Pair<Expression, Integer>> components = freeVariableComponentsMap.get(freeVariable);
for (Pair<Expression, Integer> freeVariableComponent : components) {
Expression freeVariableComponentVar = freeVariableComponent.first;
Type freeVariableComponentType = freeVariableTupleType.getElementTypes().get(freeVariableComponent.second - 1);
mapFromSymbolNameToTypeName.put(freeVariableComponentVar.toString(), freeVariableComponentType.getName());
}
}
Context result = (Context) GrinderUtil.extendRegistryWith(mapFromSymbolNameToTypeName, componentTypes, context);
return result;
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class ContextTest method testExtendWith.
@Test
public void testExtendWith() {
Context context;
ExtensionalIndexExpressionsSet indexExpressions;
ExtensionalIndexExpressionsSet expectedNewIndexExpressionsSet;
Triple<Context, ExtensionalIndexExpressionsSet, Expression> triple;
Map<Expression, Expression> symbolsAndTypes;
Expression expressionInScope;
Expression expectedNewExpressionInScope;
context = new TrueContext();
indexExpressions = new ExtensionalIndexExpressionsSet("X", "Integer", "Y", "X..10");
expectedNewIndexExpressionsSet = new ExtensionalIndexExpressionsSet("X'", "Integer", "Y'", "X'..10");
expressionInScope = parse("X = 1 and Y = 2");
expectedNewExpressionInScope = parse("X' = 1 and Y' = 2");
symbolsAndTypes = map(parse("X"), parse("Integer"), parse("Y"), parse("X..10"), parse("X'"), parse("Integer"), parse("Y'"), parse("X'..10"));
context = context.extendWith(indexExpressions);
triple = context.extendWith(indexExpressions, expressionInScope);
println(triple);
assertEquals(symbolsAndTypes, triple.first.getSymbolsAndTypes());
assertEquals(expectedNewIndexExpressionsSet, triple.second);
assertEquals(expectedNewExpressionInScope, triple.third);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class BruteForceCommonInterpreterTest method makeContext.
/**
* Makes context with initializations necessary for these tests.
* @return
*/
public Context makeContext() {
LinkedHashMap<Expression, Expression> assignment = map(parse("Two"), Expressions.TWO);
Context context = new TrueContext();
context = Assignment.extendAssignments(assignment, context);
return context;
}
Aggregations