Example #1
0
        /// <summary>
        /// Extracts a minimal necessary operator substitution needed for the backward application (other parameters remain lifted).
        /// </summary>
        /// <returns>Minimal substitution.</returns>
        public ISubstitution ExtractMinimalOperatorSubstitution()
        {
            // we need to determine, which effects were actually used for the backwards application (i.e. have to be grounded).

            ISubstitution substitution = new Substitution();

            foreach (var predicate in UsedGroundedPredicates)
            {
                IAtom liftedPredicate;
                if (Effects.OriginalLiftedPredicates.TryGetValue(predicate, out liftedPredicate))
                {
                    var unification = liftedPredicate.GetUnificationWith(predicate);
                    substitution.AddLocalSubstitution(unification);
                }
            }

            foreach (var function in UsedGroundedFunctions)
            {
                IAtom liftedFunction;
                if (Effects.OriginalLiftedFunctions.TryGetValue(function, out liftedFunction))
                {
                    var unification = liftedFunction.GetUnificationWith(function);
                    substitution.AddLocalSubstitution(unification);
                }
            }

            return(substitution);
        }
Example #2
0
        /// <summary>
        /// Creates a deep copy of the substitution.
        /// </summary>
        /// <returns>A copy of the substitution.</returns>
        public ISubstitution Clone()
        {
            Substitution newSubstitution = new Substitution();

            foreach (var item in this)
            {
                newSubstitution.Add(item.Key, item.Value);
            }
            return(newSubstitution);
        }
Example #3
0
        /// <summary>
        /// Transforms the term.
        /// </summary>
        /// <param name="term">Term.</param>
        /// <returns>Transformed term.</returns>
        public ITerm Visit(VariableTerm term)
        {
            ConstantID substitutedValue;

            if (Substitution.TryGetValue(term.NameId, out substitutedValue))
            {
                return(new ConstantTerm(substitutedValue, IdManager));
            }
            return(term.Clone());
        }
        /// <summary>
        /// Visits and handles the effect.
        /// </summary>
        /// <param name="effect">Effect.</param>
        public void Visit(ForallEffect effect)
        {
            IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(effect.Parameters);

            foreach (var localSubstitution in localSubstitutions)
            {
                Substitution.AddLocalSubstitution(localSubstitution);
                foreach (var localEffect in effect.Effects)
                {
                    localEffect.Accept(this);
                }
                Substitution.RemoveLocalSubstitution(localSubstitution);
            }
        }
        /// <summary>
        /// Creates a substitution from the given parameters and substituted constants.
        /// </summary>
        /// <param name="parameters">Parameters.</param>
        /// <param name="constNames">Constant names.</param>
        /// <returns>New substitution.</returns>
        public ISubstitution CreateSubstitution(Parameters parameters, params string[] constNames)
        {
            Debug.Assert(parameters.Count == constNames.Length);
            ISubstitution substitution = new Substitution();

            for (int i = 0; i < parameters.Count; ++i)
            {
                if (!string.IsNullOrEmpty(constNames[i]))
                {
                    substitution.Add(parameters[i].ParameterNameId, CreateConstant(constNames[i]));
                }
            }
            return(substitution);
        }
