/** * Place the cities in random locations. */ private void initCities() { cities = new City[CITIES]; for (int i = 0; i < cities.Length; i++) { int xPos = (int)(ThreadSafeRandom.NextDouble() * MAP_SIZE); int yPos = (int)(ThreadSafeRandom.NextDouble() * MAP_SIZE); cities[i] = new City(xPos, yPos); } }
/// <summary> /// Place the cities in random locations. /// </summary> private void InitCities() { Random rand = new Random(); cities = new City[CITIES]; for (int i = 0; i < cities.Length; i++) { int xPos = (int)(rand.NextDouble() * MAP_SIZE); int yPos = (int)(rand.NextDouble() * MAP_SIZE); cities[i] = new City(xPos, yPos); } }
public TSPGenome(GeneticAlgorithm owner, City[] cities) { int[] organism = new int[cities.Length]; bool[] taken = new bool[cities.Length]; for (int i = 0; i < organism.Length; i++) { taken[i] = false; } for (int i = 0; i < organism.Length - 1; i++) { int icandidate; do { icandidate = (int)(ThreadSafeRandom.NextDouble() * organism.Length); } while (taken[icandidate]); organism[i] = icandidate; taken[icandidate] = true; if (i == organism.Length - 2) { icandidate = 0; while (taken[icandidate]) { icandidate++; } organism[i + 1] = icandidate; } } this.pathChromosome = new Chromosome(); this.Chromosomes.Add(this.pathChromosome); for (int i = 0; i < organism.Length; i++) { IntegerGene gene = new IntegerGene(); gene.Value = organism[i]; this.pathChromosome.Genes.Add(gene); } Organism = organism; Encode(); }
/// <summary> /// Returns how close the city is to another city. /// </summary> /// <param name="cother">The other city.</param> /// <returns>A distance.</returns> public int Proximity(City cother) { return Proximity(cother.X, cother.Y); }
public TSPScore(City[] cities) { this.cities = cities; }