use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class SatisfiabilityOfSingleVariableEqualityConstraintStepSolver method getPropagatedCNFBesidesPropagatedLiterals.
@Override
protected Iterable<Iterable<Expression>> getPropagatedCNFBesidesPropagatedLiterals(Context context) {
if (!variableIsBoundToUniquelyNamedConstant(context)) {
// the following logic only holds if the variable is not bound to a uniquely named constants,
// since that eliminates all disequalities to other uniquely named constants as redundant
long variableDomainSize = getConstraint().getVariableTypeSize(context);
if (variableDomainSize >= 0 && getConstraint().numberOfDisequals() >= variableDomainSize) {
// the following procedure can be very expensive but the condition above will rarely be satisfied
ArrayList<Expression> variableDisequals = getVariableDisequals(context);
Set<Expression> uniquelyNamedConstantDisequals = getUniquelyNamedConstantDisequals(context);
Expression typeExpression = GrinderUtil.getTypeExpression(getConstraint().getVariable(), context);
Type type = context.getType(typeExpression);
ArrayList<Expression> remainingUniquelyNamedConstants = arrayListFrom(new PredicateIterator<>(type.iterator(), c -> !uniquelyNamedConstantDisequals.contains(c)));
CartesianProductIterator<ArrayList<Expression>> subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator = new CartesianProductIterator<ArrayList<Expression>>(() -> new SubsetsOfKIterator<Expression>(variableDisequals, remainingUniquelyNamedConstants.size()), () -> new PermutationIterator<Expression>(remainingUniquelyNamedConstants));
FunctionIterator<ArrayList<ArrayList<Expression>>, Iterable<Expression>> clausesIterator = FunctionIterator.make(subsetOfVariableDisequalsAndRemainingConstantsPermutationIterator, (ArrayList<ArrayList<Expression>> subsetAndPermutation) -> clauseNegatingAssignmentOfSubsetOfVariablesToParticularPermutationOfRemainingConstants(subsetAndPermutation));
Iterable<Iterable<Expression>> clauses = in(clausesIterator);
return clauses;
}
}
// otherwise, nothing is implied.
return list();
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class DifferenceArithmeticTheoryTestingSupport method makeRandomAtomOn.
/**
* Makes a random atom on variable by summing or subtracting terms from two random atoms generated by super class implementation.
*/
@Override
public Expression makeRandomAtomOn(String mainVariable, Context context) {
String mainVariableName = getVariableName(mainVariable);
Type mainType = getTestingVariableType(mainVariable);
List<String> variableNamesThatAreSubtypesOf = getVariableNamesWhoseTypesAreSubtypesOf(mainType);
int maxNumberOfOtherVariablesInAtom = Math.min(variableNamesThatAreSubtypesOf.size(), 2);
// used to be 3, but if literal has more than two variables, it steps out of difference arithmetic and may lead
// to multiplied variables when literals are propagated.
// For example, X = Y + Z and X = -Y - Z + 3 imply 2Y + 2Z = 3
int numberOfOtherVariablesInAtom = getRandom().nextInt(maxNumberOfOtherVariablesInAtom);
// Note: that otherVariablesForAtom will contain only one or zero elements
ArrayList<String> otherVariablesForAtom = new ArrayList<>();
if (numberOfOtherVariablesInAtom > 0) {
otherVariablesForAtom.add(pickTestingVariableAtRandom(mainType, otherName -> !otherName.equals(mainVariableName)));
}
ArrayList<Expression> constants = new ArrayList<Expression>();
int numberOfConstants = getRandom().nextInt(3);
for (int i = 0; i != numberOfConstants; i++) {
// Note: We know we can safely sample from the Difference Arithmetic Theory Types.
Expression sampledConstant = mainType.sampleUniquelyNamedConstant(getRandom());
Expression constant;
if (getRandom().nextBoolean()) {
constant = sampledConstant;
} else {
constant = makeSymbol(-sampledConstant.intValue());
}
constants.add(constant);
}
ArrayList<Expression> leftHandSideArguments = new ArrayList<Expression>();
leftHandSideArguments.add(parse(mainVariable));
// needs to be difference, so it's added as negative
Util.mapIntoList(otherVariablesForAtom, otherVariable -> UnaryMinus.make(parse(otherVariable)), leftHandSideArguments);
leftHandSideArguments.addAll(constants);
int numberOfOtherVariablesToBeCanceled = getRandom().nextInt(otherVariablesForAtom.size() + 1);
ArrayList<String> otherVariablesToBeCanceled = Util.pickKElementsWithoutReplacement(otherVariablesForAtom, numberOfOtherVariablesToBeCanceled, getRandom());
// note that this term is positive, so it will cancel the previously negative term with the same "other variable"
Util.mapIntoList(otherVariablesToBeCanceled, v -> parse(v), leftHandSideArguments);
// Note: it may seem odd to generate an "other variable" and add another term that will cancel it later.
// However, this is useful for making sure canceling works properly.
Expression leftHandSide = Plus.make(leftHandSideArguments);
String functor = pickUniformly(getTheoryFunctors(), getRandom());
Expression unsimplifiedResult = apply(functor, leftHandSide, 0);
Expression result = getTheory().simplify(unsimplifiedResult, context);
return result;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class SingleVariableDifferenceArithmeticConstraint method getType.
/**
* Returns the {@link IntegerInterval} type of the constraint's variable.
* @param context
* @return
*/
public IntegerInterval getType(Context context) {
if (cachedType == null) {
Expression variableTypeExpression = getVariableTypeExpression(context);
Type type = context.getType(variableTypeExpression);
if (type instanceof IntegerExpressoType) {
cachedType = new IntegerInterval("-infinity..infinity");
// represents Integer as integer interval for uniformity
} else {
cachedType = (IntegerInterval) type;
}
}
return cachedType;
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class CompoundTheoryTestingSupport method aggregateTestingInformation.
//
//
private void aggregateTestingInformation() throws Error {
Map<String, Type> variableNamesAndTypesForTesting = new LinkedHashMap<>();
for (TheoryTestingSupport theoryTestingSupport : getSubConstraintTheoryTestingSupports()) {
Set<Entry<String, Type>> variableNamesAndTypeNameEntries = theoryTestingSupport.getVariableNamesAndTypesForTesting().entrySet();
for (Map.Entry<String, Type> variableNameAndTypeName : variableNamesAndTypeNameEntries) {
String variableName = variableNameAndTypeName.getKey();
Type type = variableNameAndTypeName.getValue();
if (variableNamesAndTypesForTesting.containsKey(variableName)) {
variableName = primedUntilUnique(variableName, s -> variableNamesAndTypesForTesting.containsKey(s.toString()));
}
variableNamesAndTypesForTesting.put(variableName, type);
}
}
setVariableNamesAndTypesForTesting(variableNamesAndTypesForTesting);
}
use of com.sri.ai.expresso.api.Type in project aic-expresso by aic-sri-international.
the class ExpressionStepSolverToLiteralSplitterStepSolverAdapterTest method testCompoundTheoryWithDifferenceArithmeticWithRandomDisjunctiveFormulas.
@Test
public void testCompoundTheoryWithDifferenceArithmeticWithRandomDisjunctiveFormulas() {
TheoryTestingSupport theoryTestingSupport = 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);
theoryTestingSupport.setVariableNamesAndTypesForTesting(variablesAndTypes);
runRandomDisjunctiveFormulasTest(theoryTestingSupport);
}
Aggregations