Exemple #1
0
    public static void Main()
    {
        GAIndividual.adj_matrix = GAUtils.readMatrix(@"d:\graph.txt");
        int nvert = GAIndividual.adj_matrix.Length;         // number of vertices

        GAEvolve.evolveAndMakeMFile(@"d:\ga.m", 1, 400, 200, nvert, 50, 50);
    }
Exemple #2
0
    public static void evolveAndMakeMFile(string file_name, int runs, int generations, int pop_size, int nvert, int xrate, int mrate)
    {
        // makes a M file (file_name) so that if we run this file in MATLAB
        // the best fitness versus generation, average fitness versus generation
        // and wrost fitness versus generation are plotted.

        // best, average and worst fitness values are the average of "runs" evolutions.

        GAEvolve[] gae = new GAEvolve[runs];
        for (int i = 0; i < runs; i++)
        {
            gae[i] = new GAEvolve(generations, pop_size, nvert, xrate, mrate);
        }

        float[] avr_best_fitness  = new float[generations];
        float[] avr_avr_fitness   = new float[generations];
        float[] avr_worst_fitness = new float[generations];

        for (int i = 0; i < generations; i++)
        {
            avr_best_fitness[i]  = 0;
            avr_avr_fitness[i]   = 0;
            avr_worst_fitness[i] = 0;
            for (int j = 0; j < runs; j++)
            {
                avr_best_fitness[i]  += gae[j].best_fitness[i] / (float)runs;
                avr_avr_fitness[i]   += gae[j].avr_fitness[i] / (float)runs;
                avr_worst_fitness[i] += gae[j].worst_fitness[i] / (float)runs;
            }
        }

        try
        {
            System.IO.StreamWriter @out = new System.IO.StreamWriter(file_name, false);

            @out.WriteLine("\n% M.M.Haji");
            @out.WriteLine("% EC project # 3");
            @out.WriteLine("% GA progression results:\n");

            @out.Write("best_fitness = [");
            for (int i = 0; i < avr_best_fitness.Length; i++)
            {
                @out.Write(avr_best_fitness[i] + " ");
            }
            @out.WriteLine("];");

            @out.Write("average_fitness = [");
            for (int i = 0; i < avr_avr_fitness.Length; i++)
            {
                @out.Write(avr_avr_fitness[i] + " ");
            }
            @out.WriteLine("];");

            @out.Write("worst_fitness = [");
            for (int i = 0; i < avr_worst_fitness.Length; i++)
            {
                @out.Write(avr_worst_fitness[i] + " ");
            }
            @out.WriteLine("];\n");

            @out.WriteLine("plot(1:" + generations + ",best_fitness,1:" + generations + ",average_fitness,1:" + generations + ",worst_fitness)");
            @out.WriteLine("\nlegend('best fitness','average fitness','worst fitness');");
            @out.WriteLine("xlabel('generation');");
            @out.WriteLine("ylabel('fitness');");

            @out.Close();
        }
        catch (Exception ex1)
        {
            Console.WriteLine("error: can't create " + file_name);
        }
        Console.WriteLine(gae[gae.Length - 1].best_ind);
    }