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())));
}
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);
}
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);
}
}
}
}
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;
}
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;
}
Aggregations