Exemple #1
0
        public List<HTNTaskSet> AssignAndRefineTaskSet(
            ResourceProfileSet resourceSet,
            HTNState currentHTNState,
            HTNState targetHTNState,
            SpecificationKnowledge specificationKnowledge)
        {
            List<HTNTaskSet> newRefinedHTNTaskSetList = new List<HTNTaskSet>();

            List<string> waitingTaskList = new List<string>();
            foreach (string taskID in this.TaskIDList)
            {
                waitingTaskList.Add(taskID);
            }

            while (waitingTaskList.Count > 0)
            {
                List<string> allocatedResourceIDList = new List<string>();
                List<string> allocatedTaskIDList = new List<string>();
                List<bool> resourceAllocatedIndicator = new List<bool>();

                HTNTaskSet htnTaskSet = new HTNTaskSet();

                for (int i = 0; i < resourceSet.Resources.Count; i++)
                {
                    resourceAllocatedIndicator.Add(true);
                }

                foreach (string taskID in waitingTaskList)
                {
                    for (int i = 0; i < resourceSet.Resources.Count; i++)
                    {
                        ResourceProfile ra = resourceSet.Resources[i];

                        if (resourceAllocatedIndicator[i] && ra.CanExecuteThisTask(taskID))
                        {
                            resourceAllocatedIndicator[i] = false;
                            htnTaskSet.AddTaskID(taskID);
                            htnTaskSet.TaskExecutors.Add(taskID, ra.ID);
                            allocatedTaskIDList.Add(taskID);

                            //  added to prevent the re-added value
                            break;
                        }
                    }
                }

                foreach (string taskID in allocatedTaskIDList)
                {
                    waitingTaskList.Remove(taskID);
                }

                newRefinedHTNTaskSetList.Add(htnTaskSet);
            }
            return newRefinedHTNTaskSetList;
        }
        public ComplexHTNPlanner()
        {
            InitialState = new HTNState();
            TargetState = new HTNState();
            TaskNetwork = new HTNTaskNetwork();

            StateConstraints = new List<HTNState>();
            TaskSets = new List<HTNTaskSet>();
            AllocationDictionary = new Dictionary<string, string>();
            ResourceProfileSet = new ResourceProfileSet();
        }
Exemple #3
0
        public HTNState(HTNState htnState)
            : this()
        {
            PlanningDomain = htnState.PlanningDomain;

            foreach (KeyValuePair<string, string> kvp in this.States)
            {
                States.Remove(kvp.Key);
            }

            foreach (KeyValuePair<string, string> kvp in htnState.States)
            {
                string key = new string(kvp.Key.ToCharArray());
                string value = new string(kvp.Value.ToCharArray());
                States.Add(key, value);
            }
        }
Exemple #4
0
        /// <summary>
        /// Decomposes and schedules the transition between current state and target state.
        /// </summary>
        public List<HTNTaskSet> DecomposeTaskSet(
            ResourceProfileSet resourceProfileSet,
            SpecificationKnowledge specificationKnowledge,
            ComplexHTNPlanner htnPlanner,
            HTNState currentHTNState,
            HTNState targetHTNState)
        {
            List<List<HTNTaskSet>> allNewDecomposedTaskSetList = new List<List<HTNTaskSet>>();
            HTNState currentState = currentHTNState;
            int longestSize = 1;

            // Start to decompose
            foreach (string taskID in TaskIDList)
            {
                // If the task needs to be decomposed, decompose it
                if (!specificationKnowledge.IsTaskAPrimitive(taskID))
                {
                    HTNMethod method = htnPlanner.FindASuitableMethodOrOperator(taskID, currentState);

                    HTNTaskNetwork decomposedTaskNetwork = specificationKnowledge.GetTaskNetwork(method.TaskNetworkID);
                    int constraintsCount = decomposedTaskNetwork.Constraints.Count;

                    Random random = new Random();
                    int selectedConstraint = random.Next(0, constraintsCount);
                    HTNTaskNetworkConstraints htnTaskNetworkConstraints
                        = decomposedTaskNetwork.Constraints.ElementAt(selectedConstraint).Value;

                    List<HTNTaskSet> decomposedTaskSetList = htnTaskNetworkConstraints.HTNTaskConstraints;
                    if (decomposedTaskSetList.Count > longestSize)
                    {
                        longestSize = decomposedTaskSetList.Count;
                    }

                    string resourceID = TaskExecutors[taskID];
                    foreach (HTNTaskSet decomposedHTNTask in decomposedTaskSetList)
                    {
                        foreach (string subTaskID in decomposedHTNTask.TaskIDList)
                        {
                            decomposedHTNTask.TaskExecutors.Add(subTaskID, resourceID);
                        }
                    }

                    allNewDecomposedTaskSetList.Add(decomposedTaskSetList);
                }
                // The task is primitive so does not need to be decomposed further
                else
                {
                    List<HTNTaskSet> decomposedTaskSetList = new List<HTNTaskSet>();
                    HTNTaskSet htnTaskSet = new HTNTaskSet();

                    string resourceID = TaskExecutors[taskID];

                    htnTaskSet.TaskExecutors.Add(taskID, resourceID);
                    htnTaskSet.TaskIDList.Add(taskID);

                    decomposedTaskSetList.Add(htnTaskSet);
                    allNewDecomposedTaskSetList.Add(decomposedTaskSetList);
                }
            }
            // end of decomposing

            // Schedule all tasks into a single list of task sets

            List<HTNTaskSet> mergeredHTNSetList = new List<HTNTaskSet>();
            for (int i = 0; i < longestSize; i++)
            {
                HTNTaskSet newlyMergeredHTNTaskSet = new HTNTaskSet();

                foreach (List<HTNTaskSet> htnTaskSetList in allNewDecomposedTaskSetList)
                {
                    if (htnTaskSetList.Count > i)
                    {
                        foreach (string taskID in htnTaskSetList[i].TaskIDList)
                        {
                            string resourceID = htnTaskSetList[i].TaskExecutors[taskID];

                            newlyMergeredHTNTaskSet.TaskExecutors.Add(taskID, resourceID);
                            newlyMergeredHTNTaskSet.AddTaskID(taskID);

                        }
                    }
                }
                mergeredHTNSetList.Add(newlyMergeredHTNTaskSet);
            }

            return mergeredHTNSetList;
        }
