Search in sources :

Example 26 with Theory

use of com.sri.ai.grinder.api.Theory in project aic-expresso by aic-sri-international.

the class PartitionTree method messageFromVariableToFactor.

public Bound messageFromVariableToFactor() {
    Bound childrenProduct = this.childrenProduct();
    Context context = this.model.getContext();
    Theory theory = this.model.getTheory();
    ArrayList<Expression> varToSum = this.getVarToSumInMessageFromVariableToFactor();
    return childrenProduct.summingBound(varToSum, context, theory);
}
Also used : Context(com.sri.ai.grinder.api.Context) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) Bound(com.sri.ai.grinder.library.bounds.Bound)

Example 27 with Theory

use of com.sri.ai.grinder.api.Theory in project aic-expresso by aic-sri-international.

the class BasicTest method debug.

private void debug(Expression problem) {
    Theory theory = new DifferenceArithmeticTheory(true, true);
    Context context = new TrueContext(theory);
    context = context.putGlobalObject(BRUTE_FORCE_CHECKING_OF_NON_CONDITIONAL_PROBLEMS, "Yes");
    Expression symbolicSolution = theory.evaluate(problem, context);
    println(symbolicSolution);
    BruteForceInterpreter bruteForceInterpreter = new BruteForceCommonInterpreter();
    Expression bruteForceSolution = bruteForceInterpreter.apply(problem, context);
    println(bruteForceSolution);
    Assert.assertEquals(bruteForceSolution, symbolicSolution);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) BruteForceInterpreter(com.sri.ai.grinder.interpreter.BruteForceInterpreter) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Example 28 with Theory

use of com.sri.ai.grinder.api.Theory in project aic-expresso by aic-sri-international.

the class SummationOnDifferenceArithmeticAndPolynomialStepSolverTest method runTest.

private void runTest(Expression variable, String constraintString, Expression body, Expression expected, Context context) {
    Theory theory = context.getTheory();
    SingleVariableDifferenceArithmeticConstraint constraint = new SingleVariableDifferenceArithmeticConstraint(variable, true, theory);
    constraint = (SingleVariableDifferenceArithmeticConstraint) constraint.conjoin(parse(constraintString), context);
    Expression typeExpression = context.getTypeExpressionOfRegisteredSymbol(variable);
    SingleQuantifierEliminationProblem problem = new DefaultSingleQuantifierEliminationProblem(new Sum(), variable, typeExpression, constraint, body);
    ExpressionStepSolver stepSolver = new SummationOnDifferenceArithmeticAndPolynomialStepSolver(problem);
    Expression actual = stepSolver.solve(context);
    expected = simplify(expected, context);
    System.out.println("sum({{ (on " + variable + " in " + GrinderUtil.getTypeExpressionOfExpression(variable, context) + ") " + body + " : " + constraintString + " }} = " + actual + "\n");
    if (!expected.equals(actual)) {
        Expression difference = apply(MINUS, expected, actual);
        Expression differenceResult = simplify(difference, context);
        if (!differenceResult.equals(ZERO)) {
            System.err.println("Expressions are not equal and even difference is not zero");
            System.err.println("Expected: " + expected);
            System.err.println("Actual: " + actual);
            System.err.println("Difference: " + differenceResult);
            fail("Expressions are not equal and even difference is not zero. Expected: " + expected + ", actual: " + actual);
        }
    }
// TODO: correctness test against grounding
}
Also used : SummationOnDifferenceArithmeticAndPolynomialStepSolver(com.sri.ai.grinder.theory.differencearithmetic.SummationOnDifferenceArithmeticAndPolynomialStepSolver) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) Expression(com.sri.ai.expresso.api.Expression) ExpressionStepSolver(com.sri.ai.grinder.api.ExpressionStepSolver) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) Sum(com.sri.ai.grinder.group.Sum) SingleVariableDifferenceArithmeticConstraint(com.sri.ai.grinder.theory.differencearithmetic.SingleVariableDifferenceArithmeticConstraint)

Example 29 with Theory

use of com.sri.ai.grinder.api.Theory in project aic-expresso by aic-sri-international.

the class ModelGenerator method MaxMinProbability.

public static Pair<Double, Double> MaxMinProbability(Bound b, Model m) {
    Context context = m.getContext();
    Theory theory = m.getTheory();
    Expression query = m.getQuery().getValue();
    Type type = context.getTypeOfRegisteredSymbol(query);
    if (type.getName().equals("Boolean")) {
        double maxProbabilityOfTrue = -1;
        double minProbabilityOfTrue = 10;
        if (b.isExtensionalBound()) {
            DefaultExtensionalBound extensionalBound = (DefaultExtensionalBound) b;
            List<Expression> listOfElements = extensionalBound.getArguments();
            for (Expression distribution : listOfElements) {
                // replace and evaluate
                Expression replacingQueryByTrue = distribution.replaceAllOccurrences(query, parse("true"), context);
                Expression evaluating = theory.evaluate(replacingQueryByTrue, context);
                // convert to double
                double value = evaluating.doubleValue();
                // update max and min
                if (value > maxProbabilityOfTrue) {
                    maxProbabilityOfTrue = value;
                }
                if (value < minProbabilityOfTrue) {
                    minProbabilityOfTrue = value;
                }
            }
            Pair<Double, Double> result = new Pair<>(minProbabilityOfTrue, maxProbabilityOfTrue);
            return result;
        } else if (b.isIntensionalBound()) {
        }
    }
    return null;
}
Also used : Context(com.sri.ai.grinder.api.Context) Type(com.sri.ai.expresso.api.Type) Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression) DefaultExtensionalBound(com.sri.ai.grinder.library.bounds.DefaultExtensionalBound) Pair(com.sri.ai.util.base.Pair)

Example 30 with Theory

use of com.sri.ai.grinder.api.Theory in project aic-expresso by aic-sri-international.

the class BruteForceFallbackTheoryTest method runTest.

private static void runTest(String expressionString, String expectedString, String... symbolsAndTypes) {
    Theory theory = new BruteForceFallbackTheory(new DifferenceArithmeticTheory(false, true));
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes(symbolsAndTypes);
    Expression expression = parse(expressionString);
    Expression expected = parse(expectedString);
    Expression actual = theory.evaluate(expression, context);
    println(actual);
    assertEquals(expected, actual);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) Expression(com.sri.ai.expresso.api.Expression) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) TrueContext(com.sri.ai.grinder.core.TrueContext)

Aggregations

Theory (com.sri.ai.grinder.api.Theory)46 Expression (com.sri.ai.expresso.api.Expression)32 Context (com.sri.ai.grinder.api.Context)23 Type (com.sri.ai.expresso.api.Type)14 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)9 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)7 SingleQuantifierEliminationProblem (com.sri.ai.grinder.api.SingleQuantifierEliminationProblem)7 SingleVariableConstraint (com.sri.ai.grinder.api.SingleVariableConstraint)7 TrueContext (com.sri.ai.grinder.core.TrueContext)7 DefaultSingleQuantifierEliminationProblem (com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem)7 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)7 BruteForceCommonInterpreter (com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter)7 IndexExpressions (com.sri.ai.grinder.library.indexexpression.IndexExpressions)7 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)7 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)7 Beta (com.google.common.annotations.Beta)6 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)6 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)6 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)6 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)6