Repository navigation
Expand file tree
/
Copy pathIndividual.java
More file actions
187 lines (125 loc) · 4.06 KB
/
Copy pathIndividual.java
File metadata and controls
187 lines (125 loc) · 4.06 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Individual {
public int[] genotype;
Random rand;
String newline = System.getProperty("line.separator");
int product;
int sum;
List<Integer> sumPile;
List<Integer> productPile;
int numOfGenes;
/*
* Constructor for an individual
* post: individual's genotype is randomized
* */
public Individual(int numOfGenes){
this.numOfGenes = numOfGenes;
rand = new Random();
//randomize genotype
genotype = new int[numOfGenes];
for(int i = 0; i<numOfGenes;i++){
genotype[i] = rand.nextInt(2);
}
}
/*
* mutates the genotype of the individual depending on mutation rate
*
* param: double representing mutationRate
* pre: 0<mutationRate<1
* post: individual's genotype is mutated
* */
public void mutate(double mutationRate){
mutationRate = mutationRate*100;
for(int i = 0; i<numOfGenes; i++) {
if(rand.nextInt(100)<(int)mutationRate){
genotype[i] = (genotype[i] + 1 )%2;
}
}
}
/*
* returns the fitness of the individual, based on sumTarger and prodTarget
* and on the sum of the absolute values
* of the scaled sum error and the scaled product error
*
* param: int sumTarget and int prodTarget
* returns: fitness as a Double
*
* */
public Double calculateFitness(int sumTarget, int prodTarget){
int runningSum = 0;
int runningProduct = 1;
for(int i = 0; i<numOfGenes; i++){
if(genotype[i]==0){ //current site is a zero
runningSum = runningSum + (i+1);
}else{ //current site is a one
runningProduct = runningProduct * (i+1);
}
}
this.sum = runningSum;
this.product = runningProduct;
double scaledSumError = ((double)runningSum - (double)sumTarget)/(double)sumTarget;
double scaledProductError = ((double)runningProduct - (double)prodTarget)/(double)prodTarget;
double combinedError = Math.abs(scaledProductError) + Math.abs(scaledSumError);
return combinedError;
}
/*
* recombines the individual's genotype with another based on some rate
*
* param: double encoding the recombination rate
* Individual whose genotype is to be recombined with this Individual
*
* pre: 0<recombRate<1
*
* post: this individuals genotype is recombined
*
* */
public void recombination(double recombRate, Individual winner){
recombRate = recombRate*100;
for(int i = 0; i<numOfGenes; i++) {
if(rand.nextInt(100)<(int)recombRate){
this.genotype[i] = winner.genotype[i];
}
}
}
/*
* generates the phenotype (the piles) encoded by the
* individuals genotype
*
* post: sumPile and prodPile are updated.
* */
public void updatePiles(){
sumPile = new ArrayList<Integer>();
productPile = new ArrayList<Integer>();
for(int i = 0; i<numOfGenes; i++){
if(genotype[i]==0){ //current site is a zero
sumPile.add((Integer)(i+1));
}else{ //current site is a one
productPile.add((Integer)(i+1));
}
}
}
public String toString(){
String s = "";
updatePiles();
s = "genotype: [" ;
for(int i = 0; i<numOfGenes-1;i++){
s = s + genotype[i] + ",";
}
s = s + genotype[numOfGenes-1] + "]" + newline;
String s2 = "";
for(Integer i : sumPile){
s2 = s2 + i + " ";
}
s = s + "sum pile: [ " + s2 + "]" +newline;
s2 = "";
for(Integer i : productPile){
s2 = s2 + i + " ";
}
s = s + "product pile: [ " + s2 + "]" +newline;
s = s + "sum: " + sum + newline;
s = s + "product: " + product + newline;
return s;
}
}