Пример #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
        public TravellingSalesmanGA(TravellingSalesmanParams p) :
            base(p)
        {
            dataset        = p.GetDataset();
            locations      = dataset.Locations;
            genotypeLength = locations.Count - 2;
            geneDomain     = Enumerable.Range(1, genotypeLength).ToList();

            LocationCount = locations.Count;

            //TODO see performance of using pre-calculated distance
            //distances = new Dictionary<LocationPair, double>(locations.Count);
            //for (int i = 0; i < locations.Count; i++)
            //  for (int j = i; j < locations.Count; j++)
            //  {
            //    Location a = locations[i];
            //    Location b = locations[j];
            //    double distance = Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));
            //    distances.Add(new LocationPair(a, b), distance);
            //  }
        }
Пример #3
0
        /// <summary>
        /// Generates random locations.
        /// </summary>
        /// <param name="numLocations">The number locations.</param>
        private static TravelingSalesmanDataset GenerateRandom(int numLocations, int?seed = null)
        {
            TravelingSalesmanDataset d;
            Random rand;

            d = new TravelingSalesmanDataset();

            if (seed != null)
            {
                rand = new Random((int)seed);
            }
            else
            {
                rand = new Random();
            }

            for (int i = 0; i < numLocations; i++)
            {
                d.AddLocation(new TravellingSalesmanGA.Location("Location #" + i, rand.NextDouble() * 100, rand.NextDouble() * 100));
            }

            return(d);
        }
Пример #4
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);
        }
Пример #5
0
 public TravelingSalesmanDataset GetDataset()
 {
     return(TravelingSalesmanDataset.Generate(DatasetType, NumLocations));
 }