public Tuple <MoveList, MoveList> DoCrossover(MoveList firstMovelist, MoveList secondMovelist) { var notSelectedFields = new List <int>(); for (var i = 0; i < firstMovelist.Count; i++) { var selectField = _randomizer.NextBoolean(); if (selectField) { notSelectedFields.Add(i); } } var child1 = new MoveList(); for (var i = 0; i < firstMovelist.Count; i++) { if (notSelectedFields.Contains(i)) { child1.Add(secondMovelist[i]); } else { child1.Add(firstMovelist[i]); } } notSelectedFields.Clear(); for (var i = 0; i < firstMovelist.Count; i++) { var selectField = _randomizer.NextBoolean(); if (selectField) { notSelectedFields.Add(i); } } var child2 = new MoveList(); for (var i = 0; i < firstMovelist.Count; i++) { if (notSelectedFields.Contains(i)) { child2.Add(firstMovelist[i]); } else { child2.Add(secondMovelist[i]); } } return(new Tuple <MoveList, MoveList>(child1, child2)); }
public Tuple <MoveList, MoveList> DoCrossover(MoveList firstMovelist, MoveList secondMovelist) { var child1 = new MoveList(); var child2 = new MoveList(); if (firstMovelist.Count >= 3) { var firstCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); var secondCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); while (secondCrossoverPoint == firstCrossoverPoint) { secondCrossoverPoint = _randomizer.Next(1, firstMovelist.Count - 1); } var lowestCrossoverPoint = (firstCrossoverPoint < secondCrossoverPoint ? firstCrossoverPoint : secondCrossoverPoint); var highestCrossoverPoint = (firstCrossoverPoint > secondCrossoverPoint ? firstCrossoverPoint : secondCrossoverPoint); var difference = highestCrossoverPoint - lowestCrossoverPoint; child1.AddRange( firstMovelist.Take(lowestCrossoverPoint) .Concat(secondMovelist.Skip(lowestCrossoverPoint).Take(difference)) .Concat(firstMovelist.Skip(highestCrossoverPoint)) ); child2.AddRange( secondMovelist.Take(lowestCrossoverPoint) .Concat(firstMovelist.Skip(lowestCrossoverPoint).Take(difference)) .Concat(secondMovelist.Skip(highestCrossoverPoint)) ); var children = new Tuple <MoveList, MoveList>(child1, child2); return(children); } else { var performOnePointCrossover = _randomizer.NextBoolean(); //Not using the crossover factory to make sure a TwoPointCrossover won't be created. if (performOnePointCrossover) { var opc = new OnePointCrossover(_randomizer); var children = opc.DoCrossover(firstMovelist, secondMovelist); return(children); } else { var oc = new OrderedCrossover(_randomizer); var children = oc.DoCrossover(firstMovelist, secondMovelist); return(children); } } }
public Tuple <MoveList, MoveList> DoCrossover(MoveList firstMovelist, MoveList secondMovelist) { var child1 = new MoveList(); var child2 = new MoveList(); if (firstMovelist.Count > 2) { //If crossover = 0, you simply swap the entire sequence. This doesn't yield a new child //Same if the crossoverPoint = length - 1. var elementsToSkip = _randomizer.Next(1, firstMovelist.Count - 2); child1.AddRange(firstMovelist.Take(elementsToSkip).Concat(secondMovelist.Skip(elementsToSkip))); child2.AddRange(secondMovelist.Take(elementsToSkip).Concat(firstMovelist.Skip(elementsToSkip))); } else if (firstMovelist.Count == 1) { child1.Add(firstMovelist[0]); child2.Add(secondMovelist[0]); } else { var swapFirstMove = _randomizer.NextBoolean(); if (swapFirstMove) { child1.Add(secondMovelist[0]); child1.Add(firstMovelist[1]); child2.Add(firstMovelist[0]); child2.Add(secondMovelist[1]); } else { child1.Add(firstMovelist[0]); child1.Add(secondMovelist[1]); child2.Add(secondMovelist[0]); child2.Add(firstMovelist[1]); } } var children = new Tuple <MoveList, MoveList>(child1, child2); return(children); }