⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gajava.txt

📁 这是一个应用遗传算法解决的函数优化问题
💻 TXT
📖 第 1 页 / 共 5 页
字号:
                                population[j].upper[i]);

}
}
}
            infile.close();
}

catch (IOException e1) {
            System.out.println("Can not open input file!");
            System.exit(0);
}
}

/***********************************************************/
/* Random value generator: Generates a value within bounds */
/***********************************************************/

double randval(double low, double high) {
double val;
        val = (double) java.lang.Math.random() * (high - low) + low;
return (val);
}

/*************************************************************/
/* Evaluation function: This takes a user defined function.  */
/* Each time this is changed, the code has to be recompiled. */
/* The current function is:  x[1]^2-x[1]*x[2]+x[3]           */
/*************************************************************/
void display() {
int i, j;
for (j = 0; j <= POPSIZE; j++) {
for (i = 0; i < NVARS; i++) {
                System.out.print(population[j].gene[i] + "\t");
}
            System.out.print("             " + population[j].fitness);
            System.out.print("\n");
}
        System.out.print("\n");
}

void evaluate() {
int mem;
int i, j, k;
double t[] = new double[NVARS + 1];

double sum = 0;
double min = 0;

for (mem = 0; mem < POPSIZE; mem++) {
            sum = 0;
            min = 0;
for (i = 0; i < NVARS; i++) {
                t[i] = population[mem].gene[i];
                sum += t[i];
}
            t[3] = T - sum;
if (t[3] < E || t[3] > (T - 3 * E)) {
                population[mem].fitness = 0;
continue;
}
for (i = 1; i < 5; i++) {
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
if (s[i - 1][j][k] + a[i - 1][j][k] * t[i - 1] >=
                            p[i - 1][j][k] * u[i - 1][j][k] * t[i - 1]) {
                            s[i][j][k] = s[i - 1][j][k] + a[i - 1][j][k] * t[i -
                                         1] - p[i - 1][j][k] * u[i -
                                         1][j][k] * t[i - 1];
} else {
                            s[i][j][k] = 0;
}
}
}
}
for (j = 0; j < 4; j++) {
for (k = 0; k < 3; k++) {
//s[0][j][k]=s[4][j][k];

                    min += s[4][j][k];
}
}
            population[mem].fitness = 10000 - min;
}
}

/***************************************************************/
/* Keep_the_best function: This function keeps track of the    */
/* best member of the population. Note that the last entry in  */
/* the array Population holds a copy of the best individual    */
/***************************************************************/

void keep_the_best() {
int mem;
int i;
        cur_best = 0; /* stores the index of the best individual */

for (mem = 0; mem < POPSIZE; mem++) {
if (population[mem].fitness > population[POPSIZE].fitness) {
                cur_best = mem;
                population[POPSIZE].fitness = population[mem].fitness;
}
}
/* once the best member in the population is found, copy the genes */
for (i = 0; i < NVARS; i++) {
            population[POPSIZE].gene[i] = population[cur_best].gene[i];
}

}

/****************************************************************/
/* Elitist function: The best member of the previous generation */
/* is stored as the last in the array. If the best member of    */
/* the current generation is worse then the best member of the  */
/* previous generation, the latter one would replace the worst  */
/* member of the current population                             */
/****************************************************************/

void elitist() {
int i;
double best, worst; /* best and worst fitness values */
int best_mem = 0;
int worst_mem = 0;
/* indexes of the best and worst member */

        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;
}
}
}
/* if best individual from the new population is better than */
/* the best individual from the previous population, then    */
/* copy the best from the new population; else replace the   */
/* worst individual from the current population with the     */
/* best one from the previous generation                     */
        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();
}

/**************************************************************/
/* Selection function: Standard proportional selection for    */
/* maximization problems incorporating elitist model - makes  */
/* sure that the best member survives                         */
/**************************************************************/

void select() {
int mem, i, j;
double sum = 0;
double p;

/* find total fitness of the population */
for (mem = 0; mem < POPSIZE; mem++) {
            sum += population[mem].fitness;
}

/* calculate relative 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;
}
}
}
}
/* once a new population is created, copy it back */

for (i = 0; i < POPSIZE; i++) {
            population[i] = newpopulation[i];
}
}

/***************************************************************/
/* Crossover selection: selects two parents that take part in  */
/* the crossover. Implements a single point crossover          */
/***************************************************************/

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++) {
}
}
}

/**************************************************************/
/* Crossover: performs crossover of the two selected parents. */
/**************************************************************/

void Xover(int one, int two) {
int i;
int point; /* crossover point */
double temp;

/* select crossover point */
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;
}
}
}

/*************************************************************/
/* Swap: A swap procedure that helps in swapping 2 variables */
/*************************************************************/

/*void swap(double x, double y) {
        double temp;

        temp = x;
        x = y;
        y = temp;

         }*/

/**************************************************************/
/* Mutation: Random uniform mutation. A variable selected for */
/* mutation is replaced by a random value between lower and   */
/* upper bounds of this variable                              */
/**************************************************************/

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);
}
}

}
}

/***************************************************************/
/* Report function: Reports progress of the simulation. Data   */
/* dumped into the  output file are separated by commas        */
/***************************************************************/

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) {
}
}

/**************************************************************/
/* Main function: Each generation involves selecting the best */
/* members, performing crossover & mutation and then          */
/* evaluating the resulting population, until the terminating */
/* condition is satisfied                                     */
/**************************************************************/

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();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -