Search in sources :

Example 26 with Literal

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

the class LiteralBindingNonBindingVariablesTest method testPositiveExternalLiteral.

@Test
public void testPositiveExternalLiteral() {
    externals.put("ext", new IntPredicateInterpretation(i -> i > 0));
    Rule<Head> rule = parser.parse("p(X) :- q(Y), &ext[Y](X).", externals).getRules().get(0);
    Literal literal = rule.getBody().stream().filter((lit) -> lit.getPredicate().getName().equals("ext")).findFirst().get();
    assertEquals(false, literal.isNegated());
    expectVariables(literal.getBindingVariables(), "X");
    expectVariables(literal.getNonBindingVariables(), "Y");
}
Also used : Arrays(java.util.Arrays) Rule(at.ac.tuwien.kr.alpha.api.rules.Rule) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) Collection(java.util.Collection) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Set(java.util.Set) HashMap(java.util.HashMap) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Disabled(org.junit.jupiter.api.Disabled) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) IntPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation) PredicateInterpretation(at.ac.tuwien.kr.alpha.api.common.fixedinterpretations.PredicateInterpretation) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Map(java.util.Map) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ComparisonOperators(at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators) Head(at.ac.tuwien.kr.alpha.api.rules.heads.Head) IntPredicateInterpretation(at.ac.tuwien.kr.alpha.core.common.fixedinterpretations.IntPredicateInterpretation) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Test(org.junit.jupiter.api.Test)

Example 27 with Literal

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

the class DefaultSolver method justifyMbtAndBacktrack.

private boolean justifyMbtAndBacktrack() {
    mbtAtFixpoint++;
    // Run justification only if enabled and possible.
    if (disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
        if (!backtrack()) {
            logStats();
            return false;
        }
        return true;
    }
    ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
    // Justify one MBT assigned atom.
    int atomToJustify = assignment.getBasicAtomAssignedMBT();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Searching for justification of {} / {}", atomToJustify, atomStore.atomToString(atomToJustify));
        LOGGER.debug("Assignment is (TRUE part only): {}", translate(assignment.getTrueAssignments()));
    }
    Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomToJustify, assignment);
    NoGood noGood = noGoodFromJustificationReasons(atomToJustify, reasonsForUnjustified);
    int noGoodID = grounder.register(noGood);
    Map<Integer, NoGood> obtained = new LinkedHashMap<>();
    obtained.put(noGoodID, noGood);
    LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
    // Add NoGood and trigger backjumping.
    if (!ingest(obtained)) {
        logStats();
        return false;
    }
    return true;
}
Also used : ProgramAnalyzingGrounder(at.ac.tuwien.kr.alpha.core.grounder.ProgramAnalyzingGrounder) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToNegatedLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) LinkedHashMap(java.util.LinkedHashMap)

Example 28 with Literal

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

the class DefaultSolver method treatConflictAfterClosing.

