-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticProgrammer.java
More file actions
108 lines (94 loc) · 2.66 KB
/
GeneticProgrammer.java
File metadata and controls
108 lines (94 loc) · 2.66 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
import java.util.*;
public class GeneticProgrammer
{
private Organism[] population;
private ArrayList<Integer> target;
public static void main(String[] args)
{
ArrayList<Integer> target = new ArrayList<Integer>();
//Strings can be obtained from ascii
target.add(84);
target.add(104);
target.add(97);
target.add(110);
target.add(107);
target.add(32);
target.add(89);
target.add(111);
target.add(117);
target.add(33);
GeneticProgrammer g = new GeneticProgrammer(target);
g.loop();
}
public GeneticProgrammer(ArrayList<Integer> target)
{
population = new Organism[100];
for (int i = 0; i < population.length; i++)
population[i] = new Organism(target);
this.target = target;
}
public void loop()
{
int number = 0;
while (true)
{
for (int i = 0; i < population.length; i++)
{
population[i].findFitness();
}
sortPopulationByFitness(population);
number++;
//pick winners and go again
System.out.println("Generation: " + number + ": Fitness " + population[99].getFitness() + "/"+ target.size()*256);
//selection CHANGE THE 100!!!!!!!!! based on num games
for (int i = 0; i < population.length *.5; i++)
{
if (Math.random() > 0.5)
population[i] = population[population.length - 1 - i].copy();
}
//this is for once you understand what values will be given
if (population[population.length - 1].getFitness() >= target.size()*256)
{
population[population.length - 1].printBrain();
population[population.length - 1].printMap();
BFToCode b = new BFToCode();
b.translate(population[population.length - 1].getBrain());
System.out.println(b.getOutputs());
}
if (population[population.length - 1].getFitness() >= target.size()*256)
System.exit(0);
for (Organism org: population)
org.resetFitness();
//only for hi test
/*if (number > 10000)
{
population[population.length - 1].printBrain();
System.exit(0);
}*/
}
}
public void sortPopulationByFitness(Organism[] a)
{
for (int i = 0; i < a.length; i++)
{
int min = indexOfMin(a, i);
Organism save = a[i];
a[i] = a[min];
a[min] = save;
}
}
public static int indexOfMin(Organism[] a, int startIndex)
{
int lowest = startIndex;
Organism y = a[lowest];
for (int i = startIndex; i < a.length; i++)
{
if (y.getFitness() > a[i].getFitness())
{
y = a[i];
lowest = i;
}
}
return lowest;
}
}