Repository navigation
Expand file tree
/
Copy pathOperations.java
More file actions
290 lines (229 loc) · 9.23 KB
/
Copy pathOperations.java
File metadata and controls
290 lines (229 loc) · 9.23 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Created by Sam on 18.11.2016.
*/
public class Operations {
static double a = 0;
static double c = 10 * Math.PI;
static double power = 7;
static double leftside = 0;
static double rightside = Math.pow(2, power);
static Random random = new Random();
public static double func(double x) {
return 20 * Math.sin(x / 10) - 2;
}
public static List<Chromosome> createPopulation(int n) {
List<Chromosome> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int pos = getNum();
String binary = fromNumToBinary(pos);
double x = fromNumToFloat(pos);
double val = func(x);
list.add(new Chromosome(pos, binary, x, val));
}
return list;
}
public static List<Chromosome> selection(List<Chromosome> chromosomeList, int maxSize) {
List<Chromosome> list = new ArrayList<>();
chromosomeList = sortList(chromosomeList);
double a = 1 + getRand() * (2 - 1);
double b = 2 - a;
double N = chromosomeList.size();
for (int i = 0; i < N; i++) {
chromosomeList.get(i).setProbability(1 / N * (a - (a - b) * (i - 1 + 1) / (N - 1)));
}
int k = 0;
for (int i = 0; i < maxSize; i++) {
double rand = getRand();
double sum = 0;
for (int j = 0; j < N; j++) {
sum += chromosomeList.get(j).getProbability();
if (rand < sum) {
int finalJ = j;
Chromosome m = new Chromosome(chromosomeList.get(j));
if (chromosomeList.stream().filter(e -> e.getPosition() == m.getPosition()).count() != 0 && k < 10) {
i--;
k++;
break;
}
k=0;
list.add(new Chromosome(m));
break;
}
}
}
return list;
}
public static List<Chromosome> reproduction(List<Chromosome> chromosomeList, int maxSize) {
List<Chromosome> list = new ArrayList<>();
chromosomeList = sortList(chromosomeList);
double a = 1 + getRand() * (2 - 1);
double b = 2 - a;
double N = chromosomeList.size();
for (int i = 0; i < N; i++) {
chromosomeList.get(i).setProbability(1 / N * (a - (a - b) * (i - 1 + 1) / (N - 1)));
}
boolean pair = false;
for (int i = 0; i < maxSize; i++) {
double rand = getRand();
double sum = 0;
for (int j = 0; j < N; j++) {
sum += chromosomeList.get(j).getProbability();
if (rand < sum) {
if (pair) {
if (list.get(i - 1).getPosition() == chromosomeList.get(j).getPosition()) {
i--;
break;
} else {
list.add(new Chromosome(chromosomeList.get(j)));
pair = false;
}
} else {
list.add(new Chromosome(chromosomeList.get(j)));
pair = true;
}
break;
}
}
}
return list;
}
public static List<Chromosome> crossingover(List<Chromosome> chromosomeList) {
List<Chromosome> list = new ArrayList<>();
int N = chromosomeList.size();
for (int i = 0; i < N; i += 2) {
double pr = getRand();
list.add(chromosomeList.get(i));
list.add(chromosomeList.get(i + 1));
int k = (int) Math.round(getRand() * (power - 1));
StringBuilder b1 = new StringBuilder();
StringBuilder b2 = new StringBuilder();
char[] buf1;
char[] buf2;
buf1 = list.get(i).getBinary().toCharArray();
buf2 = list.get(i + 1).getBinary().toCharArray();
/* if (buf1.length != buf2.length) {
System.out.println(list.get(i));
System.out.println(list.get(i + 1));
System.out.println(buf1.length);
System.out.println(buf2.length);
}*/
for (int j = 0; j < buf1.length; j++) {
if (j < k) {
b1.append(String.valueOf(buf1[j]));
b2.append(String.valueOf(buf2[j]));
} else {
b1.append(String.valueOf(buf2[j]));
b2.append(String.valueOf(buf1[j]));
}
}
list.get(i).setBinary(b1.toString());
list.get(i + 1).setBinary(b2.toString());
list.get(i).setPartner(i + "");
list.get(i + 1).setPartner((i + 1) + "");
}
for (int i = 0; i < N; i++) {
list.get(i).setPosition(Integer.parseInt(list.get(i).getBinary(), 2));
list.get(i).setValue(fromNumToFloat(list.get(i).getPosition()));
list.get(i).setFuncValue(func(list.get(i).getValue()));
}
return list;
}
public static List<Chromosome> mutation(List<Chromosome> chromosomeList, int pos, double pm, List<Integer> integerList) {
List<Chromosome> list = new ArrayList<>();
int k = 0;
for (int i = 0; i < chromosomeList.size(); i++) {
double rand = getRand();
if (rand < pm) {
integerList.add(i);
char[] arr = chromosomeList.get(i).getBinary().toCharArray();
arr[pos] = (arr[pos] == '1') ? '0' : '1';
Chromosome chromosome = new Chromosome(new Chromosome(chromosomeList.get(i)));
chromosome.setBinary(String.valueOf(arr));
chromosome.setPosition(Integer.parseInt(chromosome.getBinary(), 2));
chromosome.setValue(fromNumToFloat(chromosome.getPosition()));
chromosome.setFuncValue(func(chromosome.getValue()));
//System.out.println(chromosome);
list.add(chromosome);
}
}
return list;
}
public static List<Chromosome> cutting(List<Chromosome> clone, double pc) {
List<Chromosome> list = new ArrayList<>();
for (int i = 0; i < clone.size() * pc; i++) {
list.add(new Chromosome(clone.get(i)));
}
if (list.size() % 2 != 0) list.add(new Chromosome(clone.get(list.size())));
return list;
}
public static List<Chromosome> sortList(List<Chromosome> list) {
list.sort((o1, o2) -> o1.getFuncValue() < o2.getFuncValue() ? 1 : -1);
return list;
}
public static List<Chromosome> addAll(List<Chromosome> ishList, List<Chromosome> rep,
List<Chromosome> cross, List<Chromosome> mut) {
List<Chromosome> chromosomes = new ArrayList<>();
ishList.forEach(e -> chromosomes.add(new Chromosome(e)));
/*
rep.forEach(e -> {
if (chromosomes.stream().filter(o -> o.getPosition() == e.getPosition()).count() == 0)
chromosomes.add(new Chromosome(e));
});*/
cross.forEach(e -> {
if (chromosomes.stream().filter(o -> o.getPosition() == e.getPosition()).count() == 0)
chromosomes.add(new Chromosome(e));
});
mut.forEach(e -> {
if (chromosomes.stream().filter(o -> o.getPosition() == e.getPosition()).count() == 0)
chromosomes.add(new Chromosome(e));
});
return chromosomes;
}
private static String fromNumToBinary(int num) {
String binary = Integer.toBinaryString(num);
binary = checkLength(binary);
return binary;
}
private static double fromNumToFloat(int x_int) {
return a + x_int * (c - a) / (Math.pow(2, power) - 1);
}
private static int getNum() {
return (int) (leftside + Math.round(getRand() * (rightside - leftside)));
}
private static double getRand() {
return random.nextDouble();
}
public static String checkLength(String binary) {
if (binary.length() != power) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < power - binary.length(); i++) buf.append("0");
buf.append(binary);
binary = buf.toString();
}
return binary;
}
public static List<Chromosome> usech(List<Chromosome> bufList, int n) {
List<Chromosome> list = new ArrayList<>();
List<Chromosome> list1 = Operations.selection(bufList, n / 2);
int l = 0;
if (n % 2 != 0) l = 1;
for (int i = 0; i < n / 2 + l; i++) {
for (Chromosome aBufList : bufList) {
boolean flag = false;
for (int k = 0; k < list1.size(); k++) {
if (list1.stream().filter(e -> e.getPosition() == aBufList.getPosition()).count() == 0) {
list1.add(new Chromosome(aBufList));
flag = true;
break;
}
}
if (flag) break;
}
}
return list1;
}
}