Search in sources :

Example 1 with AssociativeCommutativeGroup

use of com.sri.ai.grinder.group.AssociativeCommutativeGroup in project aic-expresso by aic-sri-international.

the class SGDPLLTTester method runGroupProblemSolvingTesterForSuccessiveConstraints.

private static void runGroupProblemSolvingTesterForSuccessiveConstraints(String problemName, TestRunner tester, boolean testAgainstBruteForce, AssociativeCommutativeGroup group, TheoryTestingSupport theoryTestingSupport, long numberOfTests, int maxNumberOfLiterals, boolean outputCount) throws Error {
    Context context = theoryTestingSupport.makeContextWithTestingInformation();
    NullaryFunction<Constraint> makeInitialConstraint = () -> theoryTestingSupport.getTheory().makeSingleVariableConstraint(parse(theoryTestingSupport.pickTestingVariableAtRandom()), context);
    Function<Constraint, Expression> makeRandomLiteral = c -> theoryTestingSupport.makeRandomLiteralOn(((SingleVariableConstraint) c).getVariable().toString(), context);
    runTesterGivenOnSuccessiveConjunctionsOfLiterals(problemName, tester, numberOfTests, maxNumberOfLiterals, testAgainstBruteForce, theoryTestingSupport, makeInitialConstraint, makeRandomLiteral, outputCount, context);
}
Also used : Context(com.sri.ai.grinder.api.Context) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) BinaryFunction(com.sri.ai.util.base.BinaryFunction) Util.removeFromSetNonDestructively(com.sri.ai.util.Util.removeFromSetNonDestructively) SubExpressionsDepthFirstIterator(com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator) Expression(com.sri.ai.expresso.api.Expression) Sets(com.sri.ai.grinder.library.set.Sets) Function(java.util.function.Function) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) And(com.sri.ai.grinder.library.boole.And) Util.in(com.sri.ai.util.Util.in) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) BruteForceCommonInterpreter(com.sri.ai.grinder.interpreter.BruteForceCommonInterpreter) IndexExpressions(com.sri.ai.grinder.library.indexexpression.IndexExpressions) Expressions.parse(com.sri.ai.expresso.helper.Expressions.parse) Map(java.util.Map) ThereExists(com.sri.ai.grinder.library.boole.ThereExists) Context(com.sri.ai.grinder.api.Context) ZERO(com.sri.ai.expresso.helper.Expressions.ZERO) SingleQuantifierEliminationProblem(com.sri.ai.grinder.api.SingleQuantifierEliminationProblem) LinkedHashSet(java.util.LinkedHashSet) Type(com.sri.ai.expresso.api.Type) DefaultSingleQuantifierEliminationProblem(com.sri.ai.grinder.core.solver.DefaultSingleQuantifierEliminationProblem) NullaryFunction(com.sri.ai.util.base.NullaryFunction) Util.join(com.sri.ai.util.Util.join) Expressions.getVariablesBeingReferenced(com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced) Collection(java.util.Collection) Util.list(com.sri.ai.util.Util.list) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) Set(java.util.Set) CompleteMultiVariableContext(com.sri.ai.grinder.core.constraint.CompleteMultiVariableContext) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression) AssignmentMapsIterator(com.sri.ai.grinder.helper.AssignmentMapsIterator) Assignment(com.sri.ai.grinder.interpreter.Assignment) Beta(com.google.common.annotations.Beta) Theory(com.sri.ai.grinder.api.Theory) Expressions.makeSymbol(com.sri.ai.expresso.helper.Expressions.makeSymbol) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Simplifier(com.sri.ai.grinder.rewriter.api.Simplifier) Util(com.sri.ai.util.Util) Util.pickKElementsWithoutReplacement(com.sri.ai.util.Util.pickKElementsWithoutReplacement) TRUE(com.sri.ai.expresso.helper.Expressions.TRUE) Constraint(com.sri.ai.grinder.api.Constraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) DefaultMultiVariableConstraint(com.sri.ai.grinder.core.constraint.DefaultMultiVariableConstraint) SingleVariableConstraint(com.sri.ai.grinder.api.SingleVariableConstraint) Constraint(com.sri.ai.grinder.api.Constraint) Expression(com.sri.ai.expresso.api.Expression) QuantifiedExpression(com.sri.ai.expresso.api.QuantifiedExpression)

Example 2 with AssociativeCommutativeGroup

use of com.sri.ai.grinder.group.AssociativeCommutativeGroup in project aic-expresso by aic-sri-international.

the class AbstractSGVET method solve.

