/// <summary> /// Processes forall effects. /// </summary> /// <param name="expression">Conditions expression in CNF.</param> /// <returns>Tuple of two values, where the first is true when the positive relevance condition (inclusion) is satisfied, while the second is /// true when the negative condition (exclusion) is not violated. False otherwise.</returns> private Tuple <bool, bool> ProcessForallEffects(ConditionsCNF expression) { bool positiveCondition = false; bool negativeCondition = true; foreach (var forallEffect in Effects.ForallEffects) { EffectsRelevanceConditionsEvaluator evaluator = new EffectsRelevanceConditionsEvaluator(forallEffect.Effects, GroundingManager); IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(forallEffect.Parameters); foreach (var localSubstitution in localSubstitutions) { OperatorSubstitution.AddLocalSubstitution(localSubstitution); var result = evaluator.EvaluateWithExtendedResult(expression, OperatorSubstitution, null); positiveCondition |= result.Item1; negativeCondition &= result.Item2; OperatorSubstitution.RemoveLocalSubstitution(localSubstitution); if (!negativeCondition) { return(result); } } } return(Tuple.Create(positiveCondition, true)); }
/// <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 <double, double> Visit(ForallExpression expression) { double positiveValue = 0; double negativeValue = 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); if (EvaluationStrategy == ForwardCostEvaluationStrategy.ADDITIVE_VALUE) { positiveValue += childPropertyCounts.Item1; negativeValue += childPropertyCounts.Item2; } else { positiveValue = Math.Max(positiveValue, childPropertyCounts.Item1); negativeValue = Math.Max(negativeValue, childPropertyCounts.Item2); } } return(Tuple.Create(positiveValue, negativeValue)); }
/// <summary> /// Processes forall effects. /// </summary> /// <param name="relativeState">Relative state.</param> /// <returns>Effect relevance (relevant, irrelevant, or anti-relevant).</returns> private EffectRelevance ProcessForallEffects(IRelativeState relativeState) { bool anyRelevant = false; foreach (var forallEffect in Effects.ForallEffects) { EffectsRelevanceRelativeStateEvaluator evaluator = new EffectsRelevanceRelativeStateEvaluator(forallEffect.Effects, GroundingManager); IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(forallEffect.Parameters); foreach (var localSubstitution in localSubstitutions) { OperatorSubstitution.AddLocalSubstitution(localSubstitution); var result = evaluator.EvaluateInternal(relativeState, OperatorSubstitution); anyRelevant |= (result == EffectRelevance.RELEVANT); OperatorSubstitution.RemoveLocalSubstitution(localSubstitution); if (result == EffectRelevance.ANTI_RELEVANT) { return(EffectRelevance.ANTI_RELEVANT); } } } return((anyRelevant) ? EffectRelevance.RELEVANT : EffectRelevance.IRRELEVANT); }
/// <summary> /// Applies backwards the relevant operator effects and operator preconditions to the given target conditions. /// </summary> /// <param name="conditions">Target conditions to be modified.</param> /// <param name="operatorSubstitution">Variables substitution.</param> /// <returns>Preceding conditions.</returns> public IConditions ApplyBackwards(IConditions conditions, ISubstitution operatorSubstitution) { ConditionsCNF expression = (ConditionsCNF)conditions?.GetCNF(); if (expression == null) { return(null); } OperatorSubstitution = operatorSubstitution; Effects.GroundEffectsByCurrentOperatorSubstitution(GroundingManager, operatorSubstitution); if (expression.Parameters != null) { EffectsRelevanceConditionsEvaluator evaluator = new EffectsRelevanceConditionsEvaluator(Effects, GroundingManager); List <ConditionsCNF> subResults = new List <ConditionsCNF>(); IEnumerable <ISubstitution> expressionSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(expression.Parameters); expression.Parameters = null; foreach (var expressionSubstitution in expressionSubstitutions) { // do backward apply only when the effects are relevant to the current grounding of the expression if (evaluator.Evaluate(expression, operatorSubstitution, expressionSubstitution)) { subResults.Add(ApplyBackwardsImpl(expression, expressionSubstitution)); } } return(ConstructCNFFromDisjunction(subResults)); } return(ApplyBackwardsImpl(expression, new Substitution())); }
/// <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> /// Auxiliary function for transforming the argument sub-expression of a quantified expression. The expression is grounded /// by all possible substitutions and each of the resulting expression is recursively transformed. /// </summary> /// <param name="parameters">Quantified expression parameters.</param> /// <param name="expression">Argument sub-expression of the quantified expression.</param> /// <returns>List of all possible grounded and transformed sub-expressions.</returns> private List <IExpression> TransformQuantifiedSubExpression(Parameters parameters, IExpression expression) { List <IExpression> arguments = new List <IExpression>(); IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(parameters); foreach (var localSubstitution in localSubstitutions) { var groundedSubExpression = GroundingManager.GroundExpression(expression, localSubstitution); arguments.Add(groundedSubExpression.Accept(this)); } return(arguments); }
/// <summary> /// Gets a collection of all possible successors (forward transitions) from the specified state. Lazy generated via yield return. /// </summary> /// <param name="state">Original state.</param> /// <returns>Lazy generated collection of successors.</returns> public IEnumerable <ISuccessor> GetSuccessors(IState state) { foreach (var liftedOperator in LiftedOperators) { IEnumerable <ISubstitution> operatorSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(liftedOperator.Parameters); foreach (var operatorSubstitution in operatorSubstitutions) { if (liftedOperator.IsApplicable(state, operatorSubstitution)) { yield return(new Successor(state, new Operator(liftedOperator, operatorSubstitution))); } } } }
/// <summary> /// Gets a collection of all relevant predecessors (backwards transitions) from the specified relative state. Lazy generated via yield return. /// </summary> /// <param name="relativeState">Original state.</param> /// <returns>Lazy generated collection of relevant predecessors.</returns> public IEnumerable <IPredecessor> GetPredecessors(IRelativeState relativeState) { foreach (var liftedOperator in LiftedOperators) { IEnumerable <ISubstitution> operatorSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(liftedOperator.Parameters); foreach (var operatorSubstitution in operatorSubstitutions) { if (liftedOperator.IsRelevant(relativeState, operatorSubstitution)) { yield return(new Predecessor(relativeState, new Operator(liftedOperator, operatorSubstitution))); } } } }
/// <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)); }
/// <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)); }
/// <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)); }
/// <summary> /// Processes forall effects. /// </summary> /// <param name="expression">Conditions expression in CNF.</param> private ConditionsCNF ProcessForallEffects(ConditionsCNF expression) { if (Effects.ForallEffects.Count == 0) { return(expression); } foreach (var forallEffect in Effects.ForallEffects) { EffectsBackwardsConditionsApplier innerApplier = new EffectsBackwardsConditionsApplier(null, forallEffect.Effects, EvaluationManager); IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(forallEffect.Parameters); foreach (var localSubstitution in localSubstitutions) { OperatorSubstitution.AddLocalSubstitution(localSubstitution); expression = (ConditionsCNF)innerApplier.ApplyBackwards(expression, OperatorSubstitution); OperatorSubstitution.RemoveLocalSubstitution(localSubstitution); } } return(expression); }
/// <summary> /// Processes forall effects. /// </summary> /// <param name="relativeState">Relative state to be modified.</param> /// <returns>Possibly modified relative state.</returns> private IRelativeState ProcessForallEffects(IRelativeState relativeState) { if (Effects.ForallEffects.Count == 0) { return(relativeState); } foreach (var forallEffect in Effects.ForallEffects) { EffectsBackwardsRelativeStateApplier innerApplier = new EffectsBackwardsRelativeStateApplier(null, forallEffect.Effects, EvaluationManager); IEnumerable <ISubstitution> localSubstitutions = GroundingManager.GenerateAllLocalSubstitutions(forallEffect.Parameters); foreach (var localSubstitution in localSubstitutions) { OperatorSubstitution.AddLocalSubstitution(localSubstitution); relativeState = (IRelativeState)innerApplier.ApplyBackwards(relativeState, OperatorSubstitution).First(); OperatorSubstitution.RemoveLocalSubstitution(localSubstitution); } } return(relativeState); }