读书人

小弟我用java写的遗传算法的严重有关问

发布时间: 2012-01-30 21:15:58 作者: rapoo

我用java写的遗传算法的严重问题,程序没错,但不知道为什么结果总和预想的不同,实在是困惑,请帮忙啊!!!!
源程序如下:
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.*;
public class GeneticAlgorithm {
static int POPSIZE = 5; /* population size */
static int MAXGENS = 5; /* max. number of generations */
static int NVARS = 3; /* no. of problem variables */
static float PXOVER = 0.8f; /* probability of crossover */
static float PMUTATION = 0.3f; /* probability of mutation */
int E = 6;
int T = 120;
int generation; /* current generation no. */
int cur_best; /* best individual */
BufferedReader infile;
FileReader in;
BufferedWriter outfile;
FileWriter out;
Genotype population[];
Genotype newpopulation[];
int p[][][] = { { {0, 1, 1}, {0, 0, 0}, {0, 1, 1}, {0, 0, 0}
}, { {1, 0, 0}, {0, 0, 0}, {1, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 1, 1}, {0, 0, 0}, {0, 1, 1}
}, { {0, 0, 0}, {1, 0, 0}, {0, 0, 0}, {1, 0, 0}
}
};
double a[][][] = { { {10, 1, 1}, {10, 2, 4}, {1, 2.3, 1}, {3, 5, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 2}, {2, 0.7, 4}
}, { {2.5, 1.2, 3.4}, {10, 1, 1}, {1, 0.8, 13}, {1.9, 1, 1}
}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {3.2, 1, 0.5}, {1, 1.4, 1.6}
}
};
double u[][][] = { { {2, 1, 1}, {3, 2, 4}, {1, 2.3, 1}, {3, 2, 1}
}, { {1, 3, 0.8}, {2, 3, 1.5}, {1, 1, 0.9}, {2, 0.7, 4}
}, { {0.7, 1.2, 3.4}, {0.5, 1, 1}, {1, 0.8, 0.9}, {1.9, 1, 1}


}, { {0.4, 3, 2.3}, {1, 2.1, 4}, {0.4, 1, 0.3}, {1, 1.4, 0.8}
}
};
double s[][][] = { { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}, { {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}
}
};
GeneticAlgorithm() {
population = new Genotype[POPSIZE + 1]; /* population */
newpopulation = new Genotype[POPSIZE + 1]; /* new population; */
for (int i = 0; i <= POPSIZE; i++) {
population[i] = new Genotype();
newpopulation[i] = new Genotype();
}
}
class Genotype /* genotype (GT), a member of the population */
{
double gene[];
double fitness;
double upper[];
double lower[];
double cfitness;
double rfitness;
Genotype() {
gene = new double[NVARS]; /* a string of variables */
fitness = 0; /* GT 's fitness */
upper = new double[NVARS];
lower = new double[NVARS]; /* GT 's variables lower bound */


rfitness = 0;
cfitness = 0;
}
}
void initialize() {
String str;
String parts[];
try {
in = new FileReader( "gadata.txt ");
infile = new BufferedReader(in);
int i, j;
double lbound, ubound;
while ((str = infile.readLine()) != null) {
parts = str.split( " ");
for (i = 0; i < NVARS; i++) {
lbound = Double.parseDouble(parts[0]);
ubound = Double.parseDouble(parts[1]);

for (j = 0; j < POPSIZE; j++) {
population[j].fitness = 0;
population[j].rfitness = 0;
population[j].cfitness = 0;
population[j].lower[i] = lbound;
population[j].upper[i] = ubound;
population[j].gene[i] = randval(population[j].lower[i],


population[j].upper[i]);


}
}
}
infile.close();
}

catch (IOException e1) {
System.out.println( "Can not open input file! ");
System.exit(0);
}
}
double randval(double low, double high) {
double val;
val = (double) java.lang.Math.random() * (high - low) + low;
return (val);
}
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;
}
}
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];
}

}


[解决办法]
楼主,你写的关于遗传的东西太专业,
而且程序写的很不规范,没有详细的注释,比如
int E = 6;
int T = 120;
别人很难看懂的。
你可以介绍一下整个程序的流程(当然最好能介绍点遗传的基本知识)
再做下详细的注释,或许就有很多人帮你了!
[解决办法]
至少也对变量算法注释下!!!
[解决办法]
而且你不应该(double) java.lang.Math.random() * (high - low) + low;
应该用Random类,rd.nextDouble();

读书人网 >J2SE开发

热点推荐