use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable in project aic-praise by aic-sri-international.
the class ProceduralAttachmentFactorTest method testReals.
@Test
public void testReals() {
ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(false);
ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(3);
Theory theory;
Context context;
Procedure<Double> procedure1;
Procedure<Double> procedure2;
Procedure<Double> procedure3;
Procedure<Double> procedure4;
Procedure<Double> procedure5;
Wrapper<Boolean> ran1 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran2 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran3 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran4 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran5 = new Wrapper<Boolean>(false);
ExpressionFactorNetwork network;
Factor queryResult;
theory = new CommonTheory();
context = new TrueContext(theory);
context = context.extendWithSymbolsAndTypes(parse("V1"), parse("Boolean"), parse("V2"), parse("Boolean"), parse("V3"), parse("Boolean"), parse("V4"), parse("Boolean"), parse("V5"), parse("Boolean"), parse("O1"), parse("Real"), parse("O2"), parse("Real"), parse("O3"), parse("Real"), parse("O4"), parse("Real"), parse("O5"), parse("Real"));
procedure1 = p -> {
println("Proceeding with procedure 1!");
ran1.value = true;
return 1.0;
};
procedure2 = p -> {
println("Proceeding with procedure 2!");
ran2.value = true;
return 2.0;
};
procedure3 = p -> {
println("Proceeding with procedure 3!");
ran3.value = true;
// this is going to make the condition fail!
return 30.0;
};
procedure4 = p -> {
println("Proceeding with procedure 4!");
ran4.value = true;
return 4.0;
};
procedure5 = p -> {
println("Proceeding with procedure 5!");
ran5.value = true;
return 5.0;
};
ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("O1", procedure1, "O2", procedure2, "O3", procedure3, "O4", procedure4, "O5", procedure5));
List<Expression> factorExpressions = list(parse("if V1 and O1 > 0.9 and O1 < 1.1 and V2 then 0.8 else 0.2"), parse("if V2 and O2 > 1.9 and O2 < 2.1 and V3 then 0.8 else 0.2"), parse("if V3 and O3 > 2.9 and O3 < 3.1 and V4 then 0.8 else 0.2"), parse("if V4 and O4 > 3.9 and O4 < 4.1 and V5 then 0.8 else 0.2"), parse("if V5 and O5 > 4.9 and O5 < 5.1 then 0.8 else 0.2"));
network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
ExactBP algorithm = new ExactBP(new DefaultExpressionVariable(parse("V1")), network);
queryResult = algorithm.apply();
println("Result computed for query V1.");
println("Result: " + queryResult);
assertTrue(ran1.value);
assertTrue(ran2.value);
assertTrue(ran3.value);
assertFalse(ran4.value);
assertFalse(ran5.value);
ran1.value = false;
ran2.value = false;
ran3.value = false;
ran4.value = false;
ran5.value = false;
// TODO: why is the test below failing?
// // We need to create the procedural attachments and network again because procedures are run only once and then just use cached values.
// proceduralAttachments =
// new DefaultProceduralAttachments(
// map(
// "O1", procedure1,
// "O2", procedure2,
// "O3", procedure3,
// "O4", procedure4,
// "O5", procedure5
// ));
// network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
//
// algorithm = new ExactBP(new DefaultExpressionVariable(parse("O1")), network);
// queryResult = algorithm.apply();
// println("Result computed for query O1.");
// println("Result: " + queryResult);
//
// assertTrue(ran1.value);
// assertTrue(ran2.value);
// assertTrue(ran3.value);
// assertFalse(ran4.value);
// assertFalse(ran5.value);
// assertEquals(queryResult.toString(), "if O1 > 0.99 then if O1 < 1.01 then 0.006 else 0 else 0");
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable in project aic-praise by aic-sri-international.
the class ProceduralAttachmentFactorTest method testBooleans.
@Test
public void testBooleans() {
ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(false);
ExpressoConfiguration.setDisplayNumericsMostDecimalPlacesInApproximateRepresentationOfNumericalSymbols(3);
Theory theory;
Context context;
Procedure<Boolean> procedure1;
Procedure<Boolean> procedure2;
Procedure<Boolean> procedure3;
Procedure<Boolean> procedure4;
Procedure<Boolean> procedure5;
Wrapper<Boolean> ran1 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran2 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran3 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran4 = new Wrapper<Boolean>(false);
Wrapper<Boolean> ran5 = new Wrapper<Boolean>(false);
ExpressionFactorNetwork network;
Factor queryResult;
theory = new CommonTheory();
context = new TrueContext(theory);
context = context.extendWithSymbolsAndTypes(parse("V1"), parse("Boolean"), parse("V2"), parse("Boolean"), parse("V3"), parse("Boolean"), parse("V4"), parse("Boolean"), parse("V5"), parse("Boolean"), parse("O1"), parse("Boolean"), parse("O2"), parse("Boolean"), parse("O3"), parse("Boolean"), parse("O4"), parse("Boolean"), parse("O5"), parse("Boolean"));
procedure1 = p -> {
println("Proceeding with procedure 1!");
ran1.value = true;
return true;
};
procedure2 = p -> {
println("Proceeding with procedure 2!");
ran2.value = true;
return true;
};
procedure3 = p -> {
println("Proceeding with procedure 3!");
ran3.value = true;
// THIS ONE RETURNS FALSE! This will short-circuit and procedures 4 and 5 will not be run
return false;
};
procedure4 = p -> {
println("Proceeding with procedure 4!");
ran4.value = true;
return true;
};
procedure5 = p -> {
println("Proceeding with procedure 5!");
ran5.value = true;
return true;
};
ProceduralAttachments proceduralAttachments = new DefaultProceduralAttachments(map("O1", procedure1, "O2", procedure2, "O3", procedure3, "O4", procedure4, "O5", procedure5));
List<Expression> factorExpressions = list(parse("if V1 and O1 and V2 then 0.8 else 0.2"), parse("if V2 and O2 and V3 then 0.8 else 0.2"), parse("if V3 and O3 and V4 then 0.8 else 0.2"), parse("if V4 and O4 and V5 then 0.8 else 0.2"), parse("if V5 and O5 then 0.8 else 0.2"));
network = expressionFactorNetwork(factorExpressions, proceduralAttachments, context);
ExactBP algorithm = new ExactBP(new DefaultExpressionVariable(parse("V1")), network);
queryResult = algorithm.apply();
println("Result computed for query V1.");
println("Result: " + queryResult);
assertTrue(ran1.value);
assertTrue(ran2.value);
assertTrue(ran3.value);
assertFalse(ran4.value);
assertFalse(ran5.value);
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable in project aic-praise by aic-sri-international.
the class ExpressionFactorTest method testSumOut.
@Test
public void testSumOut() {
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
ExpressionVariable u = new DefaultExpressionVariable(parse("U"));
List<ExpressionVariable> variablesToSumOut = new ArrayList<>();
variablesToSumOut.add(u);
ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U and V then 2 else 3"), context);
Factor summedOutFactor = factorUV.sumOut(variablesToSumOut);
assertEquals("if V then 5 else 6", summedOutFactor.toString());
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable in project aic-praise by aic-sri-international.
the class ExpressionBasedProblemToInterfaceBasedProblemConversion method translate.
public static Problem translate(ExpressionBasedProblem expressionBasedProblem) {
ExpressionVariable queryVariable = new DefaultExpressionVariable(expressionBasedProblem.getQuerySymbol());
ExpressionFactorNetwork factorNetwork = makeFactorNetwork(expressionBasedProblem);
Predicate<Expression> isExpressionParameterPredicate = expressionBasedProblem.getIsParameterPredicate();
Predicate<Variable> isParameterPredicate = makeIsParameterPredicate(isExpressionParameterPredicate);
Problem problem = new DefaultVariableMarginalQuery(queryVariable, factorNetwork, isParameterPredicate);
return problem;
}
use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable in project aic-praise by aic-sri-international.
the class ExpressionFactorTest method testMax.
@Test
public void testMax() {
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes("U", "Boolean", "V", "Boolean");
ExpressionFactor factorUV = new DefaultExpressionFactor(parse("if U then if V then 4 else 2 else 3"), context);
ExpressionVariable u = new DefaultExpressionVariable(parse("U"));
List<ExpressionVariable> variablesToMaxOut = new ArrayList<>();
variablesToMaxOut.add(u);
Factor maxFactor = factorUV.max(variablesToMaxOut);
assertEquals("if V then 4 else 3", maxFactor.toString());
}
Aggregations