-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticAlgorithm.java
168 lines (144 loc) · 5.59 KB
/
GeneticAlgorithm.java
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
package algorithm;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author harry
*
* Created on: Aug 26, 2014
*/
public class GeneticAlgorithm {
private int generationCount;
private int populationSize;
private int chromosomeSize;
private double mutationRate;
private List<String> population;
private Map<String, Double> evaluationSheet;
public GeneticAlgorithm() {
generationCount = 100;
populationSize = 100;
chromosomeSize = 25;
mutationRate = 0.02; //2 in 100 will mutate
population = new ArrayList<String>();
}
public static void main(String[] args){
GeneticAlgorithm ga = new GeneticAlgorithm();
ga.demo();
}
public void demo(){
System.out.println("Formula: \nf(x) = x*cos(10*pi*x) * sin(x) \n[0, 3)");
System.out.println("Question: \nWhat is the value x to maximize f(x)?\n\n");
String cBest = "";
double xBest = 0;
double fBest = 0;
initialize();
for(int i = 0; i < generationCount; i++){
List<String> kids = crossover();
mutate(kids);
select(kids);
}
Map<String, Double> kidsRanked = sortEvaluation(evaluationSheet);
int iKidCount = 0;
for(String c : kidsRanked.keySet()){
System.out.println(c +" : "+kidsRanked.get(c) + " x=["+xValueOfChromosome(c)+"]");
iKidCount++;
if(iKidCount == populationSize){
xBest = xValueOfChromosome(c);
fBest = kidsRanked.get(c);
cBest = c;
}
}
System.out.println("\n\n[Optimization Result]");
System.out.println(cBest);
System.out.println(" x: "+xBest);
System.out.println("f(x): "+fBest);
}
private void initialize(){
for(int i = 0; i < populationSize; i++){
population.add(randomChromosome());
}
}
private double evaluate(String chromosome){
double xInDecimal = xValueOfChromosome(chromosome);
double fValueOfX = xInDecimal*Math.cos(10*3.1415926*xInDecimal) * Math.sin(xInDecimal);
return fValueOfX;
}
private List<String> crossover(){
List<String> offsprings = new ArrayList<String>();
for(int i = 0; i < populationSize-1; i=i+2){
String c1 = population.get(i).substring(0, 6) + population.get(i+1).substring(6);
String c2 = population.get(i+1).substring(0, 6) + population.get(i).substring(6);
offsprings.add(c1);
offsprings.add(c2);
}
for(int i = 0; i < populationSize - offsprings.size(); i++){
offsprings.add(randomChromosome());
}
return offsprings;
}
private void mutate(List<String> offsprings){
for(int i = 0; i < populationSize; i++){
if(Math.random() * 0.1 < mutationRate){
StringBuilder mutated = new StringBuilder(offsprings.get(i));
int mutatingIndex = (int) Math.ceil(Math.random()*(chromosomeSize-1));
if(mutated.charAt(mutatingIndex) == '0'){
mutated.replace(mutatingIndex, mutatingIndex+1, "1");
} else {
mutated.replace(mutatingIndex, mutatingIndex+1, "0");
}
offsprings.set(i, mutated.toString());
}
}
}
private void select(List<String> offsprings){
evaluationSheet = new LinkedHashMap<String, Double>();
Map<String, Double> tests = new LinkedHashMap<String, Double>();
for(int i = 0; i < populationSize; i++){
tests.put(population.get(i), evaluate(population.get(i)));
tests.put(offsprings.get(i), evaluate(offsprings.get(i)));
}
tests = sortEvaluation(tests);
List<String> family = new LinkedList<String>(tests.keySet());
List<String> survivors = new ArrayList<String>();
for(int i=(family.size()-1); i>=(family.size() - populationSize); i--){
survivors.add(family.get(i));
evaluationSheet.put(family.get(i), tests.get(family.get(i)));
}
population = survivors;
}
/*
* Utility functions
*/
private String randomChromosome(){
StringBuilder chromosome = new StringBuilder();
for(int j = 0; j < chromosomeSize; j++){
if(Math.random() < 0.5){
chromosome.append("0");
} else {
chromosome.append("1");
}
}
return chromosome.toString();
}
private double xValueOfChromosome(String chromosome){
return Integer.parseInt(chromosome, 2) * (3/(Math.pow(2, chromosomeSize) - 1));
}
private static Map<String, Double> sortEvaluation(Map<String, Double> unsortMap){
List<Entry<String, Double>> list = new LinkedList<Entry<String, Double>>(unsortMap.entrySet());
Collections.sort(list, new Comparator<Entry<String, Double>>(){
public int compare(Entry<String, Double> o1, Entry<String, Double> o2){
return o1.getValue().compareTo(o2.getValue());
}
});
Map<String, Double> sortedMap = new LinkedHashMap<String, Double>();
for (Entry<String, Double> entry : list){
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}