Search in sources :

Example 6 with WritableAssignment

use of at.ac.tuwien.kr.alpha.core.solver.WritableAssignment 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)

Example 7 with WritableAssignment

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

the class LiteralInstantiationStrategyTest method defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance.

/**
 * 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 empty assignment, so we
 * expect the assignment status (i.e. assignment status of the found ground
 * instance) passed back with the substitution to be UNASSIGNED. Since
 * UNASSIGNED and FALSE atoms are (potentially) stale in working memory, we
 * expect the atom "q(a, b)" to be added to the stale set by the instantiation
 * strategy.
 */
@Test
public void defaultLazyGroundingSubstituteNonGroundLiteralWithUnassignedInstance() {
    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);
    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.UNASSIGNED, assignmentStatus);
    assertTrue(substitution.isVariableSet(Terms.newVariable("X")));
    assertEquals(Terms.newSymbolicConstant("b"), substitution.eval(Terms.newVariable("X")));
    assertEquals(1, staleSet.size());
    assertTrue(staleSet.contains(Atoms.newBasicAtom(q, Terms.newSymbolicConstant("a"), Terms.newSymbolicConstant("b"))));
}
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 8 with WritableAssignment

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

the class BranchingHeuristicFactoryTest method setUp.

@BeforeEach
public void setUp() {
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    NoGoodStore store = new NoGoodStoreAlphaRoaming(assignment, debugInternalChecks);
    this.choiceManager = new ChoiceManager(assignment, store);
}
Also used : ChoiceManager(at.ac.tuwien.kr.alpha.core.solver.ChoiceManager) AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGoodStoreAlphaRoaming(at.ac.tuwien.kr.alpha.core.solver.NoGoodStoreAlphaRoaming) NoGoodStore(at.ac.tuwien.kr.alpha.core.solver.NoGoodStore) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with WritableAssignment

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

the class ReplayHeuristicTest method setUp.

@BeforeEach
public void setUp() {
    AtomStore atomStore = new AtomStoreImpl();
    WritableAssignment assignment = new TrailAssignment(atomStore);
    NoGoodStore store = new NoGoodStoreAlphaRoaming(assignment, debugInternalChecks);
    this.choiceManager = new PseudoChoiceManager(assignment, store);
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) NoGoodStoreAlphaRoaming(at.ac.tuwien.kr.alpha.core.solver.NoGoodStoreAlphaRoaming) NoGoodStore(at.ac.tuwien.kr.alpha.core.solver.NoGoodStore) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 10 with WritableAssignment

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

the class BerkMinTest method setUp.

@BeforeEach
public void setUp() {
    AtomStore atomStore = new AtomStoreImpl();
    TestUtils.fillAtomStore(atomStore, 2);
    WritableAssignment assignment = new TrailAssignment(atomStore);
    assignment.growForMaxAtomId();
    this.berkmin = new BerkMin(assignment, new PseudoChoiceManager(assignment, new NaiveNoGoodStore(assignment)), new Random());
}
Also used : AtomStore(at.ac.tuwien.kr.alpha.core.common.AtomStore) Random(java.util.Random) NaiveNoGoodStore(at.ac.tuwien.kr.alpha.core.solver.NaiveNoGoodStore) AtomStoreImpl(at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl) WritableAssignment(at.ac.tuwien.kr.alpha.core.solver.WritableAssignment) TrailAssignment(at.ac.tuwien.kr.alpha.core.solver.TrailAssignment) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

AtomStore (at.ac.tuwien.kr.alpha.core.common.AtomStore)10 AtomStoreImpl (at.ac.tuwien.kr.alpha.core.common.AtomStoreImpl)10 TrailAssignment (at.ac.tuwien.kr.alpha.core.solver.TrailAssignment)10 WritableAssignment (at.ac.tuwien.kr.alpha.core.solver.WritableAssignment)10 Predicate (at.ac.tuwien.kr.alpha.api.programs.Predicate)7 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)7 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)7 WorkingMemory (at.ac.tuwien.kr.alpha.core.grounder.WorkingMemory)7 LinkedHashSet (java.util.LinkedHashSet)7 Test (org.junit.jupiter.api.Test)7 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)3 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)2 NoGoodStore (at.ac.tuwien.kr.alpha.core.solver.NoGoodStore)2 NoGoodStoreAlphaRoaming (at.ac.tuwien.kr.alpha.core.solver.NoGoodStoreAlphaRoaming)2 ChoiceManager (at.ac.tuwien.kr.alpha.core.solver.ChoiceManager)1 NaiveNoGoodStore (at.ac.tuwien.kr.alpha.core.solver.NaiveNoGoodStore)1 Random (java.util.Random)1