Search in sources :

Example 1 with NoGood

use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.

the class NaiveGrounderTest method groundConstraintAlreadyGround.

/**
 * Asserts that a ground constraint whose positive body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundConstraintAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + ":- b.");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    InternalProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
    assertTrue(noGoods.containsValue(NoGood.fromConstraint(Collections.singletonList(litB), Collections.emptyList())));
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 2 with NoGood

use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.

the class NaiveGrounderTest method groundRuleWithLongerBodyAlreadyGround.

/**
 * Asserts that a ground rule whose positive non-unary body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundRuleWithLongerBodyAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + "c :- b. " + "d :- b, c. ");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    InternalProgram prog = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normal));
    AtomStore atomStore = new AtomStoreImpl();
    Grounder grounder = GrounderFactory.getInstance("naive", prog, atomStore, true);
    Map<Integer, NoGood> noGoods = grounder.getNoGoods(new TrailAssignment(atomStore));
    int litANeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("a")), false);
    int litBNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")), false);
    int litCNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("c")), false);
    int litDNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("d")), false);
    assertExistsNoGoodContaining(noGoods.values(), litANeg);
    assertExistsNoGoodContaining(noGoods.values(), litBNeg);
    assertExistsNoGoodContaining(noGoods.values(), litCNeg);
    assertExistsNoGoodContaining(noGoods.values(), litDNeg);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 3 with NoGood

use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.

the class ChoiceManagerTests method testIsAtomChoice.

@Test
public void testIsAtomChoice() {
    Collection<NoGood> noGoods = getNoGoods();
    choiceManager.addChoiceInformation(grounder.getChoiceAtoms(), grounder.getHeadsToBodies());
    for (NoGood noGood : noGoods) {
        for (Integer literal : noGood) {
            int atom = atomOf(literal);
            String atomToString = atomStore.atomToString(atom);
            if (atomToString.startsWith(RuleAtom.PREDICATE.getName())) {
                assertTrue(choiceManager.isAtomChoice(atom), "Atom not choice: " + atomToString);
            }
        }
    }
}
Also used : NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) Test(org.junit.jupiter.api.Test)

Example 4 with NoGood

use of at.ac.tuwien.kr.alpha.core.common.NoGood 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 5 with NoGood

use of at.ac.tuwien.kr.alpha.core.common.NoGood 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)

Aggregations

NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)91 Test (org.junit.jupiter.api.Test)73 Disabled (org.junit.jupiter.api.Disabled)10 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)4 LinkedHashMap (java.util.LinkedHashMap)4 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)3 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)3 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)3 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)3 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)3 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)3 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)3 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)3 CompiledRule (at.ac.tuwien.kr.alpha.core.rules.CompiledRule)3 ConflictAnalysisResult (at.ac.tuwien.kr.alpha.core.solver.learning.GroundConflictNoGoodLearner.ConflictAnalysisResult)3 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)2 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)2 Literals.atomToLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral)2 Literals.atomToNegatedLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral)2