1
+ import java .util .ArrayList ;
1
2
import java .util .List ;
2
3
import java .util .Random ;
3
4
import java .util .function .ToDoubleFunction ;
5
+ import java .util .stream .Collectors ;
4
6
5
7
/**
6
8
* Created by ovikdevil on 25.08.16.
@@ -20,25 +22,147 @@ public String generate(int length) {
20
22
return stringBuilder .toString ();
21
23
}
22
24
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 ;
25
75
}
26
76
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 ;
30
91
}
31
92
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
+
34
105
}
35
106
107
+
108
+
36
109
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 ;
38
135
}
39
136
40
137
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
+ }
43
167
44
168
}
0 commit comments