Skip to content

Commit ed6d78e

Browse files
author
ovikdevil
committed
No Refractoring, No Optimazing, But Working!
1 parent 7565857 commit ed6d78e

File tree

2 files changed

+149
-19
lines changed

2 files changed

+149
-19
lines changed

src/GeneticAlgorithm.java

Lines changed: 134 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import java.util.ArrayList;
12
import java.util.List;
23
import java.util.Random;
34
import java.util.function.ToDoubleFunction;
5+
import java.util.stream.Collectors;
46

57
/**
68
* Created by ovikdevil on 25.08.16.
@@ -20,25 +22,147 @@ public String generate(int length) {
2022
return stringBuilder.toString();
2123
}
2224

23-
private String[] select(List<String> population, List<Double> fitnesses) {
24-
// TODO: Implement the select method
25+
public String mutate(String chromosome, double p) {
26+
StringBuilder line = new StringBuilder();
27+
for (int i = 0; i < chromosome.length(); i++) {
28+
boolean b = (chromosome.charAt(i) == '1');
29+
if (random.nextDouble()<=p) {
30+
b = !b;
31+
}
32+
line.append((b?'1':'0'));
33+
}
34+
return line.toString();
35+
}
36+
37+
public List<String> crossover(String chromosome1, String chromosome2) {
38+
int index = random.nextInt(chromosome1.length()-1);
39+
StringBuilder new_chromosome1 = new StringBuilder();
40+
StringBuilder new_chromosome2 = new StringBuilder();
41+
42+
for (int i = 0; i < chromosome1.length(); i++) {
43+
if (i<=index) {
44+
new_chromosome1.append(chromosome1.charAt(i));
45+
new_chromosome2.append(chromosome2.charAt(i));
46+
}
47+
else {
48+
new_chromosome1.append(chromosome2.charAt(i));
49+
new_chromosome2.append(chromosome1.charAt(i));
50+
}
51+
}
52+
53+
List<String> crossover_List = new ArrayList<>();
54+
crossover_List.add(new_chromosome1.toString());
55+
crossover_List.add(new_chromosome2.toString());
56+
return crossover_List;
57+
}
58+
59+
public int rouletteSelect(List<Double> weight) {
60+
double weight_sum = 0;
61+
for(int i=0; i<weight.size(); i++) {
62+
weight_sum += weight.get(i);
63+
}
64+
// get a random value
65+
double value = random.nextDouble() * weight_sum;
66+
// locate the random value based on the weights
67+
for(int i=0; i<weight.size(); i++) {
68+
value -= weight.get(i);
69+
if(value <= 0) {
70+
return i;
71+
}
72+
}
73+
// only when rounding errors occur
74+
return weight.size() - 1;
2575
}
2676

27-
/*
28-
private String mutate(String chromosome, double p) {
29-
// TODO: Implement the mutate method
77+
public List<String> select_from_base(ToDoubleFunction<String> fitness, List<String> population) {
78+
List<String> selected_List = new ArrayList<>();
79+
List<Double> fitness_List = population
80+
.stream()
81+
.map(fitness::applyAsDouble)
82+
.collect(Collectors.toList());
83+
84+
while (selected_List.size()<2) {
85+
int index = rouletteSelect(fitness_List);
86+
if (!selected_List.contains(population.get(index))) {
87+
selected_List.add(population.get(index));
88+
}
89+
}
90+
return selected_List;
3091
}
3192

32-
private String[] crossover(String chromosome1, String chromosome2) {
33-
// TODO: Implement the crossover method
93+
public void selection(ToDoubleFunction<String> fitness, List<String> population, double p_c, double p_m) {
94+
List<String> selected_List = select_from_base(fitness, population);
95+
96+
if (random.nextDouble() < p_c) { //Crossover prob
97+
selected_List = crossover(selected_List.get(0), selected_List.get(1));
98+
}
99+
100+
selected_List.set(0, mutate(selected_List.get(0), p_m));
101+
selected_List.set(1, mutate(selected_List.get(1), p_m));
102+
103+
population.addAll(selected_List);
104+
34105
}
35106

107+
108+
36109
public String run(ToDoubleFunction<String> fitness, int length, double p_c, double p_m) {
37-
// TODO: Implement the run method
110+
List<String> base_population = new ArrayList<>();
111+
for (int i = 0; i < 1000; i++) {
112+
base_population.add(generate(length));
113+
}
114+
List<String> new_population = select_from_base(fitness, base_population);
115+
116+
while (new_population.size() < base_population.size()) {
117+
selection(fitness, new_population, p_c, p_m);
118+
}
119+
120+
double max_fit = 0;
121+
String result_chromo = "";
122+
123+
for (String chromo : new_population) {
124+
double fit = fitness.applyAsDouble(chromo);
125+
if (fit == 1) {
126+
return chromo;
127+
}
128+
if (fit > max_fit) {
129+
result_chromo = chromo;
130+
max_fit = fit;
131+
}
132+
}
133+
134+
return result_chromo;
38135
}
39136

40137
public String run(ToDoubleFunction<String> fitness, int length, double p_c, double p_m, int iterations) {
41-
// TODO: Implement the run method
42-
}*/
138+
List<String> base_population = new ArrayList<>();
139+
for (int i = 0; i < 1000; i++) {
140+
base_population.add(generate(length));
141+
}
142+
List<String> new_population = select_from_base(fitness, base_population);
143+
144+
int it = 0;
145+
146+
while (it < iterations) {
147+
selection(fitness, new_population, p_c, p_m);
148+
it++;
149+
}
150+
151+
double max_fit = 0;
152+
String result_chromo = "";
153+
154+
for (String chromo : new_population) {
155+
double fit = fitness.applyAsDouble(chromo);
156+
if (fit == 1) {
157+
return chromo;
158+
}
159+
if (fit > max_fit) {
160+
result_chromo = chromo;
161+
max_fit = fit;
162+
}
163+
}
164+
165+
return result_chromo;
166+
}
43167

44168
}

src/Test.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.List;
14
import java.util.Random;
5+
import java.util.function.ToDoubleFunction;
26

37
/**
48
* Created by ovikdevil on 25.08.16.
59
*/
610
public class Test {
711
public static void main(String[] args) {
812
GeneticAlgorithm geneticAlgorithm = new GeneticAlgorithm();
9-
Random random = new Random();
1013

11-
for (int i = 0; i < 100000; i++) {
12-
if (!geneticAlgorithm.generate(10).contains("0")) {
13-
System.out.println("ALL ONES");
14-
}
15-
if (!geneticAlgorithm.generate(10).contains("1")) {
16-
System.out.println("ALL ZEROES");
17-
}
18-
}
14+
ToDoubleFunction<String> fitness =
15+
(x) -> {
16+
double i = 0;
17+
for(char c : x.toCharArray()) {
18+
if (c == '0')
19+
i++;
20+
}
21+
return 1/(i+1);
22+
};
23+
24+
System.out.println(geneticAlgorithm.run(fitness, 7, 0.6, 0.002, 10000));
1925
}
2026
}

0 commit comments

Comments
 (0)