use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Expressions method makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees.
private static Expression makeDefaultUniversallyQuantifiedFormulaFromLabelAndSubTrees(Object label, Object[] subTreeObjects) {
ArrayList<Expression> subTreeExpressions = Util.mapIntoArrayList(subTreeObjects, Expressions::makeFromObject);
Expression indexExpressionsKleeneList = subTreeExpressions.get(0);
IndexExpressionsSet indexExpressions = new ExtensionalIndexExpressionsSet(ensureListFromKleeneList(indexExpressionsKleeneList));
Expression body = subTreeExpressions.get(1);
Expression result = new DefaultUniversallyQuantifiedFormula(indexExpressions, body);
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class Registry method extendWithSymbolsAndTypes.
/**
* Extends with pairs of symbols and their respective types represented as strings.
* @param symbolsAndTypes
* @return
*/
default Registry extendWithSymbolsAndTypes(Expression... symbolsAndTypes) {
Util.myAssert(symbolsAndTypes.length % 2 == 0, () -> "Need to extend registry with a sequence of symbols and their types");
List<Expression> indexExpressions = getIndexExpressionsFromSymbolsAndTypes(symbolsAndTypes);
Registry result = extendWith(new ExtensionalIndexExpressionsSet(indexExpressions));
return result;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class DefaultExtensionalBound 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, Bound 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));
// (there exists) ci in Real
ArrayList<Expression> listOfCiInReal = new ArrayList<>(listOfB.size());
for (i = 0; i < n; i++) {
listOfCiInReal.add(apply(IN, c[i], "Real"));
context = context.extendWithSymbolsAndTypes(c[i], parse("Real"));
}
IndexExpressionsSet thereExistsCiInReal = new ExtensionalIndexExpressionsSet(listOfCiInReal);
// (for all) variables in their domains
IndexExpressionsSet forAllVariablesEvaluations = getIndexExpressionsOfFreeVariablesIn(bound, context);
Expression body = apply(AND, allcibetwen0And1, sumOverCiEqualsOne, convexSum);
Expression isExtreme = new DefaultExistentiallyQuantifiedFormula(thereExistsCiInReal, body);
isExtreme = new DefaultUniversallyQuantifiedFormula(forAllVariablesEvaluations, isExtreme);
// println(isExtreme);
Expression result = theory.evaluate(isExtreme, context);
return !result.booleanValue();
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class GrinderUtil method extendRegistryWith.
/**
* @param mapFromSymbolNameToTypeName
* @param types
* @param registry
* @return
*/
public static Registry extendRegistryWith(Map<String, String> mapFromSymbolNameToTypeName, Collection<? extends Type> types, Registry registry) {
List<Expression> symbolDeclarations = new ArrayList<>();
for (Map.Entry<String, String> variableNameAndTypeName : mapFromSymbolNameToTypeName.entrySet()) {
String symbolName = variableNameAndTypeName.getKey();
String typeName = variableNameAndTypeName.getValue();
symbolDeclarations.add(parse(symbolName + " in " + typeName));
}
Registry result = registry.extendWith(new ExtensionalIndexExpressionsSet(symbolDeclarations));
registry = result;
for (Type type : types) {
registry = registry.makeNewContextWithAddedType(type);
// System.out.println("Cardinality of type being computed: " + type);
Expression cardinality = type.cardinality();
registry = registry.putGlobalObject(parse("|" + type.getName() + "|"), cardinality);
}
return registry;
}
use of com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet in project aic-expresso by aic-sri-international.
the class GrinderUtil method getIndexExpressionsForIndicesInListAndTypesInRegistry.
/**
* Returns a list of index expressions corresponding to the given indices and their types per the registry, if any.
*/
public static ExtensionalIndexExpressionsSet getIndexExpressionsForIndicesInListAndTypesInRegistry(Collection<? extends Expression> indices, Registry registry) {
List<Expression> indexExpressions = new LinkedList<Expression>();
for (Expression index : indices) {
Expression type = registry.getTypeExpressionOfRegisteredSymbol(index);
Expression indexExpression = IndexExpressions.makeIndexExpression(index, type);
indexExpressions.add(indexExpression);
}
return new ExtensionalIndexExpressionsSet(indexExpressions);
}
Aggregations