Пример #1
0
        /// <summary>
        /// Loads airport locations.
        /// </summary>
        /// <param name="numLocations">The number locations.</param>
        /// <exception cref="Exception">Error parsing file</exception>
        private static TravelingSalesmanDataset LoadAirports(int numLocations)
        {
            TravelingSalesmanDataset d;
            Random        rand;
            List <string> lines;
            int           index;

            string[] airport;
            double   lat, lon, x, y;

            d    = new TravelingSalesmanDataset();
            rand = new Random(421784);

            lines = File.ReadAllLines(@"./Resources/US Airports.txt").ToList();

            if (numLocations > lines.Count)
            {
                numLocations = lines.Count;
            }

            try
            {
                for (int i = 0; i < numLocations; i++)
                {
                    index   = rand.Next(0, lines.Count);
                    airport = lines[index].Split('\t');

                    lat = double.Parse(airport[1]);
                    lon = double.Parse(airport[2]);

                    // Convert (lat,lon) to (x,y)
                    // https://stackoverflow.com/questions/14329691/convert-latitude-longitude-point-to-a-pixels-x-y-on-mercator-projection
                    x = (lon + 180.0) * (100.0 / 360.0);
                    y = (100.0 / 2.0) - (100.0 * Math.Log(Math.Tan((Math.PI / 4.0) + ((lat * Math.PI / 180.0) / 2.0))) / (2.0 * Math.PI));

                    d.AddLocation(new TravellingSalesmanGA.Location(airport[0], x, y));
                    lines.RemoveAt(index);
                }
            }
            catch
            {
                // TODO handle different exceptions and provide better description of what went wrong
                throw new Exception("Error parsing file");
            }

            d.Normalize(100, 100);
            return(d);
        }
Пример #2
0
        /// <summary>
        /// Generates locations in a circle pattern.
        /// </summary>
        /// <param name="numLocations">The number locations.</param>
        /// <param name="altScale">The alt scale.</param>
        /// <returns></returns>
        private static TravelingSalesmanDataset GenerateCircle(int numLocations, double altScale = 1)
        {
            TravelingSalesmanDataset d;
            double step;

            d    = new TravelingSalesmanDataset();
            step = Math.PI / ((numLocations - 1) / 2);

            for (int i = 1; i <= numLocations; i++)
            {
                if (i % 2 == 0)
                {
                    d.AddLocation(new TravellingSalesmanGA.Location(i.ToString(), Math.Cos(step * i), Math.Sin(step * i)));
                }
                else
                {
                    d.AddLocation(new TravellingSalesmanGA.Location(i.ToString(), Math.Cos(step * i) * altScale, Math.Sin(step * i) * altScale));
                }
            }

            d.Normalize(100, 100);

            return(d);
        }