use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class SolverTests method instanceEnumerationAtom.
@RegressionTest
public void instanceEnumerationAtom(RegressionTestConfig cfg) {
Set<AnswerSet> answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(1). dom(2). dom(3)." + "p(X) :- dom(X)." + "q(Y) :- p(Y)." + "unique_position(Term,Pos) :- q(Term), enum(id0,Term,Pos)." + "wrong_double_occurrence :- unique_position(T1,P), unique_position(T2,P), T1 != T2.", cfg).collectSet();
// Since enumeration depends on evaluation, we do not know which unique_position is actually assigned.
// Check manually that there is one answer set, wrong_double_occurrence has not been derived, and enum yielded a unique position for each term.
assertEquals(1, answerSets.size());
AnswerSet answerSet = answerSets.iterator().next();
assertEquals(null, answerSet.getPredicateInstances(Predicates.getPredicate("wrong_double_occurrence", 0)));
SortedSet<Atom> positions = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position", 2));
assertEnumerationPositions(positions, 3);
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class SolverTests method instanceEnumerationArbitraryTerms.
@RegressionTest
public void instanceEnumerationArbitraryTerms(RegressionTestConfig cfg) {
Set<AnswerSet> answerSets = buildSolverForRegressionTest("# enumeration_predicate_is enum." + "dom(a). dom(f(a,b)). dom(d)." + "p(X) :- dom(X)." + "q(Y) :- p(Y)." + "unique_position(Term,Pos) :- q(Term), enum(id0,Term,Pos)." + "wrong_double_occurrence :- unique_position(T1,P), unique_position(T2,P), T1 != T2.", cfg).collectSet();
// Since enumeration depends on evaluation, we do not know which unique_position is actually assigned.
// Check manually that there is one answer set, wrong_double_occurrence has not been derived, and enum yielded a unique position for each term.
assertEquals(1, answerSets.size());
AnswerSet answerSet = answerSets.iterator().next();
assertPropositionalPredicateFalse(answerSet, Predicates.getPredicate("wrong_double_occurrence", 0));
SortedSet<Atom> positions = answerSet.getPredicateInstances(Predicates.getPredicate("unique_position", 2));
assertEnumerationPositions(positions, 3);
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class ThreeColouringRandomGraphTest method testThreeColouring.
private void testThreeColouring(int nVertices, int nEdges, RegressionTestConfig cfg) {
ASPCore2Program tmpPrg = new ProgramParserImpl().parse("blue(N) :- v(N), not red(N), not green(N)." + "red(N) :- v(N), not blue(N), not green(N)." + "green(N) :- v(N), not red(N), not blue(N)." + ":- e(N1,N2), blue(N1), blue(N2)." + ":- e(N1,N2), red(N1), red(N2)." + ":- e(N1,N2), green(N1), green(N2).");
InputProgram.Builder prgBuilder = InputProgram.builder(tmpPrg);
prgBuilder.addFacts(createVertices(nVertices));
prgBuilder.addFacts(createEdges(nVertices, nEdges));
InputProgram program = prgBuilder.build();
maybeShuffle(program);
@SuppressWarnings("unused") Optional<AnswerSet> answerSet = buildSolverForRegressionTest(program, cfg).stream().findAny();
// System.out.println(answerSet);
// TODO: check correctness of answer set
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testNegatedLiteralInRecursiveRule.
/**
* Verifies correct handling of negated basic literals in StratifiedEvaluation.
* For details, see comments in test program
*
* @throws IOException
*/
@Test
public void testNegatedLiteralInRecursiveRule() throws IOException {
// @formatter:off
String expectedAnswerSet = "basefact1(1), basefact2(1), max_value(10), min_value(1), " + "basefact1(9), basefact2(9), base(1), base(9), " + "inc_value(1), inc_value(2), inc_value(2), inc_value(3), " + "inc_value(4), inc_value(5), inc_value(6), inc_value(7), " + "inc_value(8)";
// @formatter:on
ASPCore2Program prog = Programs.fromInputStream(StratifiedEvaluationTest.class.getResourceAsStream("/partial-eval/recursive_w_negated_condition.asp"), new HashMap<>());
// Run stratified evaluation and solve
CompiledProgram inputStratEval = new StratifiedEvaluation().apply(AnalyzedProgram.analyzeNormalProgram(normalizer.apply(prog)));
Set<AnswerSet> asStrat = solveCompiledProg.apply(inputStratEval);
TestUtils.assertAnswerSetsEqual(expectedAnswerSet, asStrat);
// Solve without stratified evaluation
CompiledProgram inputNoStratEval = InternalProgram.fromNormalProgram(normalizer.apply(prog));
Set<AnswerSet> as = solveCompiledProg.apply(inputNoStratEval);
TestUtils.assertAnswerSetsEqual(expectedAnswerSet, as);
}
use of at.ac.tuwien.kr.alpha.api.AnswerSet in project Alpha by alpha-asp.
the class StratifiedEvaluationTest method testEqualityWithVarTerms.
@Test
public void testEqualityWithVarTerms() {
String aspStr = "a(1). a(2). a(3). b(X) :- a(X), X = 1. c(X) :- a(X), X = 2. d(X) :- X = 3, a(X).";
CompiledProgram evaluated = parseAndEvaluate.apply(aspStr);
Set<AnswerSet> answerSets = solveCompiledProg.apply(evaluated);
TestUtils.assertAnswerSetsEqual("a(1), a(2), a(3), b(1), c(2), d(3)", answerSets);
}
Aggregations