当前位置: 首页>>代码示例>>C#>>正文


C# WorldState.Contains方法代码示例

本文整理汇总了C#中WorldState.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# WorldState.Contains方法的具体用法?C# WorldState.Contains怎么用?C# WorldState.Contains使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WorldState的用法示例。


在下文中一共展示了WorldState.Contains方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Run

    //the AStar algorithm
    public List<AStarNode> Run(AStarNode startNode, AStarNode endNode)
    {
        AStarNode currentNode = startNode;

        //special case for the first node since it is NOT an action but a postCondition. We are counting on that it will never be a goal!
        List<AStarNode> neighbourList = currentNode.GetNeighbours(true); //gives a list with actions that has startNodes preCondition

        foreach(AStarNode node in neighbourList) //for each of these neighbours
        {
            node.SetF(ActionManager.Instance.GetAction(node.GetName()).cost + HeuristicCost(node, endNode)); //calculate F of the neigbour
            node.SetParent(startNode); //Set the startNode as the neighbours parent
            openList.Add(node); //Add the neighbours of the startNode to the openList
        }

        //As long as we still have nodes in the openList
        while(openList.Count > 0)
        {
            //Take out the node with the lowest value for F in the openList
            int lowInd = 0;
            for(int i = 0; i < openList.Count; i++)
            {
                if(openList[i].GetF() < openList[lowInd].GetF())
                {
                    lowInd = i;
                }
            }
            currentNode = openList[lowInd];
            openList.Remove(currentNode);
            closedList.Add(currentNode);

            //Check if we have reached the target
            if(endNode.GetWorldState().Contains(ActionManager.Instance.GetAction(currentNode.GetName()).preConditions))
            {
                return CreatePath(currentNode, startNode);
            }

            //if action with preCondition with amount or several preConditions

            WorldState preConditions =  ActionManager.Instance.GetAction(currentNode.GetName()).preConditions;

            if(preConditions.ContainsAmount() || preConditions.GetProperties().Count > 1)
            {
                List<List<AStarNode>> lists = new List<List<AStarNode>>();

                //for each of the preConditions of the currentNodes action we need to start new AStars
                foreach(KeyValuePair<string, WorldStateValue> pair in preConditions.GetProperties())
                {
                    //if an action contains a preCondition that has amount then we need to run the algorithm several times
                    for(int j = 0; j < (int)pair.Value.propertyValues["amount"]; j++)
                    {

                        AStar astar2 = new AStar();

                        AStarNode tempNode = new AStarNode();

                        WorldState tempWorldState = new WorldState();
                        tempWorldState.SetProperty(pair.Key, pair.Value);
                        tempNode.SetWorldState(tempWorldState);

                        List<AStarNode> tempList = new List<AStarNode>();

                        tempList.AddRange(astar2.Run(tempNode, endNode)); //run the AStar for the preCondition

                        tempList[tempList.Count-1].SetParent(currentNode); //set parent to action before fork

                        if(tempList.Count > 0)
                        {
                            lists.Add(tempList); //add new plan to main plan
                        }
                        else
                        {
                            if(!tempWorldState.Contains(endNode.GetWorldState()))
                            {
                                return new List<AStarNode>(); //unreachable goal for the algorithm, cant find goal
                            }
                        }
                    }
                }

                //Sort by cost to make the plan prioritize actions with small costs
                lists.Sort((a, b) => ActionManager.Instance.GetAction(a[0].GetName()).cost.CompareTo(ActionManager.Instance.GetAction(b[0].GetName()).cost));

                foreach(List<AStarNode> list in lists)
                {
                    pathList.AddRange(list); //add all branches to final path
                }

                return CreatePath(currentNode, startNode);
            }

            neighbourList = currentNode.GetNeighbours(false); //get neighbouring nodes

            for(int i = 0; i < neighbourList.Count; i++)
            {

                AStarNode currentNeighbour = neighbourList[i];
                if(closedList.Contains(currentNeighbour)) //We have already looked at this node
                {
                    //not a valid node
//.........这里部分代码省略.........
开发者ID:RikardGehlin,项目名称:Exjobb,代码行数:101,代码来源:AStar.cs


注:本文中的WorldState.Contains方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。