Search in sources :

Example 1 with ExpressionVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable in project aic-praise by aic-sri-international.

the class ProceduralAttachmentExpressionFactor method getVariableType.

private Type getVariableType(Variable variable, Context context) {
    myAssert(variable instanceof ExpressionVariable, () -> getClass() + " must be defined for " + ExpressionVariable.class + ", but one of them has been defined for " + proceduralAttachment.getVariable() + " of type " + variable.getClass());
    ExpressionVariable expressionVariable = (ExpressionVariable) variable;
    myAssert(context.containsSymbol(expressionVariable), () -> "There is a procedural attachment for variable " + expressionVariable + " but it is not registered in the context");
    Type type = context.getTypeOfRegisteredSymbol(expressionVariable);
    myAssert(type != null, () -> "There is a procedural attachment for variable " + expressionVariable + " but its type is unknown");
    return type;
}
Also used : Type(com.sri.ai.expresso.api.Type) RealExpressoType(com.sri.ai.expresso.type.RealExpressoType) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)

Example 2 with ExpressionVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable 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());
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CommonTheory(com.sri.ai.grinder.application.CommonTheory) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ConstantFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) Factor(com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor) DefaultExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor) ArrayList(java.util.ArrayList) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 3 with ExpressionVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable 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;
}
Also used : ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) Variable(com.sri.ai.praise.core.representation.interfacebased.factor.api.Variable) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) Expression(com.sri.ai.expresso.api.Expression) DefaultVariableMarginalQuery(com.sri.ai.praise.core.representation.interfacebased.factor.core.DefaultVariableMarginalQuery) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) ExpressionBasedProblem(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedProblem) Problem(com.sri.ai.praise.core.representation.interfacebased.factor.api.Problem)

Example 4 with ExpressionVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable in project aic-praise by aic-sri-international.

the class ExpressionBayesianModelTest method generateDatasetForChildParentModel.

/**
 * Auxiliar function to generate a dataset for the childParent model based on standard datapoints (designed to test all the different subcases for the model)
 *
 * Order of variables for the datapoints: (Child, Parent)
 * @param numberOfDatapoints1 - (1, 2)
 * @param numberOfDatapoints2 - (5, 1)
 * @param numberOfDatapoints3 - (4, 3)
 * @param numberOfDatapoints4 - (1, 5)
 * @return the dataset with the specified number of datapoints
 */
private static DefaultDataset generateDatasetForChildParentModel(int numberOfDatapoints1, int numberOfDatapoints2, int numberOfDatapoints3, int numberOfDatapoints4) {
    List<ExpressionVariable> variables = list(childVariable, parentVariable);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, list(parse("1"), parse("2")));
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, list(parse("5"), parse("1")));
    DefaultDatapoint datapoint3 = new DefaultDatapoint(variables, list(parse("4"), parse("3")));
    DefaultDatapoint datapoint4 = new DefaultDatapoint(variables, list(parse("1"), parse("5")));
    List<DefaultDatapoint> datapoints = list();
    for (int i = 1; i <= numberOfDatapoints1; i++) datapoints.add(datapoint1);
    for (int i = 1; i <= numberOfDatapoints2; i++) datapoints.add(datapoint2);
    for (int i = 1; i <= numberOfDatapoints3; i++) datapoints.add(datapoint3);
    for (int i = 1; i <= numberOfDatapoints4; i++) datapoints.add(datapoint4);
    DefaultDataset dataset = new DefaultDataset(datapoints);
    return dataset;
}
Also used : DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)

Example 5 with ExpressionVariable

use of com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable in project aic-praise by aic-sri-international.

the class ExpressionBayesianModelTest method generateDatasetForEarthquakeBurglaryAlarmModel.

/**
 * Auxiliar function to generate a dataset for the Earthquake/Burglary/Alarm model based on some standard datapoints
 *
 * Order of variables for the datapoints: (Alarm, Earthquake, Burglary)
 * @param numberOfDatapoints1 - (1, 0, 1)
 * @param numberOfDatapoints2 - (1, 1, 1)
 * @return
 */
private static DefaultDataset generateDatasetForEarthquakeBurglaryAlarmModel(int numberOfDatapoints1, int numberOfDatapoints2) {
    List<ExpressionVariable> variables = list(alarm, earthquake, burglary);
    DefaultDatapoint datapoint1 = new DefaultDatapoint(variables, list(parse("1"), parse("0"), parse("1")));
    DefaultDatapoint datapoint2 = new DefaultDatapoint(variables, list(parse("1"), parse("1"), parse("1")));
    List<DefaultDatapoint> datapoints = list();
    for (int i = 1; i <= numberOfDatapoints1; i++) datapoints.add(datapoint1);
    for (int i = 1; i <= numberOfDatapoints2; i++) datapoints.add(datapoint2);
    DefaultDataset dataset = new DefaultDataset(datapoints);
    return dataset;
}
Also used : DefaultDataset(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint) DefaultExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable) ExpressionVariable(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable) DefaultDatapoint(com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)

Aggregations

ExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionVariable)12 DefaultExpressionVariable (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionVariable)9 Expression (com.sri.ai.expresso.api.Expression)6 Context (com.sri.ai.grinder.api.Context)6 CommonTheory (com.sri.ai.grinder.application.CommonTheory)5 TrueContext (com.sri.ai.grinder.core.TrueContext)5 Type (com.sri.ai.expresso.api.Type)3 ExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor)3 DefaultExpressionFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.DefaultExpressionFactor)3 ArrayList (java.util.ArrayList)3 Theory (com.sri.ai.grinder.api.Theory)2 Factor (com.sri.ai.praise.core.representation.interfacebased.factor.api.Factor)2 ConstantFactor (com.sri.ai.praise.core.representation.interfacebased.factor.core.ConstantFactor)2 DefaultDatapoint (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDatapoint)2 DefaultDataset (com.sri.ai.praise.learning.parameterlearning.representation.dataset.DefaultDataset)2 Test (org.junit.Test)2 DefaultExistentiallyQuantifiedFormula (com.sri.ai.expresso.core.DefaultExistentiallyQuantifiedFormula)1 DefaultIntensionalMultiSet (com.sri.ai.expresso.core.DefaultIntensionalMultiSet)1 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)1 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)1