Exemple #5
0
 public abstract void SetPlannerProperties(
     HTNState initialHTNState,
     HTNState targetHTNState,
     string workitemID,
     SpecificationKnowledge specificationKnowledge,
     ResourceProfileSet resourceProfileSet);
 public void AddTargetState(HTNState targetState)
 {
     this.targetState = targetState;
 }
 public void AddInitialState(HTNState initialState)
 {
     this.initialState = initialState;
 }
        public HTNMethod FindASuitableMethodOrOperator(string taskID, HTNState currentState)
        {
            Dictionary<string, HTNMethod> methodDictionary = SpecificationKnowledge.GetMethodDictionary();
            List<string> candidateMethodList = new List<string>();

            foreach (KeyValuePair<string, HTNMethod> pvMethod in methodDictionary)
            {
                if (pvMethod.Value.SuitsAMethod(taskID, currentState) && pvMethod.Value.CurrentStateSatisifyMethodPrecondition(currentState))
                    candidateMethodList.Add(pvMethod.Key);
            }

            if (candidateMethodList.Count == 0)
                return null;
            return SpecificationKnowledge.GetMethodDictionary()[candidateMethodList[0]];
        }
        private HTNMethod SelectAMethodForThisWorkitem(string key, HTNState currentState)
        {
            List<HTNMethod> suitableMethod = new List<HTNMethod>();

            foreach (KeyValuePair<string, HTNMethod> pv in SpecificationKnowledge.GetMethodDictionary())
            {
                HTNMethod method = pv.Value;
                if (method.SuitsAMethod(key, currentState))
                    suitableMethod.Add(method);
            }

            int count = suitableMethod.Count;

            HTNMethod selectMethod = null;

            if (count >= 1)
            {
                Random randObj = new Random();
                int select = randObj.Next(0, count);
                selectMethod = suitableMethod[select];
            }

            return selectMethod;
        }
Exemple #10
0
 // TODO: JUST HAD A  BRILLIANT IDEA FOR A METHOD GUYS
 public bool ContainsThisState(HTNState htnState)
 {
     return false;
 }
