use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.
the class GroundConflictNoGoodLearner method analyzeTrailBased.
private ConflictAnalysisResult analyzeTrailBased(Antecedent conflictReason) {
LOGGER.trace("Analyzing trail based.");
if (assignment.getDecisionLevel() == 0) {
LOGGER.trace("Conflict on decision level 0.");
return ConflictAnalysisResult.UNSAT;
}
int numLiteralsInConflictLevel = 0;
List<Integer> resolutionLiterals = new ArrayList<>();
List<Integer> resolutionAtoms = new ArrayList<>();
int currentDecisionLevel = assignment.getDecisionLevel();
// NOTE: other solvers use a global array for seen atoms, this might be slightly faster (initial tests with local arrays showed no significant improvement).
Set<Integer> seenAtoms = new HashSet<>();
// Since trail contains 2 entries for MBT->TRUE assigned atoms, explicitly record which seen atoms have ben processed to avoid processing seen atoms twice.
Set<Integer> processedAtoms = new HashSet<>();
int[] currentConflictReason = conflictReason.getReasonLiterals();
int backjumpLevel = -1;
conflictReason.bumpActivity();
TrailAssignment.TrailBackwardsWalker trailWalker = ((TrailAssignment) assignment).getTrailBackwardsWalker();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Current trail is: {}", trailWalker);
LOGGER.trace("Violated nogood is: {}", reasonsToString(conflictReason.getReasonLiterals()));
}
int nextAtom = -1;
do {
// Add current conflict reasons; only add those of lower decision levels, since from current one, only the 1UIP literal will be added.
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Atom {} implied by {}, resolving with that nogood", nextAtom, reasonsToString(currentConflictReason));
}
for (int literal : currentConflictReason) {
// Seen atoms have already been dealt with.
if (!seenAtoms.contains(atomOf(literal))) {
seenAtoms.add(atomOf(literal));
int literalDecisionLevel = assignment.getWeakDecisionLevel(atomOf(literal));
if (literalDecisionLevel == currentDecisionLevel) {
numLiteralsInConflictLevel++;
} else {
resolutionLiterals.add(literal);
if (literalDecisionLevel > backjumpLevel) {
backjumpLevel = literalDecisionLevel;
}
}
resolutionAtoms.add(atomOf(literal));
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("LiteralsInConflictLevel now: {}", numLiteralsInConflictLevel);
LOGGER.trace("Seen atoms are {}.", seenAtoms);
LOGGER.trace("Intermediate learned literals: {}", reasonsToString(resolutionLiterals));
}
// Find next literal, i.e. first from top of trail that has been seen but is not yet processed, also skip atoms whose TRUE assignment is on current level but their MBT/weak assignment is lower.
do {
int nextLiteral = trailWalker.getNextLowerLiteral();
nextAtom = atomOf(nextLiteral);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Next literal on trail is: {}", isPositive(nextLiteral) ? "+" + nextAtom : "-" + nextAtom);
}
} while (assignment.getWeakDecisionLevel(nextAtom) != currentDecisionLevel || !seenAtoms.contains(nextAtom) || processedAtoms.contains(nextAtom));
Antecedent impliedBy = assignment.getImpliedBy(nextAtom);
if (impliedBy != null) {
currentConflictReason = impliedBy.getReasonLiterals();
impliedBy.bumpActivity();
}
processedAtoms.add(nextAtom);
} while (numLiteralsInConflictLevel-- > 1);
// Add the 1UIP literal.
resolutionLiterals.add(atomToLiteral(nextAtom, assignment.getTruth(nextAtom).toBoolean()));
int[] learnedLiterals = minimizeLearnedLiterals(resolutionLiterals, seenAtoms);
NoGood learnedNoGood = NoGood.learnt(learnedLiterals);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Learned NoGood is: {}", atomStore.noGoodToString(learnedNoGood));
}
int backjumpingDecisionLevel = computeBackjumpingDecisionLevel(learnedNoGood);
if (backjumpingDecisionLevel < 0) {
// Due to out-of-order assigned literals, the learned nogood may be not assigning.
backjumpingDecisionLevel = computeConflictFreeBackjumpingLevel(learnedNoGood);
if (backjumpingDecisionLevel < 0) {
return ConflictAnalysisResult.UNSAT;
}
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Backjumping decision level: {}", backjumpingDecisionLevel);
}
return new ConflictAnalysisResult(learnedNoGood, backjumpingDecisionLevel, resolutionAtoms, computeLBD(learnedLiterals));
}
use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.
the class NaiveSolver method doUnitPropagation.
private void doUnitPropagation() {
// Check each NoGood if it is unit (naive algorithm)
for (NoGood noGood : knownNoGoods.values()) {
int implied = unitPropagate(noGood);
if (implied == -1) {
// NoGood is not unit, skip.
continue;
}
int impliedLiteral = noGood.getLiteral(implied);
int impliedAtomId = atomOf(impliedLiteral);
boolean impliedTruthValue = isNegated(impliedLiteral);
if (truthAssignments.get(impliedAtomId) != null) {
// Skip if value already was assigned.
continue;
}
truthAssignments.put(impliedAtomId, impliedTruthValue);
newTruthAssignments.add(impliedAtomId);
// Record to detect propagation fixpoint
didChange = true;
decisionLevels.get(decisionLevel).add(impliedAtomId);
if (impliedTruthValue) {
// Record MBT value in case true is assigned
mbtAssigned.add(impliedAtomId);
mbtAssignedFromUnassigned.get(decisionLevel).add(impliedAtomId);
}
}
}
use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.
the class DependencyDrivenHeuristic method chooseAtom.
protected int chooseAtom() {
for (NoGood noGood : stackOfNoGoods) {
int mostActiveAtom = getMostActiveAtom(noGood);
if (choiceManager.isActiveChoiceAtom(mostActiveAtom)) {
return mostActiveAtom;
}
Collection<Integer> bodies = atomsToBodiesAtoms.get(mostActiveAtom);
Optional<Integer> mostActiveBody = bodies.stream().filter(this::isUnassigned).filter(choiceManager::isActiveChoiceAtom).max(Comparator.comparingDouble(bodyActivity::get));
if (mostActiveBody.isPresent()) {
return mostActiveBody.get();
}
}
return DEFAULT_CHOICE_ATOM;
}
use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.
the class BerkMinTest method countMixedLiteralsTwice.
@Test
public void countMixedLiteralsTwice() {
NoGood violatedNoGood = new NoGood(fromOldLiterals(1, -2));
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
assertEquals(2, berkmin.getActivity(fromOldLiterals(1)), DOUBLE_COMPARISON_EPSILON);
assertEquals(2, berkmin.getActivity(fromOldLiterals(-2)), DOUBLE_COMPARISON_EPSILON);
}
use of at.ac.tuwien.kr.alpha.core.common.NoGood in project Alpha by alpha-asp.
the class BerkMinTest method reachDecayPeriodOnce.
@Test
public void reachDecayPeriodOnce() {
berkmin.setDecayPeriod(3);
berkmin.setDecayFactor(1.0 / 3);
NoGood violatedNoGood = new NoGood(fromOldLiterals(1, 2));
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
assertEquals(1, berkmin.getActivity(fromOldLiterals(1)), DOUBLE_COMPARISON_EPSILON);
assertEquals(1, berkmin.getActivity(fromOldLiterals(2)), DOUBLE_COMPARISON_EPSILON);
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
assertEquals(2, berkmin.getActivity(fromOldLiterals(1)), DOUBLE_COMPARISON_EPSILON);
assertEquals(2, berkmin.getActivity(fromOldLiterals(2)), DOUBLE_COMPARISON_EPSILON);
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
assertEquals(1, berkmin.getActivity(fromOldLiterals(1)), DOUBLE_COMPARISON_EPSILON);
assertEquals(1, berkmin.getActivity(fromOldLiterals(2)), DOUBLE_COMPARISON_EPSILON);
}
Aggregations