use of com.sri.ai.grinder.application.CommonTheory in project aic-expresso by aic-sri-international.
the class IfThenElseStepSolverTest method test04.
@Test
public void test04() {
Expression expression = parse("if X=2 then 2 else 3");
String[] symbolsAndTypes = { "X", "1..1" };
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(symbolsAndTypes);
Expression expectedResult = parse("3");
// get method name as string
String testName = new Object() {
}.getClass().getEnclosingMethod().getName();
runIfThenElseStepSolverTest(expression, context, expectedResult, testName);
}
use of com.sri.ai.grinder.application.CommonTheory in project aic-expresso by aic-sri-international.
the class IfThenElseStepSolverTest method test11.
@Test
public void test11() {
Expression expression = parse("if (X=1) or (Y=1) then 2 + 2 else 3 + 3");
String[] symbolsAndTypes = { "X", "0..1", "Y", "0..1" };
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(symbolsAndTypes);
Expression expectedResult = parse("if X = 1 then 4 else if Y = 1 then 4 else 6");
// get method name as string
String testName = new Object() {
}.getClass().getEnclosingMethod().getName();
runIfThenElseStepSolverTest(expression, context, expectedResult, testName);
}
use of com.sri.ai.grinder.application.CommonTheory in project aic-expresso by aic-sri-international.
the class OptimizationWithNelderMead method numberOfVariablesInExpression.
public int numberOfVariablesInExpression() {
Theory theory = new CommonTheory();
Context context = new TrueContext(theory);
Set<Expression> variablesInExpression = Expressions.freeVariables(expressionToOptimize, context);
return variablesInExpression.size();
}
use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest method runGabriels.
private void runGabriels(String[] variableAndTypes, String factorNetworkString, String queryVariableString, Expression expected) {
Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(variableAndTypes);
ExpressionFactorNetwork factorNetwork = expressionFactorNetwork(factorNetworkString, context);
Expression query = Expressions.parse(queryVariableString);
// not sure it will work
Set<Expression> setOfFactors = new HashSet<>();
for (IdentityWrapper iw : factorNetwork.getAs()) {
ExpressionFactor f = (ExpressionFactor) iw.getObject();
Expression expressionFactor = f;
boolean successfullyAdded = setOfFactors.add(expressionFactor);
if (!successfullyAdded) {
setOfFactors.remove(expressionFactor);
Expression squareFactor = apply("*", expressionFactor, expressionFactor);
squareFactor = theory.evaluate(squareFactor, context);
setOfFactors.add(squareFactor);
}
}
// create model
Model m = new Model(setOfFactors, theory, context, false, query);
// do all iterations until the end, storing time
Iterator<PartitionTree> bfsExpander = new BFS(m);
IncrementalAnytimeBeliefPropagationWithSeparatorConditioning sbp = new IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(m, bfsExpander);
long initialTime = System.currentTimeMillis();
Bound inferenceResult = null;
println("----------------solving with Gabriels----------------");
while (bfsExpander.hasNext()) {
inferenceResult = sbp.expandAndComputeInference();
// .normalize(theory, context));
println("Current bound on " + query + ": " + inferenceResult);
}
long finalTime = System.currentTimeMillis();
Expression normalizedResult = inferenceResult.normalize(theory, context);
normalizedResult = ((IntensionalSet) normalizedResult).getHead();
Expression normalizedexpected = PRAiSEUtil.normalize(query, expected, context);
println("Result factor: " + ((IntensionalSet) inferenceResult).getHead());
println("Normalized : " + normalizedResult);
// print the way it is done above
println("Time: " + (finalTime - initialTime) + " ms.");
println(normalizedexpected.equals(normalizedResult) ? "Correct!" : "Error!");
Expression test = parse("(" + normalizedResult + ") = (" + normalizedexpected + ")");
Expression testResult = context.evaluate(test);
assertEquals(TRUE, testResult);
}
use of com.sri.ai.grinder.application.CommonTheory in project aic-praise by aic-sri-international.
the class ExpressionFactorTest method testInvert.
@Test
public void testInvert() {
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U and V then 2 else 3"), context);
Factor invertedFactor = factorUV.invert();
assertEquals("if U then if V then 0.5 else 1/3 else 1/3", invertedFactor.toString());
}
Aggregations