Search in sources :

Example 76 with NoGood

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

the class DefaultSolver method learnBackjumpAddFromConflict.

/**
 * Analyzes the conflict and learns a new NoGood (causing backjumping and addition to the NoGood store).
 *
 * @return false iff the analysis result shows that the set of NoGoods is unsatisfiable.
 */
private boolean learnBackjumpAddFromConflict(ConflictCause conflictCause) {
    GroundConflictNoGoodLearner.ConflictAnalysisResult analysisResult = learner.analyzeConflictingNoGood(conflictCause.getAntecedent());
    LOGGER.debug("Analysis result: {}", analysisResult);
    if (analysisResult == UNSAT) {
        // Halt if unsatisfiable.
        return false;
    }
    branchingHeuristic.analyzedConflict(analysisResult);
    if (analysisResult.learnedNoGood == null) {
        throw oops("Did not learn new NoGood from conflict.");
    }
    choiceManager.backjump(analysisResult.backjumpLevel);
    final NoGood learnedNoGood = analysisResult.learnedNoGood;
    int noGoodId = grounder.register(learnedNoGood);
    return addAndBackjumpIfNecessary(noGoodId, learnedNoGood, analysisResult.lbd);
}
Also used : NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) GroundConflictNoGoodLearner(at.ac.tuwien.kr.alpha.core.solver.learning.GroundConflictNoGoodLearner)

Example 77 with NoGood

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

the class DefaultSolver method prepareForSubsequentAnswerSet.

private void prepareForSubsequentAnswerSet() {
    // We already found one Answer-Set and are requested to find another one.
    searchState.afterAllAtomsAssigned = false;
    if (assignment.getDecisionLevel() == 0) {
        // Solver is at decision level 0 again after finding some answer-set
        searchState.isSearchSpaceCompletelyExplored = true;
        return;
    }
    // Create enumeration NoGood to avoid finding the same Answer-Set twice.
    final NoGood enumerationNoGood = choiceManager.computeEnumeration();
    final int backjumpLevel = assignment.minimumConflictLevel(enumerationNoGood);
    if (backjumpLevel == -1) {
        throw oops("Enumeration nogood is not violated");
    }
    if (backjumpLevel == 0) {
        // Search space exhausted (only happens if first choice is for TRUE at decision level 1 for an atom that was MBT at decision level 0 already).
        searchState.isSearchSpaceCompletelyExplored = true;
        return;
    }
    // Backjump instead of backtrackSlow, enumerationNoGood will invert last choice.
    choiceManager.backjump(backjumpLevel - 1);
    LOGGER.debug("Adding enumeration nogood: {}", enumerationNoGood);
    if (!addAndBackjumpIfNecessary(grounder.register(enumerationNoGood), enumerationNoGood, Integer.MAX_VALUE)) {
        searchState.isSearchSpaceCompletelyExplored = true;
    }
}
Also used : NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood)

Example 78 with NoGood

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

the class NaiveNoGoodStore method propagate.

@Override
public ConflictCause propagate() {
    hasInferredAssignments = false;
    boolean any = false;
    boolean retry;
    do {
        retry = false;
        ConflictCause conflictCause;
        for (NoGood noGood : delegate.values()) {
            hasInferredAssignments = false;
            conflictCause = propagateWeakly(noGood);
            if (conflictCause != null) {
                return conflictCause;
            }
            if (hasInferredAssignments) {
                any = true;
                hasInferredAssignments = false;
                retry = true;
            }
        }
        for (NoGood noGood : delegate.values()) {
            hasInferredAssignments = false;
            conflictCause = propagateStrongly(noGood);
            if (conflictCause != null) {
                return conflictCause;
            }
            if (hasInferredAssignments) {
                any = true;
                hasInferredAssignments = false;
                retry = true;
            }
        }
    } while (retry);
    for (NoGood noGood : delegate.values()) {
        if (assignment.violates(noGood)) {
            return new ConflictCause(noGood.asAntecedent());
        }
    }
    if (any) {
        hasInferredAssignments = true;
    }
    return null;
}
Also used : NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood)

Example 79 with NoGood

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

the class NogoodRegistry method register.

void register(Iterable<NoGood> noGoods, Map<Integer, NoGood> difference) {
    for (NoGood noGood : noGoods) {
        // Check if noGood was already derived earlier, add if it is new
        if (!registeredIdentifiers.containsKey(noGood)) {
            int noGoodId = ID_GENERATOR.getNextId();
            registeredIdentifiers.put(noGood, noGoodId);
            difference.put(noGoodId, noGood);
        }
    }
}
Also used : NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood)

Example 80 with NoGood

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

the class NaiveGrounderTest method groundRuleAlreadyGround.

/**
 * Asserts that a ground rule whose positive body is not satisfied by the empty assignment
 * is grounded immediately.
 */
@Test
public void groundRuleAlreadyGround() {
    ASPCore2Program program = PROGRAM_PARSER.parse("a :- not b. " + "b :- not a. " + "c :- b.");
    NormalProgram normal = NORMALIZE_TRANSFORM.apply(program);
    CompiledProgram 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 litCNeg = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("c")), false);
    int litB = Literals.atomToLiteral(atomStore.get(PROGRAM_PART_PARSER.parseBasicAtom("b")));
    assertExistsNoGoodContaining(noGoods.values(), litCNeg);
    assertExistsNoGoodContaining(noGoods.values(), litB);
}
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) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) 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)

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