The given key where not in the Dictionary

Topics: graph
Jul 14, 2011 at 8:32 PM

Hi, Im new to this forum and to using this nifty little thing. Im atm working on a VRP solution for varehouses, and i have the following code for a shortest path:

 

            int[,] KnudeArray =  new int[,] {{ -1, -1, -1, -1, 9, 10, -1, 5, -1 }, { -1, -1, -1, 9, 10, 11, 5, -1, -1 },{ -1, -1, -1, 10, 11, 12, -1, -1, 8 },{ -1, -1, -1, 11, 12, -1, -1, 8, -1 },
                { -1, 9, 10, -1, 5, -1, -1, 1, 2 },{ -1, -1, -1, -1, 6, -1, -1, -1, -1 },{ -1, -1, -1, -1, 7, -1, -1, -1, -1 },{ 11, 12, -1, -1, 8, -1, 3, 4, -1 },
                { -1, 5, -1, -1, 1, 2, -1, -1, -1 },{ 5, -1, -1, 1, 2, 3, -1, -1, -1 },{ -1, -1, 8, 2, 3, 4, -1, -1, -1 }, { -1, 8, -1, 3, 4, -1, -1, -1, -1 }};

            double[,] DistArray = new double[,] {{ -1, -1, -1, -1, 0, 1, -1, 1, -1 },{ -1, -1, -1, 1, 0, 1, 1.414, -1, -1 },{ -1, -1, -1, 1, 0, 1, -1, -1, 1.414 },{ -1, -1, -1, 1, 0, -1, -1, 1, -1 },
                { -1, 1, 1.414, -1, 0, -1, -1, 1, 1.414 }, { -1, -1, -1, -1, 0, -1, -1, -1, -1 },{ -1, -1, -1, -1, 0, -1, -1, -1, -1 },{ 1.414, 1, -1, -1, 0, -1, 1.414, 1, -1 },
                { -1, 1, -1, -1, 0, 1, -1, -1, -1 },{ 1.414, -1, -1, 1, 0, 1, -1, -1, -1 },{ -1, -1, 1.414, 1, 0, 1, -1, -1, -1 },{ -1, 1, -1, 1, 0, -1, -1, -1, -1 }};

            // Begynder med at konstruere en ny graf, hvor <int,Edge<int>> betyder at knuderne
            // er repr�senteret ved heltal og kanterne er repr�senteret ved Edge<int> med heltal som
            // knuder.
            AdjacencyGraph<int, Edge<int>> graph = new AdjacencyGraph<int, Edge<int>>(false);

            // Vi kan nu begynde at fylde knuder i grafen.
            int n = KnudeArray.GetLength(0); // knuderne 0,1,2,3,...,11 ialt 12 knuder bliver tilf�jet. 
            for (int i = 0; i < n; i++)
            {
                // Tilf�jer knude med nummeret i til grafen.
                graph.AddVertex(i);
            }

            Dictionary<Edge<int>, double> cost = new Dictionary<Edge<int>, double>();
            // Hver af kanterne konstrueres og tilf�jes b�de graph (AdjacencyGraph) 
            // og cost (Dictionary)
            Dictionary<string, Edge<int>> dic = new Dictionary<string,Edge<int>>();
            try
            {

                //// Kanter som g�r ud af knude 0
                int iterationsCounter = 1;
                for (int i = 0; i < KnudeArray.GetLength(0); i++)
                {
                    for (int j = 0; j < KnudeArray.GetLength(1); j++)
                    {
                        if (KnudeArray[i, j] != -1)
                        {
                            dic.Add("e" + iterationsCounter, new Edge<int>(i, KnudeArray[i, j]));
                            Edge<int> kant = dic["e" + iterationsCounter];
                            graph.AddEdge(kant); // Tilf�jer kanten e1 til grafen
                            cost.Add(kant, DistArray[i, j]);
                            iterationsCounter++;
                        }

                    }
                }

            }
            catch (KeyNotFoundException ex)
            {
                // Denne exception opst�r hvis du pr�ver at inds�tte en kant, som bruger knuder der
                // ikke allerede findes i grafen.
                Console.WriteLine(" Der er ikke overensstemmelse mellem knuder og kant argumenter. ");
                Console.WriteLine(ex);
            }

            DijkstraShortestPathAlgorithm<int, Edge<int>> shortest_path
                = new DijkstraShortestPathAlgorithm<int, Edge<int>>(graph,
                        delegate(Edge<int> e)
                        {
                            double value = 0.0;
                            cost.TryGetValue(e, out value);
                            return value;
                        }
                    );

            // Nu kan jeg l�se denne korteste vej problemet fra knude 0 til alle andre
            // knuder ved at anvende Dijkstra's algoritme
            try
            {
                for (int j = 0; j <= DistArray.GetLength(0); j++)
                {
                    shortest_path.Compute(j);

                    // Herefter kan jeg hente afstanden ved blot at referere til Distance-array'et
                    // for shortest_path. 
                    for (int i = 0; i <= DistArray.GetLength(0); i++)
                    {
                        Console.WriteLine("Korteste vej fra {2} til {0} er lig {1}.", i, shortest_path.Distances[i], j);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

 

Now my problem is, when i hardcode the paths like this:

 

                Edge<int> e1 = new Edge<int>(0, 1); // Kant fra knude 0 til knude 1
                graph.AddEdge(e1); // Tilf�jer kanten e1 til grafen
                cost.Add(e1, 10);  // Tildeler kanten e1 omkostningen 10. 

                Edge<int> e2 = new Edge<int>(0, 2);
                graph.AddEdge(e2);
                cost.Add(e2, 5);

                // Kanter som g�r ud af knude 1
                Edge<int> e3 = new Edge<int>(1, 2);
                graph.AddEdge(e3);
                cost.Add(e3, 2);

and so on there is no problems what so ever with compute metode. But now that i have extended the problem to take in a full section of the warehouse, the 2 arrays in the top, node array and edgecost array i get this error: 

System.Collections.Generic.KeyNotFoundException: Den givne n�gle var ikke til stede i ordbogen.
   ved System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   ved QuickGraph.Algorithms.Search.BreadthFirstSearchAlgorithm`2.FlushVisitQueue()
   ved QuickGraph.Algorithms.Search.BreadthFirstSearchAlgorithm`2.Visit(TVertexs)
   ved QuickGraph.Algorithms.ShortestPath.DijkstraShortestPathAlgorithm`2.ComputeNoInit(TVertex s)
   ved QuickGraph.Algorithms.ShortestPath.DijkstraShortestPathAlgorithm`2.ComputeFromRoot(TVertex rootVertex)
   ved QuickGraph.Algorithms.ShortestPath.DijkstraShortestPathAlgorithm`2.InternalCompute()
   ved QuickGraph.Algorithms.AlgorithmBase`1.Compute()
   ved QuickGraph.Algorithms.RootedAlgorithmBase`2.Compute(TVertex rootVertex)
   ved GraphExample.DijkstraExample.Main(String[] args) i C:\Users\Jonas\Documents\Visual Studio 2010\Projects\pla.cs:linje 139T

The Danish part of the error means something like: The Given key where not in the Dictionary.

Could someone help me avoid that error?

/sajon

 

Jul 14, 2011 at 9:56 PM

NVM i figured it out my self, it was an error in my arrays, 12 was not a vertex, since i counted first vertex from 0 to 11...