-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringGene.java
More file actions
173 lines (155 loc) · 4.39 KB
/
StringGene.java
File metadata and controls
173 lines (155 loc) · 4.39 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
import java.util.Random;
public class StringGene extends Gene {
private boolean bullsEye;
public StringGene(String str, String target, boolean aging) {
// TODO Auto-generated constructor stub
super(str, target, aging);
bullsEye = false;
fitness_calculation();
}
@Override
public void fitness_calculation() {
// fitness Calulation
int fitness = 0;
if (bullsEye) {
fitness = bullsEye();
} else {
String targetS = (String) this.getTarget();
String val = (String) this.value;
char[] valC = val.toCharArray();
char[] tarC = targetS.toCharArray();
for (int i = 0; i < valC.length; i++) {
fitness += Math.abs(valC[i] - tarC[i]);
}
}
this.setFitness(fitness);
}
@Override
public void mutate(Mutation option) {
Random r = new Random();
String val = (String) this.value;
String tarS = (String) this.target;
char[] valC = val.toCharArray();
int ipos = r.nextInt(tarS.length());
int delta = r.nextInt(90) + 32;
valC[ipos] = (char) ((valC[ipos] + delta) % 122);
this.value = String.copyValueOf(valC);
fitness_calculation();
}
@Override
public Gene mate(Gene gene, Crossover option) {
String val = (String) this.value;
String target = (String) this.target;
String mateS = (String) gene.getValue();
StringGene newSG =null;
switch (option) {
case DEFULAT: {
newSG = new StringGene(singlePointCO(val, mateS), target, this.aging);
break;
}
case SINGLEPOINT: {
newSG = new StringGene(singlePointCO(val, mateS), target, this.aging);
break;
}
case TWOPOINT: {
newSG = new StringGene(twoPointCO(val, mateS), target, this.aging);
break;
}
case UINFORM: {
newSG = new StringGene(uniformCO(val, mateS), target, this.aging);
break;
}
default: {
System.out.println("Error - Choose Crossover Option");
break;
}
}
if(newSG==null)
{
System.out.println("Error");
System.exit(1);
}
newSG.setDeathAge(this.getDeathAge());
newSG.setMatureAge(this.getMatureAge());
return newSG;
}
public String twoPointCO(String string1, String string2) {
Random r = new Random();
String target = (String) this.target;
int i1 = r.nextInt(target.length());
int i2 = r.nextInt(target.length() - i1) + i1;
String crossed = "";
crossed = string1.substring(0, i1);
crossed += string2.substring(i1, i2);
crossed += string1.substring(i2, target.length());
return crossed;
}
public String singlePointCO(String string1, String string2) {
Random r = new Random();
String target = (String) this.target;
int spos = r.nextInt(target.length());
String newStr = string1.substring(0, spos);
newStr += string2.substring(spos, target.length());
return newStr;
}
public String uniformCO(String string1, String string2) {
Random r = new Random();
String target = (String) this.target;
String newStr = "";
for (int i = 0; i < target.length(); i++) {
char newChar;
if (r.nextInt(2) == 1) // either 1 or 0 - 50% chance for each
{
newChar = string1.charAt(i);
} else {
newChar = string2.charAt(i);
}
newStr += newChar;
}
return newStr;
}
@Override
public Gene getRandom() {
String tarS = (String) this.target;
char[] valC = new char[tarS.length()];
Random r = new Random();
for (int i = 0; i < tarS.length(); i++) {
valC[i] = (char) (r.nextInt(90) + 32);
}
StringGene randSg = new StringGene(String.copyValueOf(valC), tarS, this.aging);
randSg.setDeathAge(this.getDeathAge());
randSg.setMatureAge(this.getMatureAge());
return randSg;
}
@Override
public String toString() {
String val = (String) this.value;
return val;
}
public int bullsEye() {
String tarS = (String) this.target;
String val = (String) this.value;
char[] valC = val.toCharArray();
int fitness = 122 * 12;
for (int i = 0; i < valC.length; i++) {
int index = tarS.indexOf(valC[i]);
int cindex = val.indexOf(valC[i]);
// the case in which their are more then one specific char in the string
while (cindex != i) {
index = tarS.indexOf(valC[i], index + 1);
cindex = val.indexOf(valC[i], cindex + 1);
}
if (index != i)
fitness -= 12;
else
fitness -= 122;
}
return fitness;
}
public boolean isBullsEye() {
return bullsEye;
}
public void setBullsEye(boolean bullsEye) {
this.bullsEye = bullsEye;
}
}