Esempio n. 1
0
        /// <summary>
        /// Given a primary effect (one that is not the effect of a primitive step), calculate heuristic value.
        /// Let that heuristic value be the shortest (height) step that can contribute, plus all of its preconditions.
        /// Recursively, if any of its preconditions are primary effects, then repeat until we have either a step that is true in the initial state or has no primary effects as preconditions.
        /// </summary>
        /// <param name="InitialState"></param>
        /// <param name="primaryEffect"></param>
        /// <returns></returns>
        public static void PrimaryEffectHack(IState InitialState)
        {
            var initialMap = new TupleMap <IPredicate, int>();
            var primaryEffectsInInitialState = new List <IPredicate>();

            foreach (var item in InitialState.Predicates)
            {
                if (IsPrimaryEffect(item))
                {
                    primaryEffectsInInitialState.Add(item);
                    initialMap.Get(item.Sign)[item] = 0;
                }
            }

            var heurDict = PrimaryEffectRecursiveHeuristicCache(initialMap, primaryEffectsInInitialState);

            foreach (var keyvalue in heurDict.Get(true))
            {
                HeuristicMethods.visitedPreds.Get(true)[keyvalue.Key] = keyvalue.Value;
            }
            foreach (var keyvalue in heurDict.Get(false))
            {
                HeuristicMethods.visitedPreds.Get(false)[keyvalue.Key] = keyvalue.Value;
            }
        }
Esempio n. 2
0
        //public static Dictionary<IPredicate, List<int>> PosCausalMap = new Dictionary<IPredicate, List<int>>();
        //public static Dictionary<IPredicate, List<int>> NegCausalMap = new Dictionary<IPredicate, List<int>>();

        ///// <summary>
        ///// Stored mappings for threat detection. Do Not Use unless you are serializing
        ///// </summary>
        //public static Dictionary<IPredicate, List<int>> PosThreatMap = new Dictionary<IPredicate, List<int>>();
        //public static Dictionary<IPredicate, List<int>> NegThreatMap = new Dictionary<IPredicate, List<int>>();

        public static void Reset()
        {
            CausalTupleMap = new TupleMap <IPredicate, List <int> >();
            ThreatTupleMap = new TupleMap <IPredicate, List <int> >();
            //PosCausalMap = new Dictionary<IPredicate, List<int>>();
            //NegCausalMap = new Dictionary<IPredicate, List<int>>();
            //PosThreatMap = new Dictionary<IPredicate, List<int>>();
            //NegThreatMap = new Dictionary<IPredicate, List<int>>();
        }
