private Location[] _Reproduce(Location[] parent) { var result = (Location[])parent.Clone(); if (!MustDoCrossovers) { // When we are not using cross-overs, we always apply mutations. RandomProvider.MutateRandomLocations(result); return(result); } // if you want, you can ignore the next three lines of code and the next // if, keeping the call to RandomProvider.MutateRandomLocations(result); always // invoked and without crossovers. Doing that you will not promove evolution through // "sexual reproduction", yet the good result will probably be found. int otherIndex = RandomProvider.GetRandomValue(_populationWithDistances.Length / 2); var other = _populationWithDistances[otherIndex].Key; RandomProvider._CrossOver(result, other, MustMutateFailedCrossovers); if (!MustMutateFailedCrossovers) { if (RandomProvider.GetRandomValue(10) == 0) { RandomProvider.MutateRandomLocations(result); } } return(result); }
public void MutateDuplicates() { bool needToSortAgain = false; int countDuplicates = 0; var previous = _populationWithDistances[0]; for (int i = 1; i < _populationWithDistances.Length; i++) { var current = _populationWithDistances[i]; if (!previous.Key.SequenceEqual(current.Key)) { previous = current; continue; } countDuplicates++; needToSortAgain = true; RandomProvider.MutateRandomLocations(current.Key); _populationWithDistances[i] = new KeyValuePair <Location[], double>(current.Key, Location.GetTotalDistance(_startLocation, current.Key)); } if (needToSortAgain) { Array.Sort(_populationWithDistances, _sortDelegate); } }