use of at.ac.tuwien.kr.alpha.common.NoGood in project Alpha by alpha-asp.
the class BerkMinTest method countPositiveLiteralsTwice.
@Test
public void countPositiveLiteralsTwice() {
NoGood violatedNoGood = new NoGood(1, 2);
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
berkmin.violatedNoGood(violatedNoGood);
berkmin.analyzedConflict(pseudo(violatedNoGood));
assertEquals(2, berkmin.getActivity(1), EPSILON);
assertEquals(2, berkmin.getActivity(2), EPSILON);
}
use of at.ac.tuwien.kr.alpha.common.NoGood in project Alpha by alpha-asp.
the class DefaultSolver method tryAdvance.
@Override
protected boolean tryAdvance(Consumer<? super AnswerSet> action) {
// Initially, get NoGoods from grounder.
if (initialize) {
if (!obtainNoGoodsFromGrounder()) {
logSizeOfSearchTree();
return false;
}
initialize = false;
} else {
// Create enumeration NoGood to avoid finding the same Answer-Set twice.
if (!isSearchSpaceExhausted()) {
NoGood enumerationNoGood = createEnumerationNoGood();
int backjumpLevel = computeMinimumConflictLevel(enumerationNoGood);
if (backjumpLevel == -1) {
throw new RuntimeException("Enumeration NoGood is currently not violated. Should not happen.");
}
if (backjumpLevel == 0) {
// Search space exhausted (only happens if first guess is for TRUE at decision level 1 for an atom that was MBT at decision level 0 already).
return false;
}
// Backjump instead of backtrack, enumerationNoGood will invert lass guess.
doBackjump(backjumpLevel - 1);
LOGGER.debug("Adding enumeration NoGood: {}", enumerationNoGood);
NoGoodStore.ConflictCause conflictCause = store.add(grounder.registerOutsideNoGood(enumerationNoGood), enumerationNoGood);
if (conflictCause != null) {
throw new RuntimeException("Adding enumeration NoGood causes conflicts after backjump. Should not happen.");
}
} else {
logSizeOfSearchTree();
return false;
}
}
int nextChoice;
boolean afterAllAtomsAssigned = false;
// Try all assignments until grounder reports no more NoGoods and all of them are satisfied
while (true) {
didChange |= store.propagate();
LOGGER.trace("Assignment after propagation is: {}", assignment);
if (store.getViolatedNoGood() != null) {
// Learn from conflict.
NoGood violatedNoGood = store.getViolatedNoGood();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("NoGood violated ({}) by wrong choices ({} violated): {}", grounder.noGoodToString(violatedNoGood), choiceStack);
}
LOGGER.debug("Violating assignment is: {}", assignment);
branchingHeuristic.violatedNoGood(violatedNoGood);
if (!afterAllAtomsAssigned) {
if (!learnBackjumpAddFromConflict()) {
logSizeOfSearchTree();
return false;
}
} else {
// 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.");
doBacktrack();
afterAllAtomsAssigned = false;
if (isSearchSpaceExhausted()) {
logSizeOfSearchTree();
return false;
}
}
} else if (!propagationFixpointReached()) {
// Ask the grounder for new NoGoods, then propagate (again).
LOGGER.trace("Doing propagation step.");
updateGrounderAssignment();
if (!obtainNoGoodsFromGrounder()) {
logSizeOfSearchTree();
return false;
}
} else if ((nextChoice = computeChoice()) != 0) {
LOGGER.debug("Doing choice.");
doChoice(nextChoice);
} else if (!allAtomsAssigned()) {
LOGGER.debug("Closing unassigned known atoms (assigning FALSE).");
assignUnassignedToFalse();
afterAllAtomsAssigned = true;
} else if (assignment.getMBTCount() == 0) {
AnswerSet as = translate(assignment.getTrueAssignments());
LOGGER.debug("Answer-Set found: {}", as);
LOGGER.debug("Choices of Answer-Set were: {}", choiceStack);
action.accept(as);
logSizeOfSearchTree();
return true;
} else {
LOGGER.debug("Backtracking from wrong choices ({} MBTs): {}", assignment.getMBTCount(), choiceStack);
counters.remainingMBTAtFixpointCounter++;
doBacktrack();
afterAllAtomsAssigned = false;
if (isSearchSpaceExhausted()) {
logSizeOfSearchTree();
return false;
}
}
}
}
use of at.ac.tuwien.kr.alpha.common.NoGood in project Alpha by alpha-asp.
the class DefaultSolver method obtainNoGoodsFromGrounder.
/**
* Obtains new NoGoods from grounder and adds them to the NoGoodStore and the heuristics.
* @return false iff the set of NoGoods is detected to be unsatisfiable.
*/
private boolean obtainNoGoodsFromGrounder() {
Map<Integer, NoGood> obtained = grounder.getNoGoods(new BooleanAssignmentReader(assignment));
LOGGER.debug("Obtained NoGoods from grounder: {}", obtained);
if (!obtained.isEmpty()) {
// Record to detect propagation fixpoint, checking if new NoGoods were reported would be better here.
didChange = true;
}
// Record choice atoms.
final Pair<Map<Integer, Integer>, Map<Integer, Integer>> choiceAtoms = grounder.getChoiceAtoms();
choiceManager.addChoiceInformation(choiceAtoms);
// Inform heuristics.
branchingHeuristic.newNoGoods(obtained.values());
return addAllNoGoodsAndTreatContradictions(obtained);
}
use of at.ac.tuwien.kr.alpha.common.NoGood in project Alpha by alpha-asp.
the class GroundConflictNoGoodLearner method analyzeConflictingNoGoodRepetition.
private ConflictAnalysisResult analyzeConflictingNoGoodRepetition(NoGood violatedNoGood, Set<NoGood> noGoodsResponsible) {
noGoodsResponsible.add(violatedNoGood);
// Clone violated NoGood and remove potential head.
NoGood currentResolutionNoGood = new NoGood(violatedNoGood.getLiteralsClone());
// Find decision level where conflict occurs (i.e., highest decision level of violatedNoGood).
int conflictDecisionLevel = -1;
for (Integer literal : currentResolutionNoGood) {
Assignment.Entry literalEntry = getAssignmentEntryRespectingLowerMBT(literal);
int literalDL = literalEntry.getDecisionLevel();
if (literalDL > conflictDecisionLevel) {
conflictDecisionLevel = literalDL;
}
}
if (conflictDecisionLevel == 0) {
// The given set of NoGoods is unsatisfiable (conflict at decisionLevel 0).
return new ConflictAnalysisResult(null, 0, false, null, true);
}
FirstUIPPriorityQueue firstUIPPriorityQueue = new FirstUIPPriorityQueue(conflictDecisionLevel);
for (Integer literal : currentResolutionNoGood) {
firstUIPPriorityQueue.add(getAssignmentEntryRespectingLowerMBT(literal));
//sortLiteralToProcessIntoList(sortedLiteralsToProcess, literal, conflictDecisionLevel);
}
// TODO: create ResolutionSequence
if (firstUIPPriorityQueue.size() == 1) {
// There is only one literal to process, i.e., only one literal in the violatedNoGood is from conflict decision level.
// This means that the NoGood already was unit but got violated, because another NoGood propagated earlier or a wrong guess was made.
// The real conflict therefore is caused by either:
// a) two NoGoods propagating the same atom to different truth values in the current decisionLevel, or
// b) a NoGood propagating at a lower decision level to the inverse value of a guess with higher decision level.
// For a) we need to work also with the other NoGood.
// For b) we need to backtrack the wrong guess.
Assignment.Entry atomAssignmentEntry = firstUIPPriorityQueue.poll();
NoGood otherContributingNoGood = atomAssignmentEntry.getImpliedBy();
if (otherContributingNoGood == null) {
// Case b), the other assignment is a decision.
return new ConflictAnalysisResult(null, atomAssignmentEntry.getDecisionLevel(), true, noGoodsResponsible, false);
}
// Case a) take other implying NoGood into account.
currentResolutionNoGood = new NoGood(resolveNoGoods(firstUIPPriorityQueue, currentResolutionNoGood, otherContributingNoGood, atomAssignmentEntry), -1);
noGoodsResponsible.add(otherContributingNoGood);
// TODO: create/edit ResolutionSequence
}
while (true) {
// Check if 1UIP was reached.
if (firstUIPPriorityQueue.size() == 1) {
// Only one remaining literals to process, we reached 1UIP.
return new ConflictAnalysisResult(currentResolutionNoGood, computeBackjumpingDecisionLevel(currentResolutionNoGood), false, noGoodsResponsible, false);
} else if (firstUIPPriorityQueue.size() < 1) {
// For the moment, just report the learned NoGood.
return repeatAnalysisIfNotAssigning(currentResolutionNoGood, noGoodsResponsible);
//return new ConflictAnalysisResult(currentResolutionNoGood, computeBackjumpingDecisionLevel(currentResolutionNoGood), false, noGoodsResponsible);
} else if (currentResolutionNoGood.size() > 32) {
// Flag unsatisfiable abused here.
return new ConflictAnalysisResult(null, conflictDecisionLevel, true, noGoodsResponsible, false);
/*if (getAssignmentEntryRespectingLowerMBT(lastGuessedAtom).getDecisionLevel() <= conflictDecisionLevel) {
// If lastGuessedAtom is not unassigned after backjump, use repeatAnalysisIfNotAssigning.
return repeatAnalysisIfNotAssigning(replaceAllFromConflictDecisionLevelWithGuess(currentResolutionNoGood, conflictDecisionLevel, lastGuessedAtom), noGoodsResponsible, lastGuessedAtom);
}
return new ConflictAnalysisResult(replaceAllFromConflictDecisionLevelWithGuess(currentResolutionNoGood, conflictDecisionLevel, lastGuessedAtom), conflictDecisionLevel - 1, false, noGoodsResponsible, false, isLearntTooLarge);*/
}
// Resolve next NoGood based on current literal
Assignment.Entry currentLiteralAssignment = firstUIPPriorityQueue.poll();
if (currentLiteralAssignment.getPrevious() != null) {
// Use previous MBT assignment if it exists.
currentLiteralAssignment = currentLiteralAssignment.getPrevious();
}
// Get NoGood it was implied by.
NoGood impliedByNoGood = currentLiteralAssignment.getImpliedBy();
if (impliedByNoGood == null) {
// Literal was a decision, keep it in the currentResolutionNoGood by simply skipping.
continue;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("ImpliedBy NoGood is: {}.", impliedByNoGood);
for (Integer literal : impliedByNoGood) {
LOGGER.debug("Literal assignment: {}={}, previously {}.", atomOf(literal), assignment.get(atomOf(literal)), assignment.get(atomOf(literal)).getPrevious());
}
}
// TODO: add entry in ResolutionSequence.
currentResolutionNoGood = new NoGood(resolveNoGoods(firstUIPPriorityQueue, currentResolutionNoGood, impliedByNoGood, currentLiteralAssignment));
noGoodsResponsible.add(impliedByNoGood);
}
}
use of at.ac.tuwien.kr.alpha.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);
}
}
}
Aggregations