Search in sources :

Example 1 with ComparisonAtom

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());
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 2 with ComparisonAtom

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")));
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) BasicSubstitution(at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) EnumerationLiteral(at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral) Test(org.junit.jupiter.api.Test)

Example 3 with ComparisonAtom

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();
}
Also used : ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) Term(at.ac.tuwien.kr.alpha.api.terms.Term) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm)

Example 4 with ComparisonAtom

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;
}
Also used : FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) ArrayList(java.util.ArrayList) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) Term(at.ac.tuwien.kr.alpha.api.terms.Term) IntervalTerm(at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm) VariableTerm(at.ac.tuwien.kr.alpha.api.terms.VariableTerm) FunctionTerm(at.ac.tuwien.kr.alpha.api.terms.FunctionTerm) ComparisonLiteral(at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) IntervalAtom(at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)

Example 5 with ComparisonAtom

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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) ProgramAnalyzingGrounder(at.ac.tuwien.kr.alpha.core.grounder.ProgramAnalyzingGrounder) NoGood(at.ac.tuwien.kr.alpha.core.common.NoGood) ArrayList(java.util.ArrayList) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) Atom(at.ac.tuwien.kr.alpha.api.programs.atoms.Atom) ComparisonAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom) LinkedHashMap(java.util.LinkedHashMap) Substitution(at.ac.tuwien.kr.alpha.api.grounder.Substitution) CompiledRule(at.ac.tuwien.kr.alpha.core.rules.CompiledRule) Literal(at.ac.tuwien.kr.alpha.api.programs.literals.Literal) Literals.atomToNegatedLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral) Literals.atomToLiteral(at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral) BasicAtom(at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom) RuleAtom(at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)

Aggregations

ComparisonAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.ComparisonAtom)5 Substitution (at.ac.tuwien.kr.alpha.api.grounder.Substitution)3 Literal (at.ac.tuwien.kr.alpha.api.programs.literals.Literal)3 Atom (at.ac.tuwien.kr.alpha.api.programs.atoms.Atom)2 BasicAtom (at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom)2 Term (at.ac.tuwien.kr.alpha.api.terms.Term)2 VariableTerm (at.ac.tuwien.kr.alpha.api.terms.VariableTerm)2 BasicSubstitution (at.ac.tuwien.kr.alpha.commons.substitutions.BasicSubstitution)2 EnumerationLiteral (at.ac.tuwien.kr.alpha.core.atoms.EnumerationLiteral)2 ArrayList (java.util.ArrayList)2 Test (org.junit.jupiter.api.Test)2 ComparisonLiteral (at.ac.tuwien.kr.alpha.api.programs.literals.ComparisonLiteral)1 FunctionTerm (at.ac.tuwien.kr.alpha.api.terms.FunctionTerm)1 IntervalTerm (at.ac.tuwien.kr.alpha.commons.terms.IntervalTerm)1 IntervalAtom (at.ac.tuwien.kr.alpha.core.atoms.IntervalAtom)1 Literals.atomToLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToLiteral)1 Literals.atomToNegatedLiteral (at.ac.tuwien.kr.alpha.core.atoms.Literals.atomToNegatedLiteral)1 RuleAtom (at.ac.tuwien.kr.alpha.core.atoms.RuleAtom)1 NoGood (at.ac.tuwien.kr.alpha.core.common.NoGood)1 ProgramAnalyzingGrounder (at.ac.tuwien.kr.alpha.core.grounder.ProgramAnalyzingGrounder)1