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