예제 #1
0
 public override bool InitStates(INode Start, INode Goal)
 {
     startNode   = new ANode(Start);
     goalNode    = new ANode(Goal);
     startNode.G = 0;
     startNode.H = hCalc.Calculate(startNode, goalNode);
     openList.AddValue(startNode);
     return(true);
 }
예제 #2
0
 public abstrA()
 {
     expansions    = 0;
     foundGoalNode = null;
     startNode     = null;
     goalNode      = null;
     timeAvaliable = 5000;
     rewards       = new Dictionary <double, double>();
     openList      = new NodeTable <ANode>(Comparer);
     closedList    = new NodeTable <ANode>(Comparer);
 }
예제 #3
0
 private double fvalue(ANode node)
 {
     return(node.G + node.H);
 }
예제 #4
0
        public bool winA(double TimeAvaliable)
        {
            currentLvl = -1;

            while (stopwatch.Elapsed.TotalMilliseconds < TimeAvaliable && !stopped)
            {
                if (openList.Count == 0)
                {
                    return(false);
                }


                ANode currentNode = openList.Min;

                openList.RemoveValue(currentNode);

                closedList.AddValue(currentNode);

                if (foundGoalNode != null && fvalue(currentNode) > fvalue(foundGoalNode))
                {
                    return(false);
                }


                if (currentNode.Level <= currentLvl - winSize)
                {
                    closedList.RemoveValue(currentNode);
                    suspendList.AddValue(currentNode);
                    continue;
                }

                if (currentNode.Level > currentLvl)
                {
                    currentLvl = currentNode.Level;
                }


                if (currentNode.Equals(goalNode))
                {
                    foundGoalNode = currentNode;
                    rewards.Add(stopwatch.Elapsed.TotalMilliseconds, currentNode.G);
                    return(true);
                }

                IEnumerable <ANode> successorNodes = succGen.Generate(currentNode);
                expansions++;
                nodesGenerated += successorNodes.Count();

                foreach (ANode successorNode in successorNodes)
                {
                    ANode node1;
                    ANode node2;
                    ANode node3;

                    if (openList.TryGetValue(successorNode, out node1))
                    {
                        if (node1.G > successorNode.G)
                        {
                            successorNode.H     = hCalc.Calculate(successorNode, goalNode);
                            successorNode.Level = currentNode.Level + 1;


                            openList.RemoveValue(node1);
                            openList.AddValue(successorNode);
                        }
                    }

                    else
                    if (suspendList.TryGetValue(successorNode, out node3))
                    {
                        if (node3.G > successorNode.G)
                        {
                            successorNode.H     = hCalc.Calculate(successorNode, goalNode);
                            successorNode.Level = currentNode.Level + 1;


                            suspendList.RemoveValue(node3);
                            suspendList.AddValue(successorNode);
                        }
                    }
                    else
                    if (closedList.TryGetValue(successorNode, out node2))
                    {
                        if (node2.G > successorNode.G)
                        {
                            closedList.RemoveValue(node2);

                            successorNode.H     = hCalc.Calculate(successorNode, goalNode);
                            successorNode.Level = currentNode.Level + 1;
                            openList.AddValue(successorNode);
                        }
                    }

                    else

                    {
                        successorNode.Parent = currentNode;
                        successorNode.H      = hCalc.Calculate(successorNode, goalNode);
                        successorNode.Level  = currentNode.Level + 1;
                        openList.AddValue(successorNode);
                    }
                }
            }

            return(false);
        }
예제 #5
0
 private double fvalue(ANode n)
 {
     return(n.G + n.H);
 }
예제 #6
0
 private double fvalue2(ANode n)
 {
     return(n.G + w * n.H);
 }
예제 #7
0
        public override void Execute()
        {
            stopwatch.Restart();

            while (stopwatch.Elapsed.TotalMilliseconds < timeAvaliable && !stopped)
            {
                if (openList.Count == 0)
                {
                    isOptimalFound = true;
                    stopwatch.Stop();
                    return;
                }



                ANode currentNode = openList.Min;
                openList.RemoveValue(currentNode);
                if (currentNode.Equals(goalNode))
                {
                    foundGoalNode = currentNode;

                    rewards.Add(stopwatch.Elapsed.TotalMilliseconds, currentNode.G);
                    continue;
                }


                if (foundGoalNode == null || fvalue(currentNode) < foundGoalNode.G)
                {
                    closedList.AddValue(currentNode);
                    IEnumerable <ANode> successorNodes = succGen.Generate(currentNode);
                    expansions++;


                    nodesGenerated = nodesGenerated + successorNodes.Count();
                    foreach (ANode successorNode in successorNodes)
                    {
                        if (foundGoalNode == null || successorNode.G + hCalc.Calculate(successorNode, goalNode) < foundGoalNode.G)
                        {
                            ANode node1;
                            ANode node2;
                            bool  InOpen;
                            bool  InClosed;

                            if (InOpen = openList.TryGetValue(successorNode, out node1))
                            {
                                if (node1.G > successorNode.G)
                                {
                                    successorNode.H = hCalc.Calculate(successorNode, goalNode);


                                    openList.RemoveValue(node1);
                                    openList.AddValue(successorNode);
                                }
                            }

                            else

                            if (InClosed = closedList.TryGetValue(successorNode, out node2))
                            {
                                if (node2.G > successorNode.G)
                                {
                                    closedList.RemoveValue(node2);
                                    successorNode.H = hCalc.Calculate(successorNode, goalNode);

                                    openList.AddValue(successorNode);
                                }
                            }

                            else

                            {
                                successorNode.H = hCalc.Calculate(successorNode, goalNode);
                                openList.AddValue(successorNode);
                            }
                        }
                    }
                }
            }



            stopwatch.Stop();
        }
예제 #8
0
파일: Astar.cs 프로젝트: Akneman93/Solvers
        private bool Search(double timeAvaliable)
        {
            while (stopwatch.Elapsed.TotalMilliseconds < timeAvaliable && !stopped)
            {
                if (openList.Count == 0)
                {
                    isOptimalFound = true;
                    return(true);
                }
                ANode currentNode = openList.Min;
                openList.RemoveValue(currentNode);
                if (currentNode.Equals(goalNode))
                {
                    foundGoalNode  = currentNode;
                    isOptimalFound = true;

                    rewards.Add(stopwatch.Elapsed.TotalMilliseconds, currentNode.G);
                    return(false);
                }

                closedList.AddValue(currentNode);
                IEnumerable <ANode> successorNodes = succGen.Generate(currentNode);
                expansions++;
                nodesGenerated = nodesGenerated + successorNodes.Count();
                foreach (ANode successorNode in successorNodes)
                {
                    ANode node1;
                    ANode node2;
                    bool  InOpen;
                    bool  InClosed;


                    if (InOpen = openList.TryGetValue(successorNode, out node1))
                    {
                        if (node1.G > successorNode.G)
                        {
                            successorNode.H = hCalc.Calculate(successorNode, goalNode);


                            openList.RemoveValue(node1);
                            openList.AddValue(successorNode);
                        }
                    }

                    else

                    if (InClosed = closedList.TryGetValue(successorNode, out node2))
                    {
                        if (node2.G > successorNode.G)
                        {
                            closedList.RemoveValue(node2);
                            successorNode.H = hCalc.Calculate(successorNode, goalNode);

                            openList.AddValue(successorNode);
                        }
                    }

                    else
                    {
                        successorNode.H = hCalc.Calculate(successorNode, goalNode);
                        openList.AddValue(successorNode);
                    }
                }
            }



            return(false);
        }