void mutate() {
        int i, j;
        double lbound, hbound;
        double x;        for (i = 0; i < POPSIZE; i++) {
            for (j = 0; j < NVARS; j++) {
                x = java.lang.Math.random();
                if (x < PMUTATION) {
                    /* find the bounds on the variable to be mutated */
                    lbound = population[i].lower[j];
                    hbound = population[i].upper[j];
                    population[i].gene[j] = randval(lbound, hbound);
                }
            }        }
    }
   void report() {
        int i;
        double best_val; /* best population fitness */
        double avg; /* avg population fitness */
        double stddev; /* std. deviation of population fitness */
        double sum_square; /* sum of square for std. calc */
        double square_sum; /* square of sum for std. calc */
        double sum; /* total population fitness */
        sum = 0.0;
        sum_square = 0.0;
        for (i = 0; i < POPSIZE; i++) {
            sum += population[i].fitness;
            sum_square += population[i].fitness * population[i].fitness;
        }
        avg = sum / (double) POPSIZE;
        square_sum = avg * avg * POPSIZE;
        stddev = java.lang.Math.sqrt((sum_square - square_sum) / (POPSIZE - 1));        best_val = population[POPSIZE].fitness;
        try {
            outfile.write("\r\n" + generation + "       ");
            outfile.write(best_val + "      "); 
            outfile.write(avg + "      "); 
            outfile.write(stddev + "      ");
        } catch (IOException e1) {
        }
    }
    public void Entrance() {
        int i;
        double sum = 0;
        try {
            out = new FileWriter("galog.txt");
            outfile = new BufferedWriter(out);
            outfile.write("\r\ngeneration  best  average  standard\r\n");
            outfile.write("number      value fitness  deviation \r\n");
        }
        catch (IOException e1) {
            return;
        }
        generation = 0;
        initialize();
        System.out.println("初始化后");
        display();
        evaluate();
        System.out.println("计算适应值后");
        display();
        keep_the_best();
        System.out.println("保持最好值后");
        display();
        while (generation < MAXGENS) {
            generation++;
            select();
            System.out.println("选择后");
            display();
            crossover();
            System.out.println("交叉后");
            display();
            mutate();
            System.out.println("变异后");
            display();
            evaluate();
            System.out.println("计算适应值后");
            display();
            elitist();
            System.out.println("精英主义后");
            display();
            report();
        }
        try {
            outfile.write("\r\n\r\n Simulation completed\r\n");
            outfile.write("\r\n Best member: \r\n");
            for (i = 0; i < NVARS; i++) {
                outfile.write("\r\n var(" + i + ") = " +
                              (int) population[POPSIZE].gene[i]);
                sum += (int) population[POPSIZE].gene[i];
            }
            outfile.write("\r\n var(" + i + ") = " + (T - (int) sum));            outfile.write("\r\n\r\n Best fitness =" +
                          population[POPSIZE].fitness);
            outfile.close();
        } catch (IOException e1) {
        }
        System.out.println("Success\r\n");
    }
    public static void main(String[] args) {
        GeneticAlgorithm a = new GeneticAlgorithm();
        a.Entrance();
    }
}
输入文件为:gadata.txt
内容为:
6 102
6 102
6 102
输出文件为:galog.txt
下面是我机子上出现的一种结果:
刚完成精英主义前
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
36.911334415772814  65.37750347990035  69.6563480569634   0.0
36.911334415772814  65.37750347990035  69.6563480569634   0.0
36.911334415772814  65.37750347990035  69.6563480569634   0.0
8.469410421756518 13.734213877165864  46.36169514458831   6850.4153805348211 3 6130.934874824773 0.01 3 6130.934874824773 0.0刚完成精英主义后
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
这是j2se1.5上的结果,照理应该出现的是:
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
84.45823768486332 13.734213877165864  8.995601149398595   6130.934874824773
8.469410421756518 13.734213877165864  46.36169514458831   0.0
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
8.469410421756518 13.734213877165864  46.36169514458831   0.0
8.469410421756518 13.734213877165864  46.36169514458831   6850.415380534821
我不知道是不是函数elitist()中
else {
            for (i = 0; i < NVARS; i++) {
              population[worst_mem].gene[i] = population[POPSIZE].gene[i];
            }
            {System.out.print("\n\n"+best_mem+"\t"+worst_mem+"\t"+best+"\t"+worst+"\n\n");
            population[worst_mem].fitness = population[POPSIZE].fitness;}
        }
应该else语句只执行一吹才对呀,在此应该是
population[3].fitness = population[POPSIZE].fitness;但为什么其它的两个也改变了?
请知道者告诉我一声,本人实在是不懂。
万分感谢!!!

解决方案 »

  1.   

    源程序 前面两个贴子加上就完整了.
    我有点怀疑java.lang.Math.random()是不是随机数啊..怎么出现好多是一样的,一点也不变.
      

  2.   

    变异后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0计算适应值后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0刚完成精英主义前
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.04 3 0.0 0.0刚完成精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0选择后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0交叉后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0变异后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0计算适应值后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0刚完成精英主义前
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.04 3 0.0 0.0刚完成精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0选择后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0交叉后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0变异后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0计算适应值后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0刚完成精英主义前
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.04 3 0.0 0.0刚完成精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0精英主义后
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0
    0.0 0.0 0.0              0.0Success
      

  3.   

    上面是控制台打出来的,以下是galog.txt
    generation  best  average  standard
    number      value fitness  deviation 1       0.0      0.0      0.0      
    2       0.0      0.0      0.0      
    3       0.0      0.0      0.0      
    4       0.0      0.0      0.0      
    5       0.0      0.0      0.0       Simulation completed Best member:  var(0) = 0
     var(1) = 0
     var(2) = 0
     var(3) = 120 Best fitness =0.0
      

  4.   

    上面这位大虾,出现这种情况是可能的..就是不知道在变异的时候为什么随机数不起作用了..
    实在想不通.我仔细看过程序,没有问题.但就是不变.不知道为什么?
    然而我单独把那randval函数提出来做实验却没有问题.实在是搞不懂...
      

  5.   

    同一基数所产生的随机数序列是一样的,可以用下面这一段程序进行印证:
    import java.util.randompublic class RandomTest{     
             public static void main(String[] args){
             Random random1=new Random(100);
             Random random2=new Random(100);
             for(int i=0;i<5;i++){
                 System.out.print(random1.nextInt()+"\t");
                 System.out.println(random2.nextInt()+"\t");
             } 
        }
    }