Esempio n. 3
0
 public static void DetectStatics(TupleMap <IPredicate, List <int> > CMap, TupleMap <IPredicate, List <int> > TMap)
 {
     foreach (var op in GroundActions)
     {
         foreach (var pre in op.Preconditions)
         {
             if (Statics.Contains(pre))
             {
                 continue;
             }
             if (!CMap.Get(pre.Sign).ContainsKey(pre) && !TMap.Get(pre.Sign).ContainsKey(pre))
             {
                 Statics.Add(pre);
             }
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Given a primary effect (one that is not the effect of a primitive step), calculate heuristic value.
        /// Let that heuristic value be the shortest (height) step that can contribute, plus all of its preconditions.
        /// Recursively, if any of its preconditions are primary effects, then repeat until we have either a step that is true in the initial state or has no primary effects as preconditions.
        /// </summary>
        /// <param name="InitialState"></param>
        /// <param name="primaryEffect"></param>
        /// <returns></returns>
        public static void PrimaryEffectHack(IState InitialState)
        {
            // Finds primary effects: for each composite step, if its precondition has no visitedPreds value.
            CalculatePrimaryEffects(InitialState);

            var initialMap = new TupleMap <IPredicate, int>();
            var primaryEffectsInInitialState = new List <IPredicate>();

            foreach (var primaryeff in PrimaryEffects)
            {
                if (InitialState.InState(primaryeff))
                {
                    primaryEffectsInInitialState.Add(primaryeff);
                    initialMap.Get(primaryeff.Sign)[primaryeff] = 0;
                }
            }
            //foreach (var item in InitialState.Predicates)
            //{
            //    if (IsPrimaryEffect(item))
            //    {
            //        primaryEffectsInInitialState.Add(item);
            //        initialMap.Get(item.Sign)[item] = 0;
            //    }
            //    var reversedItem = item.GetReversed();
            //    if (IsPrimaryEffect(reversedItem))
            //    {
            //        primaryEffectsInInitialState.Add(reversedItem);
            //        initialMap.Get(reversedItem.Sign)[reversedItem] = 0;
            //    }
            //}

            var heurDict = PrimaryEffectRecursiveHeuristicCache(initialMap, primaryEffectsInInitialState);

            foreach (var keyvalue in heurDict.Get(true))
            {
                HeuristicMethods.visitedPreds.Get(true)[keyvalue.Key] = keyvalue.Value;
            }
            foreach (var keyvalue in heurDict.Get(false))
            {
                HeuristicMethods.visitedPreds.Get(false)[keyvalue.Key] = keyvalue.Value;
            }
        }
Esempio n. 5
0
        private static TupleMap <IPredicate, int> RecursiveHeuristicCache(TupleMap <IPredicate, int> currentMap, List <IPredicate> InitialConditions)
        {
            // Steps that are executable given the initial conditions. These conditions can represent a state that is logically inconsistent (and (at bob store) (not (at bob store)))
            var initiallyRelevant = GroundActionFactory.GroundActions.Where(action => action.Height == 0 && action.Preconditions.All(pre => InitialConditions.Contains(pre)));

            // a boolean tag to decide whether to continue recursively. If checked, then there is some new effect that isn't in initial conditions.
            bool toContinue = false;

            // for each step whose preconditions are executable given the initial conditions
            foreach (var newStep in initiallyRelevant)
            {
                // sum_{pre in newstep.preconditions} currentMap[pre]
                var thisStepsValue = newStep.Preconditions.Sum(pre => currentMap.Get(pre.Sign)[pre]);

                foreach (var eff in newStep.Effects)
                {
                    // ignore effects we've already seen; these occur "earlier" in planning graph
                    if (currentMap.Get(eff.Sign).ContainsKey(eff))
                    {
                        continue;
                    }

                    // If we make it this far, then we've reached an unexplored literal effect
                    toContinue = true;

                    // The current value of this effect is 1 (this new step) + the sum of the preconditions of this step in the map.
                    currentMap.Get(eff.Sign)[eff] = 1 + thisStepsValue;

                    // Add this effect to the new initial Condition for subsequent round
                    InitialConditions.Add(eff);
                }
            }

            // Only continue recursively if we've explored a new literal effect. Pass the map along to maintain a global item.
            if (toContinue)
            {
                return(RecursiveHeuristicCache(currentMap, InitialConditions));
            }

            // Otherwise, return our current map
            return(currentMap);
        }
Esempio n. 6
0
        public static void CacheAddReuseHeuristic(IState InitialState)
        {
            // Use dynamic programming
            var initialMap = new TupleMap <IPredicate, int>();

            //var initialMap = new Dictionary<IPredicate, int>();
            foreach (var item in InitialState.Predicates)
            {
                initialMap.Get(true)[item] = 0;
            }
            List <IPredicate> newInitialList = InitialState.Predicates;

            foreach (var pre in CacheMaps.CausalTupleMap.Get(false).Keys)
            {
                if (!newInitialList.Contains(pre.GetReversed()))
                {
                    newInitialList.Add(pre);
                    initialMap.Get(false)[pre] = 0;
                }
            }

            HeuristicMethods.visitedPreds = RecursiveHeuristicCache(initialMap, newInitialList);
        }
Esempio n. 7
0
        private static TupleMap <IPredicate, int> PrimaryEffectRecursiveHeuristicCache(TupleMap <IPredicate, int> currentMap, List <IPredicate> InitialConditions)
        {
            var initiallyRelevant = new List <IOperator>();
            var CompositeOps      = GroundActionFactory.GroundActions.Where(act => act.Height > 0);

            foreach (var compOp in CompositeOps)
            {
                var initiallySupported = true;
                foreach (var precond in compOp.Preconditions)
                {
                    if (IsPrimaryEffect(precond))
                    {
                        // then this is a primary effect.
                        if (!InitialConditions.Contains(precond))
                        {
                            initiallySupported = false;
                            break;
                        }
                    }
                }
                if (initiallySupported)
                {
                    initiallyRelevant.Add(compOp);
                }
            }

            // a boolean tag to decide whether to continue recursively. If checked, then there is some new effect that isn't in initial conditions.
            bool toContinue = false;

            // for each step whose preconditions are executable given the initial conditions
            foreach (var newStep in initiallyRelevant)
            {
                // sum_{pre in newstep.preconditions} currentMap[pre]
                int thisStepsValue = 0;
                foreach (var precon in newStep.Preconditions)
                {
                    if (IsPrimaryEffect(precon))
                    {
                        thisStepsValue += currentMap.Get(precon.Sign)[precon];
                    }
                    else
                    {
                        thisStepsValue += HeuristicMethods.visitedPreds.Get(precon.Sign)[precon];
                    }
                }

                foreach (var eff in newStep.Effects)
                {
                    if (!IsPrimaryEffect(eff))
                    {
                        continue;
                    }

                    // ignore effects we've already seen; these occur "earlier" in planning graph
                    if (currentMap.Get(eff.Sign).ContainsKey(eff))
                    {
                        continue;
                    }

                    // If we make it this far, then we've reached an unexplored literal effect
                    toContinue = true;

                    // The current value of this effect is 1 (this new step) + the sum of the preconditions of this step in the map.
                    currentMap.Get(eff.Sign)[eff] = 1 + thisStepsValue;

                    // Add this effect to the new initial Condition for subsequent round
                    InitialConditions.Add(eff);
                }
            }

            // Only continue recursively if we've explored a new literal effect. Pass the map along to maintain a global item.
            if (toContinue)
            {
                return(PrimaryEffectRecursiveHeuristicCache(currentMap, InitialConditions));
            }

            // Otherwise, return our current map
            return(currentMap);
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
 public static void Reset()
 {
     CausalTupleMap = new TupleMap <IPredicate, List <int> >();
     ThreatTupleMap = new TupleMap <IPredicate, List <int> >();
 }