Search in sources :

Example 71 with Context

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;
}
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 72 with Context

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;
}
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 73 with Context

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

Example 74 with Context

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")));
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) Expression(com.sri.ai.expresso.api.Expression) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver) Rewriter(com.sri.ai.grinder.rewriter.api.Rewriter) ContextSplitting(com.sri.ai.grinder.core.constraint.ContextSplitting) Solution(com.sri.ai.grinder.api.ExpressionLiteralSplitterStepSolver.Solution) Test(org.junit.Test)

Example 75 with Context

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

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