Search in sources :

Example 16 with Type

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();
}
Also used : PredicateIterator(com.sri.ai.util.collect.PredicateIterator) PermutationIterator(com.sri.ai.util.collect.PermutationIterator) NestedIterator(com.sri.ai.util.collect.NestedIterator) PairOf(com.sri.ai.util.base.PairOf) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator) PairOf.makePairOf(com.sri.ai.util.base.PairOf.makePairOf) ArrayList(java.util.ArrayList) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) SubsetsOfKIterator(com.sri.ai.util.collect.SubsetsOfKIterator) Util.in(com.sri.ai.util.Util.in) Expressions.zipApply(com.sri.ai.expresso.helper.Expressions.zipApply) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Util.thereExists(com.sri.ai.util.Util.thereExists) Util.arrayList(com.sri.ai.util.Util.arrayList) FunctionIterator(com.sri.ai.util.collect.FunctionIterator) LinkedHashSet(java.util.LinkedHashSet) Equality(com.sri.ai.grinder.sgdpllt.library.Equality) Util.arrayListFrom(com.sri.ai.util.Util.arrayListFrom) Function(com.google.common.base.Function) Type(com.sri.ai.expresso.api.Type) Iterator(java.util.Iterator) DISEQUALITY(com.sri.ai.grinder.sgdpllt.library.FunctorConstants.DISEQUALITY) Util.list(com.sri.ai.util.Util.list) Set(java.util.Set) AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver(com.sri.ai.grinder.sgdpllt.core.solver.AbstractBooleanWithPropagatedLiteralsRequiringPropagatedLiteralsAndCNFToBeSatisfiedStepSolver) Context(com.sri.ai.grinder.sgdpllt.api.Context) Beta(com.google.common.annotations.Beta) Util.toLinkedHashSet(com.sri.ai.util.Util.toLinkedHashSet) List(java.util.List) FunctionIterator.functionIterator(com.sri.ai.util.collect.FunctionIterator.functionIterator) Util(com.sri.ai.util.Util) PairOfElementsInListIterator(com.sri.ai.util.collect.PairOfElementsInListIterator) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) ArrayList(java.util.ArrayList) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) CartesianProductIterator(com.sri.ai.util.collect.CartesianProductIterator)

Example 17 with Type

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;
}
Also used : Type(com.sri.ai.expresso.api.Type) AbstractTheoryWithBinaryAtomsTestingSupport(com.sri.ai.grinder.sgdpllt.theory.base.AbstractTheoryWithBinaryAtomsTestingSupport) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) Context(com.sri.ai.grinder.sgdpllt.api.Context) UnaryMinus(com.sri.ai.grinder.sgdpllt.library.number.UnaryMinus) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) Util.pickUniformly(com.sri.ai.util.Util.pickUniformly) Util.map(com.sri.ai.util.Util.map) List(java.util.List) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) Expressions.apply(com.sri.ai.expresso.helper.Expressions.apply) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Plus(com.sri.ai.grinder.sgdpllt.library.number.Plus) Util(com.sri.ai.util.Util) Type(com.sri.ai.expresso.api.Type) Expression(com.sri.ai.expresso.api.Expression) ArrayList(java.util.ArrayList)

Example 18 with Type

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;
}
Also used : Type(com.sri.ai.expresso.api.Type) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) Expression(com.sri.ai.expresso.api.Expression) IntegerExpressoType(com.sri.ai.expresso.type.IntegerExpressoType) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval)

Example 19 with Type

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);
}
Also used : Type(com.sri.ai.expresso.api.Type) Collection(java.util.Collection) Expressions(com.sri.ai.expresso.helper.Expressions) Set(java.util.Set) Util.check(com.sri.ai.util.Util.check) Random(java.util.Random) Expression(com.sri.ai.expresso.api.Expression) Context(com.sri.ai.grinder.sgdpllt.api.Context) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) LinkedHashMap(java.util.LinkedHashMap) FunctionType(com.sri.ai.expresso.type.FunctionType) Util.map(com.sri.ai.util.Util.map) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) Expressions.primedUntilUnique(com.sri.ai.expresso.helper.Expressions.primedUntilUnique) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) Entry(java.util.Map.Entry) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) Type(com.sri.ai.expresso.api.Type) FunctionType(com.sri.ai.expresso.type.FunctionType) Entry(java.util.Map.Entry) AbstractTheoryTestingSupport(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheoryTestingSupport) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 20 with Type

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);
}
Also used : EqualityTheory(com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory) Type(com.sri.ai.expresso.api.Type) TheoryTestingSupport(com.sri.ai.grinder.sgdpllt.tester.TheoryTestingSupport) DifferenceArithmeticTheory(com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory) IntegerInterval(com.sri.ai.expresso.type.IntegerInterval) PropositionalTheory(com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory) Categorical(com.sri.ai.expresso.type.Categorical) CompoundTheory(com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory) Test(org.junit.Test)

Aggregations

Type (com.sri.ai.expresso.api.Type)123 Expression (com.sri.ai.expresso.api.Expression)93 FunctionType (com.sri.ai.expresso.type.FunctionType)28 Test (org.junit.Test)25 Context (com.sri.ai.grinder.api.Context)24 LinkedHashMap (java.util.LinkedHashMap)22 Categorical (com.sri.ai.expresso.type.Categorical)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)20 RealExpressoType (com.sri.ai.expresso.type.RealExpressoType)19 TupleType (com.sri.ai.expresso.type.TupleType)19 ArrayList (java.util.ArrayList)19 Map (java.util.Map)19 IndexExpressionsSet (com.sri.ai.expresso.api.IndexExpressionsSet)18 IntegerInterval (com.sri.ai.expresso.type.IntegerInterval)18 IntegerExpressoType (com.sri.ai.expresso.type.IntegerExpressoType)15 RealInterval (com.sri.ai.expresso.type.RealInterval)14 Beta (com.google.common.annotations.Beta)13 IntensionalSet (com.sri.ai.expresso.api.IntensionalSet)12 ExtensionalIndexExpressionsSet (com.sri.ai.expresso.core.ExtensionalIndexExpressionsSet)12 List (java.util.List)12