Search in sources :

Example 1 with GrounderHeuristicsConfiguration

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());
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) InternalProgram(at.ac.tuwien.kr.alpha.core.programs.InternalProgram) BeforeEach(org.junit.jupiter.api.BeforeEach) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) Arrays(java.util.Arrays) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AnalyzedProgram(at.ac.tuwien.kr.alpha.core.programs.AnalyzedProgram) ThriceTruth(at.ac.tuwien.kr.alpha.core.solver.ThriceTruth) ProgramParser(at.ac.tuwien.kr.alpha.api.programs.ProgramParser) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) Disabled(org.junit.jupiter.api.Disabled) NormalProgram(at.ac.tuwien.kr.alpha.api.programs.NormalProgram) Terms(at.ac.tuwien.kr.alpha.commons.terms.Terms) ProgramParserImpl(at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl) Map(java.util.Map) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Literals(at.ac.tuwien.kr.alpha.core.atoms.Literals) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) NormalizeProgramTransformation(at.ac.tuwien.kr.alpha.core.programs.transformation.NormalizeProgramTransformation) Collection(java.util.Collection) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) StratifiedEvaluation(at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation) ProgramPartParser(at.ac.tuwien.kr.alpha.core.parser.ProgramPartParser) TestUtils.atom(at.ac.tuwien.kr.alpha.core.test.util.TestUtils.atom) Test(org.junit.jupiter.api.Test) List(java.util.List) Assignment(at.ac.tuwien.kr.alpha.core.common.Assignment) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) SystemConfig(at.ac.tuwien.kr.alpha.api.config.SystemConfig) ASPCore2Program(at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) InternalRule(at.ac.tuwien.kr.alpha.core.rules.InternalRule) Collections(java.util.Collections) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) BindingResult(at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult) Instance(at.ac.tuwien.kr.alpha.commons.substitutions.Instance) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule)

Example 2 with GrounderHeuristicsConfiguration

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());
}
Also used : GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) Test(org.junit.jupiter.api.Test)

Example 3 with GrounderHeuristicsConfiguration

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());
}
Also used : GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with GrounderHeuristicsConfiguration

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());
}
Also used : GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) Test(org.junit.jupiter.api.Test)

Example 5 with GrounderHeuristicsConfiguration

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);
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) GrounderHeuristicsConfiguration(at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) Grounder(at.ac.tuwien.kr.alpha.core.grounder.Grounder)

Aggregations

GrounderHeuristicsConfiguration (at.ac.tuwien.kr.alpha.api.config.GrounderHeuristicsConfiguration)8 Test (org.junit.jupiter.api.Test)7 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)2 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)2 SystemConfig (at.ac.tuwien.kr.alpha.api.config.SystemConfig)1 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)1 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)1 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)1 ProgramParser (at.ac.tuwien.kr.alpha.api.programs.ProgramParser)1 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)1 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)1 Instance (at.ac.tuwien.kr.alpha.commons.substitutions.Instance)1 Terms (at.ac.tuwien.kr.alpha.commons.terms.Terms)1 Literals (at.ac.tuwien.kr.alpha.core.atoms.Literals)1 Assignment (at.ac.tuwien.kr.alpha.core.common.Assignment)1 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)1 Grounder (at.ac.tuwien.kr.alpha.core.grounder.Grounder)1 BindingResult (at.ac.tuwien.kr.alpha.core.grounder.instantiation.BindingResult)1 ProgramParserImpl (at.ac.tuwien.kr.alpha.core.parser.ProgramParserImpl)1 ProgramPartParser (at.ac.tuwien.kr.alpha.core.parser.ProgramPartParser)1