Пример #1
0
        public bool BreadthFirstSearch(Node Start)
        {
            Queue.Enqueue(Start);

            VisitedNodes.Add(Start);

            for (; Queue.Count > 0;)
            {
                Start = Queue.Dequeue();

                for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
                {
                    if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                    {
                        ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));

                        Queue.Enqueue(InnerGraph.SetOfNodes[Index]);

                        VisitedNodes.Add(InnerGraph.SetOfNodes[Index]);
                    }
                }
            }

            return(VisitedNodes.Count.Equals(InnerGraph.NumberOfNodes));
        }
Пример #2
0
        ///
        /// bfs var 2
        /// searching path to target node from some start node
        ///
        /// <param name="Start"> - first node
        /// <param name="Target"> - our scope
        /// <param name="Path"> - for our scope
        /// <returns></returns>
        public bool BreadthFirstSearch(Node Start, Node Target, Node[] Path)
        {
            Queue.Enqueue(Start);

            VisitedNodes.Add(Start);

            Path[Start.Index] = Start;

            if (VisitedNodes.Contains(Target))
            {
                return(true);
            }

            for (; Queue.Count > 0;)
            {
                Start = Queue.Dequeue();

                for (int Index = 0; Index < InnerGraph.QuantityOfNodes; Index++)
                {
                    if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]) && InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]).Weight > 0)
                    {
                        Path[Index] = Start;

                        Queue.Enqueue(InnerGraph.SetOfNodes[Index]);

                        VisitedNodes.Add(InnerGraph.SetOfNodes[Index]);
                    }
                }
            }

            return(VisitedNodes.Contains(Target));
        }
Пример #3
0
        public void Move()
        {
            if (CompletedMoving)
            {
                return;
            }

            double prev   = 0;
            double random = new Random().NextDouble();

            foreach (var item in Choices.Where(z => !VisitedNodes.Contains(z.Item2)).OrderByDescending(z => z.Item1))
            {
                if (random > prev && random < item.Item1 + prev)
                {
                    var transition = item.Item2.Neighbours.GetValueOrDefault(CurrentNode);
                    LeavePheromone(transition);
                    VisitedNodes.Add(CurrentNode);

                    WayPassed  += transition.Value;
                    CurrentNode = item.Item2;   //.Neighbours.Where(z => z.Value == transition && z.Key != CurrentNode).First().Key;
                    RecalculateChoices();
                    return;
                }
                prev += item.Item1;
            }
            VisitedNodes.Add(CurrentNode);
            CompletedMoving = true;
        }
Пример #4
0
        public bool FindTheShortestPathes(Node Start)
        {
            ShortestPathes[Start.Index].Weight = 0;

            for (int FirstIndex = 0; FirstIndex < InnerGraph.NumberOfNodes - 1; FirstIndex++)
            {
                Node CurrentNode = new Node {
                    Weight = Infinity
                };

                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPathes[SecondIndex]) && ShortestPathes[SecondIndex].Weight <= CurrentNode.Weight)
                    {
                        CurrentNode = ShortestPathes[SecondIndex];

                        Index = ShortestPathes[SecondIndex].Index;
                    }
                }

                VisitedNodes.Add(ShortestPathes[Index]);

                for (int SecondIndex = 0; SecondIndex < InnerGraph.NumberOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPathes[SecondIndex]) && InnerGraph.SetOfNodes[Index][SecondIndex] != null && !ShortestPathes[Index].Weight.Equals(Infinity) && ShortestPathes[SecondIndex].Weight > ShortestPathes[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight)
                    {
                        ShortestPathes[SecondIndex].Weight = ShortestPathes[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight;
                    }
                }
            }

            return(InnerGraph.NegativeCycleChecker(ShortestPathes));
        }
Пример #5
0
        public bool DijkstraSWSAlgorithm(Node Start)
        {
            ShortestPath[Start.Index].Weight = 0;

            for (int FirstIndex = 0; FirstIndex < InnerGraph.QuantityOfNodes - 1; FirstIndex++)
            {
                Node CurrentNode = new Node {
                    Weight = double.PositiveInfinity
                };
                for (int SecondIndex = 0; SecondIndex < InnerGraph.QuantityOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPath[SecondIndex]) && ShortestPath[SecondIndex].Weight <= CurrentNode.Weight)
                    {
                        CurrentNode = ShortestPath[SecondIndex];
                        Index       = ShortestPath[SecondIndex].Index;
                    }
                }
                VisitedNodes.Add(ShortestPath[Index]);
                for (int SecondIndex = 0; SecondIndex < InnerGraph.QuantityOfNodes; SecondIndex++)
                {
                    if (!VisitedNodes.Contains(ShortestPath[SecondIndex]) && InnerGraph.SetOfNodes[Index][SecondIndex] != null &&
                        !ShortestPath[Index].Weight.Equals(double.PositiveInfinity) &&
                        ShortestPath[SecondIndex].Weight >
                        ShortestPath[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight)

                    {
                        ShortestPath[SecondIndex].Weight = ShortestPath[Index].Weight + InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], InnerGraph.SetOfNodes[SecondIndex]).Weight;
                    }
                }
            }

            return(InnerGraph.NegativeCycleChecker(ShortestPath));
        }
