/// <summary> /// This method fixes the chromosome - there could be cycles before => this method removes the cycles /// using graph. /// </summary> public void FixChromosome() { if (Fitness != null) { return; } var graphHandler = new GraphHandler(); // in order to fix the chromosome we need to create graph and remove cycles from it // cycles are removed by rotating edges of the cycle until there is no cycle var graph = graphHandler.CreateGraph(this); graphHandler.BreakCycles(graph); // update the chromosome => get topologically sorted graph and rewrite values in machine indices var topologicalOrder = graphHandler.GetTopologicalOrder(graph); var machineIndices = new int[JobShop.MachinesCount]; var machineChromosomes = GetGenes().Select(x => x.Value).Cast <MachineChromosome>().ToList(); foreach (var operation in topologicalOrder.Where(x => x.Id > int.MinValue && x.Id < int.MaxValue)) { int machineId = operation.MachineId; ref int machineOperationIndex = ref machineIndices[machineId]; machineChromosomes[machineId].ReplaceGene(machineOperationIndex, new Gene(operation)); machineOperationIndex++; }
public void BreakCyclesReverseBackEdgeTest() { var randomizationProviderMock = new Mock <IRandomization>(); randomizationProviderMock.Setup(x => x.GetDouble()).Returns(Global.Config.BackEdgeSwitchOrientationProbability - 10e-6); // fix random sort order of AsShuffledEnumerable randomizationProviderMock.Setup(x => x.GetInt(0, int.MaxValue)).Returns(0); RandomizationProvider.Current = randomizationProviderMock.Object; var graph = graphHandler.CreateGraph(chromosome); bool[,] edgesBefore = GetNeighborhoodMatrix(graph, jobShop); List <(Operation Operation1, Operation Operation2)> reversedEdges = graphHandler.BreakCycles(graph); Assert.That(graphHandler.HasCycles(graph), Is.False); Assert.That(reversedEdges, Has.Count.EqualTo(2)); Assert.That(reversedEdges, Does.Contain((jobShop.Operations[5], jobShop.Operations[0]))); Assert.That(reversedEdges, Does.Contain((jobShop.Operations[4], jobShop.Operations[0]))); AssertGraphEqualExceptFor(edgesBefore, graph, reversedEdges); }