我用java写的遗传算法的严重问题,程序没错,但不知道为什么结果总和预想的不同,实在是困惑,请帮忙啊!!!!(三)
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.4582376848633213.734213877165864 8.995601149398595 6130.934874824773
84.4582376848633213.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.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
136130.9348748247730.0
136130.9348748247730.0
刚完成精英主义后
84.4582376848633213.734213877165864 8.995601149398595 6130.934874824773
84.4582376848633213.734213877165864 8.995601149398595 6130.934874824773
8.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
8.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
8.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
8.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
这是j2se1.5上的结果,照理应该出现的是:
84.4582376848633213.734213877165864 8.995601149398595 6130.934874824773
84.4582376848633213.734213877165864 8.995601149398595 6130.934874824773
8.46941042175651813.734213877165864 46.36169514458831 0.0
8.46941042175651813.734213877165864 46.36169514458831 6850.415380534821
8.46941042175651813.734213877165864 46.36169514458831 0.0
8.46941042175651813.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;但为什么其它的两个也改变了?
请知道者告诉我一声,本人实在是不懂。
万分感谢!!!
[解决办法]
结果不对 程序肯定有问题啊....... 初学看不懂 帮顶
[解决办法]
初始化后
89.6157682437933127.69760391949800446.56811861146418 0.0
86.257719048533937.73432345961917382.40845215523663 0.0
43.03975188617505492.047457574042286.37679052285688 0.0
76.0487130749598934.154970092443364100.46834261084781 0.0
44.48901662399876584.9814291218114385.484734373696 0.0
0.00.00.0 0.0
计算适应值后
89.6157682437933127.69760391949800446.56811861146418 0.0
86.257719048533937.73432345961917382.40845215523663 0.0
43.03975188617505492.047457574042286.37679052285688 0.0
76.0487130749598934.154970092443364100.46834261084781 0.0
44.48901662399876584.9814291218114385.484734373696 0.0
0.00.00.0 0.0
保持最好值后
89.6157682437933127.69760391949800446.56811861146418 0.0
86.257719048533937.73432345961917382.40845215523663 0.0
43.03975188617505492.047457574042286.37679052285688 0.0
76.0487130749598934.154970092443364100.46834261084781 0.0
44.48901662399876584.9814291218114385.484734373696 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
选择后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
交叉后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
变异后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
计算适应值后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
刚完成精英主义前
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
89.6157682437933127.69760391949800446.56811861146418 0.0
430.00.0
刚完成精英主义后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
精英主义后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
选择后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
交叉后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
变异后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
计算适应值后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
刚完成精英主义前
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
430.00.0
刚完成精英主义后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
精英主义后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
选择后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
交叉后
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
0.00.00.0 0.0
[解决办法]
同一基数所产生的随机数序列是一样的,可以用下面这一段程序进行印证:
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");
}
}
}