use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method testMeasureEquivalentInterval.
@Test
public void testMeasureEquivalentInterval() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Expression variable;
String constraintString;
Expression expected;
variable = parse("X");
constraintString = "true";
expected = parse("[0;4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < 3";
expected = parse("[0;3[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X < 4";
expected = parse("]3; 4[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("{ 3.2 }");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < Y";
expected = parse("if Y > 0 then [0;Y[ else {}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
// keep these tests together
variable = parse("X");
constraintString = "X < Y + 1";
// see previous test for situation in which condition is always true
expected = parse("if Y > 3 then [0;4] else [0 ; Y + 1[");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("{ 3.2 }");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.6";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("]3.1; 3.4]");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("{}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > Z and X >= Y";
expected = parse("if Z < Y then if Y <= 3.4 then [Y;3.4] else {} else if Z < 3.4 then ]Z;3.4] else {}");
runMeasureEquivalentIntervalTest(variable, constraintString, expected, context);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method testSummation.
@Test
public void testSummation() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Simplifier simplifier = (e, c) -> theoryTestingSupport.getTheory().simplify(e, c);
Expression variable;
String constraintString;
String bodyString;
Expression expected;
variable = parse("X");
constraintString = "true";
bodyString = "1";
expected = parse("4");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "1";
expected = parse("3");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "1";
expected = parse("if Y > 0 then Y else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "Y";
expected = parse("3*Y");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2";
bodyString = "X";
expected = parse("4.5");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < 3 and X != 2 and X = 2";
bodyString = "Y";
expected = parse("0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "X";
expected = parse("if Y > 0 then 0.5*Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "Y";
expected = parse("if Y > 0 then Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
variable = parse("X");
constraintString = "X < Y and X != 2";
bodyString = "X + Y";
expected = parse("if Y > 0 then 1.5*Y^2 else 0");
runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
// variable = parse("X");
// constraintString = "X < Y and X != 2";
// bodyString = "X^2 + Y";
// expected = parse("if Y > 0 then 0.333333333*Y^3 + Y^2 else 0");
// runSummationTest(variable, constraintString, bodyString, expected, simplifier, context);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class LinearRealArithmeticTheoryTest method testSatisfiability.
@Test
public void testSatisfiability() {
TheoryTestingSupport theoryTestingSupport = TheoryTestingSupport.make(makeRandom(), new LinearRealArithmeticTheory(true, true));
Context context = theoryTestingSupport.makeContextWithTestingInformation();
Expression variable;
String constraintString;
Expression expected;
variable = parse("X");
constraintString = "true";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < 3";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X < 4";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < Y";
expected = parse("Y > 0");
runSatisfiabilityTest(variable, constraintString, expected, context);
// keep these tests together
variable = parse("X");
constraintString = "X < Y + 1";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < Y - 4";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X < 2 - Y";
expected = parse("Y < 2");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > Y";
expected = parse("Y < 4");
runSatisfiabilityTest(variable, constraintString, expected, context);
// keep these tests together
variable = parse("X");
constraintString = "X > Y + 1";
expected = parse("Y < 3");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X >= Y + 4";
expected = parse("Y <= 0");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X >= Y + 2";
expected = parse("Y <= 2");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X >= Y";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
// keep these tests together
variable = parse("X");
constraintString = "X >= Y + 1";
expected = parse("Y <= 3");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3 and X <= 3";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X > 3.1 and X <= 3.4 and X != 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 3.2 and X = 3.2";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.2";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 3.6";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7";
expected = parse("true");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X != 7 and X = 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > 3.1 and X = 7 and X = 8";
expected = parse("false");
runSatisfiabilityTest(variable, constraintString, expected, context);
variable = parse("X");
constraintString = "X <= 3.4 and X > Z and X >= Y";
expected = parse("if Z < Y then Y <= 3.4 else Z < 3.4");
runSatisfiabilityTest(variable, constraintString, expected, context);
}
use of com.sri.ai.grinder.api.Context in project aic-expresso by aic-sri-international.
the class InversionPerformanceEvaluationTest method computeSize.
private Rational computeSize(Expression functionOnIntensionalSet, Rational resultSoFar, Context context) {
IntensionalSet intensionalSet = (IntensionalSet) functionOnIntensionalSet.get(0);
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() != 1) {
throw new UnsupportedOperationException("Currently only support singular indices");
}
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressionsSet);
Type type = GrinderUtil.getTypeOfExpression(index, intensionalSetContext);
Rational result = resultSoFar.multiply(type.cardinality().rationalValue());
Expression head = intensionalSet.getHead();
if (Expressions.isFunctionApplicationWithArguments(head) && Sets.isIntensionalSet(head.get(0))) {
result = computeSize(head, result, intensionalSetContext);
}
return result;
}
use of com.sri.ai.grinder.api.Context 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;
}
Aggregations