public static void MutateRandomLocations(Location[] locations)
{
if (locations == null)
{
throw new ArgumentNullException("locations");
}
if (locations.Length < 2)
{
throw new ArgumentException("The locations array must have at least two items.", "locations");
}
// I opted to give up to 10% of the chromosome size in number of mutations.
// Maybe I should find a better number of make this configurable.
int mutationCount = GetRandomValue(locations.Length / 10) + 1;
for (int mutationIndex = 0; mutationIndex < mutationCount; mutationIndex++)
{
int index1 = GetRandomValue(locations.Length);
int index2 = GetRandomValue(locations.Length - 1);
if (index2 >= index1)
{
index2++;
}
switch (GetRandomValue(3))
{
case 0: Location.SwapLocations(locations, index1, index2); break;
case 1: Location.MoveLocations(locations, index1, index2); break;
case 2: Location.ReverseRange(locations, index1, index2); break;
default: throw new InvalidOperationException();
}
}
}