Пример #6
0
 private void CreateMoreAgents()
 {
     foreach (var connectedNode in CurrentNode.Connections)
     {
         if (!VisitedNodes.Contains(connectedNode))
         {
             CreateAgent(connectedNode);
         }
     }
 }
 void MarkNodeAsVisisted(Node node)
 {
     if (!VisitedNodes.Contains(node))
     {
         VisitedNodes.Enqueue(node);
     }
     if (UnvisitedNodes.Contains(node))
     {
         UnvisitedNodes.Remove(node);
     }
 }
Пример #8
0
        private void PushNeighborsToStack()
        {
            IEnumerable <T> neighborList = _graph[Current];

            foreach (T neighbor in neighborList)
            {
                if (!VisitedNodes.Contains(neighbor))
                {
                    _pendingSearches.Push(new SearchData(neighbor, _currentSearchData.Level + 1));
                }
            }
        }
Пример #9
0
        private bool DoSearch()
        {
            AssignCurrentVertex();

            if (VisitedNodes.Contains(Current))
            {
                return(MoveNext());
            }

            VisitedNodes.Add(Current);

            PushNeighborsToStack();

            return(true);
        }
Пример #10
0
        public bool DepthFirstSearch(Node Start)
        {
            VisitedNodes.Add(Start);

            for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
            {
                if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                {
                    ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));

                    DepthFirstSearch(InnerGraph.SetOfNodes[Index]);
                }
            }

            return(VisitedNodes.Count.Equals(InnerGraph.NumberOfNodes));
        }
Пример #11
0
            protected override bool Skip(Node node)
            {
                if (base.Skip(node))
                {
                    return(true);
                }

                var dependencyNode = _dependenciesGraph.AllNodes[node.Id];

                foreach (var parent in dependencyNode.Parents)
                {
                    if (!VisitedNodes.Contains(parent.Id))
                    {
                        return(true);
                    }
                }
                return(false);
            }
Пример #12
0
        public bool FindMinimumSpanningTree()
        {
            VisitedNodes.Add(InnerGraph.SetOfNodes[0]);

            for (; VisitedNodes.Count < InnerGraph.NumberOfNodes;)
            {
                Node Start = new Node();

                Node End = new Node();

                Edge CurrentEdge = new Edge {
                    Weight = Infinity
                };

                for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
                {
                    if (VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                    {
                        foreach (Node Inheritor in InnerGraph.SetOfNodes[Index].Inheritors.Where(Node => Node != null && !VisitedNodes.Contains(Node)))
                        {
                            if (CurrentEdge.Weight > InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Inheritor).Weight)
                            {
                                CurrentEdge = InnerGraph.FindEdge(InnerGraph.SetOfNodes[Index], Inheritor);

                                Start = CurrentEdge[0];

                                End = CurrentEdge[1];
                            }
                        }
                    }
                }

                VisitedNodes.Add(End);

                MinimumSpanningTree.Add(InnerGraph.FindEdge(Start, End));
            }

            return(VisitedNodes.Count.Equals(InnerGraph.NumberOfNodes));
        }
Пример #13
0
        public bool DepthFirstSearch(Node Start, Node Target)
        {
            VisitedNodes.Add(Start);

            if (VisitedNodes.Contains(Target))
            {
                return(true);
            }

            for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
            {
                if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]))
                {
                    ResultOfSearching.Add(InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]));

                    if (DepthFirstSearch(InnerGraph.SetOfNodes[Index], Target))
                    {
                        return(true);
                    }
                }
            }

            return(VisitedNodes.Contains(Target));
        }
Пример #14
0
        public bool DepthFirstSearch(Node Start, Node Target, Node[] FoundPath)
        {
            VisitedNodes.Add(Start);

            if (VisitedNodes.Contains(Target))
            {
                return(true);
            }

            for (int Index = 0; Index < InnerGraph.NumberOfNodes; Index++)
            {
                if (InnerGraph.SetOfNodes[Start.Index][Index] != null && !VisitedNodes.Contains(InnerGraph.SetOfNodes[Index]) && InnerGraph.FindEdge(Start, InnerGraph.SetOfNodes[Index]).Weight > 0)
                {
                    FoundPath[Index] = Start;

                    if (DepthFirstSearch(InnerGraph.SetOfNodes[Index], Target, FoundPath))
                    {
                        return(true);
                    }
                }
            }

            return(VisitedNodes.Contains(Target));
        }
Пример #15
0
 private double CalculateChoice(Transition t)
 {
     return((Math.Pow(Math.Floor(t.Pheromone), Const.A) * Math.Pow(t.Visibility, Const.B)) /
            CurrentNode.Neighbours.Where(z => !VisitedNodes.Contains(z.Key)).Sum(s => (Math.Pow(Math.Floor(s.Value.Pheromone), Const.A) * Math.Pow(s.Value.Visibility, Const.B))));
 }