Search in sources :

Example 41 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class RuleGroundingInfoImpl method selectNextGroundingLiteral.

private Literal selectNextGroundingLiteral(LinkedHashSet<Literal> remainingLiterals, Set<VariableTerm> boundVariables) {
    Float bestSelectivity = Float.MAX_VALUE;
    Literal bestLiteral = null;
    boolean bestLiteralSharesVariables = false;
    // To avoid cross products, select those first that have some of their variables already bound.
    for (Literal literal : remainingLiterals) {
        if (!boundVariables.containsAll(literal.getNonBindingVariables())) {
            // Only consider literals whose nonbinding variables are already bound.
            continue;
        }
        Float selectivity = literalSelectivity.get(literal);
        boolean sharesVariables = sharesVariables(boundVariables, literal.getBindingVariables(), literal.getNonBindingVariables());
        if (bestLiteral == null || sharesVariables && selectivity < bestSelectivity || sharesVariables && !bestLiteralSharesVariables) {
            bestLiteral = literal;
            bestSelectivity = selectivity;
            bestLiteralSharesVariables = sharesVariables;
        }
    }
    return bestLiteral;
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal)

Example 42 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class RuleGroundingInfoImpl method computeStartingLiterals.

/**
 * Computes starting literals and indicates whether there is a fixed ground instantiation for this rule.
 *
 * @return true iff the rule has a fixed ground instantiation.
 */
private boolean computeStartingLiterals() {
    LinkedHashSet<Literal> fixedStartingLiterals = new LinkedHashSet<>();
    LinkedHashSet<Literal> ordinaryStartingLiterals = new LinkedHashSet<>();
    // If the rule is ground, every body literal is a starting literal and the ground instantiation is fixed.
    if (internalRule.isGround()) {
        startingLiterals = new LinkedList<>(internalRule.getBody());
        return true;
    }
    // Check each literal in the rule body whether it is eligible.
    for (Literal literal : internalRule.getBody()) {
        // Only literals that need no variables already bound can start grounding.
        if (literal.getNonBindingVariables().size() != 0) {
            continue;
        }
        if (literal.getAtom() instanceof BasicAtom && !literal.isNegated()) {
            // Positive BasicAtom is the main/ordinary case.
            ordinaryStartingLiterals.add(literal);
        } else {
            // If literal is no positive BasicAtom but requires no bound variables,
            // it can be the starting literal for some (fixed) instantiation.
            fixedStartingLiterals.add(literal);
        }
    }
    // instantiation literals and those are starting for the one-time grounding.
    if (!ordinaryStartingLiterals.isEmpty()) {
        startingLiterals = new LinkedList<>(ordinaryStartingLiterals);
        return false;
    } else if (!fixedStartingLiterals.isEmpty()) {
        startingLiterals = new LinkedList<>(fixedStartingLiterals);
        return true;
    } else {
        throw new RuntimeException("Unsafe rule encountered: " + internalRule);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) LinkedList(java.util.LinkedList)

Example 43 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class AnalyzeUnjustified method unjustCover.

private Set<LitSet> unjustCover(List<Literal> vB, Set<Unifier> vY, Set<Unifier> vN, Assignment currentAssignment) {
    padDepth += 2;
    log("Begin UnjustCoverFixed()");
    log("Finding unjustified body literals in: {} / {} excluded {}", vB, vY, vN);
    Set<LitSet> ret = new LinkedHashSet<>();
    if (vB.isEmpty() || vY.isEmpty()) {
        log("End unjustCover().");
        padDepth -= 2;
        return Collections.emptySet();
    }
    int chosenLiteralPos = 0;
    // Find a body literal that is not a ComparisonLiteral, because these do not generate/have atoms assigned.
    for (int i = 0; i < vB.size(); i++) {
        if (!(vB.get(i) instanceof ComparisonLiteral)) {
            // TODO: Should this be FixedInterpretationLiteral instead??
            chosenLiteralPos = i;
            break;
        }
    }
    Atom b = vB.get(chosenLiteralPos).getAtom();
    log("Picked literal from body is: {}", b);
    for (Unifier sigmaY : vY) {
        Atom bSigmaY = b.substitute(sigmaY);
        log("Treating substitution for: {}", bSigmaY);
        Set<Unifier> vYp = new LinkedHashSet<>();
        log("Checking atoms over predicate: {}", b.getPredicate());
        AssignedAtomsIterator assignedAtomsOverPredicate = getAssignedAtomsOverPredicate(b.getPredicate());
        atomLoop: while (assignedAtomsOverPredicate.hasNext()) {
            Atom atom = assignedAtomsOverPredicate.next();
            // Check that atom is justified/true.
            log("Checking atom: {}", atom);
            if (atomStore.contains(atom)) {
                int atomId = atomStore.get(atom);
                if (currentAssignment.getTruth(atomId) != ThriceTruth.TRUE) {
                    log("Atom is not TRUE. Skipping.");
                    continue;
                }
            }
            // Note: in case the atom is not in the atomStore, it came from a fact and hence is true.
            Unifier sigma = Unification.instantiate(b, atom);
            if (sigma == null) {
                log("Atom does not unify with picked body literal.");
                continue;
            }
            Atom bSigma = b.substitute(sigma);
            if (!bSigma.isGround()) {
                throw oops("Resulting atom is not ground.");
            }
            Set<VariableTerm> variablesOccurringInSigma = sigma.getMappedVariables();
            if (Unification.instantiate(bSigmaY, bSigma) != null) {
                for (Unifier sigmaN : vN) {
                    ArrayList<Term> occurringVariables = new ArrayList<>(variablesOccurringInSigma);
                    occurringVariables.addAll(sigmaN.getMappedVariables());
                    BasicAtom genericAtom = Atoms.newBasicAtom(Predicates.getPredicate("_", occurringVariables.size(), true), occurringVariables);
                    Atom genericSubstituted = genericAtom.substitute(sigmaN).renameVariables("_analyzeTest");
                    if (Unification.instantiate(genericSubstituted, genericAtom.substitute(sigma)) != null) {
                        log("Atom {} is excluded by: {} via {}", genericSubstituted, sigmaN, sigma);
                        continue atomLoop;
                    }
                }
                log("Adding corresponding substitution to Y': {}", sigma);
                vYp.add(sigma);
            }
        }
        log("Unjustified body literals: {}", vYp);
        Set<Unifier> vYpUN = new LinkedHashSet<>();
        vYpUN.addAll(vYp);
        vYpUN.addAll(vN);
        LitSet toJustify = new LitSet(bSigmaY, vYpUN);
        if (!toJustify.coversNothing()) {
            log("New litset to do: {}", toJustify);
            ret.add(toJustify);
        } else {
            log("Generated LitSet covers nothing. Ignoring: {}", toJustify);
        }
        ArrayList<Literal> newB = new ArrayList<>(vB);
        newB.remove(chosenLiteralPos);
        ret.addAll(unjustCover(newB, vYp, vN, currentAssignment));
        log("Literal set(s) to treat: {}", ret);
    }
    log("End unjustCover().");
    padDepth -= 2;
    return ret;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

Example 44 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class NoGoodGenerator method collectNegLiterals.

List<Integer> collectNegLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
    final List<Integer> bodyLiteralsNegative = new ArrayList<>();
    for (Literal lit : nonGroundRule.getNegativeBody()) {
        Atom groundAtom = lit.getAtom().substitute(substitution);
        final Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
        if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
            // Negative atom that is always true encountered, skip whole rule as it will never fire.
            return null;
        }
        if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
            // Negative atom is no fact and no rule defines it, it is always false, skip it.
            continue;
        }
        bodyLiteralsNegative.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
    }
    return bodyLiteralsNegative;
}
Also used : Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Literals.negateLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) ArrayList(java.util.ArrayList) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Example 45 with Literal

