use of com.sri.ai.grinder.theory.propositional.PropositionalTheory in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method makeTheoryTestingSupport.
@Override
protected TheoryTestingSupport makeTheoryTestingSupport() {
TheoryTestingSupport result = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
// using different testing variables and types to test distribution of testing information
// to sub constraint theories.
Categorical booleanType = BOOLEAN_TYPE;
Categorical dogsType = new Categorical("Dogs", 4, arrayList(parse("fido"), parse("rex")));
IntegerInterval oneTwoThree = new IntegerInterval(1, 3);
Map<String, Type> variablesAndTypes = map("F", booleanType, "G", booleanType, "R", dogsType, "S", dogsType, "T", oneTwoThree, "U", oneTwoThree);
result.setVariableNamesAndTypesForTesting(variablesAndTypes);
return result;
}
use of com.sri.ai.grinder.theory.propositional.PropositionalTheory in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method runCompleteSatisfiabilityTest.
/**
* @param conjunction
* @param expected
*/
private void runCompleteSatisfiabilityTest(String conjunction, Expression expected, Map<String, Type> variableNamesAndTypesForTesting) {
TheoryTestingSupport equalityTheoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new EqualityTheory(true, true));
equalityTheoryTestingSupport.setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), equalityTheoryTestingSupport, TheoryTestingSupport.make(makeRandom(), new PropositionalTheory()));
Constraint context = theoryTestingSupport.makeContextWithTestingInformation();
for (Expression literal : And.getConjuncts(parse(conjunction))) {
context = context.conjoin(literal, theoryTestingSupport.makeContextWithTestingInformation());
}
assertEquals(expected, context);
}
use of com.sri.ai.grinder.theory.propositional.PropositionalTheory in project aic-expresso by aic-sri-international.
the class CompoundTheoryWithDifferenceArithmeticTest method basicTests.
@Test
public void basicTests() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, true), new PropositionalTheory()));
Expression condition = parse("X = Y and Y = X and P and not Q and P and X = a and X != b");
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Constraint constraint = new CompleteMultiVariableContext(theoryTestingSupport.getTheory(), context);
constraint = constraint.conjoin(condition, context);
Expression expected = parse("(Y = a) and not Q and P and (X = Y)");
assertEquals(expected, constraint);
// nested indices
Expression expression = parse("sum({{(on I in 1..2, J in 2..3) sum({{ (on I in 1..10, J in 1..2) I + J : I != J }}) }})");
context = new TrueContext(theoryTestingSupport.getTheory());
expected = parse("536");
Expression actual = theoryTestingSupport.getTheory().evaluate(expression, context);
println(actual);
assertEquals(expected, actual);
}
use of com.sri.ai.grinder.theory.propositional.PropositionalTheory in project aic-expresso by aic-sri-international.
the class SymbolicShell method makeTheory.
private static Theory makeTheory() {
Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory(), new BruteForceFunctionTheory());
theory = new BruteForceFallbackTheory(theory);
return theory;
}
use of com.sri.ai.grinder.theory.propositional.PropositionalTheory in project aic-praise by aic-sri-international.
the class AnytimeExactBPTest method runGabriels.
private void runGabriels(String[] variableAndTypes, String factorNetworkString, String queryVariableString, Expression expected) {
Theory theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
Context context = new TrueContext(new CommonTheory()).extendWithSymbolsAndTypes(variableAndTypes);
ExpressionFactorNetwork factorNetwork = expressionFactorNetwork(factorNetworkString, context);
Expression query = Expressions.parse(queryVariableString);
// not sure it will work
Set<Expression> setOfFactors = new HashSet<>();
for (IdentityWrapper iw : factorNetwork.getAs()) {
ExpressionFactor f = (ExpressionFactor) iw.getObject();
Expression expressionFactor = f;
boolean successfullyAdded = setOfFactors.add(expressionFactor);
if (!successfullyAdded) {
setOfFactors.remove(expressionFactor);
Expression squareFactor = apply("*", expressionFactor, expressionFactor);
squareFactor = theory.evaluate(squareFactor, context);
setOfFactors.add(squareFactor);
}
}
// create model
Model m = new Model(setOfFactors, theory, context, false, query);
// do all iterations until the end, storing time
Iterator<PartitionTree> bfsExpander = new BFS(m);
IncrementalAnytimeBeliefPropagationWithSeparatorConditioning sbp = new IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(m, bfsExpander);
long initialTime = System.currentTimeMillis();
Bound inferenceResult = null;
println("----------------solving with Gabriels----------------");
while (bfsExpander.hasNext()) {
inferenceResult = sbp.expandAndComputeInference();
// .normalize(theory, context));
println("Current bound on " + query + ": " + inferenceResult);
}
long finalTime = System.currentTimeMillis();
Expression normalizedResult = inferenceResult.normalize(theory, context);
normalizedResult = ((IntensionalSet) normalizedResult).getHead();
Expression normalizedexpected = PRAiSEUtil.normalize(query, expected, context);
println("Result factor: " + ((IntensionalSet) inferenceResult).getHead());
println("Normalized : " + normalizedResult);
// print the way it is done above
println("Time: " + (finalTime - initialTime) + " ms.");
println(normalizedexpected.equals(normalizedResult) ? "Correct!" : "Error!");
Expression test = parse("(" + normalizedResult + ") = (" + normalizedexpected + ")");
Expression testResult = context.evaluate(test);
assertEquals(TRUE, testResult);
}
Aggregations