public static MoveLocations ( |
||
locations | ||
fromIndex | int | |
toIndex | int | |
return | void |
public static void MoveLocations(Location[] locations, int fromIndex, int toIndex)
{
if (locations == null)
throw new ArgumentNullException("locations");
if (fromIndex < 0 || fromIndex >= locations.Length)
throw new ArgumentOutOfRangeException("fromIndex");
if (toIndex < 0 || toIndex >= locations.Length)
throw new ArgumentOutOfRangeException("toIndex");
var temp = locations[fromIndex];
if (fromIndex < toIndex)
{
for(int i=fromIndex+1; i<=toIndex; i++)
locations[i-1] = locations[i];
}
else
{
for(int i=fromIndex; i>toIndex; i--)
locations[i] = locations[i-1];
}
locations[toIndex] = temp;
}
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(); } } }