Search in sources :

Example 1 with DefaultExtensionalBound

use of com.sri.ai.grinder.library.bounds.DefaultExtensionalBound in project aic-expresso by aic-sri-international.

the class BoundsTest method declareTheoryContextAndSetOfFactors.

@Before
public void declareTheoryContextAndSetOfFactors() {
    ExpressoConfiguration.setDisplayNumericsExactlyForSymbols(true);
    extensionalBound = new DefaultExtensionalBound();
    intensionalBound = new DefaultIntensionalBound();
    theory = new CompoundTheory(new EqualityTheory(false, true), new DifferenceArithmeticTheory(false, false), new LinearRealArithmeticTheory(false, false), new TupleTheory(), new PropositionalTheory());
    context = new TrueContext(theory);
    context = context.extendWithSymbolsAndTypes("X", "Boolean");
    context = context.extendWithSymbolsAndTypes("Y", "Boolean");
    context = context.extendWithSymbolsAndTypes("A", "Boolean");
    context = context.extendWithSymbolsAndTypes("B", "Boolean");
    context = context.extendWithSymbolsAndTypes("C", "1..5");
    // context = context.extendWithSymbolsAndTypes("D","{1,3,4,8}");
    // Set of functions
    Expression phi1 = parse("if X = true then 1 else if Y = true then 2 else 3");
    Expression phi2 = parse("if A = true then if Y = true then 4 else 5 else 6");
    Expression phi3 = parse("if X = true then 7 else if B = true then 8 else 9");
    Expression phi4 = parse("if B = true then 10 else if A = true then 11 else 12");
    Expression phi5 = parse("if C < 4 then 10 else if C = 4 then 11 else 12");
    /*
		 * This is how we create a non empty extensional bound
		 */
    setOfFactors = new DefaultExtensionalBound(arrayList(phi1, phi2, phi3, phi4, phi5));
    // Set of numbers
    setOfNumbers = new DefaultExtensionalBound(arrayList(ONE, TWO));
    Set<Expression> factor = new HashSet<Expression>();
    model = new Model(factor);
    model.context = model.context.extendWithSymbolsAndTypes("A", "Boolean");
    model.context = model.context.extendWithSymbolsAndTypes("B", "Boolean");
    model.context = model.context.extendWithSymbolsAndTypes("Q", "Boolean");
    model.context = model.context.extendWithSymbolsAndTypes("C", "1..4");
    model.context = model.context.extendWithSymbolsAndTypes("D", "6..9");
    intensionalSetOfFactors1 = new DefaultIntensionalBound(arrayList(parse("A' in Boolean"), parse("C' in 1..5")), parse("if C = C' then if A = A' then 1 else 4 else 0"), TRUE);
    intensionalSetOfFactors2 = DefaultIntensionalBound.simplex(arrayList(parse("A")), model);
    intensionalSetOfFactors3 = DefaultIntensionalBound.simplex(arrayList(parse("C"), parse("B")), model);
}
Also used : EqualityTheory(com.sri.ai.grinder.theory.equality.EqualityTheory) DefaultIntensionalBound(com.sri.ai.grinder.library.bounds.DefaultIntensionalBound) 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) TrueContext(com.sri.ai.grinder.core.TrueContext) Expression(com.sri.ai.expresso.api.Expression) DefaultExtensionalBound(com.sri.ai.grinder.library.bounds.DefaultExtensionalBound) Model(com.sri.ai.grinder.anytime.Model) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 2 with DefaultExtensionalBound

use of com.sri.ai.grinder.library.bounds.DefaultExtensionalBound in project aic-expresso by aic-sri-international.

the class BoundsTest method testBoundProduct.

