Search in sources :

Example 41 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 42 with Theory

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

the class PartitionTree method messageFromFactorToVariable.

public Bound messageFromFactorToVariable() {
    Bound childrenProduct = this.childrenProduct();
    Context context = model.getContext();
    Theory theory = model.getTheory();
    ArrayList<Expression> varToSum = this.getVarToSumInMessageFromFactorToVariable();
    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 43 with Theory

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

the class Tests method main.

public static void main(String[] args) {
    // Theory initialization
    Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    Context context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("A", "Boolean");
    Model m;
    String modelName;
    modelName = "Ising Model";
    m = new Model(IsingModel(3, 2, context, parse("Boolean")), theory, true);
    testFunction(modelName, m, true);
    // modelName = "Line Model";
    // m = new Model(lineModel(10, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    // 
    // modelName = "Binary Tree Model";
    // m = new Model(nTreeModel(4, 2, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    // 
    // modelName = "Random Model";
    // m = new Model(ModelGenerator.randomModel(8, 10, context, parse("Boolean")),theory, true);
    // 
    // testFunction(modelName, m,true);
    modelName = "Ising Model";
    List<List<TupleOfData>> listOdModelsToPrintInFile = new ArrayList<>();
    // m = new Model(IsingModel(20, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel2X2 = testing("IsingModel",m,2,2);
    // listOdModelsToPrintInFile.add(IsingModel2X2);
    // println("ok");
    // 
    // m = new Model(IsingModel(3, 3, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel3X3 = testing("IsingModel",m,3,3);
    // listOdModelsToPrintInFile.add(IsingModel3X3);
    // println("ok");
    // 
    // m = new Model(IsingModel(3, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel3X4 = testing("IsingModel",m,3,4);
    // listOdModelsToPrintInFile.add(IsingModel3X4);
    // println("ok");
    // 
    // m = new Model(IsingModel(4, 4, context, parse("Boolean")),theory, true);
    // List<InferenceResult> IsingModel4X4 = testing("IsingModel",m,4,4);
    // listOdModelsToPrintInFile.add(IsingModel4X4);
    // println("ok");
    // 
    // //		m = new Model(IsingModel(4, 5, context, parse("Boolean")),theory, true);
    // //		List<InferenceResult> IsingModel4X5 = testing("IsingModel",m,4,5);
    // //		listOdModelsToPrintInFile.add(IsingModel4X5);
    // //		println("ok");
    // 
    // modelName = "Line Model";
    // m = new Model(lineModel(20, context, parse("Boolean")),theory, true);
    // List<InferenceResult> line10 = testing(modelName,m,4,5);
    // listOdModelsToPrintInFile.add(line10);
    // println("ok");
    modelName = "Binary Tree Model";
    m = new Model(IsingModel(4, 4, context, parse("Boolean")), theory, true);
    List<TupleOfData> btree = testing(modelName, m, 4, 5);
    listOdModelsToPrintInFile.add(btree);
    println("ok");
    testingAndWritingToFile(modelName + ".csv", listOdModelsToPrintInFile);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Theory(com.sri.ai.grinder.api.Theory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) ArrayList(java.util.ArrayList) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) Model(anytimeExactBeliefPropagation.Model.Model) IsingModel(anytimeExactBeliefPropagation.ModelGenerator.IsingModel) ArrayList(java.util.ArrayList) List(java.util.List)

Example 44 with Theory

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

the class Derivative method difCase.

public static Expression difCase(Expression expression, Expression variable, Context context) {
    Theory theory = context.getTheory();
    List<Expression> arguments = expression.getArguments();
    Expression sum = arguments.get(1);
    for (int i = 2; i < arguments.size(); i++) {
        sum = apply("*", sum, arguments.get(i));
    }
    Expression toEvaluate = apply("-", computeDerivative(arguments.get(0), variable, context), computeDerivative(sum, variable, context));
    return theory.simplify(toEvaluate, context);
}
Also used : Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression)

Example 45 with Theory

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

the class Derivative method lnCase.

public static Expression lnCase(Expression expression, Expression variable, Context context) {
    Theory theory = context.getTheory();
    Expression insideLn = expression.getArguments().get(0);
    Expression toEvaluate = apply("/", Derivative.computeDerivative(insideLn, variable, context), insideLn);
    return theory.simplify(toEvaluate, context);
}
Also used : Theory(com.sri.ai.grinder.api.Theory) Expression(com.sri.ai.expresso.api.Expression)

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