use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class ModelGenerator method isingModel.
/**
* Generates an Ising model with random probabilities
* @param nLines
* @param nCols
* @param context
* @param possibleValues
* Example: Boolean, 1..6
* @return
*/
public static Triple<Set<Expression>, Context, Expression> isingModel(int nLines, int nCols, Context context, Expression possibleValues) {
Set<Expression> factorsInModel = new HashSet<Expression>();
Expression[][] a = new Expression[nLines][nCols];
for (int i = 0; i < nLines; i++) {
for (int j = 0; j < nCols; j++) {
a[i][j] = makeSymbol("A_" + i + "_" + j);
context = context.extendWithSymbolsAndTypes(a[i][j], possibleValues);
}
}
for (int i = 0; i < nLines; i++) {
for (int j = 0; j < nCols; j++) {
if (j < nCols - 1) {
Expression fHor = generateProbability(context, a[i][j], a[i][j + 1]);
factorsInModel.add(fHor);
}
if (i < nLines - 1) {
Expression fVer = generateProbability(context, a[i][j], a[i + 1][j]);
factorsInModel.add(fVer);
}
}
}
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 ModelGenerator method lineModel.
/**
* Creates a model that looks like a queue of factors and variables
* Q - F - V - F -...- F - V
* @param nVariables
* @param context
* @param possibleValues
* @return
*/
public static Triple<Set<Expression>, Context, Expression> lineModel(int nVariables, 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);
}
for (int i = 0; i < a.length - 1; i++) {
Expression factor = generateProbability(context, a[i], a[i + 1]);
factorsInModel.add(factor);
}
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 ExhaustiveTest method testSimpleExhaustiveRewriter.
@Test
public void testSimpleExhaustiveRewriter() {
RewriterFromStepMaker rewriter = (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() < 10) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() + 1));
}
return new Solution(e);
};
Expression initial = parse("1");
Expression expected = parse("10");
runTest(rewriter, initial, expected, map());
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class ExhaustiveTest method testSimpleExhaustiveConditionalRewriter.
@Test
public void testSimpleExhaustiveConditionalRewriter() {
class FunkyStepSolver implements ExpressionLiteralSplitterStepSolver {
private Expression expression;
public FunkyStepSolver(Expression expression) {
this.expression = expression;
}
@Override
public FunkyStepSolver clone() {
FunkyStepSolver result = null;
try {
result = (FunkyStepSolver) super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return result;
}
@Override
public Step step(Context context) {
if (Expressions.isNumber(expression) && expression.intValue() % 10 != 0) {
if (expression.intValue() == 5) {
Expression literal = parse("JumpAt5");
ContextSplitting splitting = new ContextSplitting(literal, context);
switch(splitting.getResult()) {
case LITERAL_IS_TRUE:
return new Solution(parse("11"));
case LITERAL_IS_FALSE:
return new Solution(parse("6"));
case LITERAL_IS_UNDEFINED:
return new ItDependsOn(literal, splitting, this, this);
default:
throw new Error("Unpredicted case");
}
} else {
return new Solution(DefaultSymbol.createSymbol(expression.intValue() + 1));
}
} else
return new Solution(expression);
}
}
;
Rewriter rewriter = (Expression e) -> new FunkyStepSolver(e);
Expression initial = parse("1");
Expression expected = parse("if JumpAt5 then 20 else 10");
runTest(rewriter, initial, expected, map(parse("JumpAt5"), parse("Boolean")));
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class FirstOfTest method testSimpleFirstOfRewriter.
@Test
public void testSimpleFirstOfRewriter() {
List<RewriterFromStepMaker> rewriters = Util.<RewriterFromStepMaker>list((Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 10) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 9) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 8) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 7) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
}, (Expression e, Context c) -> {
if (Expressions.isNumber(e) && e.intValue() == 6) {
return new Solution(DefaultSymbol.createSymbol(e.intValue() - 1));
}
return new Solution(e);
});
Expression initial = parse("8");
Expression expected = parse("7");
runTest(new LinkedList<Rewriter>(rewriters), initial, expected, map());
initial = parse("7");
expected = parse("6");
runTest(new LinkedList<Rewriter>(rewriters), initial, expected, map());
}
Aggregations