public static List <CausalLink <IPlanStep> > CausalLinksFromJsonArray(JsonArray jsoncausallinkslist)
        {
            var clinks = new List <CausalLink <IPlanStep> >();

            foreach (var jsonlink in jsoncausallinkslist)
            {
                var       clink = CausalLinkFromJsonArray(jsonlink as JsonArray);
                IPlanStep head;
                if (clink.Head.Height > 0)
                {
                    head = new CompositePlanStep(clink.Head as IComposite);
                }
                else
                {
                    head = new PlanStep(clink.Head as IOperator);
                }
                IPlanStep tail;
                if (clink.Tail.Height > 0)
                {
                    tail = new CompositePlanStep(clink.Tail as IComposite);
                }
                else
                {
                    tail = new PlanStep(clink.Tail as IOperator);
                }
                var newLink = new CausalLink <IPlanStep>(clink.Predicate, head, tail);
                clinks.Add(newLink);
            }
            return(clinks);
        }
        public static List <Tuple <IPlanStep, IPlanStep> > SubOrderingsFromJson(JsonArray suborderingsjson)
        {
            var tupleList = new List <Tuple <IPlanStep, IPlanStep> >();

            foreach (var jsonTuple in suborderingsjson)
            {
                var       tupleItem = IntTupleFromJsonArray(jsonTuple as JsonArray);
                IPlanStep head;
                if (tupleItem.First.Height > 0)
                {
                    head = new CompositePlanStep(tupleItem.First as IComposite);
                }
                else
                {
                    head = new PlanStep(tupleItem.First as IOperator);
                }
                IPlanStep tail;
                if (tupleItem.Second.Height > 0)
                {
                    tail = new CompositePlanStep(tupleItem.Second as IComposite);
                }
                else
                {
                    tail = new PlanStep(tupleItem.Second as IOperator);
                }
                var modTuple = new Tuple <IPlanStep, IPlanStep>(head, tail);
                tupleList.Add(modTuple);
            }
            return(tupleList);
        }
        public static List <IPlanStep> SubStepsFromJson(JsonArray substepjson)
        {
            var substeps = new List <IPlanStep>();

            // each item is another list of form <intID, openCondition JsonObject predicates>
            foreach (JsonArray substepArray in substepjson)
            {
                var       intID = int.Parse(substepArray[0].ToString());
                var       underlyingComposite = GroundActionFactory.GroundLibrary[intID];
                IPlanStep plansubstep;
                if (underlyingComposite.Height == 0)
                {
                    plansubstep = new PlanStep(underlyingComposite as IOperator);
                }
                else
                {
                    plansubstep = new CompositePlanStep(underlyingComposite as IComposite);
                }

                var openConditions = new List <IPredicate>();
                foreach (JsonObject openCondition in substepArray[1] as JsonArray)
                {
                    var removeThis = PredicateFromJsonObject(openCondition);
                    openConditions.Add(removeThis);
                }

                plansubstep.OpenConditions = openConditions;
                substeps.Add(plansubstep);
            }
            return(substeps);
        }
        public void AddStep(IPlan plan, OpenCondition oc)
        {
            foreach (var cndt in CacheMaps.GetCndts(oc.precondition))
            {
                if (cndt == null)
                {
                    continue;
                }

                var planClone = plan.Clone() as IPlan;

                planClone.ID += "a";

                IPlanStep newStep;
                if (cndt.Height > 0)
                {
                    //continue;
                    var compCndt = cndt as Composite;
                    newStep = new CompositePlanStep(compCndt.Clone() as Composite)
                    {
                        Depth = oc.step.Depth
                    };
                    //planClone.Hdepth += compCndt.SubSteps.Count; // this is done now in beginning of Insert Decomp
                }
                else
                {
                    newStep = new PlanStep(cndt.Clone() as IOperator)
                    {
                        Depth = oc.step.Depth
                    };
                }

                // For debugging
                //planClone.ID += "(" + GroundActionFactory.GroundActions.IndexOf(newStep.Action) + ")";

                planClone.Insert(newStep);
                planClone.Repair(oc, newStep);

                if (oc.isDummyGoal)
                {
                    if (newStep.Height > 0)
                    {
                        var compNewStep = newStep as CompositePlanStep;
                        planClone.Orderings.Insert(oc.step.InitCndt, compNewStep.InitialStep);
                    }
                    else
                    {
                        planClone.Orderings.Insert(oc.step.InitCndt, newStep);
                    }
                }

                planClone.DetectThreats(newStep);
                Insert(planClone);
            }
        }
        public void AddStep(IPlan plan, OpenCondition oc)
        {
            foreach (var cndt in CacheMaps.GetCndts(oc.precondition))
            {
                if (cndt == null)
                {
                    continue;
                }

                var       planClone = plan.Clone() as IPlan;
                IPlanStep newStep;
                if (cndt.Height > 0)
                {
                    //continue;
                    var compCndt = cndt as IComposite;
                    newStep = new CompositePlanStep(compCndt.Clone() as IComposite)
                    {
                        Depth = oc.step.Depth
                    };
                }
                else
                {
                    newStep = new PlanStep(cndt.Clone() as IOperator)
                    {
                        Depth = oc.step.Depth
                    };
                }
                //newStep.Height = cndt.Height;

                planClone.Insert(newStep);
                planClone.Repair(oc, newStep);


                // check if inserting new Step (with orderings given by Repair) add cndts/risks to existing open conditions, affecting their status in the heap
                //planClone.Flaws.UpdateFlaws(planClone, newStep);

                if (oc.isDummyGoal)
                {
                    if (newStep.Height > 0)
                    {
                        var compNewStep = newStep as ICompositePlanStep;
                        planClone.Orderings.Insert(oc.step.InitCndt, compNewStep.InitialStep);
                    }
                    else
                    {
                        planClone.Orderings.Insert(oc.step.InitCndt, newStep);
                    }
                }
                planClone.DetectThreats(newStep);
                Insert(planClone);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Filters candidates for substeps, at least one with height "height"
        /// </summary>
        /// <param name="decomp"></param>
        /// <returns> List of decompositions with ground sub-steps. </returns>
        public static List <Decomposition> FilterDecompCandidates(Decomposition decomp, int height)
        {
            // find and replace sub-steps
            var comboList = new List <List <IOperator> >();
            var ID_List   = new List <int>();

            foreach (var substep in decomp.SubSteps)
            {
                ID_List.Add(substep.ID);
                // each substep has ground terms that are already consistent. Composite IS-A Operator
                var cndts = ConsistentSteps(substep.Action as Operator);

                // If there's no cndts for this substep, then abandon this decomp.
                if (cndts.Count == 0)
                {
                    return(new List <Decomposition>());
                }

                comboList.Add(cndts);
            }

            List <Decomposition> decompList = new List <Decomposition>();

            foreach (var combination in EnumerableExtension.GenerateCombinations(comboList))
            {
                var decompClone           = decomp.Clone() as Decomposition;
                var newSubsteps           = new List <IPlanStep>();
                var substepDict           = new Dictionary <int, IPlanStep>();
                var order                 = 0;
                var hasPrerequisiteHeight = false;
                foreach (var item in combination)
                {
                    if (item.Height >= height)
                    {
                        // meets height requirement
                        hasPrerequisiteHeight = true;
                    }
                    var originalID = ID_List[order++];
                    if (item.Height > 0)
                    {
                        var newPlanStep = new CompositePlanStep(item as Composite);
                        substepDict[originalID] = newPlanStep;
                        newSubsteps.Add(newPlanStep);
                    }
                    else
                    {
                        var newPlanStep = new PlanStep(item);
                        substepDict[originalID] = newPlanStep;
                        newSubsteps.Add(newPlanStep);
                    }
                }

                // Did not meet requirements for height.
                if (!hasPrerequisiteHeight)
                {
                    continue;
                }

                var newSuborderings = new List <Tuple <IPlanStep, IPlanStep> >();
                foreach (var subordering in decomp.SubOrderings)
                {
                    var first  = substepDict[subordering.First.ID];
                    var second = substepDict[subordering.Second.ID];
                    newSuborderings.Add(new Tuple <IPlanStep, IPlanStep>(first, second));
                }

                var linkWorlds = new List <List <CausalLink <IPlanStep> > >();
                linkWorlds.Add(new List <CausalLink <IPlanStep> >());
                var newSublinks = new List <CausalLink <IPlanStep> >();
                foreach (var sublink in decomp.SubLinks)
                {
                    var head  = substepDict[sublink.Head.ID];
                    var tail  = substepDict[sublink.Tail.ID];
                    var cndts = head.Effects.Where(eff => eff.IsConsistent(sublink.Predicate) && tail.Preconditions.Any(pre => pre.Equals(eff)));

                    //// swap tall members
                    //if (head.Height > 0)
                    //{
                    //    var Chead = head as CompositePlanStep;
                    //    head = Chead.GoalStep;
                    //}
                    //if (tail.Height > 0)
                    //{
                    //    var Ctail = tail as CompositePlanStep;
                    //    tail = Ctail.InitialStep;
                    //}

                    if (cndts.Count() == 0)
                    {
                        // forfeit this entire subplan
                        linkWorlds = new List <List <CausalLink <IPlanStep> > >();
                        continue;
                    }
                    if (cndts.Count() == 1)
                    {
                        var cndt       = cndts.First();
                        var dependency = cndt.Clone() as Predicate;
                        var newLink    = new CausalLink <IPlanStep>(dependency, head, tail);
                        newLink.Tail.Fulfill(cndt);
                        foreach (var linkworld in linkWorlds)
                        {
                            linkworld.Add(newLink);
                        }
                    }
                    else
                    {
                        foreach (var cndt in cndts)
                        {
                            var dependency = cndt.Clone() as Predicate;

                            var newLink = new CausalLink <IPlanStep>(dependency, head, tail);
                            newLink.Tail.Fulfill(cndt);

                            var clonedLinks = EnumerableExtension.CloneList(newSublinks);

                            linkWorlds.Add(clonedLinks);
                            foreach (var linkworld in linkWorlds)
                            {
                                linkworld.Add(newLink);
                            }
                        }
                    }
                }

                foreach (var linkworld in linkWorlds)
                {
                    var newDecomp = decomp.Clone() as Decomposition;
                    newDecomp.SubSteps     = newSubsteps;
                    newDecomp.SubOrderings = newSuborderings;
                    newDecomp.SubLinks     = linkworld;

                    decompList.Add(newDecomp);
                }
            }
            return(decompList);
        }