예제 #1
0
        // h^r_add(pi) = sum_(oc in plan) 0 if exists a step possibly preceding oc.step and h_add(oc.precondition) otherwise.
        public static int AddReuseHeuristic(IPlan plan)
        {
            // we are just taking the sum of the visitedPreds values of the open conditions, unless there is a step that establishes the condition already in plan (reuse).
            int sumo = 0;

            foreach (var oc in plan.Flaws.OpenConditions)
            {
                // Does there exist a step in the plan that can establish this needed precondition?
                var existsA = false;
                foreach (var existingStep in plan.Steps)
                {
                    if (existingStep.Height > 0)
                    {
                        continue;
                    }

                    if (plan.Orderings.IsPath(oc.step, existingStep))
                    {
                        continue;
                    }

                    if (CacheMaps.IsCndt(oc.precondition, existingStep))
                    {
                        existsA = true;
                        break;
                    }
                }

                // append heuristic for open condition
                if (!existsA)
                {
                    // we should always have the conditions in the visitedPreds dictionary if we processed correctly
                    if (visitedPreds.Get(oc.precondition.Sign).ContainsKey(oc.precondition))
                    {
                        sumo += visitedPreds.Get(oc.precondition.Sign)[oc.precondition];
                        continue;
                    }

                    throw new System.Exception("visitedPreds does not contain " + oc.precondition.ToString());

                    //currentlyEvaluatedPreds.Add(oc.precondition);
                    //var amountToAdd = AddHeuristic(plan.Initial, oc.precondition, new HashSet<IPredicate>() { oc.precondition });
                    var amountToAdd = AddHeuristic(oc.precondition);
                    sumo += amountToAdd;
                    visitedPreds.Get(oc.precondition.Sign)[oc.precondition] = amountToAdd;
                }
            }
            return(sumo);
        }
예제 #2
0
        public void Add(IPlan plan, OpenCondition oc)
        {
            // Don't add an open condition if it's already in the Flawque.
            if (OpenConditions.Contains(oc))
            {
                return;
            }

            // Predicates are labeled as static before planning
            if (GroundActionFactory.Statics.Contains(oc.precondition))
            {
                oc.isStatic = true;
                OpenConditions.Add(oc);
                return;
            }

            // Calculate risks and cndts
            foreach (var step in plan.Steps)
            {
                if (step.ID == oc.step.ID)
                {
                    continue;
                }

                if (plan.Orderings.IsPath(oc.step, step))
                {
                    continue;
                }

                if (CacheMaps.IsCndt(oc.precondition, step))
                {
                    oc.cndts += 1;
                }

                if (CacheMaps.IsThreat(oc.precondition, step))
                {
                    oc.risks += 1;
                }
            }

            if (oc.risks == 0 && plan.Initial.InState(oc.precondition))
            {
                oc.isInit = true;
            }

            OpenConditions.Add(oc);
        }
예제 #3
0
        // When we add a new step, update cndts and risks (Current never called)
        public void UpdateFlaws(IPlan plan, IPlanStep action)
        {
            if (action.Height > 0)
            {
                //var comp = action as ICompositePlanStep;
                //foreach (var substep in comp.SubSteps)
                //{
                //    if (substep.Height == 0)
                //    {
                //        var stepRef = plan.Find(substep as IPlanStep);
                //        UpdateFlaws(plan, stepRef);
                //    }
                //    else
                //        UpdateFlaws(plan, substep);
                //}
                return;
            }
            else
            {
                foreach (var oc in OpenConditions.ToList())
                {
                    // ignore any open conditions that cannot possibly be affected by this action's effects, such as those occurring after
                    if (plan.Orderings.IsPath(oc.step, action))
                    {
                        continue;
                    }

                    if (CacheMaps.IsCndt(oc.precondition, action))
                    {
                        //if (action.Effects.Contains(oc.precondition))
                        oc.cndts += 1;
                    }

                    if (CacheMaps.IsThreat(oc.precondition, action))
                    {
                        //if (action.Effects.Any(x => oc.precondition.IsInverse(x)))
                        oc.risks += 1;
                    }
                }
            }
        }
예제 #4
0
        // h^r_add(pi) = sum_(oc in plan) 0 if exists a step possibly preceding oc.step and h_add(oc.precondition) otherwise.
        public static int AddReuseHeuristic(IPlan plan)
        {
            int sumo = 0;

            foreach (var oc in plan.Flaws.OpenConditions)
            {
                // Refresh to new list
                currentlyEvaluatedPreds = new List <IPredicate>();

                if (visitedPreds.ContainsKey(oc.precondition))
                {
                    sumo += visitedPreds[oc.precondition];
                    continue;
                }

                // Does there exist a step in the plan that can establish this needed precondition?
                var existsA = false;
                foreach (var existingStep in plan.Steps)
                {
                    if (plan.Orderings.IsPath(oc.step, existingStep))
                    {
                        continue;
                    }

                    if (CacheMaps.IsCndt(oc.precondition, existingStep))
                    {
                        existsA = true;
                        break;
                    }
                }

                // append heuristic for open condition
                if (!existsA)
                {
                    currentlyEvaluatedPreds.Add(oc.precondition);
                    sumo += AddHeuristic(plan.Initial, oc.precondition);
                }
            }
            return(sumo);
        }