use of at.ac.tuwien.kr.alpha.api.programs.literals.Literal in project Alpha by alpha-asp.

the class NoGoodGenerator method collectPosLiterals.

private List<Integer> collectPosLiterals(final CompiledRule nonGroundRule, final Substitution substitution) {
    final List<Integer> bodyLiteralsPositive = new ArrayList<>();
    for (Literal lit : nonGroundRule.getPositiveBody()) {
        if (lit instanceof FixedInterpretationLiteral) {
            // FixedInterpretationAtoms need not be shown to the solver, skip it.
            continue;
        }
        final Atom atom = lit.getAtom();
        // Skip the special enumeration atom.
        if (atom instanceof EnumerationAtom) {
            continue;
        }
        final Atom groundAtom = atom.substitute(substitution);
        // Consider facts to eliminate ground atoms from the generated nogoods that are always true
        // and eliminate nogoods that are always satisfied due to facts.
        Set<Instance> factInstances = factsFromProgram.get(groundAtom.getPredicate());
        if (factInstances != null && factInstances.contains(new Instance(groundAtom.getTerms()))) {
            // Skip positive atoms that are always true.
            continue;
        }
        if (!existsRuleWithPredicateInHead(groundAtom.getPredicate())) {
            // Atom is no fact and no rule defines it, it cannot be derived (i.e., is always false), skip whole rule as it will never fire.
            return null;
        }
        bodyLiteralsPositive.add(atomToLiteral(atomStore.putIfAbsent(groundAtom)));
    }
    return bodyLiteralsPositive;
}
Also used : FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) FixedInterpretationLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.FixedInterpretationLiteral) Literals.negateLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.negateLiteral) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) ArrayList(java.util.ArrayList) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) EnumerationAtom(at.ac.tuwien.kr.alpha.core.atoms.EnumerationAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Aggregations

Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)82 Test (org.junit.jupiter.api.Test)42 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)20 ArrayList (java.util.ArrayList)20 AggregateLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral)17 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)16 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)14 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)14 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)13 Map (java.util.Map)12 Head (at.ac.tuwien.kr.alpha.api.rules.heads.Head)11 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)11 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)10 HashSet (java.util.HashSet)10 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)9 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)9 LinkedHashSet (java.util.LinkedHashSet)8 List (java.util.List)8 Term (at.ac.tuwien.kr.alpha.api.terms.Term)7