Example #6
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);
            }

            Substitution other = obj as Substitution;

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

            return(CollectionsEquality.Equals(this, other));
        }
        /// <summary>
        /// Visits and evaluates forall expression.
        /// </summary>
        /// <param name="expression">Forall expression.</param>
        /// <returns>True if the specified expression evaluates as true, false otherwise.</returns>
        public bool Visit(ForallExpression expression)
        {
            IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(expression.Parameters);

            foreach (var localSubstitution in localSubstitutions)
            {
                Substitution.AddLocalSubstitution(localSubstitution);
                bool subExpressionResult = expression.Child.Accept(this);
                Substitution.RemoveLocalSubstitution(localSubstitution);

                if (!subExpressionResult)
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Visits and performs a property count on exists expression.
        /// </summary>
        /// <param name="expression">Exists expression.</param>
        /// <returns>Tuple (property satisfied count, property not satisfied count).</returns>
        public Tuple <double, double> Visit(ExistsExpression expression)
        {
            double positiveValue = 0.0;
            double negativeValue = 0.0;

            IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(expression.Parameters);

            foreach (var localSubstitution in localSubstitutions)
            {
                Substitution.AddLocalSubstitution(localSubstitution);
                var childPropertyCounts = expression.Child.Accept(this);
                Substitution.RemoveLocalSubstitution(localSubstitution);

                positiveValue = Math.Max(positiveValue, childPropertyCounts.Item1);
                negativeValue = Math.Max(negativeValue, childPropertyCounts.Item2);
            }

            return(Tuple.Create(positiveValue, negativeValue));
        }
Example #9
0
        /// <summary>
        /// Visits and performs a property count on forall expression.
        /// </summary>
        /// <param name="expression">Forall expression.</param>
        /// <returns>Tuple (property satisfied count, property not satisfied count).</returns>
        public Tuple <int, int> Visit(ForallExpression expression)
        {
            int fulfilled    = 0;
            int notFulfilled = 0;

            IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(expression.Parameters);

            foreach (var localSubstitution in localSubstitutions)
            {
                Substitution.AddLocalSubstitution(localSubstitution);
                var childPropertyCounts = expression.Child.Accept(this);
                Substitution.RemoveLocalSubstitution(localSubstitution);

                fulfilled    += childPropertyCounts.Item1;
                notFulfilled += childPropertyCounts.Item2;
            }

            return(Tuple.Create(fulfilled, notFulfilled));
        }
Example #10
0
        /// <summary>
        /// Visits and performs a property count on exists expression.
        /// </summary>
        /// <param name="expression">Exists expression.</param>
        /// <returns>Tuple (property satisfied count, property not satisfied count).</returns>
        public Tuple <int, int> Visit(ExistsExpression expression)
        {
            int minFulfilled    = int.MaxValue;
            int minNotFulfilled = int.MaxValue;

            IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(expression.Parameters);

            foreach (var localSubstitution in localSubstitutions)
            {
                Substitution.AddLocalSubstitution(localSubstitution);
                var childPropertyCounts = expression.Child.Accept(this);
                Substitution.RemoveLocalSubstitution(localSubstitution);

                minFulfilled    = Math.Min(minFulfilled, childPropertyCounts.Item1);
                minNotFulfilled = Math.Min(minNotFulfilled, childPropertyCounts.Item2);
            }

            return(Tuple.Create(minFulfilled, minNotFulfilled));
        }
Example #11
0
        /// <summary>
        /// Gets the unification with the specified atom, i.e. calculates the variable substitution required to ground variable terms of the
        /// current atom with the grounded terms (i.e. constants) of the other atom.
        /// </summary>
        /// <param name="referenceAtom">Reference atom.</param>
        /// <returns>Unification in the form of variable substitution.</returns>
        public ISubstitution GetUnificationWith(IAtom referenceAtom)
        {
            Debug.Assert(GetNameId() == referenceAtom.GetNameId() && GetTerms().Count == referenceAtom.GetTerms().Count, "Unification of incompatible atoms!");

            ISubstitution unification = new Substitution();

            int termIndex = 0;

            foreach (var term in GetTerms())
            {
                VariableTerm variableTerm = term as VariableTerm;
                if (variableTerm != null)
                {
                    ConstantTerm valueTerm = referenceAtom.GetTerms()[termIndex] as ConstantTerm;
                    if (valueTerm != null)
                    {
                        unification.Add(variableTerm.NameId, valueTerm.NameId);
                    }
                }
                ++termIndex;
            }

            return(unification);
        }
Example #12
0
 /// <summary>
 /// Constructs the counter.
 /// </summary>
 /// <param name="groundingManager">Grounding manager.</param>
 /// <param name="expressionEvaluator">Expression evaluator.</param>
 public NotAccomplishedConstraintsCounter(GroundingManager groundingManager, Lazy <ExpressionEvaluator> expressionEvaluator)
 {
     GroundingManager    = groundingManager;
     ExpressionEvaluator = expressionEvaluator;
     Substitution        = new Substitution();
 }