コード例 #1
0
        private static PathGraph[] CycleCrossover(PathGraph Parent1, PathGraph Parent2)
        {
            Mark[]    P1Marked = new Mark[Parent1.Count];
            Mark[]    P2Marked = new Mark[Parent1.Count];
            PathGraph Child1   = new PathGraph(Parent1.Count, Parent1.SupersetGraph);
            PathGraph Child2   = new PathGraph(Parent1.Count, Parent1.SupersetGraph);

            for (int i = 1; i < Parent1.Count - 1; ++i)
            {
                if (P1Marked[Parent1[i].Index].IsMarked == true)
                {
                    continue;
                }

                Graph.Vertex X1 = Parent1[i]; P1Marked[X1.Index].Set(true, i);
                Graph.Vertex X2 = Parent2[i]; P2Marked[X2.Index].Set(true, i);
                do
                {
                    X1 = Parent1[Parent1.IndexOf(X2)]; P1Marked[X1.Index].Set(true, i);
                    X2 = Parent2[Parent1.IndexOf(X2)]; P2Marked[X2.Index].Set(true, i);
                } while (X2 != Parent1[i]);
            }

            // Set initial and final vertices to 0
            Child1[0] = Parent1[0];
            Child2[0] = Parent1[0];
            Child1[Parent1.Count - 1] = Parent1[0];
            Child2[Parent1.Count - 1] = Parent1[0];

            // Fill the children from cycles detected above
            for (int i = 1; i < Parent2.Count - 1; ++i)
            {
                if (P1Marked[Parent1[i].Index].Order % 2 == 0)         // 1, 3, 5 - Cycles
                {
                    Child1[i] = Parent1[i];
                    Child2[i] = Parent2[i];
                }
                else                                            // 2, 4, 6 - Cycles
                {
                    Child1[i] = Parent2[i];
                    Child2[i] = Parent1[i];
                }
            }
            return(new PathGraph[] { Child1, Child2 });
        }