Exemple #1
0
        public Tuple <string, double> runBFS(Dictionary <string, AggregationNode> nodeList)
        {
            StringBuilder path     = new StringBuilder();
            double        cost     = 0;
            int           frontier = 0;

            Queue         fringe  = new Queue();
            List <string> visited = new List <string>();

            visited.Add(this.name);
            path.AppendLine(this.name);

            foreach (KeyValuePair <string, double> nextNode in this.connectedNodes)
            {
                fringe.Enqueue(nextNode);
            }

            while (fringe.Count != 0)
            {
                if (fringe.Count > frontier)
                {
                    frontier = fringe.Count;
                }

                KeyValuePair <string, double> dequeued = (KeyValuePair <string, double>)fringe.Dequeue();
                AggregationNode current = nodeList[dequeued.Key];
                if (!visited.Contains(current.name))
                {
                    path.AppendLine(current.name);
                    visited.Add(current.name);
                    cost += dequeued.Value;
                }

                foreach (KeyValuePair <string, double> nextNode in current.connectedNodes)
                {
                    if (!visited.Contains(nextNode.Key))
                    {
                        fringe.Enqueue(nextNode);
                    }
                }
            }
            return(new Tuple <string, double>(path.ToString() + "Time: " + nodeList.Count + "\nFrontier: " + frontier +
                                              "\nExplored: " + visited.Count + "\n", cost));
        }
Exemple #2
0
        private static void parseAggregationEdges(String edgeList, Dictionary <string, AggregationNode> nodeList)
        {
            var charToRemove = new String[] { "[", "]", "(", ")", "\"" };

            String[] edges = edgeList.Split(',');

            for (int i = 0; i < edges.Length - 1; i = i + 3)
            {
                foreach (var c in charToRemove)
                {
                    edges[i]     = edges[i].Replace(c, "").Trim();
                    edges[i + 1] = edges[i + 1].Replace(c, "").Trim();
                    edges[i + 2] = edges[i + 2].Replace(c, "").Trim();
                }

                AggregationNode firstNode  = nodeList[edges[i]];
                AggregationNode secondNode = nodeList[edges[i + 1]];

                firstNode.connectedNodes.Add(edges[i + 1], Double.Parse(edges[i + 2]));
                secondNode.connectedNodes.Add(edges[i], Double.Parse(edges[i + 2]));
            }
        }
Exemple #3
0
        public Tuple <string, double> runIDDFS(Dictionary <string, AggregationNode> nodeList, int depth, ref bool isCutOff)
        {
            StringBuilder path          = new StringBuilder();
            double        cost          = 0;
            int           frontier      = 0;
            int           currentDepth  = 0;
            int           numberVisited = 1;

            Stack         fringe  = new Stack();
            List <string> visited = new List <string>();

            visited.Add(this.name);
            path.AppendLine(this.name);

            foreach (KeyValuePair <string, double> nextNode in this.connectedNodes)
            {
                fringe.Push(nextNode);
            }

            while (fringe.Count != 0)
            {
                if (fringe.Count > frontier)
                {
                    frontier = fringe.Count;
                }

                KeyValuePair <string, double> dequeued = (KeyValuePair <string, double>)fringe.Pop();
                AggregationNode current = nodeList[dequeued.Key];
                numberVisited++;

                if (!visited.Contains(current.name))
                {
                    path.AppendLine(current.name);
                    visited.Add(current.name);
                    cost += dequeued.Value;
                }

                foreach (KeyValuePair <string, double> nextNode in current.connectedNodes)
                {
                    if (!visited.Contains(nextNode.Key))
                    {
                        fringe.Push(nextNode);
                    }
                }

                if (numberVisited == nodeList.Count) //goal state is when all are visited
                {
                    isCutOff = true;
                }
                else if (depth == currentDepth)
                {
                    continue;
                }
                else
                {
                    foreach (KeyValuePair <string, double> nextNode in current.connectedNodes)
                    {
                        fringe.Push(nextNode);
                    }
                    currentDepth++;
                }
            }
            return(new Tuple <string, double>(path.ToString() + "Time: " + nodeList.Count + "\nFrontier: " + frontier +
                                              "\nExplored: " + visited.Count + "\n", cost));
        }
Exemple #4
0
        public Tuple <string, double> runUCS(Dictionary <string, AggregationNode> nodeList)
        {
            StringBuilder path     = new StringBuilder();
            double        cost     = 0;
            int           frontier = 0;

            List <Tuple <string, double> > fringe = new List <Tuple <string, double> >();
            List <string> visited = new List <string>();

            visited.Add(this.name);
            path.AppendLine(this.name);

            foreach (KeyValuePair <string, double> nextNode in this.connectedNodes)
            {
                if (fringe.Count == 0)
                {
                    fringe.Add(new Tuple <string, double>(nextNode.Key, nextNode.Value));
                }
                else
                {
                    for (int i = 0; i < fringe.Count; i++)
                    {
                        if (fringe[i].Item2 > nextNode.Value)
                        {
                            fringe.Insert(i, new Tuple <string, double>(nextNode.Key, nextNode.Value));
                            break;
                        }
                        else if (i == fringe.Count - 1)
                        {
                            fringe.Add(new Tuple <string, double>(nextNode.Key, nextNode.Value));
                            break;
                        }
                    }
                }
            }

            while (fringe.Count != 0)
            {
                if (fringe.Count > frontier)
                {
                    frontier = fringe.Count;
                }

                Tuple <string, double> dequeued = fringe[0];
                fringe.RemoveAt(0);
                AggregationNode current = nodeList[dequeued.Item1];

                if (!visited.Contains(current.name))
                {
                    path.AppendLine(current.name);
                    visited.Add(current.name);
                    cost += dequeued.Item2;
                }

                foreach (KeyValuePair <string, double> nextNode in current.connectedNodes)
                {
                    if (!visited.Contains(nextNode.Key))
                    {
                        if (fringe.Count == 0)
                        {
                            fringe.Add(new Tuple <string, double>(nextNode.Key, nextNode.Value));
                        }
                        else
                        {
                            for (int i = 0; i < fringe.Count; i++)
                            {
                                if (fringe[i].Item2 > nextNode.Value)
                                {
                                    fringe.Insert(i, new Tuple <string, double>(nextNode.Key, nextNode.Value));
                                    break;
                                }
                                else if (i == fringe.Count - 1)
                                {
                                    fringe.Add(new Tuple <string, double>(nextNode.Key, nextNode.Value));
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            return(new Tuple <string, double>(path.ToString() + "Time: " + nodeList.Count + "\nFrontier: " + frontier +
                                              "\nExplored: " + visited.Count + "\n", cost));
        }