-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathChromosome.java
121 lines (104 loc) · 3.08 KB
/
Chromosome.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import javax.print.DocFlavor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Chromosome {
private boolean[] gene;
private double score;
public Chromosome() {
}
public Chromosome(int size) {
if (size <= 0)
return;
gene = new boolean[size];
for (int i = 0; i < size; i++)
gene[i] = Math.random() >= 0.5;
}
public static Chromosome clone(Chromosome chromosome) {
if (chromosome == null || chromosome.gene == null)
return null;
Chromosome copy = new Chromosome();
copy.gene = new boolean[chromosome.gene.length];
copy.score = chromosome.score;
System.arraycopy(chromosome.gene, 0, copy.gene, 0, chromosome.gene.length);
return copy;
}
/**
* 交叉操作
* @param p1:交叉对象1
* @param p2:交叉对象2
* @return 交叉后的对象
*/
public static List<Chromosome> genetic(Chromosome p1, Chromosome p2, double crossRate) {
if (p1 == null ||p2 == null)
return null;
if (p1.gene == null || p2.gene == null) {
return null;
}
if (p1.gene.length != p2.gene.length) {
return null;
}
Chromosome c1 = clone(p1);
Chromosome c2 = clone(p2);
if (Math.random() < crossRate) {
int size = c1.gene.length;
int roundA = (int)(Math.random() * size);
int roundB = (int)(Math.random() * size);
int max = Math.max(roundA, roundB);
int min = Math.min(roundA, roundB);
for (int i = min; i <= max; i++) {
boolean temp = c1.gene[i];
c1.gene[i] = c2.gene[i];
c2.gene[i] = temp;
}
}
List<Chromosome> chromosomes = new ArrayList<>();
chromosomes.add(c1);
chromosomes.add(c2);
return chromosomes;
}
public void mutation(int num) {
if (num == 0)
return;
int size = gene.length;
for (int i = 0; i < num; i++) {
int at = (int)(Math.random() * size) % size;
gene[at] = !gene[at];
}
}
public int binary2dec() {
if (gene == null)
return 0;
int num = 0;
for (boolean b : gene) {
num <<= 1;
if (b)
num++;
}
return num;
}
public boolean[] getGene() {
return gene;
}
public void setGene(boolean[] gene) {
this.gene = gene;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Chromosome{" +
"gene=" + Arrays.toString(gene) +
", score=" + score +
'}';
}
public static void main(String[] args) {
Chromosome chromosome = new Chromosome();
chromosome.gene = new boolean[]{false, true, false, false};
System.out.println(chromosome.binary2dec());
}
}