Skip to content

Commit d3c4bc9

Browse files
committed
add evolutionary algorithms
1 parent 48a4ff5 commit d3c4bc9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1650
-1
lines changed
Binary file not shown.

archives/one-max/DNA.ctxt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#BlueJ class context
2+
comment0.target=DNA
3+
comment1.params=numberOfGenes\ pGeneSet
4+
comment1.target=DNA(int,\ char[])
5+
comment2.params=target
6+
comment2.target=void\ calculateFit(java.lang.String)
7+
comment3.params=
8+
comment3.target=float\ getFit()
9+
comment4.params=
10+
comment4.target=java.lang.String\ getPhrase()
11+
comment5.params=mate
12+
comment5.target=DNA\ crossover(DNA)
13+
comment6.params=mutationrate
14+
comment6.target=void\ mutate(float)
15+
comment7.params=
16+
comment7.target=char\ randomGene()
17+
numComments=8

archives/one-max/DNA.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
public class DNA {
3+
private char[] genes;
4+
private float fit;
5+
private char[] geneSet;
6+
7+
public DNA(int numberOfGenes, char[] pGeneSet) {
8+
this.geneSet = pGeneSet;
9+
genes = new char[numberOfGenes];
10+
for (int i = 0; i < genes.length; i++) {
11+
genes[i] = this.randomGene();
12+
}
13+
}
14+
15+
public void calculateFit(String target) {
16+
int score = 0;
17+
for (int i = 0; i < genes.length; i++) {
18+
if (genes[i] == target.charAt(i)) {
19+
score++;
20+
}
21+
}
22+
fit = (float) score / (float) target.length();
23+
}
24+
25+
public float getFit() {
26+
return fit;
27+
}
28+
29+
public String getPhrase() {
30+
if (genes.length > 60) {
31+
return new String(genes).substring(0, 60) + "...";
32+
}
33+
return new String(genes);
34+
}
35+
36+
public DNA crossover(DNA mate) {
37+
DNA kind = new DNA(genes.length, this.geneSet);
38+
39+
int cutoff = (int) (Math.random() * genes.length);
40+
41+
for (int i = 0; i < genes.length; i++) {
42+
if (i > cutoff) {
43+
kind.genes[i] = this.genes[i];
44+
} else {
45+
kind.genes[i] = mate.genes[i];
46+
}
47+
}
48+
49+
return kind;
50+
}
51+
52+
public void mutate(float mutationrate) {
53+
for (int i = 0; i < genes.length; i++) {
54+
if (Math.random() < mutationrate) {
55+
genes[i] = this.randomGene();
56+
}
57+
}
58+
}
59+
60+
private char randomGene() {
61+
int randomIndex = (int) (Math.random() * this.geneSet.length);
62+
char gene = this.geneSet[randomIndex];
63+
return gene;
64+
}
65+
}

archives/one-max/OneMax.ctxt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#BlueJ class context
2+
comment0.target=OneMax
3+
comment1.params=
4+
comment1.target=OneMax()
5+
comment2.params=
6+
comment2.target=void\ run()
7+
comment3.params=args
8+
comment3.target=void\ main(java.lang.String[])
9+
numComments=4

