Search in sources :

Example 1 with AssociativeCommutativeSemiRing

use of com.sri.ai.grinder.group.AssociativeCommutativeSemiRing 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)

Aggregations

Expression (com.sri.ai.expresso.api.Expression)1 Constraint (com.sri.ai.grinder.api.Constraint)1 AssociativeCommutativeGroup (com.sri.ai.grinder.group.AssociativeCommutativeGroup)1 AssociativeCommutativeSemiRing (com.sri.ai.grinder.group.AssociativeCommutativeSemiRing)1