Search in sources :

Example 21 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class HanoiTowerTest method testHanoiTower.

private void testHanoiTower(String instance) throws IOException {
    ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("src", "test", "resources", "HanoiTower_Alpha.asp").toString());
    ANTLRFileStream instanceInputStream = new ANTLRFileStream(Paths.get("src", "test", "resources", "HanoiTower_instances", instance + ".asp").toString());
    ParsedProgram parsedProgram = parseVisit(programInputStream);
    parsedProgram.accumulate(parseVisit(instanceInputStream));
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    System.out.println(answerSet);
    checkGoal(parsedProgram, answerSet.get());
}
Also used : ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)

Example 22 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class PigeonHoleTest method testPigeonsHoles.

/**
	 * Tries to solve the problem of assigning P pigeons to H holes.
	 */
private void testPigeonsHoles(int pigeons, int holes) throws IOException {
    List<String> rules = new ArrayList<>();
    rules.add("pos(P,H) :- pigeon(P), hole(H), not negpos(P,H).");
    rules.add("negpos(P,H) :- pigeon(P), hole(H), not pos(P,H).");
    rules.add(":- pigeon(P), hole(H1), hole(H2), pos(P,H1), pos(P,H2), H1 != H2.");
    rules.add(":- pigeon(P), not hashole(P).");
    rules.add("hashole(P) :- pigeon(P), hole(H), pos(P,H).");
    rules.add(":- pigeon(P1), pigeon(P2), hole(H), pos(P1,H), pos(P2,H), P1 != P2.");
    addPigeons(rules, pigeons);
    addHoles(rules, holes);
    String testProgram = concat(rules);
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> answerSets = solver.collectSet();
    Assert.assertEquals(numberOfSolutions(pigeons, holes), answerSets.size());
    solver.stream().findAny();
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 23 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class RacksTest method test.

private void test() throws IOException {
    ANTLRFileStream programInputStream = new ANTLRFileStream(Paths.get("benchmarks", "siemens", "racks", "racks.lp").toString());
    ParsedProgram parsedProgram = parseVisit(programInputStream);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Optional<AnswerSet> answerSet = solver.stream().findFirst();
    System.out.println(answerSet);
    // TODO: check correctness of answer set
    System.out.println(((DefaultSolver) solver).getDecisionCounter() + " choices," + ((DefaultSolver) solver).getConflictCounter() + " conflicts");
}
Also used : ANTLRFileStream(org.antlr.v4.runtime.ANTLRFileStream) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 24 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method guessingProgramConstraint.

@Test
public void guessingProgramConstraint() throws IOException {
    String testProgram = "eq(1,1).\n" + "eq(2,2).\n" + "eq(3,3).\n" + "var(1).\n" + "var(2).\n" + "var(3).\n" + "val(VAR,1):-var(VAR),not val(VAR,2),not val(VAR,3).\n" + "val(VAR,2):-var(VAR),not val(VAR,1),not val(VAR,3).\n" + "val(VAR,3):-var(VAR),not val(VAR,1),not val(VAR,2).\n" + "%:- val(VAR1,VAL1), val(VAR2,VAL2), eq(VAL1,VAL2), not eq(VAR1,VAR2).\n" + ":- eq(VAL1,VAL2), not eq(VAR1,VAR2), val(VAR1,VAL1), val(VAR2,VAL2).";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    final BasicAnswerSet.Builder base = new BasicAnswerSet.Builder().predicate("eq").instance("1", "1").instance("2", "2").instance("3", "3").predicate("var").instance("1").instance("2").instance("3");
    Set<AnswerSet> expected = new HashSet<>(Arrays.asList(new BasicAnswerSet.Builder(base).predicate("val").instance("1", "1").instance("2", "2").instance("3", "3").build(), new BasicAnswerSet.Builder(base).predicate("val").instance("1", "1").instance("3", "2").instance("2", "3").build(), new BasicAnswerSet.Builder(base).predicate("val").instance("2", "1").instance("1", "2").instance("3", "3").build(), new BasicAnswerSet.Builder(base).predicate("val").instance("2", "1").instance("3", "2").instance("1", "3").build(), new BasicAnswerSet.Builder(base).predicate("val").instance("3", "1").instance("1", "2").instance("2", "3").build(), new BasicAnswerSet.Builder(base).predicate("val").instance("3", "1").instance("2", "2").instance("1", "3").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) Test(org.junit.Test)

Example 25 with NaiveGrounder

use of at.ac.tuwien.kr.alpha.grounder.NaiveGrounder in project Alpha by alpha-asp.

the class SolverTests method testSimpleRuleWithGroundPart.

@Test
public void testSimpleRuleWithGroundPart() throws Exception {
    String testProgram = "p(1)." + "p(2)." + "q(X) :-  p(X), p(1).";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    Grounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    List<AnswerSet> answerSets = solver.collectList();
    assertEquals(1, answerSets.size());
    AnswerSet expected = new BasicAnswerSet.Builder().predicate("q").instance("1").instance("2").predicate("p").instance("1").instance("2").build();
    assertEquals(expected, answerSets.get(0));
}
Also used : BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) ParsedProgram(at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram) BasicAnswerSet(at.ac.tuwien.kr.alpha.common.BasicAnswerSet) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet) ChoiceGrounder(at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder) DummyGrounder(at.ac.tuwien.kr.alpha.grounder.DummyGrounder) Grounder(at.ac.tuwien.kr.alpha.grounder.Grounder) NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) Test(org.junit.Test)

Aggregations

NaiveGrounder (at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)34 AnswerSet (at.ac.tuwien.kr.alpha.common.AnswerSet)31 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)29 BasicAnswerSet (at.ac.tuwien.kr.alpha.common.BasicAnswerSet)24 Test (org.junit.Test)24 ChoiceGrounder (at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder)6 DummyGrounder (at.ac.tuwien.kr.alpha.grounder.DummyGrounder)6 Grounder (at.ac.tuwien.kr.alpha.grounder.Grounder)6 ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)3 Before (org.junit.Before)2