Exemple #11
0
 // TODO This appears to not be implemented ffs.
 public bool CurrentStateSatisifyMethodPrecondition(HTNState currentState)
 {
     return true;
 }
        public override List<HTNTaskSet> GeneratePlan()
        {
            //  Select a task network for the workitem
            HTNMethod htnMethod = SelectAMethodForThisWorkitem(WorkitemID, InitialState);
            if (htnMethod == null) {
                Console.WriteLine("Method undefined. Unable to generate a plan.");
                return null;
            }

            string taskNetworkID = htnMethod.TaskNetworkID;

            HTNTaskNetwork htnTaskNetwork = SpecificationKnowledge.GetTaskNetwork(taskNetworkID);
            HTNTaskNetworkConstraints constraint = htnTaskNetwork.Constraints[FirstConstraint];

            TaskSets = constraint.HTNTaskConstraints;
            for (int i = 1; i < constraint.StateConstraints.Count - 1; i++)
            {
                StateConstraints.Insert(i, constraint.StateConstraints[i]);
            }

            // Perform Decomposition
            bool allPrimitive = false;
            bool allScheduled = false;

            while (!allPrimitive && !allScheduled)
            {
                // extend the current task network, assign some tasks to resources, generate more states
                List<HTNTaskSet> newHTNTaskSetList = new List<HTNTaskSet>();

                for (int i = 0; i < TaskSets.Count; i++)
                {
                    HTNTaskSet htnTaskSet = TaskSets[i];
                    List<HTNTaskSet> newHTNSetFragment = htnTaskSet.
                        AssignAndRefineTaskSet(
                            ResourceProfileSet,
                            StateConstraints[i],
                            StateConstraints[i + 1],
                            SpecificationKnowledge
                            );

                    foreach (HTNTaskSet newHTNTaskSet in newHTNSetFragment)
                    {
                        newHTNTaskSetList.Add(newHTNTaskSet);
                    }
                }
                TaskSets = newHTNTaskSetList;

                List<HTNState> newHTNStateList = new List<HTNState>();
                newHTNStateList.Add(InitialState);
                newHTNStateList.Add(TargetState);
                HTNState localHTNState = new HTNState(InitialState);

                for (int i = 0; i < TaskSets.Count - 1; i++)
                {
                    HTNState deducedState = localHTNState.DeduceNextStateBasedOnEffects(
                        TaskSets[i].GetAllPostEffects(SpecificationKnowledge));
                    localHTNState = new HTNState(deducedState);
                    newHTNStateList.Insert(i + 1, deducedState);
                }

                StateConstraints = newHTNStateList;

                // Attempt to decompose
                List<HTNTaskSet> newlyDecomposedHTNTaskSetList = new List<HTNTaskSet>();

                for (int i = 0; i < TaskSets.Count; i++)
                {
                    HTNTaskSet htnTaskSet = TaskSets[i];

                    List<HTNTaskSet> partialDecomposedHTNTaskSetList
                        =
                        htnTaskSet.DecomposeTaskSet(
                            ResourceProfileSet,
                            SpecificationKnowledge,
                            this,
                            StateConstraints[i],
                            StateConstraints[i + 1]
                        );

                    foreach (HTNTaskSet taskSet in partialDecomposedHTNTaskSetList)
                    {
                        newlyDecomposedHTNTaskSetList.Add(taskSet);
                    }
                }

                allScheduled = true;

                TaskSets = newlyDecomposedHTNTaskSetList;

                // TODO: Hope this doesn't break. Dammit..
                allPrimitive = AreAllTasksPrimitive(TaskSets);

                //foreach (HTNTaskSet htnTaskSet in TaskSets)
                //{
                //    foreach (string taskID in htnTaskSet.TaskIDList)
                //    {
                //        if (!SpecificationKnowledge.IsTaskAPrimitive(taskID))
                //        {
                //            allPrimitive = false;
                //        }
                //    }

                //    if (!allPrimitive)
                //    {
                //        break;
                //    }
                //}

                if (!allPrimitive && allScheduled)
                {
                    newHTNStateList.Clear();
                    newHTNStateList.Add(InitialState);
                    newHTNStateList.Add(TargetState);
                    localHTNState = new HTNState(InitialState);

                    for (int i = 0; i < TaskSets.Count - 1; i++)
                    {
                        HTNState deduced_state = localHTNState.DeduceNextStateBasedOnEffects(TaskSets[i].GetAllPostEffects(SpecificationKnowledge));
                        localHTNState = new HTNState(deduced_state);
                        newHTNStateList.Insert(i + 1, deduced_state);
                    }

                    this.StateConstraints = newHTNStateList;
                    allPrimitive = false;
                }

                allScheduled = true;

                foreach (HTNTaskSet htnTaskSet in TaskSets)
                {
                    if (!htnTaskSet.IsScheduled())
                    {
                        allScheduled = false;
                    }
                }
            }

            int stateSize = StateConstraints.Count;
            for (int i = 0; i < StateConstraints.Count; i++)
            {
                HTNState htnState = new HTNState(StateConstraints[i]);

                foreach (KeyValuePair<string, string> kvp1 in htnState.States)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("state_name:   " + kvp1.Key);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("         state_value:  " + kvp1.Value);
                }

                if (i < stateSize - 1)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    HTNTaskSet htnTaskSet = new HTNTaskSet(TaskSets[i]);

                    foreach (KeyValuePair<string, string> kvp2 in htnTaskSet.TaskExecutors)
                    {
                        Console.WriteLine("task_id:  " + kvp2.Key + "       " + "executor:  " + kvp2.Value);
                    }
                }
            }

            return TaskSets;
        }
