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;
}
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);
}
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);
}
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);
}
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);
}
Aggregations