-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimple_NEAT.java
More file actions
177 lines (154 loc) · 4.79 KB
/
Simple_NEAT.java
File metadata and controls
177 lines (154 loc) · 4.79 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
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
169
170
171
172
173
174
175
176
177
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
public class Simple_NEAT{
private ArrayList<Network> agents;
private int numInputs, numOutputs, genNum;
private Network curAgent;
private boolean keepBest;
private double mutationRate;
public Simple_NEAT(int nI, int nO){
numInputs = nI;
numOutputs = nO;
genNum = 0;
keepBest = false;
agents = new ArrayList<Network>();
mutationRate = 0.60;
}
public void addAgent(){
agents.add(new Network(numInputs, numOutputs));
}
public void addAgent(Network n){
agents.add(n);
}
public void setCurrentAgent(int index){
curAgent = agents.get(index);
}
/**
* Run the entire generation with the same input(s)
* @param inputs Network input(s)
*/
public void runAll(float[] inputs){
for (Network n : agents){
n.runNetwork(inputs);
}
}
/**
* Run the currently set agent
* @param inputs Network input(s)
*/
public void runCurrent(float[] inputs){
curAgent.runNetwork(inputs);
}
public float[] getCurOutput(){
return curAgent.getSimpleOutput();
}
public Network getAgent(int i){
return agents.get(i);
}
public float[] getAgentOutput(int i){
return agents.get(i).getSimpleOutput();
}
public void setFitness(int i, double d){
Network n = getAgent(i);
n.setFitness(d);
}
public void breed(){
int popSize = 0;
ArrayList<Network> nextGen = new ArrayList<Network>();
if (keepBest){
popSize = agents.size() - 1;
}
else
popSize = agents.size();
double sum = 0.0;
for (Network a : agents){
sum += a.getFitness();
}
//Normalize the fitness values
for (Network a : agents){
a.setGenFitness(a.getFitness()/sum);
}
Collections.sort(agents);
for (Network a : agents){
// System.out.println(a + " - " + a.getGenFitness());
}
// System.out.println("------------------------------");
//Accumulate normalized fitness
sum = 0.0;
for (Network a : agents){
double temp = a.getGenFitness();
a.setGenFitness(temp + sum);
sum += temp;
}
for (int i = 0; i < popSize; i++){
double num = ThreadLocalRandom.current().nextDouble(0,1);
for (Network a : agents){
if (a.getGenFitness() >= num){
nextGen.add(a.copy());
break;
}
}
}
genNum++;
agents = nextGen;
mutate();
}
public void mutate(){
for (Network a : agents){
double num = ThreadLocalRandom.current().nextDouble(0,1);
//Should we mutate this agent?
if (mutationRate >= num){
num = ThreadLocalRandom.current().nextDouble(0,1);
if (num <= 1/3){
a.addRandHiddenNode();
}
else if(num > 1/3 && num <= 2/3){
a.addRandConnection();
}
else{
a.mutateWeight();
}
}
}
}
public Network getBestFit(){
double max = 0;
Network best = null;
for (Network n : agents){
if (n.getFitness() > max){
best = n;
max = n.getFitness();
}
}
return best;
}
public Network getNetwork(int i){
return agents.get(i);
}
public static void main(String args[]){
// Simple_NEAT n = new Simple_NEAT(3, 2);
// Locals l = new Locals();
// for (int i =0; i < 1; i++){
// Ship s = new Ship(l);
// n.addAgent(s);
// n.setCurrentAgent(s);
// // n.setFitness(s, ThreadLocalRandom.current().nextInt(0, 200));
// }
//
// float[] inputs = {0.4f, 0.9f, 0.33f};
// n.runCurrent(inputs);
// for (float i : n.getCurOutput()){
// System.out.print(i + ", ");
// }
// System.out.println("\n------------------------------------------");
//
// float[] inputs1 = {0.8f, 0.1f, 0.73f};
// n.runCurrent(inputs1);
// for (float i : n.getCurOutput()){
// System.out.print(i + ", ");
// }
// System.out.println("\n------------------------------------------");
// n.breed();
}
}