archives/one-max/OneMax.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
3+
import org.openpatch.scratch.*;
4+
import org.openpatch.scratch.extensions.text.*;
5+
6+
public class OneMax extends Stage {
7+
8+
private Text bestPhrase;
9+
private Text allPhrases;
10+
private Text statistics;
11+
12+
private String target;
13+
private int populationsize;
14+
private float mutationrate;
15+
private char[] geneSet;
16+
17+
private Population population;
18+
19+
public OneMax() {
20+
super(1000, 600);
21+
22+
this.target = "1".repeat(10000);
23+
this.populationsize = 50;
24+
this.mutationrate = 0.02f;
25+
this.geneSet = "01".toCharArray();
26+
27+
this.bestPhrase = new Text();
28+
this.bestPhrase.addFont("comic", "assets/Singkong.ttf");
29+
this.bestPhrase.setPosition(-490, 250);
30+
this.bestPhrase.setAlign(TextAlign.LEFT);
31+
this.bestPhrase.setTextSize(20);
32+
this.bestPhrase.switchFont("comic");
33+
this.bestPhrase.setTextColor(200, 50, 50);
34+
this.add(this.bestPhrase);
35+
36+
this.allPhrases = new Text();
37+
this.allPhrases.setPosition(0, 250);
38+
this.allPhrases.setAlign(TextAlign.LEFT);
39+
this.add(this.allPhrases);
40+
41+
this.statistics = new Text();
42+
this.statistics.setPosition(-490, 100);
43+
this.statistics.setAlign(TextAlign.LEFT);
44+
this.add(this.statistics);
45+
46+
this.population = new Population(this.target, this.mutationrate, this.populationsize, this.geneSet);
47+
}
48+
49+
public void run() {
50+
// run may execute before the execution of the constructor of Shakespear
51+
// is finished. Therefore, we need to test if population is set.
52+
// This does only relevant for single Stage mode.
53+
if (this.population != null && !this.population.isFinished()) {
54+
this.population.naturalselection();
55+
this.population.newGeneration();
56+
this.population.calculateFit();
57+
this.population.evaluate();
58+
59+
String statisticText = "";
60+
statisticText += "Generations: " + this.population.getGeneration() + "\n";
61+
statisticText += "Average Fit: " + this.population.getAverageFit() + "\n";
62+
statisticText += "Best Fit: " + this.population.getBest().getFit() + "\n";
63+
statisticText += "Populationsize: " + this.populationsize + "\n";
64+
statisticText += "Mutationrate: " + Math.round(this.mutationrate * 100) + "%\n";
65+
66+
this.statistics.showText(statisticText);
67+
this.bestPhrase.showText("Best Guess:\n" + this.population.getBest().getPhrase());
68+
this.allPhrases.showText("All Guesses:\n" + this.population.getAllPhrases(25));
69+
}
70+
}
71+
72+
public static void main(String[] args) {
73+
new OneMax();
74+
}
75+
}

archives/one-max/Population.ctxt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#BlueJ class context
2+
comment0.target=Population
3+
comment1.params=pTarget\ pMutationrate\ pNumber\ pGeneSet
4+
comment1.target=Population(java.lang.String,\ float,\ int,\ char[])
5+
comment10.params=limit
6+
comment10.target=java.lang.String\ getAllPhrases(int)
7+
comment2.params=
8+
comment2.target=void\ calculateFit()
9+
comment3.params=
10+
comment3.target=void\ naturalselection()
11+
comment4.params=
12+
comment4.target=void\ newGeneration()
13+
comment5.params=
14+
comment5.target=DNA\ getBest()
15+
comment6.params=
16+
comment6.target=void\ evaluate()
17+
comment7.params=
18+
comment7.target=boolean\ isFinished()
19+
comment8.params=
20+
comment8.target=int\ getGeneration()
21+
comment9.params=
22+
comment9.target=float\ getAverageFit()
23+
numComments=11

