Search in sources :

Example 36 with Context

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;
}
Also used : Context(com.sri.ai.grinder.api.Context) Triple(com.sri.ai.util.base.Triple) HashSet(java.util.HashSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 37 with Context

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;
}
Also used : Context(com.sri.ai.grinder.api.Context) Triple(com.sri.ai.util.base.Triple) HashSet(java.util.HashSet) IndexExpressionsSet(com.sri.ai.expresso.api.IndexExpressionsSet) Set(java.util.Set) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) DefaultExtensionalMultiSet(com.sri.ai.expresso.core.DefaultExtensionalMultiSet) Expression(com.sri.ai.expresso.api.Expression) HashSet(java.util.HashSet)

Example 38 with Context

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Context(com.sri.ai.grinder.api.Context) LinkedHashMap(java.util.LinkedHashMap) Type(com.sri.ai.expresso.api.Type) TupleType(com.sri.ai.expresso.type.TupleType) Expression(com.sri.ai.expresso.api.Expression) TupleType(com.sri.ai.expresso.type.TupleType) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Pair(com.sri.ai.util.base.Pair)

Example 39 with Context

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);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) ExtensionalIndexExpressionsSet(com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext) Test(org.junit.Test)

Example 40 with Context

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;
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) TrueContext(com.sri.ai.grinder.core.TrueContext)

Aggregations

Context (com.sri.ai.grinder.api.Context)132 Expression (com.sri.ai.expresso.api.Expression)100 Test (org.junit.Test)50 TrueContext (com.sri.ai.grinder.core.TrueContext)40 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)36 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 Type (com.sri.ai.expresso.api.Type)31 Theory (com.sri.ai.grinder.api.Theory)24 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)23 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)22 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)21 Set (java.util.Set)20 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)19 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)19 CompleteMultiVariableContext (com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext)16 Map (java.util.Map)16 Beta (com.google.common.annotations.Beta)15 Constraint (com.sri.ai.grinder.api.Constraint)15 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)14 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)14