protected override bool Cross <Element>(List <Element> parent1, List <Element> parent2, List <Element> offspring1, List <Element> offspring2) { // W pierwszej iteracji i tak nie mamy macierzy dsm! if (DsmMatrix == null) { return(false); } int len = parent1.Count; int sg1 = geneRNG.Next(0, len), sg2 = geneRNG.Next(0, len); List <int> candidates1 = new List <int>(), candidates2 = new List <int>(); for (int i = 0; i < len; i++) { if (sg1 != i && DsmMatrix[sg1, i] >= Threshold) { candidates1.Add(i); } if (sg2 != i && DsmMatrix[sg2, i] >= Threshold) { candidates2.Add(i); } } foreach (var c in candidates1) { offspring1[c] = parent2[c]; } foreach (var c in candidates2) { offspring2[c] = parent1[c]; } // Ustawiamy dsm na null, zeby nie dalo sie dwa razy tej samej uzyc! DsmMatrix = null; return(true); }
private void OrderCross <Element>(List <Element> parent1, List <Element> parent2, List <Element> offspring) { int firstPoint = pointRnd.Next(0, parent1.Count - 1); int secondPoint = pointRnd.Next(0, parent1.Count - 1); var start = firstPoint < secondPoint ? firstPoint : secondPoint; var end = firstPoint > secondPoint ? firstPoint : secondPoint; List <Element> saveSequence = new List <Element>(); List <Element> restSequence = new List <Element>(); for (int i = start; i < end; i++) { saveSequence.Add(parent1[i]); } for (int i = 0; i < parent2.Count; i++) { if (!saveSequence.Contains(parent2[i])) { restSequence.Add(parent2[i]); } } for (int i = 0, s = 0, r = 0; i < offspring.Count; i++) { if (i >= start && i < end) { offspring[i] = saveSequence[s++]; } else { offspring[i] = restSequence[r++]; } } }
protected override bool Cross <Element>(List <Element> parent1, List <Element> parent2, List <Element> offspring1, List <Element> offspring2) { int crossPoint = pointRNG.Next(0, parent1.Count); for (int i = 0; i < crossPoint; ++i) { offspring1[i] = parent1[i]; offspring2[i] = parent2[i]; } for (int i = crossPoint; i < parent1.Count; ++i) { offspring1[i] = parent2[i]; offspring2[i] = parent1[i]; } return(true); }