Search in sources :

Example 11 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class VSIDSTest method setUp.

@BeforeEach
public void setUp() {
    atomStore = new AtomStoreImpl();
    TestUtils.fillAtomStore(atomStore, 4);
    assignment = new TrailAssignment(atomStore);
    assignment.growForMaxAtomId();
    noGoodStore = new NoGoodStoreAlphaRoaming(assignment);
    this.vsids = new VSIDS(assignment, new PseudoChoiceManager(assignment, noGoodStore), null);
}
Also used : NoGoodStoreAlphaRoaming(at.ac.tuwien.kr.alpha.core.solver.NoGoodStoreAlphaRoaming) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to find the ground
 * instance "q(a, b)" for the partially ground positive literal "q(a, X)".
 *
 * In this case, the instantiation strategy has an assignment where q(a, b) is
 * assigned ThriceTruth.TRUE, so we expect the assignment status (i.e.
 * assignment status of the found ground instance) passed back with the
 * substitution to be TRUE. Furthermore, we expect the stale atom set to stay
 * empty.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithTrueInstance() {
    Predicate q = Predicates.getPredicate("q", 2);
    BasicAtom nonGroundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newVariable("X"));
    WorkingMemory workingMemory = new WorkingMemory();
    workingMemory.initialize(q);
    workingMemory.addInstance(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b")), true);
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    BasicAtom groundAtom = Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"));
    atomStore.putIfAbsent(groundAtom);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(groundAtom), ThriceTruth.TRUE);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    List<ImmutablePair<Substitution, AssignmentStatus>> result = strategy.getAcceptedSubstitutions(Literals.fromAtom(nonGroundAtom, true), new BasicSubstitution());
    assertEquals(1, result.size());
    ImmutablePair<Substitution, AssignmentStatus> substitutionInfo = result.get(0);
    Substitution substitution = substitutionInfo.left;
    AssignmentStatus assignmentStatus = substitutionInfo.right;
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Test(org.junit.jupiter.api.Test)

Example 13 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class AnalyzeUnjustifiedTest method justifySimpleRules.

@Test
public void justifySimpleRules() {
    String program = "p(X) :- q(X)." + "q(X) :- p(X)." + "q(5) :- r." + "r :- not nr." + "nr :- not r." + ":- not p(5).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    int rId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("r", 0)));
    int nrId = atomStore.get(Atoms.newBasicAtom(Predicates.getPredicate("nr", 0)));
    assignment.growForMaxAtomId();
    assignment.assign(rId, ThriceTruth.FALSE);
    assignment.assign(nrId, ThriceTruth.TRUE);
    BasicAtom p5 = Atoms.newBasicAtom(Predicates.getPredicate("p", 1), Collections.singletonList(Terms.newConstant(5)));
    assignment.assign(atomStore.get(p5), ThriceTruth.MBT);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(p5), assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) CompiledProgram(at.ac.tuwien.kr.alpha.core.programs.CompiledProgram) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) Test(org.junit.jupiter.api.Test)

Example 14 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class AnalyzeUnjustifiedTest method justifyMultipleReasons.