private boolean treatConflictAfterClosing(Antecedent violatedNoGood) {
    if (disableJustificationAfterClosing || disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
        // Will not learn from violated NoGood, do simple backtrack.
        LOGGER.debug("NoGood was violated after all unassigned atoms were assigned to false; will not learn from it; skipping.");
        if (!backtrack()) {
            logStats();
            return false;
        }
        return true;
    }
    ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
    LOGGER.debug("Justifying atoms in violated nogood.");
    LinkedHashSet<Integer> toJustify = new LinkedHashSet<>();
    // Find those literals in violatedNoGood that were just assigned false.
    for (Integer literal : violatedNoGood.getReasonLiterals()) {
        if (assignment.getImpliedBy(atomOf(literal)) == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
            toJustify.add(literal);
        }
    }
    // Since the violatedNoGood may contain atoms other than BasicAtom, these have to be treated.
    Map<Integer, NoGood> obtained = new LinkedHashMap<>();
    Iterator<Integer> toJustifyIterator = toJustify.iterator();
    ArrayList<Integer> ruleAtomReplacements = new ArrayList<>();
    while (toJustifyIterator.hasNext()) {
        Integer literal = toJustifyIterator.next();
        Atom atom = atomStore.get(atomOf(literal));
        if (atom instanceof BasicAtom) {
            continue;
        }
        if (!(atom instanceof RuleAtom)) {
            // Ignore atoms other than RuleAtom.
            toJustifyIterator.remove();
            continue;
        }
        // For RuleAtoms in toJustify the corresponding ground body contains BasicAtoms that have been assigned FALSE in the closing.
        // First, translate RuleAtom back to NonGroundRule + Substitution.
        String ruleId = (String) ((ConstantTerm<?>) atom.getTerms().get(0)).getObject();
        CompiledRule nonGroundRule = analyzingGrounder.getNonGroundRule(Integer.parseInt(ruleId));
        String substitution = (String) ((ConstantTerm<?>) atom.getTerms().get(1)).getObject();
        Substitution groundingSubstitution = Substitutions.fromString(substitution);
        // Find ground literals in the body that have been assigned false and justify those.
        for (Literal bodyLiteral : nonGroundRule.getBody()) {
            Atom groundAtom = bodyLiteral.getAtom().substitute(groundingSubstitution);
            if (groundAtom instanceof ComparisonAtom || analyzingGrounder.isFact(groundAtom)) {
                // Facts and ComparisonAtoms are always true, no justification needed.
                continue;
            }
            int groundAtomId = atomStore.get(groundAtom);
            Antecedent impliedBy = assignment.getImpliedBy(groundAtomId);
            // Check if atom was assigned to FALSE during the closing.
            if (impliedBy == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
                ruleAtomReplacements.add(atomToNegatedLiteral(groundAtomId));
            }
        }
        toJustifyIterator.remove();
    }
    toJustify.addAll(ruleAtomReplacements);
    for (Integer literalToJustify : toJustify) {
        LOGGER.debug("Searching for justification(s) of {} / {}", toJustify, atomStore.atomToString(atomOf(literalToJustify)));
        Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomOf(literalToJustify), assignment);
        NoGood noGood = noGoodFromJustificationReasons(atomOf(literalToJustify), reasonsForUnjustified);
        int noGoodID = grounder.register(noGood);
        obtained.put(noGoodID, noGood);
        LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
    }
    // Backtrack to remove the violation.
    if (!backtrack()) {
        logStats();
        return false;
    }
    // Add newly obtained noGoods.
    if (!ingest(obtained)) {
        logStats();
        return false;
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) ProgramAnalyzingGrounder(at.ac.tuwien.kr.alpha.core.grounder.ProgramAnalyzingGrounder) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) ArrayList(java.util.ArrayList) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) LinkedHashMap(java.util.LinkedHashMap) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToNegatedLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Example 29 with Literal

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

the class DefaultSolver method noGoodFromJustificationReasons.

private NoGood noGoodFromJustificationReasons(int atomToJustify, Set<Literal> reasonsForUnjustified) {
    // Turn the justification into a NoGood.
    int[] reasons = new int[reasonsForUnjustified.size() + 1];
    reasons[0] = atomToLiteral(atomToJustify);
    int arrpos = 1;
    for (Literal literal : reasonsForUnjustified) {
        reasons[arrpos++] = atomToLiteral(atomStore.get(literal.getAtom()), !literal.isNegated());
    }
    return NoGood.learnt(reasons);
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToNegatedLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral)

Example 30 with Literal

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

the class InternalRule method renameVariables.

/**
 * Returns a new Rule that is equal to this one except that all variables are renamed to have the newVariablePostfix
 * appended.
 *
 * @param newVariablePostfix
 * @return
 */
@Override
public InternalRule renameVariables(String newVariablePostfix) {
    List<VariableTerm> occurringVariables = new ArrayList<>();
    BasicAtom headAtom = this.getHeadAtom();
    occurringVariables.addAll(headAtom.getOccurringVariables());
    for (Literal literal : this.getBody()) {
        occurringVariables.addAll(literal.getOccurringVariables());
    }
    Unifier variableReplacement = new Unifier();
    for (VariableTerm occurringVariable : occurringVariables) {
        final String newVariableName = occurringVariable.toString() + newVariablePostfix;
        variableReplacement.put(occurringVariable, Terms.newVariable(newVariableName));
    }
    BasicAtom renamedHeadAtom = headAtom.substitute(variableReplacement);
    ArrayList<Literal> renamedBody = new ArrayList<>(this.getBody().size());
    for (Literal literal : this.getBody()) {
        renamedBody.add(literal.substitute(variableReplacement));
    }
    return new InternalRule(Heads.newNormalHead(renamedHeadAtom), renamedBody);
}
Also used : Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AggregateLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.AggregateLiteral) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Unifier(at.ac.tuwien.kr.alpha.commons.substitutions.Unifier)

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