-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniform_crossover.java
More file actions
92 lines (77 loc) · 2.15 KB
/
uniform_crossover.java
File metadata and controls
92 lines (77 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* 自己按教材中原理实现,如有错误还请指出(qiang.learning@gmail.com)
*/
import java.util.BitSet;
public class Main {
public static void main(String[] args) {
final int chrom_len = 5;
int i;
BitSet temp1Node = new BitSet(chrom_len);
BitSet temp2Node = new BitSet(chrom_len);
temp1Node.set(0);temp1Node.clear(1);;temp1Node.set(2);temp1Node.set(3);temp1Node.clear(4); //{1,0,1,1,0}
// temp2Node.set(0);temp2Node.clear(1);;temp2Node.set(2);temp2Node.set(3);temp2Node.clear(4);
temp2Node.clear(0);temp2Node.clear(1);;temp2Node.set(2);temp2Node.clear(3);temp2Node.set(4); //{0,0,1,0,1}
temp1Node.xor(temp2Node);
System.out.println(temp1Node.cardinality()==0);
//显示初始化值
System.out.println("---------初始值---------");
System.out.println(temp1Node);
System.out.println(temp2Node);
for(i=0;i<chrom_len;i++)
{
System.out.print(temp1Node.get(i)+" ");
}
System.out.println();
for(i=0;i<chrom_len;i++)
{
System.out.print(temp2Node.get(i)+" ");
}
//uniform crossover
double r;
boolean temp_p1,temp_p2;
int[] W= new int[chrom_len]; // 屏蔽字
int j;
for(j=0;j<chrom_len;j++)
{
r = Math.random();
if(r>0.5)//执行互换操作
{
W[j]=1;
temp_p1 = temp1Node.get(j);
temp_p2 = temp2Node.get(j);
if (temp_p1==temp_p2) // 相同,交叉也没有效果
continue;
else
{
if (temp_p1) // parent1的第i位为1,parent2的第i位为0
{
temp1Node.clear(j);
temp2Node.set(j);
}
else // parent2的第i位为1,parent1的第i位为0
{
temp2Node.clear(j);
temp1Node.set(j);
}
}
}
}
System.out.println("\n---------均匀交叉后结果---------");
System.out.println(temp1Node);
System.out.println(temp2Node);
for(i=0;i<chrom_len;i++)
{
System.out.print(temp1Node.get(i)+" ");
}
System.out.println();
for(i=0;i<chrom_len;i++)
{
System.out.print(temp2Node.get(i)+" ");
}
System.out.println("\n屏蔽字为:");
for(int a:W)
System.out.print(a+" ");
System.out.println("\n temp2Node中设置为true的位数");
System.out.println(temp2Node.cardinality());
}
}