use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class SetOfArgumentTuplesForFunctionOccurringInExpression method computeUnionArgs.
private static void computeUnionArgs(List<Expression> unionArgs, Predicate<Expression> isF, FunctionType fType, Predicate<Expression> isFApplication, Expression e) {
// if E does not contain ƒ, oc<sub>ƒ</sub>[E] is ∅
if (!containsF(e, isF)) {
unionArgs.add(Sets.EMPTY_SET);
} else // if E is ƒ(t) for t a tuple, oc<sub>ƒ</sub>[E] is {t}
if (isFApplication.apply(e)) {
Expression tupleT = Expressions.makeTuple(e.getArguments());
Expression setT = ExtensionalSets.makeUniSet(tupleT);
unionArgs.add(setT);
} else // if E is ƒ, oc<sub>ƒ</sub>[E] is Α
if (isF.apply(e)) {
List<Expression> domainTypes = new ArrayList<>();
for (Type domainType : fType.getArgumentTypes()) {
domainTypes.add(Expressions.parse(domainType.getName()));
}
Expression tupleT = Expressions.makeTuple(domainTypes);
Expression setT = ExtensionalSets.makeUniSet(tupleT);
unionArgs.add(setT);
} else // if E is g(E′) for g a function symbol distinct from ƒ and t a k-tuple of expressions
if (Expressions.isFunctionApplicationWithArguments(e)) {
// if g(E′) is if C the E<sub>1</sub> else E<sub>2</sub> and C does not contain ƒ
if (IfThenElse.isIfThenElse(e) && !containsF(IfThenElse.condition(e), isF)) {
// then oc<sub>ƒ</sub>[E] is<br>
// if C then oc<sub>ƒ</sub>[E<sub>1</sub>] else oc<sub>ƒ</sub>[E<sub>2</sub>]
Expression e1 = IfThenElse.thenBranch(e);
List<Expression> thenUnionArgs = new ArrayList<>();
computeUnionArgs(thenUnionArgs, isF, fType, isFApplication, e1);
Expression thenBranch = makeUnion(thenUnionArgs);
Expression e2 = IfThenElse.elseBranch(e);
List<Expression> elseUnionArgs = new ArrayList<>();
computeUnionArgs(elseUnionArgs, isF, fType, isFApplication, e2);
Expression elseBranch = makeUnion(elseUnionArgs);
Expression ifThenElse = IfThenElse.make(IfThenElse.condition(e), thenBranch, elseBranch);
unionArgs.add(ifThenElse);
} else {
// oc<sub>ƒ</sub>[t<sub>1</sub>] ∪ … ∪ oc<sub>ƒ</sub>[t<sub>k</sub>]
for (Expression t : e.getArguments()) {
computeUnionArgs(unionArgs, isF, fType, isFApplication, t);
}
}
} else // quantified expression,
if (isArbitraryQuantifier(e) && !containsF(getQuantifierCondition(e), isF)) {
// then oc<sub>ƒ</sub>[E] is<br>
// ⋃<sub>x:C</sub>oc<sub>ƒ</sub>[E′]
QuantifiedExpression q = (QuantifiedExpression) e;
IndexExpressionsSet x = q.getIndexExpressions();
Expression c = getQuantifierCondition(e);
Expression ePrime = getQuantifiedExpression(q);
List<Expression> ePrimeUnionArgs = new ArrayList<>();
computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
Expression ocfOfEPrime = makeUnion(ePrimeUnionArgs);
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, ocfOfEPrime, c);
Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
unionArgs.add(intensionalUnion);
} else // quantified expression,
if (isArbitraryQuantifier(e)) {
// then oc<sub>ƒ</sub>[E] is<br>
// ⋃<sub>x</sub>(oc<sub>ƒ</sub>[C] ∪ oc<sub>ƒ</sub>[E′])
QuantifiedExpression q = (QuantifiedExpression) e;
IndexExpressionsSet x = q.getIndexExpressions();
Expression c = getQuantifierCondition(e);
Expression ePrime = getQuantifiedExpression(q);
List<Expression> conditionUnionArgs = new ArrayList<>();
computeUnionArgs(conditionUnionArgs, isF, fType, isFApplication, c);
List<Expression> ePrimeUnionArgs = new ArrayList<>();
computeUnionArgs(ePrimeUnionArgs, isF, fType, isFApplication, ePrime);
List<Expression> combinedUnionArgs = new ArrayList<>();
combinedUnionArgs.addAll(conditionUnionArgs);
combinedUnionArgs.addAll(ePrimeUnionArgs);
Expression combinedUnion = makeUnion(combinedUnionArgs);
Expression intensionalMultiSet = IntensionalSet.intensionalMultiSet(x, combinedUnion, Expressions.TRUE);
Expression intensionalUnion = Expressions.apply(FunctorConstants.INTENSIONAL_UNION, intensionalMultiSet);
unionArgs.add(intensionalUnion);
} else {
throw new UnsupportedOperationException("Do not have logic for handling expression of the form: " + e);
}
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class SampleCommonInterpreterTest method run.
private Expression run(int sampleSizeN, boolean alwaysSample, String expressionString) {
SampleCommonInterpreter interpreter = new SampleCommonInterpreter(sampleSizeN, alwaysSample, random);
Expression expression = parse(expressionString);
if (expression.numberOfArguments() == 1 && Sets.isIntensionalSet(expression.get(0))) {
IntensionalSet intensionalSet = (IntensionalSet) expression.get(0);
IndexExpressionsSet indexExpressions = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressions);
if (indices.size() == 1) {
Expression index = indices.get(0);
Context intensionalSetContext = context.extendWith(indexExpressions);
// Ensure condition of correct type is created
Type indexType = GrinderUtil.getType(index, intensionalSetContext);
SingleVariableConstraint singleVariableConstraint = null;
if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
singleVariableConstraint = new SingleVariableLinearRealArithmeticConstraint(index, true, intensionalSetContext.getTheory());
} else if (indexType instanceof IntegerExpressoType || indexType instanceof IntegerInterval) {
singleVariableConstraint = new SingleVariableDifferenceArithmeticConstraint(index, true, intensionalSetContext.getTheory());
}
if (singleVariableConstraint != null) {
singleVariableConstraint = singleVariableConstraint.conjoin(intensionalSet.getCondition(), intensionalSetContext);
intensionalSet = intensionalSet.setCondition(singleVariableConstraint);
expression = expression.set(0, intensionalSet);
}
}
}
Expression result = interpreter.apply(expression, context);
System.out.println("Evaluation with " + sampleSizeN + " samples of " + expressionString + " = " + result.doubleValue() + " (as rational=" + toString(result) + ")");
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class IntensionalSetConditionSimplifier method simplify.
public static Expression simplify(Expression expression, Context context) {
Expression result = expression;
if (Sets.isIntensionalSet(expression)) {
IntensionalSet intensionalSet = (IntensionalSet) expression;
Expression condition = intensionalSet.getCondition();
if (!(condition.equals(Expressions.TRUE) || condition.equals(Expressions.FALSE))) {
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
Context extendedContext = context.extendWith(indexExpressionsSet);
Expression evaluatedCondition = context.getTheory().evaluate(condition, extendedContext);
if (!evaluatedCondition.equals(condition)) {
result = IntensionalSet.make(Sets.isIntensionalMultiSet(expression) ? IntensionalSet.MULTI_SET_LABEL : IntensionalSet.UNI_SET_LABEL, indexExpressionsSet, intensionalSet.getHead(), evaluatedCondition);
}
}
}
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Measure method get.
public static Rational get(Expression intensionalSetExpression, Context context) {
Rational result;
if (Sets.isIntensionalSet(intensionalSetExpression)) {
IntensionalSet intensionalSet = (IntensionalSet) intensionalSetExpression;
IndexExpressionsSet indexExpressionsSet = intensionalSet.getIndexExpressions();
List<Expression> indices = IndexExpressions.getIndices(indexExpressionsSet);
if (indices.size() == 1) {
Expression evaluatedResult;
Expression intensionalSetIndex = indices.get(0);
Expression intensionalSetHead = intensionalSet.getHead();
if (!intensionalSetHead.equals(intensionalSetIndex)) {
throw new UnsupportedOperationException("Index and Head must be the same to calculate the meaure of an Intensional : " + intensionalSet);
}
Expression intensionalSetCondition = intensionalSet.getCondition();
Context intensionalSetContext = context.extendWith(indexExpressionsSet);
Type indexType = GrinderUtil.getType(intensionalSetIndex, intensionalSetContext);
if (intensionalSetCondition.equals(false)) {
// short circuit known empty sets up front.
evaluatedResult = Expressions.ZERO;
} else if (indexType instanceof RealExpressoType || indexType instanceof RealInterval) {
// NOTE : For Reals can always assume the condition is of this type.
SingleVariableLinearRealArithmeticConstraint svConstraint = (SingleVariableLinearRealArithmeticConstraint) intensionalSetCondition;
MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver realSolver = new MeasureOfSingleVariableLinearRealArithmeticConstraintStepSolver(svConstraint);
evaluatedResult = realSolver.solve(intensionalSetContext);
} else if (indexType instanceof FunctionType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a function type domain currently do not support conditions: " + intensionalSet);
}
// measure(co-domain)^measure(domain)
FunctionType indexFunctionType = (FunctionType) indexType;
Expression condomainIntensionalSet = constructComponentIntensionalSet(indexFunctionType.getCodomain(), intensionalSet, ZERO, intensionalSetContext);
Rational codomainMeasure = get(condomainIntensionalSet, intensionalSetContext);
Rational domainMeasure = Rational.ONE;
for (Type argDomainType : indexFunctionType.getArgumentTypes()) {
Expression argDomainIntensionalSet = constructComponentIntensionalSet(argDomainType, intensionalSet, ZERO, intensionalSetContext);
Rational argMeasure = get(argDomainIntensionalSet, intensionalSetContext);
domainMeasure = domainMeasure.multiply(argMeasure);
}
evaluatedResult = Expressions.makeSymbol(codomainMeasure.pow(domainMeasure.intValueExact()));
} else if (indexType instanceof TupleType) {
if (!intensionalSetCondition.equals(true)) {
throw new UnsupportedOperationException("Measure of intensional set with a tuple type domain currently do not support conditions: " + intensionalSet);
}
// (element_1, ..., element_n) = measure(element_1) * ... * measure(element_n)
TupleType indexTupleType = (TupleType) indexType;
Rational elementMeasuresProduct = Rational.ONE;
for (Type elementType : indexTupleType.getElementTypes()) {
Expression elementDomainIntensionalSet = constructComponentIntensionalSet(elementType, intensionalSet, ZERO, intensionalSetContext);
Rational elementMeasure = get(elementDomainIntensionalSet, intensionalSetContext);
elementMeasuresProduct = elementMeasuresProduct.multiply(elementMeasure);
}
evaluatedResult = Expressions.makeSymbol(elementMeasuresProduct);
} else {
Expression countingFormula = new DefaultCountingFormula(indexExpressionsSet, intensionalSet.getCondition());
evaluatedResult = context.getTheory().evaluate(countingFormula, context);
}
if (Expressions.isNumber(evaluatedResult)) {
result = evaluatedResult.rationalValue();
} else {
throw new UnsupportedOperationException("Unable to compute a finite measure for: " + intensionalSet + ", got : " + evaluatedResult);
}
} else {
throw new UnsupportedOperationException("Currently only support the measure of single indexed intensional sets: " + intensionalSet);
}
} else {
throw new IllegalArgumentException("Not an intensional set: " + intensionalSetExpression);
}
return result;
}
use of com.sri.ai.expresso.api.IndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Bounds method isExtremePoint.
/**
* Checks if \phi is a convex combination of the elements in bound
* @param phi
* factor
* @param bound
* @return
*/
public static boolean isExtremePoint(Expression phi, int indexPhi, Expression bound, Theory theory, Context context) {
//TODO
//caro pq recopia a lista toda
Expression boundWithoutPhi = removeNonDestructively(bound, indexPhi);
List<Expression> listOfB = getElements(boundWithoutPhi);
int n = listOfB.size();
Expression[] c = new Expression[n];
for (int i = 0; i < n; i++) {
c[i] = makeSymbol("c" + i);
context = context.extendWithSymbolsAndTypes("c" + i, "Real");
}
// 0<=ci<=1
ArrayList<Expression> listOfC = new ArrayList<>(listOfB.size());
for (int i = 0; i < n; i++) {
Expression cibetwen0And1 = apply(AND, apply(GREATER_THAN_OR_EQUAL_TO, 1, c[i]), apply(GREATER_THAN_OR_EQUAL_TO, c[i], 0));
listOfC.add(cibetwen0And1);
}
Expression allcibetwen0And1 = apply(AND, listOfC);
//sum over ci =1
listOfC = new ArrayList<>(Arrays.asList(c));
Expression sumOverCiEqualsOne = apply(EQUAL, 1, apply(PLUS, listOfC));
//sum of ci*phi1 = phi
ArrayList<Expression> prodciphii = new ArrayList<>(listOfB.size());
int i = 0;
for (Expression phii : listOfB) {
prodciphii.add(apply(TIMES, phii, c[i]));
i++;
}
Expression convexSum = apply(EQUAL, phi, apply(PLUS, prodciphii));
ArrayList<Expression> listOfCiInReal = new ArrayList<>(listOfB.size());
for (i = 0; i < n; i++) {
listOfCiInReal.add(apply(IN, c[i], "Real"));
}
IndexExpressionsSet thereExistsCiInReal = new ExtensionalIndexExpressionsSet(listOfCiInReal);
Expression body = apply(AND, allcibetwen0And1, sumOverCiEqualsOne, convexSum);
Expression isExtreme = new DefaultExistentiallyQuantifiedFormula(thereExistsCiInReal, body);
if (debug)
println(isExtreme);
//Expression result = theory.evaluate(isExtreme, context);
return true;
}
Aggregations