Ejemplo n.º 1
0
        /// <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++;
            }
Ejemplo n.º 2
0
        public void CreateGraphAndHasCycleTest()
        {
            var graph = graphHandler.CreateGraph(chromosome);

            // reference vertex is source
            var source = graph.ReferenceVertex;

            Assert.That(source.InEdges, Is.Empty);
            Assert.That(source.OutEdges, Has.Count.EqualTo(2));

            var target = graph.FindVertex(new Operation(int.MaxValue, 0, 0, 0, 0));

            Assert.That(target.InEdges, Has.Count.EqualTo(2));
            Assert.That(target.OutEdges, Is.Empty);

            var operation0 = graph.FindVertex(jobShop.Operations[0]);
            var operation1 = graph.FindVertex(jobShop.Operations[1]);
            var operation2 = graph.FindVertex(jobShop.Operations[2]);
            var operation3 = graph.FindVertex(jobShop.Operations[3]);
            var operation4 = graph.FindVertex(jobShop.Operations[4]);
            var operation5 = graph.FindVertex(jobShop.Operations[5]);

            // 0-th operation
            Assert.That(operation0.InEdges.Count, Is.EqualTo(3));
            Assert.That(operation0.InEdges, Does.Contain(source));
            Assert.That(operation0.InEdges, Does.Contain(operation4));
            Assert.That(operation0.InEdges, Does.Contain(operation5));
            Assert.That(operation0.OutEdges.Count, Is.EqualTo(1));
            Assert.That(operation0.OutEdges, Does.Contain(operation1));

            // 1-th operation
            Assert.That(operation1.InEdges.Count, Is.EqualTo(1));
            Assert.That(operation1.InEdges, Does.Contain(operation0));
            Assert.That(operation1.OutEdges.Count, Is.EqualTo(2));
            Assert.That(operation1.OutEdges, Does.Contain(operation2));
            Assert.That(operation1.OutEdges, Does.Contain(operation3));

            // 2-th operation
            Assert.That(operation2.InEdges.Count, Is.EqualTo(3));
            Assert.That(operation2.InEdges, Does.Contain(operation1));
            Assert.That(operation2.InEdges, Does.Contain(operation5));
            Assert.That(operation2.InEdges, Does.Contain(operation4));
            Assert.That(operation2.OutEdges.Count, Is.EqualTo(1));
            Assert.That(operation2.OutEdges, Does.Contain(target));

            // 3-th operation
            Assert.That(operation3.InEdges.Count, Is.EqualTo(2));
            Assert.That(operation3.InEdges, Does.Contain(source));
            Assert.That(operation3.InEdges, Does.Contain(operation1));
            Assert.That(operation3.OutEdges.Count, Is.EqualTo(1));
            Assert.That(operation3.OutEdges, Does.Contain(operation4));

            // 4-th operation
            Assert.That(operation4.InEdges.Count, Is.EqualTo(1));
            Assert.That(operation4.InEdges, Does.Contain(operation3));
            Assert.That(operation4.OutEdges.Count, Is.EqualTo(3));
            Assert.That(operation4.OutEdges, Does.Contain(operation5));
            Assert.That(operation4.OutEdges, Does.Contain(operation2));
            Assert.That(operation4.OutEdges, Does.Contain(operation0));

            // 5-th operation
            Assert.That(operation5.InEdges.Count, Is.EqualTo(1));
            Assert.That(operation5.InEdges, Does.Contain(operation4));
            Assert.That(operation5.OutEdges.Count, Is.EqualTo(3));
            Assert.That(operation5.OutEdges, Does.Contain(target));
            Assert.That(operation5.OutEdges, Does.Contain(operation0));
            Assert.That(operation5.OutEdges, Does.Contain(operation2));

            // has cycle verification
            Assert.That(graphHandler.HasCycles(graph), Is.True);
        }