コード例 #1
0
ファイル: Planning.cs プロジェクト: shadrd0g/HTNFramework
    static List <PrimitiveTask> SHOPNoBack(List <Task> tasks, List <PrimitiveTask> plan, string personality, State state, HTNAgent agent)
    {
        Task task = tasks [0];

        if (task.GetType() == typeof(PrimitiveTask))
        {
            PrimitiveTask pt = (PrimitiveTask)task;


            PrimitiveTask groundAction = InstantiateAction(pt, state, agent);

            if (groundAction != null)
            {
                plan.Add(groundAction);

                tasks.Remove(pt);

                if (tasks.Count > 0)
                {
                    return(SHOPNoBack(tasks, plan, personality, pt.apply(state), agent));
                }
            }
            else
            {
                return(null);
            }
        }
        else if (task.GetType() == typeof(CompoundTask))
        {
            CompoundTask ct = (CompoundTask)task;


            List <Method> orderdedMethods = new List <Method>();

            switch (agent.methodsOrdering)
            {
            case MethodsOrdering.NONE:
                orderdedMethods = ct.methods;
                break;

            case MethodsOrdering.PREF:
                orderdedMethods = ct.methods.OrderBy(m => m.preference != personality).ToList();
                break;

            case MethodsOrdering.COSTANDPREF:
                orderdedMethods = ct.methods.OrderBy(m => m.leastCost()).ThenBy(m => m.preference != personality).ToList();
                break;
            }


            foreach (Method m in orderdedMethods)
            {
                if (RuleMatcher.MatchCondition(m.preconditions, state, m.logicalOperator))
                {
                    List <Task> tempTasks = new List <Task>(tasks);

                    tempTasks.Remove(task);

                    int j = 0;

                    foreach (Task subtask in m.subtasks)
                    {
                        tempTasks.Insert(0 + j, subtask);
                        j++;
                    }
                    List <PrimitiveTask> tempPlan = SHOPNoBack(tempTasks, plan, personality, state, agent);


                    if (tempPlan != null)
                    {
                        plan.Concat(tempPlan);
                        break;
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
        }

        return(plan);
    }