use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class NaiveSolver method tryAdvance.
@Override
protected boolean tryAdvance(Consumer<? super AnswerSet> action) {
// Get basic rules and facts from grounder
if (doInit) {
obtainNoGoodsFromGrounder();
doInit = false;
} else {
// We already found one Answer-Set and are requested to find another one
doBacktrack();
if (isSearchSpaceExhausted()) {
return false;
}
}
// Try all assignments until grounder reports no more NoGoods and all of them are satisfied
while (true) {
if (!propagationFixpointReached()) {
LOGGER.trace("Propagating.");
// After a choice, it would be more efficient to propagate first and only then ask the grounder.
updateGrounderAssignments();
obtainNoGoodsFromGrounder();
doUnitPropagation();
doMBTPropagation();
LOGGER.trace("Assignment after propagation is: {}", truthAssignments);
} else if (assignmentViolatesNoGoods()) {
LOGGER.trace("Backtracking from wrong choices:");
LOGGER.trace("Choice stack: {}", choiceStack);
doBacktrack();
if (isSearchSpaceExhausted()) {
return false;
}
} else if (choicesLeft()) {
doChoice();
} else if (!allAtomsAssigned()) {
LOGGER.trace("Closing unassigned known atoms (assigning FALSE).");
assignUnassignedToFalse();
didChange = true;
} else if (noMBTValuesReamining()) {
AnswerSet as = getAnswerSetFromAssignment();
LOGGER.debug("Answer-Set found: {}", as);
LOGGER.trace("Choice stack: {}", choiceStack);
action.accept(as);
return true;
} else {
LOGGER.debug("Backtracking from wrong choices (MBT remaining):");
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Currently MBT:");
for (Integer integer : mbtAssigned) {
LOGGER.trace(atomStore.atomToString(atomOf(integer)));
}
LOGGER.trace("Choice stack: {}", choiceStack);
}
doBacktrack();
if (isSearchSpaceExhausted()) {
return false;
}
}
}
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class SolverStatisticsTests method collectAnswerSetsAndCheckStats.
private void collectAnswerSetsAndCheckStats(Solver solver, int expectedNumberOfAnswerSets, int expectedNumberOfGuesses, int expectedTotalNumberOfBacktracks, int expectedNumberOfBacktracksWithinBackjumps, int expectedNumberOfBackjumps, int expectedNumberOfMBTs, int expectedNumberOfConflictsAfterClosing, int expectedNumberOfDeletedNoGoods) {
Set<AnswerSet> answerSets = solver.collectSet();
assertEquals(expectedNumberOfAnswerSets, answerSets.size());
StatisticsReportingSolver solverMaintainingStatistics = (StatisticsReportingSolver) solver;
assertEquals(String.format("g=%d, bt=%d, bj=%d, bt_within_bj=%d, mbt=%d, cac=%d, del_ng=%d", expectedNumberOfGuesses, expectedTotalNumberOfBacktracks, expectedNumberOfBackjumps, expectedNumberOfBacktracksWithinBackjumps, expectedNumberOfMBTs, expectedNumberOfConflictsAfterClosing, expectedNumberOfDeletedNoGoods), solverMaintainingStatistics.getStatisticsString());
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AlphaImplTest method filterOutput.
@Test
public void filterOutput() throws Exception {
Alpha system = new AlphaImpl();
InputConfig inputCfg = InputConfig.forString("node(1). node(2). outgoing13(X) :- node(X), &getLargeGraphEdges(13,X).");
inputCfg.addPredicateMethod("getLargeGraphEdges", Externals.processPredicate(() -> new HashSet<>(asList(asList(Terms.newConstant(1), Terms.newConstant(2)), asList(Terms.newConstant(2), Terms.newConstant(1)), asList(Terms.newConstant(13), Terms.newConstant(1))))));
ASPCore2Program program = system.readProgram(inputCfg);
Set<AnswerSet> actual = system.solve(program).collect(Collectors.toSet());
Set<AnswerSet> expected = AnswerSetsParser.parse("{ node(1), node(2), outgoing13(1) }");
assertEquals(expected, actual);
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AlphaImplTest method basicUsage.
@Test
public void basicUsage() throws Exception {
Alpha system = new AlphaImpl();
Set<AnswerSet> actual = system.solve(system.readProgram(InputConfig.forString("p(a)."))).collect(Collectors.toSet());
Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("p").symbolicInstance("a").build()));
assertEquals(expected, actual);
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class AlphaImplTest method withExternal.
@Test
public void withExternal() throws Exception {
Alpha alpha = new AlphaImpl();
InputConfig inputCfg = InputConfig.forString("a :- &isOne[1].");
inputCfg.addPredicateMethod("isOne", Externals.processPredicateMethod(this.getClass().getMethod("isOne", int.class)));
ASPCore2Program program = alpha.readProgram(inputCfg);
Set<AnswerSet> actual = alpha.solve(program).collect(Collectors.toSet());
Set<AnswerSet> expected = new HashSet<>(singletonList(new AnswerSetBuilder().predicate("a").build()));
assertEquals(expected, actual);
}
Aggregations