Exemple #13
0
        public bool IsEqualTo(HTNState antherState)
        {
            if (antherState.StateAtoms.Count > this.StateAtoms.Count
                || this.StateAtoms.Count > antherState.StateAtoms.Count)
            {
                return false;
            }

            foreach (string state in antherState.StateAtoms)
            {
                if (!this.StateAtoms.Contains(state))
                    return false;
            }

            return true;
        }
Exemple #14
0
        public bool HasSameStates(HTNState inputState)
        {
            int lengthOfThis = this.StateAtoms.Count;
            int lengthOfInput = inputState.StateAtoms.Count;

            List<string> baseStateSet;
            List<string> otherStateSet;

            if (lengthOfThis > lengthOfInput)
            {
                baseStateSet = this.StateAtoms;
                otherStateSet = inputState.StateAtoms;
            }
            else
            {
                baseStateSet = inputState.StateAtoms;
                otherStateSet = this.StateAtoms;
            }

            foreach (string state in otherStateSet)
            {
                if (!baseStateSet.Contains(state))
                {
                    return false;
                }
            }
            return true;
        }
Exemple #15
0
        public HTNState DeduceNextStateBasedOnEffects(List<HTNEffect> effectList)
        {
            HTNState nextState = new HTNState(this);

            foreach (HTNEffect htnEffect in effectList)
            {
                nextState.States[htnEffect.StateAtomName] = htnEffect.StateAtomValue;
            }

            return nextState;
        }
Exemple #16
0
        private bool DeduceFollowingStates(
            IEnumerable<HTNTaskSet> refinedHTNTaskSetList,
            HTNState currentState,
            HTNState targetState,
            SpecificationKnowledge specificationKnowledge)
        {
            List<HTNState> newlyDeducedHTNStateList = new List<HTNState>();
            newlyDeducedHTNStateList.Add(currentState);

            HTNState localCurrentHTNState = new HTNState(currentState);
            List<List<HTNEffect>> htnEffectList = new List<List<HTNEffect>>();

            foreach (HTNTaskSet htnTaskSet in refinedHTNTaskSetList)
            {
                List<HTNEffect> postEffects = new List<HTNEffect>();

                foreach (string taskID in htnTaskSet.TaskIDList)
                {
                    HTNTask htnTask = specificationKnowledge.GetHTNTask(taskID);
                    postEffects.AddRange(htnTask.PostConditions);
                }
                htnEffectList.Add(postEffects);
            }

            HTNState nextHTNState = null;
            foreach (List<HTNEffect> htnEffects in htnEffectList)
            {
                nextHTNState = new HTNState(localCurrentHTNState.DeduceNextStateBasedOnEffects(htnEffects));
                newlyDeducedHTNStateList.Add(nextHTNState);
            }

            if (nextHTNState != null && nextHTNState.IsEqualTo(targetState))
            {
                newlyDeducedHTNStateList.Remove(nextHTNState);
                RefinedStateList = newlyDeducedHTNStateList;
                return true;
            }

            return false;
        }
        public override void SetPlannerProperties(
            HTNState initialHTNState,
            HTNState targetHTNState,
            string workitemID,
            SpecificationKnowledge specificationKnowledge,
            ResourceProfileSet resourceProfileSet)
        {
            ResourceProfileSet = resourceProfileSet;
            SpecificationKnowledge = specificationKnowledge;

            StateConstraints.Clear();
            StateConstraints.Add(initialHTNState);
            StateConstraints.Add(targetHTNState);

            InitialState.StateAtoms.Clear();
            InitialState = new HTNState(initialHTNState);
            TargetState = new HTNState(targetHTNState);

            WorkitemID = workitemID;
        }
Exemple #18
0
 public override void SetPlannerProperties(HTNState initialHTNState, HTNState targetHTNState, string workitemID, SpecificationKnowledge specificationKnowledge, ResourceProfileSet resourceProfileSet)
 {
     throw new NotImplementedException();
 }
Exemple #19
0
 public bool SuitsAMethod(string taskID, HTNState htnState)
 {
     if (TaskID.Equals(taskID) && htnState.IsThisAViolatingEffect(PreConditions))
         return true;
     return false;
 }