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;但为什么其它的两个也改变了?
请知道者告诉我一声,本人实在是不懂。
万分感谢!!!
解决方案 »
- sql语句怎么设置字段的默认值
- 从oracle中取出空值数据,在Java中该如何处理啊,等待大侠!
- 一个和tomcat 相关的问题
- 急! JTable页角在我的单位可以打印出来,别的单位打印不出来了,但内容都能正常打印(看代码)。
- 各位前辈.怎么写给*@tom.com发mail的JAVA CLASS 急急急急急急急急
- 帮我看看怎么了?解决就给分~~~~~GUI的问题
- 有关 java 异常的 异常表现
- jce 求助
- 我用的是win2k为什么我的jdk路径怎么设都不对,请帮帮我
- 请问那种JAVA工具带有丰富的控件(组件),就象VB MSCHART
- jsp生成动态树
- 哪里有java的面试笔试题目啊?
我有点怀疑java.lang.Math.random()是不是随机数啊..怎么出现好多是一样的,一点也不变.
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0计算适应值后
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0刚完成精英主义前
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.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
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
实在想不通.我仔细看过程序,没有问题.但就是不变.不知道为什么?
然而我单独把那randval函数提出来做实验却没有问题.实在是搞不懂...
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");
}
}
}