Example #1
0
        public bool Equals(CompEdge p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((base.Target == p.Target) && (base.Source == p.Source));
        }
Example #2
0
        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return(false);
            }

            // If parameter cannot be cast to Point return false.
            CompEdge p = obj as CompEdge;

            if ((System.Object)p == null)
            {
                return(false);
            }

            // Return true if the fields match:
            return((base.Source == p.Source) && (base.Target == p.Target));
        }
Example #3
0
        public static void Create(List <Node> Nodes, List <Way> Ways)
        {
            //create the graph
            var Graph = new BidirectionalGraph <int, CompEdge>();

            //add each node referenced by a way
            foreach (Way Path in Ways)
            {
                for (int i = 0; i < Path.References.Count; i++)
                {
                    int Id = (int)Path.References[i].Id;

                    // make sure the node was not already added
                    if (!Graph.Vertices.Contains(Id))
                    {
                        Graph.AddVertex(Id);

                        short Altitude = AltitudeMap.AltitudeAtPosition(Path.References[i].Location);

                        NodeAltitudes.Add(Id, (int)Altitude);
                    }
                }
            }


            //create an altitude map
            Bitmap AltitudeImage = new Bitmap(64, 64, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            List <double> LongitudeGradient = Gradient(Program.LowerBound.Longitude, Program.UpperBound.Longitude, 64);
            List <double> LatitudeGradient  = Gradient(Program.LowerBound.Latitude, Program.UpperBound.Latitude, 64);



            //iterate over altitudes
            for (int longitudeindex = 0; longitudeindex < LongitudeGradient.Count; longitudeindex++)
            {
                for (int latitudeindex = 0; latitudeindex < LatitudeGradient.Count; latitudeindex++)
                {
                    short Altitude = AltitudeMap.AltitudeAtPosition(new GeoCoordinate(LatitudeGradient[latitudeindex], LongitudeGradient[longitudeindex]));

                    AltitudeImage.SetPixel(longitudeindex, latitudeindex, Color.FromArgb(Altitude, Altitude, Altitude));
                }
            }


            // setup the bitmap that all of the data will be saved to
            AltitudeImage.Save("RegionalAltitudeImage.bmp");

            //add each edge
            foreach (Way Path in Ways)
            {
                //add all of the edges
                for (int i = 1; i < Path.References.Count; i++)
                {
                    Node FirstNode  = Path.References[i - 1];
                    Node SecondNode = Path.References[i];

                    //add the edge
                    Graph.AddEdge(new CompEdge((int)FirstNode.Id, (int)SecondNode.Id));
                    Graph.AddEdge(new CompEdge((int)SecondNode.Id, (int)FirstNode.Id));

                    //add the distance edge
                    double Distance = DistanceBetweenPlaces(FirstNode.Location, SecondNode.Location);

                    //add both distances
                    Distances.Add(new CompEdge((int)FirstNode.Id, (int)SecondNode.Id), Distance);
                    Distances.Add(new CompEdge((int)SecondNode.Id, (int)FirstNode.Id), Distance);

                    //get the altitudes
                    double FirstNodeAltitude  = NodeAltitudes[(int)FirstNode.Id];
                    double SecondNodeAltitude = NodeAltitudes[(int)SecondNode.Id];

                    //if we don't know either of the altitudes, assume 0 change
                    if (FirstNodeAltitude != -1 || SecondNodeAltitude != -1)
                    {
                        AltitudeChange.Add(new CompEdge((int)FirstNode.Id, (int)SecondNode.Id), FirstNodeAltitude - SecondNodeAltitude);
                        AltitudeChange.Add(new CompEdge((int)SecondNode.Id, (int)FirstNode.Id), SecondNodeAltitude - FirstNodeAltitude);
                    }
                    else
                    {
                        AltitudeChange.Add(new CompEdge((int)FirstNode.Id, (int)SecondNode.Id), 0);
                        AltitudeChange.Add(new CompEdge((int)SecondNode.Id, (int)FirstNode.Id), 0);
                    }
                }
            }

            //calcuate costs for each edge
            var Costs = new Dictionary <CompEdge, double>();

            //cycle through each edge and calculate its cost
            foreach (Edge <int> Edge in Graph.Edges)
            {
                CompEdge Key = new CompEdge(Edge.Source, Edge.Target);

                double DistanceCost = Distances[Key] * 1000;            //convert to meters
                double AltitudeCost = Math.Pow(AltitudeChange[Key], 2); //square the altitude (in meters)

                double Weight = DistanceCost;
                Costs.Add(Key, Weight);
            }


            //create arrays that the data will be stored in
            string[] NodeLines = new string[Graph.Vertices.Count()];
            string[] EdgeLines = new string[Graph.Edges.Count()];

            //save nodes to a .csv file
            for (int i = 0; i < Graph.Vertices.Count(); i++)
            {
                int Id = Graph.Vertices.ElementAt(i);

                NodeLines[i] = Id + "," + Node.GetById(Id).Location.Latitude + "," + Node.GetById(Id).Location.Longitude;
            }

            //save edges to a .csv file
            for (int i = 0; i < Graph.Edges.Count(); i++)
            {
                int Source = Graph.Edges.ElementAt(i).Source;
                int Target = Graph.Edges.ElementAt(i).Target;

                EdgeLines[i] = Source + "," + Target + "," + Costs[new CompEdge(Source, Target)];
            }

            File.WriteAllLines("nodes.csv", NodeLines);
            File.WriteAllLines("edges.csv", EdgeLines);


            int From = 0;
            int To   = 5;


            var CostIndexer = AlgorithmExtensions.GetIndexer(Costs);
            var tryGetPath  = Graph.ShortestPathsDijkstra(CostIndexer, From);

            IEnumerable <CompEdge> path;

            if (tryGetPath(To, out path))
            {
                string ToWrite = "";

                foreach (CompEdge Edge in path)
                {
                    ToWrite += Convert.ToString(Edge.Source) + ",";
                }

                File.WriteAllText("chosenpath.csv", ToWrite.Remove(ToWrite.Length - 1)); // remove the last comma
            }

            //create a visualizer for the graph
            //GraphvizAlgorithm<int, CompEdge> graphviz = new GraphvizAlgorithm<int, CompEdge>(Graph);
            //string output = "output.dot";
            //graphviz.Generate(new FileDotEngine(), output);

            // assumes dot.exe is on the path:
            //var args = string.Format(@"{0} -Tjpg -O", output);
            //System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Graphviz2.38\bin\dot.exe", args);
        }