public static ReverseRange ( |
||
locations | ||
startIndex | int | |
endIndex | int | |
return | void |
public static void ReverseRange(Location[] locations, int startIndex, int endIndex)
{
if (locations == null)
throw new ArgumentNullException("locations");
if (startIndex < 0 || startIndex >= locations.Length)
throw new ArgumentOutOfRangeException("startIndex");
if (endIndex < 0 || endIndex >= locations.Length)
throw new ArgumentOutOfRangeException("endIndex");
if (endIndex < startIndex)
{
int temp = endIndex;
endIndex = startIndex;
startIndex = temp;
}
while(startIndex<endIndex)
{
Location temp = locations[endIndex];
locations[endIndex] = locations[startIndex];
locations[startIndex] = temp;
startIndex++;
endIndex--;
}
}
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(); } } }