public override int[] GenerateOffspring(int[][] parents) { var parentLength = parents[0].Length; var currentVertex = parents[0][0]; var offspring = new int[parentLength]; offspring[0] = currentVertex; var availableVertexes = new List <int>(parents[0]); availableVertexes.Remove(currentVertex); var counter = 1; var parentsLength = parents.Length; var currentParentIndex = 0; while (counter < parentLength) { var nextVertex = -1; var selectedParent = parents[currentParentIndex]; var indexOfCurrentVertexInSelectedParent = Array.IndexOf(selectedParent, currentVertex); if (indexOfCurrentVertexInSelectedParent < parentLength - 1 && !offspring.Contains(selectedParent[indexOfCurrentVertexInSelectedParent + 1])) { nextVertex = selectedParent[indexOfCurrentVertexInSelectedParent + 1]; if (currentParentIndex >= parentsLength - 1) { currentParentIndex = 0; } else { currentParentIndex++; } } _randomizationChances++; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { _randomizedResolvesCount++; var resolverResult = ResolverRandomized.ResolveConflict(selectedParent[indexOfCurrentVertexInSelectedParent], availableVertexes); if (resolverResult != -1) { nextVertex = resolverResult; } } if (nextVertex == -1) { nextVertex = ResolverConflict.ResolveConflict(selectedParent[indexOfCurrentVertexInSelectedParent], availableVertexes); _resolveCount++; } offspring[counter] = nextVertex; availableVertexes.Remove(nextVertex); currentVertex = nextVertex; counter++; } return(offspring); }
public override int[] GenerateOffspring(int[][] parents) { var parentLength = parents[0].Length; var currentVertex = parents[0][0]; var offspring = new int[parentLength]; offspring[0] = currentVertex; var availableVertexes = new List <int>(parents[0]); availableVertexes.Remove(currentVertex); var counter = 1; while (counter < parentLength) { var feasibleParents = new List <int[]>(parents); var nextVertex = -1; for (var i = 0; i < parents.Length; i++) { var whichParent = Random.Next(0, feasibleParents.Count); var selectedParent = feasibleParents[whichParent]; var indexOfCurrentVertexInSelectedParent = Array.IndexOf(selectedParent, currentVertex); if (indexOfCurrentVertexInSelectedParent >= parentLength - 1 || offspring.Contains(selectedParent[indexOfCurrentVertexInSelectedParent + 1])) { feasibleParents.Remove(selectedParent); continue; } nextVertex = selectedParent[indexOfCurrentVertexInSelectedParent + 1]; } _randomizationChances++; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { _randomizedResolvesCount++; var resolverResult = ResolverRandomized.ResolveConflict(currentVertex, availableVertexes); if (resolverResult != -1) { nextVertex = resolverResult; } } if (nextVertex == -1) { _resolveCount++; nextVertex = ResolverConflict.ResolveConflict(currentVertex, availableVertexes); } offspring[counter] = nextVertex; availableVertexes.Remove(nextVertex); currentVertex = nextVertex; counter++; } return(offspring); }
public override int[] GenerateOffspring(int[][] parents) { var geneLength = parents[0].Length; var parentsNumber = parents.Length; var offspring = new int[geneLength]; var available = parents[0].ToList(); available.Remove(parents[0][0]); offspring[0] = parents[0][0]; int iterator = 1; for (int j = 1; j < geneLength; j++) { if (offspring.Contains(parents[j % parentsNumber][j])) { int selected = -1; while (selected == -1 || offspring.Contains(selected)) { _resolveCount++; selected = ResolverConflict.ResolveConflict(offspring[j - 1], available); } available.Remove(selected); offspring[iterator++] = selected; } else { var selected = parents[j % parentsNumber][j]; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { var resolverResult = ResolverRandomized.ResolveConflict(offspring[j - 1], available); if (resolverResult != -1) { selected = resolverResult; } } available.Remove(selected); offspring[iterator++] = selected; } } return(offspring); }
public override int[] GenerateOffspring(int[][] parents) { var parentLength = parents[0].Length; var currentVertex = parents[0][0]; var offspring = new int[parentLength]; offspring[0] = currentVertex; var availableVertexes = new List <int>(parents[0]); availableVertexes.Remove(currentVertex); var counter = 1; while (counter < parentLength) { var feasibleParents = new List <int[]>(parents); var nextVertex = -1; for (var i = 0; i < parents.Length; i++) { var selectedParent = parents[i]; var indexOfCurrentVertexInSelectedParent = Array.IndexOf(selectedParent, currentVertex); if (indexOfCurrentVertexInSelectedParent >= parentLength - 1 || offspring.Contains(selectedParent[indexOfCurrentVertexInSelectedParent + 1])) { feasibleParents.Remove(selectedParent); } } var min = double.MaxValue; int minIndex = -1; for (int i = 0; i < feasibleParents.Count; i++) { if (DistancesMatrix[currentVertex][ feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]] < min) { min = DistancesMatrix[currentVertex][ feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]]; minIndex = i; } } if (minIndex >= 0) { nextVertex = feasibleParents[minIndex][Array.IndexOf(feasibleParents[minIndex], currentVertex) + 1]; } _randomizationChances++; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { _randomizedResolvesCount++; var resolverResult = ResolverRandomized.ResolveConflict(currentVertex, availableVertexes); if (resolverResult != -1) { nextVertex = resolverResult; } } if (nextVertex == -1) { _resolveCount++; nextVertex = ResolverConflict.ResolveConflict(currentVertex, availableVertexes); } offspring[counter] = nextVertex; availableVertexes.Remove(nextVertex); currentVertex = nextVertex; counter++; } return(offspring); }
public override int[] GenerateOffspring(int[][] parents) { var parentLength = parents[0].Length; var currentVertex = parents[0][0]; var offspring = new int[parentLength]; offspring[0] = currentVertex; var availableVertexes = new List <int>(parents[0]); availableVertexes.Remove(currentVertex); var counter = 1; while (counter < parentLength) { var feasibleParents = new List <int[]>(parents); var nextVertex = -1; for (var i = 0; i < parents.Length; i++) { var selectedParent = parents[i]; var indexOfCurrentVertexInSelectedParent = Array.IndexOf(selectedParent, currentVertex); if (indexOfCurrentVertexInSelectedParent >= parentLength - 1 || offspring.Contains(selectedParent[indexOfCurrentVertexInSelectedParent + 1])) { feasibleParents.Remove(selectedParent); } } double[] fitness = new double[feasibleParents.Count]; for (int i = 0; i < feasibleParents.Count; i++) { fitness[i] = 1 / DistancesMatrix[currentVertex][ feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]]; } var sum = fitness.Sum(); var approx = Random.NextDouble() * sum; for (int i = 0; i < fitness.Length; i++) { approx += fitness[i]; if (approx >= sum) { nextVertex = feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]; break; } } _randomizationChances++; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { _randomizedResolvesCount++; var resolverResult = ResolverRandomized.ResolveConflict(currentVertex, availableVertexes); if (resolverResult != -1) { nextVertex = resolverResult; } } if (nextVertex == -1) { _resolveCount++; nextVertex = ResolverConflict.ResolveConflict(currentVertex, availableVertexes); } offspring[counter] = nextVertex; availableVertexes.Remove(nextVertex); currentVertex = nextVertex; counter++; } return(offspring); }
public override int[] GenerateOffspring(int[][] parents) { var parentLength = parents[0].Length; var currentVertex = parents[0][0]; var offspring = new int[parentLength]; offspring[0] = currentVertex; var availableVertexes = new List <int>(parents[0]); availableVertexes.Remove(currentVertex); var counter = 1; while (counter < parentLength) { var feasibleParents = new List <int[]>(parents); var nextVertex = -1; //for (var i = 0; i < parents.Length; i++) //{ // var selectedParent = parents[i]; // var indexOfCurrentVertexInSelectedParent = Array.IndexOf(selectedParent, currentVertex); // if (indexOfCurrentVertexInSelectedParent >= parentLength - 1 || // offspring.Contains(selectedParent[indexOfCurrentVertexInSelectedParent + 1])) // { // feasibleParents.Remove(selectedParent); // } //} //var min = double.MaxValue; //int minIndex = -1; //for (int i = 0; i < feasibleParents.Count; i++) //{ // if (DistancesMatrix[currentVertex][ // feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]] < min) // { // min = DistancesMatrix[currentVertex][ // feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1]]; // minIndex = i; // } //} Dictionary <int, double> fitness = new Dictionary <int, double>(); for (int i = 0; i < feasibleParents.Count; i++) { if (Array.IndexOf(feasibleParents[i], currentVertex) < (parentLength - 1)) { if (!fitness.ContainsKey(feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1])) { fitness.Add(feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) + 1], DistancesMatrix[currentVertex][feasibleParents[i][Array.IndexOf (feasibleParents[i], currentVertex) + 1]]); } } if (Array.IndexOf(feasibleParents[i], currentVertex) != 0) { if (!fitness.ContainsKey(feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) - 1])) { fitness.Add(feasibleParents[i][Array.IndexOf(feasibleParents[i], currentVertex) - 1], DistancesMatrix[currentVertex][feasibleParents[i][Array.IndexOf (feasibleParents[i], currentVertex) - 1]]); } } } foreach (var elem in fitness) { if (offspring.ToList().Contains(elem.Key)) { fitness.Remove(elem.Key); } } if (fitness.Count() == 0) { bool flag = false; while (flag = false) { var rand = new Random(); int randomParent = rand.Next(parents.Count()); int randomElem = rand.Next(parentLength); flag = true; for (int i = 0; i < parentLength; i++) { if (offspring[i] == parents[randomParent][randomElem]) { flag = false; break; } } if (flag) { nextVertex = parents[randomParent][randomElem]; } } } else { int minimalValue = fitness.Aggregate((l, r) => l.Value < r.Value ? l : r).Key; nextVertex = minimalValue; } _randomizationChances++; if (Random.NextDouble() < ResolverRandomized.RandomizationProbability) { _randomizedResolvesCount++; nextVertex = ResolverRandomized.ResolveConflict(currentVertex, availableVertexes); } if (nextVertex == -1) { _resolveCount++; nextVertex = ResolverConflict.ResolveConflict(currentVertex, availableVertexes); } offspring[counter] = nextVertex; availableVertexes.Remove(nextVertex); currentVertex = nextVertex; counter++; } return(offspring); }