/// <summary>
        /// Visits the expression.
        /// </summary>
        /// <param name="expression">Expression.</param>
        public Tuple <bool, bool> Visit(EqualsLiteralCNF expression)
        {
            bool positiveCondition = false;
            bool negativeCondition = true;

            Action <ITerm, ITerm> checkObjectFunctionArgument = (argument, secondaryArgument) =>
            {
                ObjectFunctionTerm objectFunction = argument as ObjectFunctionTerm;
                if (objectFunction != null)
                {
                    ITerm assignValue;
                    if (Effects.GroundedObjectFunctionAssignmentEffects.TryGetValue(GroundingManager.GroundAtom(objectFunction.FunctionAtom, ExpressionSubstitution), out assignValue))
                    {
                        positiveCondition = true;
                        if (!(secondaryArgument is ObjectFunctionTerm) && !(assignValue is ObjectFunctionTerm))
                        {
                            bool valueDiffersValueAssign = !GroundingManager.GroundTerm(secondaryArgument, ExpressionSubstitution).Equals(GroundingManager.GroundTerm(assignValue, OperatorSubstitution));
                            if (valueDiffersValueAssign && !expression.IsNegated || !valueDiffersValueAssign && expression.IsNegated)
                            {
                                // surely assigning different value (or assigning exact value with negated equals) -> not relevant
                                negativeCondition = false;
                            }
                        }
                    }
                }
            };

            checkObjectFunctionArgument(expression.LeftArgument, expression.RightArgument);
            checkObjectFunctionArgument(expression.RightArgument, expression.LeftArgument);

            return(Tuple.Create(positiveCondition, negativeCondition));
        }
        /// <summary>
        /// Evaluates the expression.
        /// </summary>
        /// <param name="expression">Expression.</param>
        /// <returns>True if the expression is logically true, false otherwise.</returns>
        public bool Visit(EqualsLiteralCNF expression)
        {
            bool evaluationResult = GroundingManager.GroundTermDeep(expression.LeftArgument, Substitution, ReferenceState).Equals(
                GroundingManager.GroundTermDeep(expression.RightArgument, Substitution, ReferenceState));

            return(!expression.IsNegated == evaluationResult);
        }
Beispiel #3
0
        /// <summary>
        /// Checks the equality of objects.
        /// </summary>
        /// <param name="obj">Object to be checked.</param>
        /// <returns>True if the objects are equal, false otherwise.</returns>
        public override bool Equals(object obj)
        {
            if (obj == this)
            {
                return(true);
            }

            EqualsLiteralCNF other = obj as EqualsLiteralCNF;

            if (other == null)
            {
                return(false);
            }

            return(LeftArgument.Equals(other.LeftArgument) && RightArgument.Equals(other.RightArgument) && IsNegated == other.IsNegated);
        }
Beispiel #4
0
        /// <summary>
        /// Visits the expression.
        /// </summary>
        /// <param name="expression">Expression.</param>
        public IElementCNF Visit(EqualsLiteralCNF expression)
        {
            var objectAssignEffects = Effects.GroundedObjectFunctionAssignmentEffects;

            if (objectAssignEffects.Count == 0)
            {
                return(expression.Clone());
            }

            Func <ITerm, ITerm> transformArgument = null;

            transformArgument = (term) =>
            {
                ObjectFunctionTerm objectFunction = term as ObjectFunctionTerm;
                if (objectFunction != null)
                {
                    ITerm assignmentValue;
                    if (objectAssignEffects.TryGetValue(GroundAtom(objectFunction.FunctionAtom), out assignmentValue))
                    {
                        UsedGroundedFunctions.Add(objectFunction.FunctionAtom);
                        return(transformArgument(assignmentValue));
                    }
                }
                return(term.Clone());
            };

            ITerm transformedLeftTerm  = transformArgument(expression.LeftArgument);
            ITerm transformedRightTerm = transformArgument(expression.RightArgument);

            bool termsEqual = transformedLeftTerm.Equals(transformedRightTerm);

            if ((termsEqual && !expression.IsNegated) || (!termsEqual && expression.IsNegated))
            {
                // exact constant assignment (or unassignment with negated equals) -> positively contributing, i.e. remove
                return(null);
            }

            return(new EqualsLiteralCNF(transformedLeftTerm, transformedRightTerm, expression.IsNegated));
        }
