use of at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method instantiateUnsatisfiedFixedInterpretationLiteral.
@Test
public void instantiateUnsatisfiedFixedInterpretationLiteral() {
ComparisonAtom fiveEqualsThree = Atoms.newComparisonAtom(Terms.newVariable("FIVE"), Terms.newVariable("THREE"), ComparisonOperators.EQ);
Literal lit = Literals.fromAtom(fiveEqualsThree, true);
Substitution substitution = new BasicSubstitution();
substitution.put(Terms.newVariable("FIVE"), Terms.newConstant(5));
substitution.put(Terms.newVariable("THREE"), Terms.newConstant(3));
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(null));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
assertEquals(LiteralInstantiationResult.Type.STOP_BINDING, result.getType());
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom in project Alpha by alpha-asp.
the class LiteralInstantiatorTest method instantiateSatisfiedFixedInterpretationLiteral.
@Test
public void instantiateSatisfiedFixedInterpretationLiteral() {
ComparisonAtom equalsThree = Atoms.newComparisonAtom(Terms.newConstant(3), Terms.newVariable("THREE"), ComparisonOperators.EQ);
Literal lit = Literals.fromAtom(equalsThree, true);
Substitution substitution = new BasicSubstitution();
LiteralInstantiator instantiator = new LiteralInstantiator(new WorkingMemoryBasedInstantiationStrategy(null));
LiteralInstantiationResult result = instantiator.instantiateLiteral(lit, substitution);
assertEquals(LiteralInstantiationResult.Type.CONTINUE, result.getType());
List<ImmutablePair<Substitution, AssignmentStatus>> resultSubstitutions = result.getSubstitutions();
assertEquals(1, resultSubstitutions.size());
assertEquals(AssignmentStatus.TRUE, resultSubstitutions.get(0).right);
Substitution extendedSubstitution = resultSubstitutions.get(0).left;
assertTrue(extendedSubstitution.isVariableSet(Terms.newVariable("THREE")));
assertEquals(Terms.newConstant(3), extendedSubstitution.eval(Terms.newVariable("THREE")));
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom in project Alpha by alpha-asp.
the class AggregateOperatorNormalization method createPlusOneTerm.
/**
* Creates a new {@link Literal} that assigns the given target variable to the given (integer) term plus one.
*
* @param term
* @param targetVariable
* @return
*/
private static Literal createPlusOneTerm(Term term, VariableTerm targetVariable) {
Term increment = Terms.newArithmeticTerm(term, ArithmeticOperator.PLUS, Terms.newConstant(1));
ComparisonAtom atom = Atoms.newComparisonAtom(targetVariable, increment, ComparisonOperators.EQ);
return atom.toLiteral();
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom in project Alpha by alpha-asp.
the class IntervalTermToIntervalAtom method rewriteLiteral.
/**
* Replaces every IntervalTerm by a new variable and returns a mapping of the replaced VariableTerm -> IntervalTerm.
*
* @return the rewritten literal or null if the literal should be dropped from the final rule.
*/
private static Literal rewriteLiteral(Literal lit, Map<VariableTerm, IntervalTerm> intervalReplacement) {
// final rule.
if (lit instanceof ComparisonLiteral && ((ComparisonLiteral) lit).isNormalizedEquality()) {
ComparisonAtom equalityLiteral = (ComparisonAtom) lit.getAtom();
if (equalityLiteral.getTerms().get(0) instanceof VariableTerm && equalityLiteral.getTerms().get(1) instanceof IntervalTerm) {
// Literal is of the form "X = A .. B".
intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(0), (IntervalTerm) equalityLiteral.getTerms().get(1));
return null;
}
if (equalityLiteral.getTerms().get(1) instanceof VariableTerm && equalityLiteral.getTerms().get(0) instanceof IntervalTerm) {
// Literal is of the form "A .. B = X".
intervalReplacement.put((VariableTerm) equalityLiteral.getTerms().get(1), (IntervalTerm) equalityLiteral.getTerms().get(0));
return null;
}
}
Atom atom = lit.getAtom();
List<Term> termList = new ArrayList<>(atom.getTerms());
boolean didChange = false;
for (int i = 0; i < termList.size(); i++) {
Term term = termList.get(i);
if (term instanceof IntervalTerm) {
VariableTerm replacementVariable = Terms.newVariable(INTERVAL_VARIABLE_PREFIX + intervalReplacement.size());
intervalReplacement.put(replacementVariable, (IntervalTerm) term);
termList.set(i, replacementVariable);
didChange = true;
}
if (term instanceof FunctionTerm) {
// Rewrite function terms recursively.
FunctionTerm rewrittenFunctionTerm = rewriteFunctionTerm((FunctionTerm) term, intervalReplacement);
termList.set(i, rewrittenFunctionTerm);
didChange = true;
}
}
if (didChange) {
Atom rewrittenAtom = atom.withTerms(termList);
return lit.isNegated() ? rewrittenAtom.toLiteral().negate() : rewrittenAtom.toLiteral();
}
return lit;
}
use of at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom in project Alpha by alpha-asp.
the class DefaultSolver method treatConflictAfterClosing.
private boolean treatConflictAfterClosing(Antecedent violatedNoGood) {
if (disableJustificationAfterClosing || disableJustifications || !(grounder instanceof ProgramAnalyzingGrounder)) {
// Will not learn from violated NoGood, do simple backtrack.
LOGGER.debug("NoGood was violated after all unassigned atoms were assigned to false; will not learn from it; skipping.");
if (!backtrack()) {
logStats();
return false;
}
return true;
}
ProgramAnalyzingGrounder analyzingGrounder = (ProgramAnalyzingGrounder) grounder;
LOGGER.debug("Justifying atoms in violated nogood.");
LinkedHashSet<Integer> toJustify = new LinkedHashSet<>();
// Find those literals in violatedNoGood that were just assigned false.
for (Integer literal : violatedNoGood.getReasonLiterals()) {
if (assignment.getImpliedBy(atomOf(literal)) == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
toJustify.add(literal);
}
}
// Since the violatedNoGood may contain atoms other than BasicAtom, these have to be treated.
Map<Integer, NoGood> obtained = new LinkedHashMap<>();
Iterator<Integer> toJustifyIterator = toJustify.iterator();
ArrayList<Integer> ruleAtomReplacements = new ArrayList<>();
while (toJustifyIterator.hasNext()) {
Integer literal = toJustifyIterator.next();
Atom atom = atomStore.get(atomOf(literal));
if (atom instanceof BasicAtom) {
continue;
}
if (!(atom instanceof RuleAtom)) {
// Ignore atoms other than RuleAtom.
toJustifyIterator.remove();
continue;
}
// For RuleAtoms in toJustify the corresponding ground body contains BasicAtoms that have been assigned FALSE in the closing.
// First, translate RuleAtom back to NonGroundRule + Substitution.
String ruleId = (String) ((ConstantTerm<?>) atom.getTerms().get(0)).getObject();
CompiledRule nonGroundRule = analyzingGrounder.getNonGroundRule(Integer.parseInt(ruleId));
String substitution = (String) ((ConstantTerm<?>) atom.getTerms().get(1)).getObject();
Substitution groundingSubstitution = Substitutions.fromString(substitution);
// Find ground literals in the body that have been assigned false and justify those.
for (Literal bodyLiteral : nonGroundRule.getBody()) {
Atom groundAtom = bodyLiteral.getAtom().substitute(groundingSubstitution);
if (groundAtom instanceof ComparisonAtom || analyzingGrounder.isFact(groundAtom)) {
// Facts and ComparisonAtoms are always true, no justification needed.
continue;
}
int groundAtomId = atomStore.get(groundAtom);
Antecedent impliedBy = assignment.getImpliedBy(groundAtomId);
// Check if atom was assigned to FALSE during the closing.
if (impliedBy == TrailAssignment.CLOSING_INDICATOR_ANTECEDENT) {
ruleAtomReplacements.add(atomToNegatedLiteral(groundAtomId));
}
}
toJustifyIterator.remove();
}
toJustify.addAll(ruleAtomReplacements);
for (Integer literalToJustify : toJustify) {
LOGGER.debug("Searching for justification(s) of {} / {}", toJustify, atomStore.atomToString(atomOf(literalToJustify)));
Set<Literal> reasonsForUnjustified = analyzingGrounder.justifyAtom(atomOf(literalToJustify), assignment);
NoGood noGood = noGoodFromJustificationReasons(atomOf(literalToJustify), reasonsForUnjustified);
int noGoodID = grounder.register(noGood);
obtained.put(noGoodID, noGood);
LOGGER.debug("Learned NoGood is: {}", atomStore.noGoodToString(noGood));
}
// Backtrack to remove the violation.
if (!backtrack()) {
logStats();
return false;
}
// Add newly obtained noGoods.
if (!ingest(obtained)) {
logStats();
return false;
}
return true;
}
Aggregations