@Test
public void testBoundProduct() {
    // Set of functions
    Expression phi = parse("if A = true then 1 else 6");
    Bound setOfFactors2 = new DefaultExtensionalBound(phi);
    assertEquals(parse("{ if X then if A then 1 else 6 else if Y then if A then 2 else 12 else if A then 3 else 18, " + "if X then if A then 2 else 12 else if Y then if A then 4 else 24 else if A then 6 else 36, " + "if A then if Y then 4 else 5 else 36, if A then if Y then 8 else 10 else 72, " + "if X then if A then 7 else 42 else if B then if A then 8 else 48 else if A then 9 else 54, " + "if X then if A then 14 else 84 else if B then if A then 16 else 96 else if A then 18 else 108, " + "if B then if A then 10 else 60 else if A then 11 else 72, if B then if A then 20 else 120 else " + "if A then 22 else 144, if C < 4 then if A then 10 else 60 else if C = 4 then if A then 11 else 66 else if A then 12 else 72, " + "if C < 4 then if A then 20 else 120 else if C = 4 then if A then 22 else 132 else if A then 24 else 144 }"), Bounds.boundProduct(theory, context, true, setOfFactors, setOfFactors2, setOfNumbers));
    assertEquals(null, Bounds.boundProduct(theory, context, true, new DefaultExtensionalBound(new Expression[1])));
    assertEquals(parse("{ ( on A' in Boolean, C' in 1..5, C'' in 1..4, B' in Boolean ) if C = C' then if A then if A' then if B then if B' then if C' = C'' then 1 else 0 else 0 else if not B' then if C' = C'' then 1 else 0 else 0 else if B then if B' then if C' = C'' then 4 else 0 else 0 else if not B' then if C' = C'' then 4 else 0 else 0 else if not A' then if B then if B' then if C' = C'' then 1 else 0 else 0 else if not B' then if C' = C'' then 1 else 0 else 0 else if B then if B' then if C' = C'' then 4 else 0 else 0 else if not B' then if C' = C'' then 4 else 0 else 0 else 0 }"), Bounds.boundProduct(theory, context, false, intensionalSetOfFactors1, intensionalSetOfFactors3));
    assertEquals(parse("{ ( on A' in Boolean ) if A then if A' then 1 else 0 else if not A' then 1 else 0 }"), Bounds.boundProduct(theory, context, false, DefaultIntensionalBound.simplex(arrayList(parse("A")), model)));
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultIntensionalBound(com.sri.ai.grinder.library.bounds.DefaultIntensionalBound) Bound(com.sri.ai.grinder.library.bounds.Bound) DefaultExtensionalBound(com.sri.ai.grinder.library.bounds.DefaultExtensionalBound) DefaultExtensionalBound(com.sri.ai.grinder.library.bounds.DefaultExtensionalBound) Test(org.junit.Test)

Example 3 with DefaultExtensionalBound

use of com.sri.ai.grinder.library.bounds.DefaultExtensionalBound in project aic-expresso by aic-sri-international.

the class BPTest method getError.

@SuppressWarnings("unused")
private static float getError(Bound b, Theory t, Context c) {
    if (b.isExtensionalBound()) {
        List<Expression> l = ((DefaultExtensionalBound) b).getElementsDefinitions();
        Expression e1 = l.get(0);
        Expression e2 = l.get(1);
        Expression n = t.evaluate(apply("-", e1.get(2), e2.get(2)), c);
        float f = n.getArguments().get(0).intValue() / n.getArguments().get(1).intValue();
        return f;
    }
    return 0;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) DefaultExtensionalBound(com.sri.ai.grinder.library.bounds.DefaultExtensionalBound)

Example 4 with DefaultExtensionalBound

use of com.sri.ai.grinder.library.bounds.DefaultExtensionalBound 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 5 with DefaultExtensionalBound

use of com.sri.ai.grinder.library.bounds.DefaultExtensionalBound 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)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)5 DefaultExtensionalBound (com.sri.ai.grinder.library.bounds.DefaultExtensionalBound)5 Type (com.sri.ai.expresso.api.Type)2 Context (com.sri.ai.grinder.api.Context)2 Theory (com.sri.ai.grinder.api.Theory)2 DefaultIntensionalBound (com.sri.ai.grinder.library.bounds.DefaultIntensionalBound)2 Pair (com.sri.ai.util.base.Pair)2 Model (com.sri.ai.grinder.anytime.Model)1 TrueContext (com.sri.ai.grinder.core.TrueContext)1 Bound (com.sri.ai.grinder.library.bounds.Bound)1 CompoundTheory (com.sri.ai.grinder.theory.compound.CompoundTheory)1 DifferenceArithmeticTheory (com.sri.ai.grinder.theory.differencearithmetic.DifferenceArithmeticTheory)1 EqualityTheory (com.sri.ai.grinder.theory.equality.EqualityTheory)1 LinearRealArithmeticTheory (com.sri.ai.grinder.theory.linearrealarithmetic.LinearRealArithmeticTheory)1 PropositionalTheory (com.sri.ai.grinder.theory.propositional.PropositionalTheory)1 TupleTheory (com.sri.ai.grinder.theory.tuple.TupleTheory)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 Test (org.junit.Test)1