Search in sources :

Example 1 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class SummationOnDifferenceArithmeticAndPolynomialStepSolver method computeSummationGivenValues.

private Expression computeSummationGivenValues(Expression variable, Expression literalFreeBody, RangeAndExceptionsSet values, Context context) {
    Expression result;
    if (values.equals(RangeAndExceptionsSet.EMPTY)) {
        result = ZERO;
    } else {
        Theory theory = context.getTheory();
        if (values instanceof RangeAndExceptionsSet.Singleton) {
            Expression value = ((RangeAndExceptionsSet.Singleton) values).getSingleValue();
            Expression valueAtPoint = DefaultPolynomial.make(getValueAtGivenPoint(literalFreeBody, variable, value, theory, context));
            result = valueAtPoint;
        } else {
            Expression interval;
            List<Expression> disequals;
            if (values.hasFunctor(MINUS)) {
                interval = values.get(0);
                disequals = values.get(1).getArguments();
            } else {
                interval = values;
                disequals = list();
            }
            Expression strictLowerBound = interval.get(0);
            Expression nonStrictUpperBound = interval.get(1);
            Polynomial bodyPolynomial = DefaultPolynomial.make(literalFreeBody);
            Expression intervalSummation = PolynomialSummation.sum(variable, strictLowerBound, nonStrictUpperBound, bodyPolynomial);
            ArrayList<Expression> argumentsForSubtraction = new ArrayList<>(1 + disequals.size());
            argumentsForSubtraction.add(intervalSummation);
            for (Expression disequal : disequals) {
                Expression valueAtDisequal = getValueAtGivenPoint(literalFreeBody, variable, disequal, theory, context);
                argumentsForSubtraction.add(apply(MINUS, valueAtDisequal));
            }
            Expression intervalSummationMinusValuesAtDisequals = apply(PLUS, argumentsForSubtraction);
            result = DefaultPolynomial.make(theory.simplify(intervalSummationMinusValuesAtDisequals, context));
        }
    }
    return result;
}
Also used : Polynomial(com.sri.ai.grinder.polynomial.api.Polynomial) Polynomial.makeRandomPolynomial(com.sri.ai.grinder.polynomial.api.Polynomial.makeRandomPolynomial) DefaultPolynomial(com.sri.ai.grinder.polynomial.core.DefaultPolynomial) Expression(com.sri.ai.expresso.api.Expression) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) ArrayList(java.util.ArrayList)

Example 2 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheory method getSingleVariableConstraintSatisfiabilityStepSolver.

@Override
public ExpressionLiteralSplitterStepSolver getSingleVariableConstraintSatisfiabilityStepSolver(SingleVariableConstraint constraint, Context context) {
    Theory theory = getTheory(constraint.getVariable(), context);
    ExpressionLiteralSplitterStepSolver result;
    if (theory != null) {
        result = theory.getSingleVariableConstraintSatisfiabilityStepSolver(constraint, context);
    } else {
        result = null;
    }
    return result;
}
Also used : Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheory(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory) ExpressionLiteralSplitterStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionLiteralSplitterStepSolver)

Example 3 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class CompoundTheory method makeSingleVariableConstraint.

@Override
public SingleVariableConstraint makeSingleVariableConstraint(Expression variable, Theory theory, Context context) {
    Theory theoryForVariable = getTheory(variable, context);
    SingleVariableConstraint result;
    if (theoryForVariable != null) {
        result = theoryForVariable.makeSingleVariableConstraint(variable, theory, context);
    } else {
        result = null;
    }
    return result;
}
Also used : SingleVariableConstraint(com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) AbstractTheory(com.sri.ai.grinder.sgdpllt.core.constraint.AbstractTheory)

Example 4 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class AbstractModelCountingWithPropagatedLiteralsImportedFromSatisfiabilityStepSolver method getPropagatedCNF.

/**
	 * This is overridden to re-use satisfiability's propagated CNF.
	 * It overrides caching done by overridden method, but that's fine since satisfiability should be doing that already.
	 */
@Override
protected ArrayList<ArrayList<Expression>> getPropagatedCNF(Context context) {
    Theory theory = getConstraint().getTheory();
    ExpressionStepSolver satisfiability = theory.getSingleVariableConstraintSatisfiabilityStepSolver(getConstraint(), context);
    if (satisfiability == null) {
        throw new Error("No solver present for solving satisfiability of " + getConstraint().getVariable());
    }
    AbstractExpressionWithPropagatedLiteralsStepSolver satisfiabilityWithPropagatedLiterals;
    try {
        satisfiabilityWithPropagatedLiterals = (AbstractExpressionWithPropagatedLiteralsStepSolver) satisfiability;
    } catch (ClassCastException e) {
        throw new Error(this.getClass() + " can only be used with theories providing satisfiability context-dependent step solvers" + " that are extensions of " + AbstractExpressionWithPropagatedLiteralsStepSolver.class + ", but theory " + theory.getClass() + " provided instead an instance of" + satisfiability.getClass());
    }
    return satisfiabilityWithPropagatedLiterals.getPropagatedCNF(context);
}
Also used : Theory(com.sri.ai.grinder.sgdpllt.api.Theory) ExpressionStepSolver(com.sri.ai.grinder.sgdpllt.api.ExpressionStepSolver)