@Override
public Expression solve(MultiQuantifierEliminationProblem problem, Context context) {
    AssociativeCommutativeGroup group = problem.getGroup();
    List<Expression> indices = problem.getIndices();
    Expression indicesConstraint = problem.getConstraint();
    Expression body = problem.getBody();
    checkInterrupted();
    // Make sure body is simplified and quantifier-free.
    Expression simplifiedBody = context.getTheory().evaluate(body, context);
    Expression result;
    if (getDebug()) {
        System.out.println("SGVE(T) input: " + simplifiedBody);
        System.out.println("Width        : " + width(simplifiedBody, context));
    }
    AssociativeCommutativeSemiRing semiRing = (AssociativeCommutativeSemiRing) group;
    Partition partition;
    if (indices.size() < 1) {
        partition = null;
    } else {
        Expression factoredConditionalsExpression = factoredConditionalsWithAbsorbingElseClause(semiRing, simplifiedBody, context);
        partition = pickPartition(semiRing, factoredConditionalsExpression, indices, context);
    }
    if (partition == null) {
        if (basicOutput) {
            System.out.println("No partition");
        }
        result = subSolver.solve(group, indices, indicesConstraint, simplifiedBody, context);
    } else {
        Expression indexSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.first, context);
        if (basicOutput) {
            System.out.println("Eliminating: " + getFirst(partition.index));
            System.out.println("From       : " + indexSubProblemExpression);
            System.out.println("Width      : " + width(indexSubProblemExpression, context) + " out of " + indices.size() + " indices");
        }
        // We now invoke the subsolver for summing the index out of the factors it is in.
        // Ideally, we would reuse the current constraint, but the set of index has changed and the current constraint may
        // use an internal representation that depends on its previous set of indices.
        // In the future, we should try to re-use that internal representation and re-index it appropriately, but for now
        // we rewrite the program in a way that the current constraint becomes a part of the input expression.
        // This will be equivalent to using it as a constraint, but will cause the constraint to be re-built.
        // BTW, the call to "project" below will also re-context the constraint for the same reason: re-indexing.
        // In the future it should also re-use the representation.
        // The following transformation is:  sum_C E   =   sum_{true} if C then E else 0
        Expression indexSubProblemExpressionWithConstraint = IfThenElse.make(indicesConstraint, indexSubProblemExpression, semiRing.multiplicativeAbsorbingElement());
        Expression indexSubProblemSolution = subSolver.extendContextAndSolve(group, partition.index, indexSubProblemExpressionWithConstraint, context);
        if (basicOutput) {
            System.out.println("Solution   : " + indexSubProblemSolution + "\n");
        }
        partition.expressionsOnIndexAndNot.second.add(indexSubProblemSolution);
        Expression remainingSubProblemExpression = product(semiRing, partition.expressionsOnIndexAndNot.second, context);
        // the constraint is already represented in indexSubProblemSolution
        Constraint constraintOnRemainingIndices = context;
        result = solve(group, partition.remainingIndices, constraintOnRemainingIndices, remainingSubProblemExpression, context);
        result = semiRing.multiply(result, context);
    }
    return result;
}
Also used : AssociativeCommutativeSemiRing(com.sri.ai.grinder.group.AssociativeCommutativeSemiRing) Expression(com.sri.ai.expresso.api.Expression) Constraint(com.sri.ai.grinder.api.Constraint) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup)

Example 3 with AssociativeCommutativeGroup

use of com.sri.ai.grinder.group.AssociativeCommutativeGroup 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<MultiQuantifierEliminator, MultiQuantifierEliminator> 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.
    MultiQuantifierEliminator solver = new DefaultMultiQuantifierEliminator();
    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) Collection(java.util.Collection) Expression(com.sri.ai.expresso.api.Expression) DefaultMultiQuantifierEliminator(com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminator) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Theory(com.sri.ai.grinder.api.Theory) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) Predicate(com.google.common.base.Predicate) Map(java.util.Map) Util(com.sri.ai.util.Util) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) Max(com.sri.ai.grinder.group.Max) MultiQuantifierEliminator(com.sri.ai.grinder.api.MultiQuantifierEliminator) Max(com.sri.ai.grinder.group.Max) Expression(com.sri.ai.expresso.api.Expression) PrologConstantPredicate(com.sri.ai.grinder.core.PrologConstantPredicate) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup) DefaultMultiQuantifierEliminator(com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminator) MultiQuantifierEliminator(com.sri.ai.grinder.api.MultiQuantifierEliminator) DefaultMultiQuantifierEliminator(com.sri.ai.grinder.core.solver.DefaultMultiQuantifierEliminator) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with AssociativeCommutativeGroup

use of com.sri.ai.grinder.group.AssociativeCommutativeGroup in project aic-expresso by aic-sri-international.

the class MultiQuantifierEliminator method solveSingleIndexQuantifierEliminationProblem.

// Convenience:
public default Expression solveSingleIndexQuantifierEliminationProblem(SingleQuantifierEliminationProblem problem, Context context) {
    AssociativeCommutativeGroup group = problem.getGroup();
    LinkedList<Expression> indices = list(problem.getIndex());
    Expression indicesCondition = problem.getConstraint();
    Expression body = problem.getBody();
    Expression result = solve(group, indices, indicesCondition, body, context);
    return result;
}
Also used : Expression(com.sri.ai.expresso.api.Expression) AssociativeCommutativeGroup(com.sri.ai.grinder.group.AssociativeCommutativeGroup)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)4 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)4 Type (com.sri.ai.expresso.api.Type)2 Constraint (com.sri.ai.grinder.api.Constraint)2 Theory (com.sri.ai.grinder.api.Theory)2 Util (com.sri.ai.util.Util)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Beta (com.google.common.annotations.Beta)1 Predicate (com.google.common.base.Predicate)1 QuantifiedExpression (com.sri.ai.expresso.api.QuantifiedExpression)1 TRUE (com.sri.ai.expresso.helper.Expressions.TRUE)1 ZERO (com.sri.ai.expresso.helper.Expressions.ZERO)1 Expressions.getVariablesBeingReferenced (com.sri.ai.expresso.helper.Expressions.getVariablesBeingReferenced)1 Expressions.makeSymbol (com.sri.ai.expresso.helper.Expressions.makeSymbol)1 Expressions.parse (com.sri.ai.expresso.helper.Expressions.parse)1 SubExpressionsDepthFirstIterator (com.sri.ai.expresso.helper.SubExpressionsDepthFirstIterator)1 Context (com.sri.ai.grinder.api.Context)1 MultiQuantifierEliminator (com.sri.ai.grinder.api.MultiQuantifierEliminator)1