Esempio n. 1
0
        public static Kotai[] cross(Kotai a, Kotai b, Random rand)
        {
            int div = rand.Next(1, a.Gene.Length - 1);

            char[] c = new char[div];
            char[] d = new char[a.Gene.Length - div];

            Kotai[] children = new Kotai[2];

            Array.Copy(a.Gene, c, div);
            Array.Copy(b.Gene, 0, d, 0, b.Gene.Length - div);
            children[0] = new Kotai(a.Gene.Length, c, d);

            Array.Copy(a.Gene, 0, d, 0, a.Gene.Length - div);
            Array.Copy(b.Gene, c, div);

            children[1] = new Kotai(a.Gene.Length, c, d);

            return(children);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // int seed = 1000;
            int    l            = 10;
            int    N            = 5;
            int    G            = 10000;
            double mutationRate = 0.01;

            Kotai[] hoge = new Kotai[N];

            // Random rand = new Random(seed);
            Random rand = new Random();

            for (var i = 0; i < hoge.Length; i++)
            {
                hoge[i] = new Kotai(l, rand);
            }

            for (var i = 0; i < G; i++)
            {
                for (var j = 0; j < N; j++)
                {
                    double adaption = hoge[j].fitness();
                }
                foreach (var e in hoge)
                {
                    Console.WriteLine(e);
                }
                Console.WriteLine();
                Array.Sort(hoge);
                if (hoge[hoge.Length - 1].Tekiodo == 100.0)
                {
                    Console.WriteLine(i + 1);
                    break;
                }
                // foreach (var e in hoge){
                //     Console.WriteLine(e);
                // }
                Kotai   maxAdaptionKotai = hoge[hoge.Length - 1];
                Kotai[] tmp = new Kotai[hoge.Length - 1];
                Array.Copy(hoge, tmp, tmp.Length);

                int     choicedIndex = choice(tmp, rand);
                Kotai[] children1    = Kotai.cross(maxAdaptionKotai, tmp[choicedIndex], rand);

                // foreach (var e in tmp)
                // {
                //     Console.WriteLine(e);
                // }
                // Console.WriteLine();
                Kotai[] tmp2 = new Kotai[tmp.Length - 1];
                Array.Copy(tmp, tmp2, choicedIndex);
                Array.Copy(tmp, choicedIndex + 1, tmp2, choicedIndex, tmp.Length - choicedIndex - 1);
                // foreach (var e in tmp2)
                // {
                //     Console.WriteLine(e);
                // }
                // Console.WriteLine();
                choicedIndex = choice(tmp2, rand);
                Kotai[] children2 = Kotai.cross(maxAdaptionKotai, tmp2[choicedIndex], rand);
                Array.Copy(children1, hoge, children1.Length);
                Array.Copy(children2, 0, hoge, children1.Length, children2.Length);
                hoge[N - 1] = maxAdaptionKotai;

                // 突然変異
                if (mutationRate <= rand.NextDouble())
                {
                    int kotaiIndex = rand.Next(N);
                    int geneIndex  = rand.Next(l);
                    if (hoge[kotaiIndex].Gene[geneIndex] == '0')
                    {
                        hoge[kotaiIndex].Gene[geneIndex] = '1';
                    }
                    else
                    {
                        hoge[kotaiIndex].Gene[geneIndex] = '0';
                    }
                }
            }
        }