Example 5 with Theory

use of com.sri.ai.grinder.sgdpllt.api.Theory in project aic-expresso by aic-sri-international.

the class Compilation method compile.

/**
	 * Compiles an expression to a normalized (decision-tree-like) expression.
	 * @param inputExpression
	 * @param mapFromVariableNameToTypeName
	 * @param mapFromCategoricalTypeNameToSizeString
	 * @param additionalTypes
	 * @param solverListener if not null, invoked on solver used for compilation, before and after compilation starts; returned solver on 'before' invocation is used (it may be the same one used as argument, of course).
	 * @return
	 */
public static Expression compile(Expression inputExpression, Theory theory, Map<String, String> mapFromVariableNameToTypeName, Map<String, String> mapFromUniquelyNamedConstantToTypeName, Map<String, String> mapFromCategoricalTypeNameToSizeString, Collection<Type> additionalTypes, Function<MultiIndexQuantifierEliminator, MultiIndexQuantifierEliminator> solverListener) {
    // the group actually does not matter, because we are not going to have any indices.
    AssociativeCommutativeGroup group = new Max();
    // The solver for the parameters above.
    MultiIndexQuantifierEliminator solver = new SGDPLLT();
    if (solverListener != null) {
        solver = solverListener.apply(solver);
    }
    // We use the Prolog convention of small-letter initials for constants, but we need an exception for the random variables.
    Predicate<Expression> isPrologConstant = new PrologConstantPredicate();
    Predicate<Expression> isUniquelyNamedConstantPredicate = e -> isPrologConstant.apply(e) && !mapFromVariableNameToTypeName.containsKey(e);
    Map<String, String> mapFromSymbolNameToTypeName = new LinkedHashMap<>(mapFromVariableNameToTypeName);
    mapFromSymbolNameToTypeName.putAll(mapFromUniquelyNamedConstantToTypeName);
    // Solve the problem.
    // no indices; we want to keep all variables
    List<Expression> indices = Util.list();
    Expression result = solver.solve(group, inputExpression, indices, mapFromSymbolNameToTypeName, mapFromCategoricalTypeNameToSizeString, additionalTypes, isUniquelyNamedConstantPredicate, theory);
    if (solverListener != null) {
        solverListener.apply(null);
    }
    return result;
}
Also used : Type(com.sri.ai.expresso.api.Type) MultiIndexQuantifierEliminator(com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator) SGDPLLT(com.sri.ai.grinder.sgdpllt.core.solver.SGDPLLT) Collection(java.util.Collection) Expression(com.sri.ai.expresso.api.Expression) Function(java.util.function.Function) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Predicate(com.google.common.base.Predicate) Max(com.sri.ai.grinder.sgdpllt.group.Max) Map(java.util.Map) Util(com.sri.ai.util.Util) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) MultiIndexQuantifierEliminator(com.sri.ai.grinder.sgdpllt.api.MultiIndexQuantifierEliminator) Max(com.sri.ai.grinder.sgdpllt.group.Max) SGDPLLT(com.sri.ai.grinder.sgdpllt.core.solver.SGDPLLT) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) Expression(com.sri.ai.expresso.api.Expression) AssociativeCommutativeGroup(com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Theory (com.sri.ai.grinder.sgdpllt.api.Theory)29 Expression (com.sri.ai.expresso.api.Expression)21 Context (com.sri.ai.grinder.sgdpllt.api.Context)13 Type (com.sri.ai.expresso.api.Type)10 Map (java.util.Map)9 Beta (com.google.common.annotations.Beta)8 SingleVariableConstraint (com.sri.ai.grinder.sgdpllt.api.SingleVariableConstraint)8 CompoundTheory (com.sri.ai.grinder.sgdpllt.theory.compound.CompoundTheory)8 DifferenceArithmeticTheory (com.sri.ai.grinder.sgdpllt.theory.differencearithmetic.DifferenceArithmeticTheory)8 EqualityTheory (com.sri.ai.grinder.sgdpllt.theory.equality.EqualityTheory)8 PropositionalTheory (com.sri.ai.grinder.sgdpllt.theory.propositional.PropositionalTheory)8 Collection (java.util.Collection)8 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)7 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)7 AssociativeCommutativeGroup (com.sri.ai.grinder.sgdpllt.group.AssociativeCommutativeGroup)7 And (com.sri.ai.grinder.sgdpllt.library.boole.And)7 Util (com.sri.ai.util.Util)7 Util.join (com.sri.ai.util.Util.join)7 Util.list (com.sri.ai.util.Util.list)7 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)6