archives/one-max/Population.java

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
3+
public class Population {
4+
5+
private int generations;
6+
private boolean finished;
7+
private float mutationrate;
8+
private String target;
9+
private DNA[] population;
10+
private DNA[] matingpool;
11+
private DNA best;
12+
private char[] geneSet;
13+
14+
public Population(String pTarget, float pMutationrate, int pNumber, char[] pGeneSet) {
15+
this.population = new DNA[pNumber];
16+
this.mutationrate = pMutationrate;
17+
this.geneSet = pGeneSet;
18+
this.target = pTarget;
19+
this.generations = 0;
20+
this.finished = false;
21+
22+
for (int i = 0; i < pNumber; i++) {
23+
this.population[i] = new DNA(pTarget.length(), pGeneSet);
24+
}
25+
26+
this.best = this.population[0];
27+
}
28+
29+
public void calculateFit() {
30+
for (int i = 0; i < this.population.length; i++) {
31+
this.population[i].calculateFit(this.target);
32+
}
33+
}
34+
35+
public void naturalselection() {
36+
int requiredPlaces = 0;
37+
for (int i = 0; i < this.population.length; i++) {
38+
requiredPlaces += (int) (this.population[i].getFit() * 100);
39+
}
40+
41+
this.matingpool = new DNA[requiredPlaces];
42+
int nextPlace = 0;
43+
for (int i = 0; i < this.population.length; i++) {
44+
int place = (int) (this.population[i].getFit() * 100);
45+
for (int j = 0; j < place; j++) {
46+
this.matingpool[nextPlace] = this.population[i];
47+
nextPlace++;
48+
}
49+
}
50+
}
51+
52+
public void newGeneration() {
53+
if (matingpool.length > 1) {
54+
55+
for (int i = 0; i < this.population.length; i++) {
56+
int a = (int) (Math.random() * matingpool.length);
57+
int b = (int) (Math.random() * matingpool.length);
58+
DNA mateA = matingpool[a];
59+
DNA mateB = matingpool[b];
60+
DNA child = mateA.crossover(mateB);
61+
child.mutate(mutationrate);
62+
population[i] = child;
63+
}
64+
} else {
65+
for (int i = 0; i < this.population.length; i++) {
66+
population[i].mutate(mutationrate);
67+
}
68+
}
69+
70+
this.generations += 1;
71+
}
72+
73+
public DNA getBest() {
74+
return this.best;
75+
}
76+
77+
public void evaluate() {
78+
float worldrecord = 0;
79+
80+
for (int i = 0; i < this.population.length; i++) {
81+
if (this.population[i].getFit() > worldrecord) {
82+
best = this.population[i];
83+
worldrecord = this.population[i].getFit();
84+
}
85+
}
86+
87+
if (worldrecord == 1) {
88+
this.finished = true;
89+
}
90+
}
91+
92+
public boolean isFinished() {
93+
return this.finished;
94+
}
95+
96+
public int getGeneration() {
97+
return this.generations;
98+
}
99+
100+
public float getAverageFit() {
101+
float sum = 0;
102+
for (int i = 0; i < this.population.length; i++) {
103+
sum += this.population[i].getFit();
104+
}
105+
106+
return sum / this.population.length;
107+
}
108+
109+
public String getAllPhrases(int limit) {
110+
String all = "";
111+
int displayLimit = Math.min(this.population.length, limit);
112+
113+
for (int i = 0; i < displayLimit; i++) {
114+
all += this.population[i].getPhrase() + "\n";
115+
}
116+
117+
return all;
118+
}
119+
}

archives/one-max/assets/Singkong.ttf

35.6 KB
Binary file not shown.

archives/one-max/package.bluej

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#BlueJ package file
2+
dependency1.from=OneMax
3+
dependency1.to=Population
4+
dependency1.type=UsesDependency
5+
dependency2.from=Population
6+
dependency2.to=DNA
7+
dependency2.type=UsesDependency
8+
editor.fx.0.height=1048
9+
editor.fx.0.width=960
10+
editor.fx.0.x=213
11+
editor.fx.0.y=40
12+
objectbench.height=93
13+
objectbench.width=461
14+
package.divider.horizontal=0.6
15+
package.divider.vertical=0.8003992015968064
16+
package.editor.height=394
17+
package.editor.width=649
18+
package.editor.x=514
19+
package.editor.y=200
20+
package.frame.height=600
21+
package.frame.width=800
22+
package.numDependencies=2
23+
package.numTargets=3
24+
package.showExtends=true
25+
package.showUses=true
26+
project.charset=UTF-8
27+
readme.height=60
28+
readme.name=@README
29+
readme.width=49
30+
readme.x=10
31+
readme.y=10
32+
target1.height=70
33+
target1.name=OneMax
34+
target1.showInterface=false
35+
target1.type=ClassTarget
36+
target1.width=120
37+
target1.x=10
38+
target1.y=90
39+
target2.height=70
40+
target2.name=DNA
41+
target2.showInterface=false
42+
target2.type=ClassTarget
43+
target2.width=120
44+
target2.x=130
45+
target2.y=10
46+
target3.height=70
47+
target3.name=Population
48+
target3.showInterface=false
49+
target3.type=ClassTarget
50+
target3.width=120
51+
target3.x=140
52+
target3.y=90
Binary file not shown.

0 commit comments

Comments
 (0)