接上一贴:
void elitist() {
        int i;
        double best, worst; /* best and worst fitness values */
        int best_mem = 0;
        int worst_mem = 0;
        best = population[0].fitness;
        worst = population[0].fitness;
        for (i = 0; i < POPSIZE - 1; ++i) {
            if (population[i].fitness > population[i + 1].fitness) {
                if (population[i].fitness >= best) {
                    best = population[i].fitness;
                    best_mem = i;
                }
                if (population[i + 1].fitness <= worst) {
                    worst = population[i + 1].fitness;
                    worst_mem = i + 1;
                }
            } else {
                if (population[i].fitness <= worst) {
                    worst = population[i].fitness;
                    worst_mem = i;
                }
                if (population[i + 1].fitness >= best) {
                    best = population[i + 1].fitness;
                    best_mem = i + 1;
                }
            }
        }
        System.out.println("刚完成精英主义前");
        display();
System.out.print("\n\n"+best_mem+"\t"+worst_mem+"\t"+best+"\t"+worst+"\n\n");
        if (best >= population[POPSIZE].fitness) {
            for (i = 0; i < NVARS; i++) {
                population[POPSIZE].gene[i] = population[best_mem].gene[i];
            }
            population[POPSIZE].fitness = population[best_mem].fitness;
        }
        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;}
        }
        System.out.println("刚完成精英主义后");
        display();
    }
    void select() {
        int mem, i, j;
        double sum = 0;
        double p;
        for (mem = 0; mem < POPSIZE; mem++) {
            sum += population[mem].fitness;
        }
        for (mem = 0; mem < POPSIZE; mem++) {
            population[mem].rfitness = population[mem].fitness / sum;
        }
        population[0].cfitness = population[0].rfitness;
        /* calculate cumulative fitness */
        for (mem = 1; mem < POPSIZE; mem++) {
            population[mem].cfitness = population[mem - 1].cfitness +
                                       population[mem].rfitness;
        }
        /* finally select survivors using cumulative fitness. */
        for (i = 0; i < POPSIZE; i++) {
            p = java.lang.Math.random();
            if (p < population[0].cfitness) {
                newpopulation[i] = population[0];
            } else {
                for (j = 0; j < POPSIZE - 1; j++) {
                    if (p >= population[j].cfitness &&
                        p < population[j + 1].cfitness) {
                        newpopulation[i] = population[j + 1];
                        break;
                    }
                }
            }
        }
        for (i = 0; i < POPSIZE; i++) {
            population[i] = newpopulation[i];
        }
    }
    void crossover() {
        int mem, one = 0;
        int first = 0; /* count of the number of members chosen */
        double x;
        for (mem = 0; mem < POPSIZE; mem++) {
            x = java.lang.Math.random();
            if (x < PXOVER) {
                ++first;
                if (first % 2 == 0) {
                    Xover(one, mem);
                } else {
                    one = mem;
                }
            }
            for (int j = 0; j < POPSIZE; j++) {
            }
        }
    }
    void Xover(int one, int two) {
        int i;
        int point; /* crossover point */
        double temp;
        if (NVARS > 1) {
            if (NVARS == 2) {
                point = 1;
            } else {
                point = ((int) (java.lang.Math.random() * 100) % (NVARS - 1)) +
                        1;
            }
            for (i = 0; i < point; i++) {
                temp = 0;
                temp = population[one].gene[i];
                population[one].gene[i] = population[two].gene[i];
                population[two].gene[i] = temp;
            }
        }
    }

解决方案 »

  1.   

    兄弟呀,不是我自己没调试,我差不多都搞了两天了,实在是找不出错在哪里,
    我用c++改写之后,运行得很好呀,但在jbuilder2006下面,就是不对劲,我要崩溃了!!
      

  2.   

    你是不是random()没设种子的原因,生成了相同的随机数
    java.util.Random rd = new Random();
    for(i = 0;i<3;i++)
    {
     rand.setSeed(i);   
    }
      

  3.   

    总共只有两个类,其中一个内部类.用来保存种群信息也就是newpopulation和populationr .其余的全是函数,都是基本遗传算法必须有的,不是很难.而且也没有用到二进制编码.我想出问题的地方就在精英主义这块也就是保留上次最优解而不被丢掉这个模块.