예제 #5
0
        // When we add a new step, update cndts and risks
        public void UpdateFlaws(IPlan plan, IPlanStep action)
        {
            foreach (var oc in OpenConditions.ToList())
            {
                // ignore any open conditions that cannot possibly be affected by this action's effects, such as those occurring after
                if (plan.Orderings.IsPath(oc.step, action))
                {
                    continue;
                }

                if (CacheMaps.IsCndt(oc.precondition, action))
                {
                    //if (action.Effects.Contains(oc.precondition))
                    oc.cndts += 1;
                }

                if (CacheMaps.IsThreat(oc.precondition, action))
                {
                    //if (action.Effects.Any(x => oc.precondition.IsInverse(x)))
                    oc.risks += 1;
                }
            }
        }
예제 #6
0
        // h^r_add(pi) = sum_(oc in plan) 0 if exists a step possibly preceding oc.step and h_add(oc.precondition) otherwise.
        public static int AddReuseHeuristic(IPlan plan)
        {
            Logger.LogThingToType("AddReuse", " ", "HeuristicNew");
            Logger.LogThingToType("AddReuse", " ", "HeuristicOld");

            var tuplemapping = new TupleMap <IPredicate, List <IPlanStep> >();
            // var posmapping = new Dictionary<IPredicate, List<int>>();
            //var negmapping = new Dictionary<IPredicate, List<int>>();
            var effList = new List <IPredicate>();
            int sumo    = 0;

            if (plan.Flaws.OpenConditions.Count > plan.Steps.Count)
            {
                var beforePreamble = Logger.Log();
                foreach (var existingStep in plan.Steps)
                {
                    if (existingStep.Height > 0)
                    {
                        continue;
                    }

                    foreach (var eff in existingStep.Effects)
                    {
                        effList.Add(eff);
                        if (!tuplemapping.Get(eff.Sign).ContainsKey(eff))
                        {
                            tuplemapping.Get(eff.Sign)[eff] = new List <IPlanStep>()
                            {
                                existingStep
                            };
                        }
                        else
                        {
                            tuplemapping.Get(eff.Sign)[eff].Add(existingStep);
                        }
                    }
                }

                Logger.LogTimeToType("collectStepsPreamble", Logger.Log() - beforePreamble, "HeuristicNew");

                foreach (var oc in plan.Flaws.OpenConditions)
                {
                    var beforeEachOc = Logger.Log();
                    var existsA      = false;
                    if (effList.Contains(oc.precondition))
                    {
                        foreach (var action in tuplemapping.Get(oc.precondition.Sign)[oc.precondition])
                        {
                            if (plan.Orderings.IsPath(oc.step, action))
                            {
                                continue;
                            }

                            existsA = true;
                            break;
                        }
                    }
                    if (!existsA)
                    {
                        // we should always have the conditions in the visitedPreds dictionary if we processed correctly
                        if (visitedPreds.Get(oc.precondition.Sign).ContainsKey(oc.precondition))
                        {
                            sumo += visitedPreds.Get(oc.precondition.Sign)[oc.precondition];
                            continue;
                        }
                        // Fail gracefully
                        sumo += 100000;
                        //throw new System.Exception("visitedPreds does not contain " + oc.precondition.ToString());
                    }
                    Logger.LogTimeToType("EachOC", Logger.Log() - beforeEachOc, "HeuristicNew");
                }

                return(sumo);
            }


            // we are just taking the sum of the visitedPreds values of the open conditions, unless there is a step that establishes the condition already in plan (reuse).

            foreach (var oc in plan.Flaws.OpenConditions)
            {
                var wayBefore = Logger.Log();
                // Does there exist a step in the plan that can establish this needed precondition?
                var existsA = false;
                foreach (var existingStep in plan.Steps)
                {
                    if (existingStep.Height > 0)
                    {
                        continue;
                    }

                    var before = Logger.Log();
                    if (CacheMaps.IsCndt(oc.precondition, existingStep))
                    {
                        existsA = true;
                        break;
                    }
                    Logger.LogTimeToType("IsCndt", Logger.Log() - before, "HeuristicOld");

                    if (plan.Orderings.IsPath(oc.step, existingStep))
                    {
                        continue;
                    }
                }

                // append heuristic for open condition
                if (!existsA)
                {
                    // we should always have the conditions in the visitedPreds dictionary if we processed correctly
                    if (visitedPreds.Get(oc.precondition.Sign).ContainsKey(oc.precondition))
                    {
                        sumo += visitedPreds.Get(oc.precondition.Sign)[oc.precondition];
                        continue;
                    }
                    sumo += 100000;
                    //throw new System.Exception("visitedPreds does not contain " + oc.precondition.ToString());
                }

                Logger.LogTimeToType("EachOC", Logger.Log() - wayBefore, "HeuristicOld");
            }
            return(sumo);
        }