@Test
public void justifyMultipleReasons() {
    String program = "n(a). n(b). n(c). n(d). n(e)." + "s(a,b). s(b,c). s(c,d). s(d,e)." + "{ q(X) } :- n(X)." + "p(X) :- q(X)." + "p(X) :- p(Y), s(Y,X)." + ":- not p(c).";
    CompiledProgram internalProgram = parseAndPreprocess.apply(program);
    AtomStore atomStore = new AtomStoreImpl();
    NaiveGrounder grounder = new NaiveGrounder(internalProgram, atomStore, true);
    grounder.getNoGoods(null);
    TrailAssignment assignment = new TrailAssignment(atomStore);
    Atom qa = parser.parse("q(a).").getFacts().get(0);
    Atom qb = parser.parse("q(b).").getFacts().get(0);
    Atom qc = parser.parse("q(c).").getFacts().get(0);
    Atom qd = parser.parse("q(d).").getFacts().get(0);
    Atom qe = parser.parse("q(e).").getFacts().get(0);
    int qaId = atomStore.get(qa);
    int qbId = atomStore.get(qb);
    int qcId = atomStore.get(qc);
    int qdId = atomStore.get(qd);
    int qeId = atomStore.get(qe);
    assignment.growForMaxAtomId();
    assignment.assign(qaId, ThriceTruth.FALSE);
    assignment.assign(qbId, ThriceTruth.FALSE);
    assignment.assign(qcId, ThriceTruth.FALSE);
    assignment.assign(qdId, ThriceTruth.FALSE);
    assignment.assign(qeId, ThriceTruth.FALSE);
    Predicate nq = Predicates.getPredicate("_nq", 2, true);
    Atom nqa = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("a")));
    Atom nqb = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("b")));
    Atom nqc = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("c")));
    Atom nqd = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("d")));
    Atom nqe = Atoms.newBasicAtom(nq, Arrays.asList(Terms.newConstant("1"), Terms.newSymbolicConstant("e")));
    int nqaId = atomStore.get(nqa);
    int nqbId = atomStore.get(nqb);
    int nqcId = atomStore.get(nqc);
    int nqdId = atomStore.get(nqd);
    int nqeId = atomStore.get(nqe);
    assignment.growForMaxAtomId();
    assignment.assign(nqaId, ThriceTruth.TRUE);
    assignment.assign(nqbId, ThriceTruth.TRUE);
    assignment.assign(nqcId, ThriceTruth.TRUE);
    assignment.assign(nqdId, ThriceTruth.TRUE);
    assignment.assign(nqeId, ThriceTruth.TRUE);
    Atom pc = parser.parse("p(c).").getFacts().get(0);
    Set<Literal> reasons = grounder.justifyAtom(atomStore.get(pc), assignment);
    assertFalse(reasons.isEmpty());
}
Also used : NaiveGrounder(at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) 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) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Example 15 with TrailAssignment

use of at.ac.tuwien.kr.alpha.core.solver.TrailAssignment in project Alpha by alpha-asp.

the class LiteralInstantiationStrategyTest method defaultLazyGroundingCheckMustBeTrueGroundLiteral.

/**
 * Uses {@link DefaultLazyGroundingInstantiationStrategy} to check the truth
 * (i.e. {@link AssignmentStatus}) of the positive ground literal "p(a)".
 *
 * In this case, the instantiation strategy has an assignment where the atom
 * "p(a)" is assigned ThriceTruth.MBT, so we expect the instantiation strategy
 * to determine that p(a) is TRUE. Furthermore, the stale atom set
 * (used by {@link NaiveGrounder} to clean up atoms that should be deleted from
 * working memory) must stay empty.
 */
@Test
public void defaultLazyGroundingCheckMustBeTrueGroundLiteral() {
    Predicate p = Predicates.getPredicate("p", 1);
    BasicAtom pOfA = Atoms.newBasicAtom(p, Terms.newSymbolicConstant("a"));
    WorkingMemory workingMemory = new WorkingMemory();
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    atomStore.putIfAbsent(pOfA);
    assignment.growForMaxAtomId();
    assignment.assign(atomStore.get(pOfA), ThriceTruth.MBT);
    LinkedHashSet<Atom> staleSet = new LinkedHashSet<>();
    DefaultLazyGroundingInstantiationStrategy strategy = new DefaultLazyGroundingInstantiationStrategy(workingMemory, atomStore, Collections.emptyMap(), false);
    strategy.setStaleWorkingMemoryEntries(staleSet);
    strategy.setCurrentAssignment(assignment);
    AssignmentStatus assignmentStatus = strategy.getTruthForGroundLiteral(Literals.fromAtom(pOfA, true));
    assertEquals(AssignmentStatus.TRUE, assignmentStatus);
    assertTrue(staleSet.isEmpty());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) WorkingMemory(at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) Predicate(at.ac.tuwien.kr.alpha.api.programs.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)25 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)23 AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)20 Test (org.junit.jupiter.api.Test)17 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)11 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)10 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)9 CompiledProgram (at.ac.tuwien.kr.alpha.core.programs.CompiledProgram)9 BeforeEach (org.junit.jupiter.api.BeforeEach)9 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)8 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)7 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)7 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)7 LinkedHashSet (java.util.LinkedHashSet)7 ASPCore2Program (at.ac.tuwien.kr.alpha.api.programs.ASPCore2Program)6 NormalProgram (at.ac.tuwien.kr.alpha.api.programs.NormalProgram)6 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)6 StratifiedEvaluation (at.ac.tuwien.kr.alpha.core.programs.transformation.StratifiedEvaluation)6 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)5 NaiveGrounder (at.ac.tuwien.kr.alpha.core.grounder.NaiveGrounder)5