Search in sources :

Example 16 with PropositionalTheory

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;
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) Categorical(com.sri.ai.expresso.type.Categorical) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory)

Example 17 with PropositionalTheory

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);
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory)

Example 18 with PropositionalTheory

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);
}
Also used : TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AbstractTheoryTestingSupport(com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TrueContext(com.sri.ai.grinder.core.TrueContext) AbstractTheoryTest(com.sri.ai.test.grinder.theory.base.AbstractTheoryTest) Test(org.junit.Test)

Example 19 with PropositionalTheory

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;
}
Also used : BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) BruteForceFallbackTheory(com.sri.ai.grinder.theory.bruteforce.BruteForceFallbackTheory) BruteForceFunctionTheory(com.sri.ai.grinder.theory.function.BruteForceFunctionTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) 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) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory)

Example 20 with PropositionalTheory

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);
}
Also used : PartitionTree(IncrementalAnytimeExactBeliefPropagation.PartitionTree) ExpressionFactorNetwork(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.core.ExpressionFactorNetwork) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) CommonTheory(com.sri.ai.grinder.application.CommonTheory) 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) LinearRealArithmeticTheory(com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory) Bound(com.sri.ai.grinder.library.bounds.Bound) PropositionalTheory(com.sri.ai.grinder.theory.propositional.PropositionalTheory) CompoundTheory(com.sri.ai.grinder.theory.compound.CompoundTheory) IncrementalAnytimeBeliefPropagationWithSeparatorConditioning(IncrementalAnytimeExactBeliefPropagation.IncrementalAnytimeBeliefPropagationWithSeparatorConditioning) TupleTheory(com.sri.ai.grinder.theory.tuple.TupleTheory) BFS(IncrementalAnytimeExactBeliefPropagation.Model.BFS) ExpressionFactor(com.sri.ai.praise.core.representation.interfacebased.factor.core.expression.api.ExpressionFactor) IntensionalSet(com.sri.ai.expresso.api.IntensionalSet) HashSet(java.util.HashSet) TrueContext(com.sri.ai.grinder.core.TrueContext) Context(com.sri.ai.grinder.api.Context) DifferenceArithmeticTheory(com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory) IdentityWrapper(com.sri.ai.util.base.IdentityWrapper) TrueContext(com.sri.ai.grinder.core.TrueContext) CommonTheory(com.sri.ai.grinder.application.CommonTheory) Expression(com.sri.ai.expresso.api.Expression) Model(IncrementalAnytimeExactBeliefPropagation.Model.Model)

Aggregations

PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)39 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)33 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)32 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)30 Context (com.sri.ai.grinder.api.Context)25 Expression (com.sri.ai.expresso.api.Expression)23 TrueContext (com.sri.ai.grinder.core.TrueContext)19 TheoryTestingSupport (com.sri.ai.grinder.tester.TheoryTestingSupport)18 Test (org.junit.Test)17 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)16 Theory (com.sri.ai.grinder.api.Theory)11 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)11 Type (com.sri.ai.expresso.api.Type)8 LinkedHashMap (java.util.LinkedHashMap)6 FunctionType (com.sri.ai.expresso.type.FunctionType)4 StepSolver (com.sri.ai.grinder.api.StepSolver)4 AbstractTheoryTestingSupport (com.sri.ai.grinder.core.constraint.AbstractTheoryTestingSupport)4 Rewriter (com.sri.ai.grinder.rewriter.api.Rewriter)4 UnificationStepSolver (com.sri.ai.grinder.theory.base.UnificationStepSolver)4 Before (org.junit.Before)4