public WorldMapGenetic() { InitializeComponent(); Random rand = new Random(); // place the cities at random locations int height = this.Height; int width = this.Width; this.cities = new City[WorldMapGenetic.CITY_COUNT]; for (int i = 0; i < WorldMapGenetic.CITY_COUNT; i++) { this.cities[i] = new City((int)(rand.NextDouble() * (width - 10)), (int)(rand.NextDouble() * (height - 64))); } this.genetic = new TSPGeneticAlgorithm(this.cities, WorldMapGenetic.POPULATION_SIZE, WorldMapGenetic.MUTATION_PERCENT, 0.25, 0.5, WorldMapGenetic.CITY_COUNT / 5); start(); }
public TSPChromosome(TSPGeneticAlgorithm owner, City [] cities) { this.GA = owner; this.cities = cities; int [] genes = new int[this.cities.Length]; bool [] taken = new bool[cities.Length]; Random rand = new Random(); for (int i = 0; i < genes.Length; i++) { taken[i] = false; } for (int i = 0; i < genes.Length - 1; i++) { int icandidate; do { icandidate = (int)(rand.NextDouble() * genes.Length); } while (taken[icandidate]); genes[i] = icandidate; taken[icandidate] = true; if (i == genes.Length - 2) { icandidate = 0; while (taken[icandidate]) { icandidate++; } genes[i + 1] = icandidate; } } this.Genes = genes; CalculateCost(); }