Beispiel #5
0
        /// <summary>
        /// Generates all possible PDDL relative states meeting given CNF conditions (in the form of a list of conjuncts). Lazy generated recursively via yield return.
        /// </summary>
        /// <param name="index">Current index in the conjuncts list.</param>
        /// <param name="conjuncts">List of conjuncts of the CNF conditions.</param>
        /// <param name="result">Current relative state being built.</param>
        /// <returns>All possible PDDL relative states meeting the CNF conditions.</returns>
        private static IEnumerable <IRelativeState> EnumerateRelativeStatesByCNF(int index, List <IConjunctCNF> conjuncts, IRelativeState result)
        {
            if (index == 0)
            {
                // the constructed state can have trailing values from the previous unfinished enumeration!
                result.ClearContent();
            }

            Action <IRelativeState, LiteralCNF> addLiteral = (state, literal) =>
            {
                // Note: At the moment, there is limited support for object and numeric function assignments.
                // For example, numeric comparison literals like (< (numFunc) 5) will be omitted in the resulting relative state.

                PredicateLiteralCNF predicateLiteral = literal as PredicateLiteralCNF;
                if (predicateLiteral != null)
                {
                    if (literal.IsNegated)
                    {
                        state.AddNegatedPredicate(predicateLiteral.PredicateAtom.Clone());
                    }
                    else
                    {
                        state.AddPredicate(predicateLiteral.PredicateAtom.Clone());
                    }
                    return;
                }

                EqualsLiteralCNF equalsLiteral = literal as EqualsLiteralCNF;
                if (equalsLiteral != null)
                {
                    var assignment = equalsLiteral.TryGetObjectFunctionAssignment();
                    if (assignment != null)
                    {
                        if (!literal.IsNegated)
                        {
                            state.AssignObjectFunction(assignment.Item1.FunctionAtom.Clone(), assignment.Item2.NameId);
                        }
                    }
                    return;
                }

                NumericCompareLiteralCNF compareLiteral = literal as NumericCompareLiteralCNF;
                if (compareLiteral != null)
                {
                    var assignment = compareLiteral.TryGetNumericFunctionAssignment();
                    if (assignment != null)
                    {
                        if (!compareLiteral.IsNegated)
                        {
                            state.AssignNumericFunction(assignment.Item1.FunctionAtom.Clone(), assignment.Item2.Value);
                        }
                    }
                }
            };

            Action <IRelativeState, LiteralCNF> removeLiteral = (state, literal) =>
            {
                PredicateLiteralCNF predicateLiteral = literal as PredicateLiteralCNF;
                if (predicateLiteral != null)
                {
                    if (literal.IsNegated)
                    {
                        state.RemoveNegatedPredicate(predicateLiteral.PredicateAtom.Clone());
                    }
                    else
                    {
                        state.RemovePredicate(predicateLiteral.PredicateAtom.Clone());
                    }
                    return;
                }

                EqualsLiteralCNF equalsLiteral = literal as EqualsLiteralCNF;
                if (equalsLiteral != null)
                {
                    var assignment = equalsLiteral.TryGetObjectFunctionAssignment();
                    if (assignment != null)
                    {
                        if (!literal.IsNegated)
                        {
                            state.AssignObjectFunction(assignment.Item1.FunctionAtom.Clone(), ObjectFunctionTerm.UndefinedValue);
                        }
                    }
                    return;
                }

                NumericCompareLiteralCNF compareLiteral = literal as NumericCompareLiteralCNF;
                if (compareLiteral != null)
                {
                    var assignment = compareLiteral.TryGetNumericFunctionAssignment();
                    if (assignment != null)
                    {
                        if (!compareLiteral.IsNegated)
                        {
                            state.AssignNumericFunction(assignment.Item1.FunctionAtom.Clone(), NumericFunction.UndefinedValue);
                        }
                    }
                }
            };

            if (index >= conjuncts.Count)
            {
                yield return((IRelativeState)result.Clone());
            }
            else
            {
                var conjunct = conjuncts[index];

                ClauseCNF clause = conjunct as ClauseCNF;
                if (clause != null)
                {
                    foreach (var literal in clause)
                    {
                        addLiteral(result, literal);

                        foreach (var item in EnumerateRelativeStatesByCNF(index + 1, conjuncts, result))
                        {
                            yield return(item);
                        }

                        removeLiteral(result, literal);
                    }
                }
                else
                {
                    LiteralCNF literal = conjunct as LiteralCNF;
                    Debug.Assert(literal != null);

                    addLiteral(result, literal);

                    foreach (var item in EnumerateRelativeStatesByCNF(index + 1, conjuncts, result))
                    {
                        yield return(item);
                    }

                    removeLiteral(result, literal);
                }
            }
        }
 /// <summary>
 /// Visits and performs a property count on equals expression.
 /// </summary>
 /// <param name="expression">Equals expression.</param>
 /// <returns>Tuple (property satisfied count, property not satisfied count).</returns>
 public Tuple <int, int> Visit(EqualsLiteralCNF expression)
 {
     return(ProcessPrimitiveExpression(expression));
 }
 /// <summary>
 /// Visits and performs a property count on equals expression.
 /// </summary>
 /// <param name="expression">Equals expression.</param>
 /// <returns>Tuple (property satisfied count, property not satisfied count).</returns>
 public Tuple <double, double> Visit(EqualsLiteralCNF expression)
 {
     // this property is not taken into account
     return(Tuple.Create(0.0, 0.0));
 }
 /// <summary>
 /// Evaluates the expression.
 /// </summary>
 /// <param name="expression">Expression.</param>
 public void Visit(EqualsLiteralCNF expression)
 {
 }
 /// <summary>
 /// Evaluates the expression.
 /// </summary>
 /// <param name="expression">Expression.</param>
 public void Visit(EqualsLiteralCNF expression)
 {
     CheckAndRenameTerm(expression.LeftArgument);
     CheckAndRenameTerm(expression.RightArgument);
 }
        /// <summary>
        /// Processes a single CNF literal of operator preconditions.
        /// </summary>
        /// <param name="literal">CNF literal.</param>
        /// <param name="state">Relative state to be applied to.</param>
        private static void ProcessPreconditionLiteral(LiteralCNF literal, IRelativeState state)
        {
            PredicateLiteralCNF predicateLiteral = literal as PredicateLiteralCNF;

            if (predicateLiteral != null)
            {
                if (predicateLiteral.IsNegated)
                {
                    state.AddNegatedPredicate(predicateLiteral.PredicateAtom);
                }
                else
                {
                    state.AddPredicate(predicateLiteral.PredicateAtom);
                }
                return;
            }

            EqualsLiteralCNF equalsLiteral = literal as EqualsLiteralCNF;

            if (equalsLiteral != null)
            {
                ObjectFunctionTerm objFunc   = equalsLiteral.LeftArgument as ObjectFunctionTerm;
                ConstantTerm       constTerm = equalsLiteral.RightArgument as ConstantTerm;

                if (objFunc == null || constTerm == null)
                {
                    objFunc   = equalsLiteral.RightArgument as ObjectFunctionTerm;
                    constTerm = equalsLiteral.LeftArgument as ConstantTerm;
                }

                if (objFunc != null && constTerm != null)
                {
                    if (equalsLiteral.IsNegated)
                    {
                        if (state.GetObjectFunctionValue(objFunc.FunctionAtom) == constTerm.NameId)
                        {
                            state.AssignObjectFunction(objFunc.FunctionAtom, IdManager.InvalidId);
                        }
                    }
                    else
                    {
                        state.AssignObjectFunction(objFunc.FunctionAtom, constTerm.NameId);
                    }
                }
                return;
            }

            NumericCompareLiteralCNF compareLiteral = literal as NumericCompareLiteralCNF;

            if (compareLiteral != null)
            {
                if (compareLiteral.Operator != NumericCompareExpression.RelationalOperator.EQ)
                {
                    return;
                }

                NumericFunction numFunc = compareLiteral.LeftArgument as NumericFunction;
                Number          number  = compareLiteral.RightArgument as Number;

                if (numFunc == null || number == null)
                {
                    numFunc = compareLiteral.RightArgument as NumericFunction;
                    number  = compareLiteral.LeftArgument as Number;
                }

                if (numFunc != null && number != null)
                {
                    if (compareLiteral.IsNegated)
                    {
                        if (state.GetNumericFunctionValue(numFunc.FunctionAtom).Equals(number.Value))
                        {
                            state.AssignNumericFunction(numFunc.FunctionAtom, NumericFunction.DefaultValue);
                        }
                    }
                    else
                    {
                        state.AssignNumericFunction(numFunc.FunctionAtom, number.Value);
                    }
                }
            }
        }