Search in sources :

Example 16 with AnswerSet

use of at.ac.tuwien.kr.alpha.common.AnswerSet in project Alpha by alpha-asp.

the class ThreeColouringTestWithRandom method testThreeColouring.

private void testThreeColouring(int n, boolean shuffle, int seed) throws IOException {
    ParsedProgram program = parseVisit("col(V,C) :- v(V), c(C), not ncol(V,C)." + "ncol(V,C) :- col(V,D), c(C), C != D." + ":- e(V,U), col(V,C), col(U,C).");
    List<CommonParsedObject> colours = createColors("1", "2", "3");
    program.accumulate(new ParsedProgram(colours));
    List<CommonParsedObject> vertices = createVertices(n);
    program.accumulate(new ParsedProgram(vertices));
    List<CommonParsedObject> edges = createEdges(n, shuffle, seed);
    program.accumulate(new ParsedProgram(edges));
    NaiveGrounder grounder = new NaiveGrounder(program);
    Solver solver = getInstance(grounder);
    for (ParsedFact fact : program.facts) {
        System.out.println(fact.getFact().toString() + ".");
    }
    for (ParsedRule rule : program.rules) {
        System.out.print(rule.head.toString());
        System.out.print(":-");
        for (int i = 0; i < rule.body.size(); i++) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(rule.body.get(i).toString());
        }
        System.out.println(".");
    }
    for (ParsedConstraint constraint : program.constraints) {
        System.out.print(":-");
        for (int i = 0; i < constraint.body.size(); i++) {
            if (i > 0) {
                System.out.print(", ");
            }
            System.out.print(constraint.body.get(i).toString());
        }
        System.out.println(".");
    }
    Optional<AnswerSet> answerSet = solver.stream().findAny();
    System.out.println(answerSet);
// TODO: check correctness of answer set
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.grounder.NaiveGrounder) AnswerSet(at.ac.tuwien.kr.alpha.common.AnswerSet)

Example 17 with AnswerSet

use of at.ac.tuwien.kr.alpha.common.AnswerSet in project Alpha by alpha-asp.

the class SolverTests method testSimpleRule.

@Test
public void testSimpleRule() throws Exception {
    String testProgram = "p(a). p(b). r(X) :- p(X).";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    Grounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    List<AnswerSet> answerSets = solver.collectList();
    AnswerSet expected = new BasicAnswerSet.Builder().predicate("p").instance("a").instance("b").predicate("r").instance("a").instance("b").build();
    assertEquals(1, answerSets.size());
    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)

Example 18 with AnswerSet

use of at.ac.tuwien.kr.alpha.common.AnswerSet in project Alpha by alpha-asp.

the class SolverTests method builtinAtomsGroundRule.

@Test
public void builtinAtomsGroundRule() throws IOException {
    String testProgram = "a :- 13 != 4." + "b :- 2 != 3, 2 = 3." + "c :- 2 <= 3, not 2 > 3.";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> expected = new HashSet<>(Collections.singletonList(new BasicAnswerSet.Builder().predicate("a").predicate("c").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 19 with AnswerSet

use of at.ac.tuwien.kr.alpha.common.AnswerSet in project Alpha by alpha-asp.

the class SolverTests method guessingProgram3Way.

@Test
public void guessingProgram3Way() throws IOException {
    String testProgram = "a :- not b, not c." + "b :- not a, not c." + "c :- not a, not b.";
    ParsedProgram parsedProgram = parseVisit(testProgram);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> expected = new HashSet<>(Arrays.asList(new BasicAnswerSet.Builder().predicate("a").build(), new BasicAnswerSet.Builder().predicate("b").build(), new BasicAnswerSet.Builder().predicate("c").build()));
    Set<AnswerSet> answerSets = solver.collectSet();
    assertEquals(expected, answerSets);
}
Also used : 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 20 with AnswerSet

use of at.ac.tuwien.kr.alpha.common.AnswerSet in project Alpha by alpha-asp.

the class SolverTests method sameVariableTwiceInAtom.

@Test
public void sameVariableTwiceInAtom() throws IOException {
    String program = "p(a, a).\n" + "q(X) :- p(X, X).\n";
    ParsedProgram parsedProgram = parseVisit(program);
    NaiveGrounder grounder = new NaiveGrounder(parsedProgram);
    Solver solver = getInstance(grounder);
    Set<AnswerSet> expected = new HashSet<>(Collections.singletonList(new BasicAnswerSet.Builder().predicate("p").instance("a", "a").predicate("q").instance("a").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)

Aggregations

AnswerSet (at.ac.tuwien.kr.alpha.common.AnswerSet)34 NaiveGrounder (at.ac.tuwien.kr.alpha.grounder.NaiveGrounder)31 ParsedProgram (at.ac.tuwien.kr.alpha.grounder.parser.ParsedProgram)28 BasicAnswerSet (at.ac.tuwien.kr.alpha.common.BasicAnswerSet)24 Test (org.junit.Test)24 Grounder (at.ac.tuwien.kr.alpha.grounder.Grounder)7 ChoiceGrounder (at.ac.tuwien.kr.alpha.grounder.ChoiceGrounder)6 DummyGrounder (at.ac.tuwien.kr.alpha.grounder.DummyGrounder)6 ANTLRFileStream (org.antlr.v4.runtime.ANTLRFileStream)2 ASPCore2Lexer (at.ac.tuwien.kr.alpha.antlr.ASPCore2Lexer)1 ASPCore2Parser (at.ac.tuwien.kr.alpha.antlr.ASPCore2Parser)1 NoGood (at.ac.tuwien.kr.alpha.common.NoGood)1 Predicate (at.ac.tuwien.kr.alpha.common.Predicate)1 GrounderFactory (at.ac.tuwien.kr.alpha.grounder.GrounderFactory)1 ParsedTreeVisitor (at.ac.tuwien.kr.alpha.grounder.parser.ParsedTreeVisitor)1 IdentityProgramTransformation (at.ac.tuwien.kr.alpha.grounder.transformation.IdentityProgramTransformation)1 Solver (at.ac.tuwien.kr.alpha.solver.Solver)1 SolverFactory (at.ac.tuwien.kr.alpha.solver.SolverFactory)1 Heuristic (at.ac.tuwien.kr.alpha.solver.heuristics.BranchingHeuristicFactory.Heuristic)1 java.io (java.io)1