public static Graph CreateChild(Graph mother, Graph father) { var childSize = (mother.Size + father.Size) / 2; if ((mother.Size + father.Size) % 2 == 1 && GoodRandom.Bool()) { childSize++; } var child = new int[childSize, childSize]; var decision = false; for (var i = 0; i < childSize; i++) { for (var j = 0; j < i; j++) { var edgeExistance = mother.GetValueRound(i, j) + father.GetValueRound(i, j); switch (edgeExistance) { case 0: decision = GoodRandom.Next(100) < 1; if (decision) { child[i, j] = 1; child[j, i] = 1; } break; case 1: decision = GoodRandom.Next(100) < 49; if (decision) { child[i, j] = 1; child[j, i] = 1; } break; case 2: decision = GoodRandom.Next(100) < 98; if (decision) { child[i, j] = 1; child[j, i] = 1; } break; default: throw new ArgumentException("Parent graphs are invalid, values other than 0 or 1 found"); } } } return(new Graph(child)); }
private void RemoveRandomEdges(int numberOfEdgesToRemove) { var edges = GetEdges(); if (edges.Count > 0) { for (var i = 0; i < numberOfEdgesToRemove; i++) { var edge = edges[GoodRandom.Next(edges.Count)]; _graph[edge.from, edge.to] = 0; _graph[edge.to, edge.from] = 0; edges.Remove(edge); if (edges.Count <= 0) { break; } } } }
private void AddRandomEdges(int numberOfEdgesToAdd) { var possibleEdges = GetPossibleEdges(); if (possibleEdges.Count > 0) { for (var i = 0; i < numberOfEdgesToAdd; i++) { var edge = possibleEdges[GoodRandom.Next(possibleEdges.Count)]; _graph[edge.from, edge.to] = 1; _graph[edge.to, edge.from] = 1; possibleEdges.Remove(edge); if (possibleEdges.Count <= 0) { break; } } } }
public static Graph CreateRandomGraph(int maxSize) { var size = GoodRandom.Next(maxSize) + 1; var matrix = new int[size, size]; for (var i = 0; i < size; i++) { for (var j = 0; j < i; j++) { if (GoodRandom.Bool()) { matrix[i, j] = 1; matrix[j, i] = 1; } } } return(new Graph(matrix)); }
private int SelectParentIndex(int?blockedIndex) { var indices = new List <int>(); for (var i = 0; i < _generation.Count; i++) { if (blockedIndex.HasValue && blockedIndex.Value == i) { continue; } for (var j = 0; j < _generation[i].NormalizedScore; j++) { indices.Add(i); } } var result = GoodRandom.Next(indices.Count); return(indices[result]); }
private void KillHalfOfTheGeneration() { var indicesOfGraphsToRemove = new List <int>(); var index = 0; while (indicesOfGraphsToRemove.Count < _generationSize / 2) { if (GoodRandom.Next(_generationSize) > index && !indicesOfGraphsToRemove.Contains(index)) { indicesOfGraphsToRemove.Add(index); } index = (index + 1) % _generationSize; } indicesOfGraphsToRemove.Sort((index1, index2) => - index1.CompareTo(index2)); foreach (var i in indicesOfGraphsToRemove) { _generation.RemoveAt(i); } }
public void Mutate() { var vertexProbability = GoodRandom.Next(100); var edgeProbability = GoodRandom.Next(100); var signDecision = GoodRandom.Bool(); var sign = signDecision ? -1 : 1; var vertexChange = GetChange(vertexProbability); var edgeChange = GetChange(edgeProbability); vertexChange *= sign; edgeChange *= sign; if (vertexChange != 0) { var newSize = Size + vertexChange; if (newSize > 0) { if (newSize > Size) { AddRandomVertex(vertexChange); } else { RemoveRandomVertex(-vertexChange); } } } if (edgeChange != 0) { if (edgeChange > 0) { AddRandomEdges(edgeChange); } else { RemoveRandomEdges(-edgeChange); } } }