use of at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration in project Alpha by alpha-asp.
the class NaiveGrounderTest method testPermissiveGrounderHeuristicTolerance.
/**
* Tests if {@link NaiveGrounder#getGroundInstantiations(InternalRule, RuleGroundingOrder, Substitution, Assignment)}
* produces ground instantiations for the rule with ID {@code ruleID} in {@code program} when {@code startingLiteral}
* unified with the numeric instance {@code startingInstance} is used as starting literal and the following
* additional conditions are established:
* <ul>
* <li>The atoms {@code b([startingInstance], 1), ..., b([startingInstance], n)} are added to the grounder's
* working memory without changing the assignment, where {@code arityOfB-1} occurences of {@code startingInstance}
* are used instead of {@code [startingInstance]} and {@code n} is the length of the {@code truthsOfB} array.
* For example, if the length of {@code truthsOfB} is 2 and {@code arityOfB} is also 2, these atoms are
* {@code b(1,1), b(1,2)}.
* </li>
* <li>The same atoms are assigned the truth values in the {@code truthsOfB} array.</li>
* </ul>
* It is asserted that ground instantiations are produced if and only if {@code expectNoGoods} is true.
* If ground instantiations are produced, it is also asserted that the numbers of unassigned positive body atoms
* determined by {@code getGroundInstantiations} match those given in {@code expectedNumbersOfUnassignedPositiveBodyAtoms}.
*/
private void testPermissiveGrounderHeuristicTolerance(ASPCore2Program program, int ruleID, Literal startingLiteral, int startingInstance, int tolerance, ThriceTruth[] truthsOfB, int arityOfB, boolean expectNoGoods, List<Integer> expectedNumbersOfUnassignedPositiveBodyAtoms) {
CompiledProgram internalPrg = InternalProgram.fromNormalProgram(NORMALIZE_TRANSFORM.apply(program));
AtomStore atomStore = new AtomStoreImpl();
TrailAssignment currentAssignment = new TrailAssignment(atomStore);
GrounderHeuristicsConfiguration heuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(tolerance, tolerance);
NaiveGrounder grounder = (NaiveGrounder) GrounderFactory.getInstance("naive", internalPrg, atomStore, p -> true, heuristicConfiguration, true);
int[] bAtomIDs = new int[truthsOfB.length];
for (int i = 0; i < truthsOfB.length; i++) {
int[] bTerms = new int[arityOfB];
for (int n = 0; n < arityOfB; n++) {
bTerms[n] = (n == arityOfB - 1) ? i + 1 : startingInstance;
}
bAtomIDs[i] = atomStore.putIfAbsent(atom("b", bTerms));
}
addAtomsToWorkingMemoryWithoutChangingTheAssignment(atomStore, grounder, bAtomIDs);
assign(currentAssignment, bAtomIDs, truthsOfB);
grounder.bootstrap();
final CompiledRule nonGroundRule = grounder.getNonGroundRule(ruleID);
final Substitution substStartingLiteral = BasicSubstitution.specializeSubstitution(startingLiteral, new Instance(Terms.newConstant(startingInstance)), BasicSubstitution.EMPTY_SUBSTITUTION);
final BindingResult bindingResult = grounder.getGroundInstantiations(nonGroundRule, nonGroundRule.getGroundingInfo().orderStartingFrom(startingLiteral), substStartingLiteral, currentAssignment);
assertEquals(expectNoGoods, bindingResult.size() > 0);
if (bindingResult.size() > 0) {
assertEquals(expectedNumbersOfUnassignedPositiveBodyAtoms, bindingResult.getNumbersOfUnassignedPositiveBodyAtoms());
} else {
assertTrue(bindingResult.getNumbersOfUnassignedPositiveBodyAtoms().isEmpty());
}
}
use of at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration in project Alpha by alpha-asp.
the class GrounderHeuristicConfigurationTest method testGetInstanceStrictPermissive.
@Test
public void testGetInstanceStrictPermissive() {
GrounderHeuristicsConfiguration grounderHeuristicsConfiguration = GrounderHeuristicsConfiguration.getInstance(STRICT_STRING, PERMISSIVE_STRING);
assertFalse(grounderHeuristicsConfiguration.isPermissive(true));
assertTrue(grounderHeuristicsConfiguration.isPermissive(false));
assertEquals(0, grounderHeuristicsConfiguration.getToleranceConstraints());
assertEquals(-1, grounderHeuristicsConfiguration.getToleranceRules());
}
use of at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration in project Alpha by alpha-asp.
the class GrounderHeuristicConfigurationTest method testGetInstancePermissiveStrict.
@Test
public void testGetInstancePermissiveStrict() {
GrounderHeuristicsConfiguration grounderHeuristicsConfiguration = GrounderHeuristicsConfiguration.getInstance(PERMISSIVE_STRING, STRICT_STRING);
assertTrue(grounderHeuristicsConfiguration.isPermissive(true));
assertFalse(grounderHeuristicsConfiguration.isPermissive(false));
assertEquals(-1, grounderHeuristicsConfiguration.getToleranceConstraints());
assertEquals(0, grounderHeuristicsConfiguration.getToleranceRules());
}
use of at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration in project Alpha by alpha-asp.
the class GrounderHeuristicConfigurationTest method testGetInstanceStringIntStringInt.
@Test
public void testGetInstanceStringIntStringInt() {
GrounderHeuristicsConfiguration grounderHeuristicsConfiguration = GrounderHeuristicsConfiguration.getInstance("5", "1");
assertTrue(grounderHeuristicsConfiguration.isPermissive(true));
assertTrue(grounderHeuristicsConfiguration.isPermissive(false));
assertEquals(5, grounderHeuristicsConfiguration.getToleranceConstraints());
assertEquals(1, grounderHeuristicsConfiguration.getToleranceRules());
}
use of at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration in project Alpha by alpha-asp.
the class AlphaImpl method prepareSolverFor.
/**
* Prepares a solver (and accompanying grounder) instance pre-loaded with the given program. Use this if the
* solver is needed after reading answer sets (e.g. for obtaining statistics).
*
* @param program the program to solve.
* @param filter a (java util) predicate that filters (asp-)predicates which should be contained in the answer
* set stream from the solver.
* @return a solver (and accompanying grounder) instance pre-loaded with the given program.
*/
private Solver prepareSolverFor(CompiledProgram program, java.util.function.Predicate<Predicate> filter) {
String grounderName = config.getGrounderName();
boolean doDebugChecks = config.isDebugInternalChecks();
GrounderHeuristicsConfiguration grounderHeuristicConfiguration = GrounderHeuristicsConfiguration.getInstance(config.getGrounderToleranceConstraints(), config.getGrounderToleranceRules());
grounderHeuristicConfiguration.setAccumulatorEnabled(config.isGrounderAccumulatorEnabled());
AtomStore atomStore = new AtomStoreImpl();
Grounder grounder = GrounderFactory.getInstance(grounderName, program, atomStore, filter, grounderHeuristicConfiguration, doDebugChecks);
return SolverFactory.getInstance(config, atomStore, grounder);
}
Aggregations