Search in sources :

Example 66 with AnswerSet

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;
            }
        }
    }
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet)

Example 67 with AnswerSet

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());
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) StatisticsReportingSolver(at.ac.tuwien.kr.alpha.api.StatisticsReportingSolver)

Example 68 with AnswerSet

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);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 69 with AnswerSet

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);
}
Also used : AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 70 with AnswerSet

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);
}
Also used : ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AnswerSet(at.ac.tuwien.kr.alpha.api.AnswerSet) Alpha(at.ac.tuwien.kr.alpha.api.Alpha) InputConfig(at.ac.tuwien.kr.alpha.api.config.InputConfig) AnswerSetBuilder(at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

AnswerSet (at.ac.tuwien.kr.alpha.api.AnswerSet)80 Test (org.junit.jupiter.api.Test)64 Alpha (at.ac.tuwien.kr.alpha.api.Alpha)25 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)24 AnswerSetBuilder (at.ac.tuwien.kr.alpha.commons.AnswerSetBuilder)20 InputConfig (at.ac.tuwien.kr.alpha.api.config.InputConfig)17 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)14 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)14 HashSet (java.util.HashSet)13 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)10 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)9 Solver (at.ac.tuwien.kr.alpha.api.Solver)8 InputProgram (at.ac.tuwien.kr.alpha.core.programs.InputProgram)7 Disabled (org.junit.jupiter.api.Disabled)7 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)4 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)4 ConstantTerm (at.ac.tuwien.kr.alpha.api.terms.ConstantTerm)4 TestUtils.assertRegressionTestAnswerSet (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.assertRegressionTestAnswerSet)4 TestUtils.buildSolverForRegressionTest (at.ac.tuwien.kr.alpha.core.test.util.TestUtils.buildSolverForRegressionTest)4 HashMap (java.util.HashMap)4