public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations) { ExecutionEnvironment.Arr <int> f = field.Clone(); int validPositionCount = 0; int pos = 0; for (int x = 0; x < field.W; x++) { for (int y = 0; y < field.H; y++) { if (8 > f.SimilarNeighbors(x, y, IncludeFieldBorders)) { f[pos] = -1; validPositionCount++; } pos++; } } mutations = Math.Min(mutations, validPositionCount); for (int i = 0; i < mutations; i++) { field.Swap(f.FreePosition(random.Next(0, validPositionCount), -1), f.FreePosition(random.Next(0, validPositionCount), -1)); } }
public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations) { for (int m = 0; m < mutations; m++) { ExecutionEnvironment.Arr <int> f = field.Clone(); int startPos = random.Next(0, field.Length); int color = f[startPos]; int validPositionCount = markField(f, color, startPos); field.Swap(startPos, f.FreePosition(random.Next(0, validPositionCount), -1)); } }
public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations) { for (int m = 0; m < mutations; m++) { // remove invalid cells (cells with only neighbors of same color) ExecutionEnvironment.Arr <int> f = field.Clone(); int validCount = 0; for (int y = 0; y < f.H; y++) { for (int x = 0; x < f.W; x++) { if (field.NeighborsWithValueHV(field[x, y], x, y, 1, true) == 4) { f[x, y] = -1; } else { validCount++; } } } // choose start pos and color int startPos = f.ValidPosition(random.Next(0, validCount), -1); f[startPos] = -1; validCount--; int color = field[startPos]; ExecutionEnvironment.Arr <int> f2 = f.Clone(); // remove all cells that have no neighbor with matching color and cells with same color for (int y = 0; y < f.H; y++) { for (int x = 0; x < f.W; x++) { if (f[x, y] != -1 && (f[x, y] == color || f.NeighborsWithValueHV(color, x, y, 1, false) == 0)) { f2[x, y] = -1; validCount--; } } } // select target at random int targetPos = f2.ValidPosition(random.Next(0, validCount), -1); // swap field.Swap(startPos, targetPos); } }
public static int markField(ExecutionEnvironment.Arr <int> f, int color, int startpos) { ExecutionEnvironment.Arr <int> field = f.Clone(); field[startpos] = -1; int validPositionCount = 0; int pos = 0; for (int y = 0; y < f.H; y++) { for (int x = 0; x < f.W; x++) { if (pos != startpos && field[pos] != color && 0 < field.NeighborsWithValueHV(color, x, y, 1, false)) { f[pos] = -1; validPositionCount++; } pos++; } } return(validPositionCount); }
public override void Mutate(Random random, ExecutionEnvironment.Arr <int> field, int mutations) { for (int i = 0; i < mutations; i++) { ExecutionEnvironment.Arr <int> f = field.Clone(); int validPositionCount = 0; int pos = 0; for (int y = 0; y < field.H; y++) { for (int x = 0; x < field.W; x++) { if (8 > f.SimilarNeighbors(x, y, IncludeFieldBorders)) { f[pos] = -1; validPositionCount++; } pos++; } } int steps = random.Next(1, validPositionCount); int position = f.FreePosition(random.Next(0, validPositionCount), -1); int xPos = position % field.W; int yPos = position / field.W; int processor = f[position]; for (int i2 = 0; i2 < steps; i2++) { f[position] = -1; int next = f.NextPosition(random, processor, -1, xPos, yPos); if (next == -1) { break; } field.Swap(position, next); position = next; } } }