FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FightMicroGA/Simulation/Main.java at master · TheSimpleSoldier/FightMicroGA · GitHub
TheSimpleSoldier
/
FightMicroGA
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
FightMicroGA
/
Simulation
/
Main.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
286 lines (237 loc) · 8.65 KB
Breadcrumbs
FightMicroGA
/
Simulation
/
Main.java
Copy path
File metadata and controls
286 lines (237 loc) · 8.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package
Simulation
;
public
class
Main
{
/**
* This is the main function which triggers simulations
* @param args
*/
public
static
void
main
(
String
[]
args
)
{
double
mutationRate
=
0.3
;
double
crossOverRate
=
0.1
;
double
mutationAmount
=
0.1
;
boolean
verbose
=
false
;
double
globalScale
=
0.1
;
double
localScale
=
0.2
;
double
randomScale
=
0.3
;
//runFightSimulation(inputs, inputs);
// System.out.println("Basic vs. Advanced");
// runFightSimulation(null, null, 1, 2, true, 2);
// System.out.println("Advanced vs. Basic");
// runFightSimulation(null, null, 2, 1, true, 2);
double
[][]
idealWeights
=
getIdealWeights
(
10
,
1
,
mutationRate
,
crossOverRate
,
mutationAmount
);
PSO
pso
=
new
PSO
(
globalScale
,
localScale
,
randomScale
);
double
[][]
idealWeights2
=
pso
.
getBestWeights
(
200
,
10
);
long
startTime
=
System
.
currentTimeMillis
();
System
.
out
.
println
();
for
(
int
i
=
0
;
i
<
idealWeights2
.
length
;
i
++)
{
for
(
int
j
=
0
;
j
<
idealWeights2
[
i
].
length
;
j
++)
{
System
.
out
.
print
(
idealWeights2
[
i
][
j
] +
", "
);
}
System
.
out
.
println
();
}
verbose
=
true
;
for
(
int
i
=
0
;
i
<
2
;
i
++)
{
System
.
out
.
println
(
"Net is Red:"
);
runFightSimulation
(
idealWeights
,
idealWeights2
,
0
,
0
,
verbose
,
1
);
System
.
out
.
println
(
"Net is blue"
);
runFightSimulation
(
idealWeights2
,
idealWeights
,
0
,
0
,
verbose
,
1
);
System
.
out
.
println
(
"Net vs. basic"
);
runFightSimulation
(
idealWeights
,
idealWeights
,
0
,
1
,
verbose
,
1
);
System
.
out
.
println
(
"basic vs. Net"
);
runFightSimulation
(
idealWeights
,
idealWeights
,
1
,
0
,
verbose
,
1
);
System
.
out
.
println
(
"PSO vs. basic"
);
runFightSimulation
(
idealWeights2
,
idealWeights2
,
0
,
1
,
verbose
,
1
);
System
.
out
.
println
(
"basic vs. PSO"
);
runFightSimulation
(
idealWeights2
,
idealWeights2
,
1
,
0
,
verbose
,
1
);
}
System
.
out
.
println
(
"Run Time: "
+ (
System
.
currentTimeMillis
() -
startTime
));
}
public
static
double
[][]
getIdealWeights
(
int
popSize
,
int
rounds
,
double
mutationRate
,
double
crossOverRate
,
double
mutationAmount
)
{
FeedForwardNeuralNetwork
net
=
new
FeedForwardNeuralNetwork
(
1
,
new
int
[]{
6
,
10
,
4
},
ActivationFunction
.
LOGISTIC
,
ActivationFunction
.
LOGISTIC
);
double
[][][]
population
=
new
double
[
popSize
][][];
for
(
int
i
=
0
;
i
<
popSize
;
i
++)
{
net
.
generateRandomWeights
();
population
[
i
] =
new
double
[
1
][];
population
[
i
][
0
] =
net
.
getWeights
();
// System.out.println("net length:" + net.getWeights().length);
}
for
(
int
i
=
0
;
i
<
rounds
;
i
++)
{
population
=
runTheGA
(
population
,
mutationRate
,
crossOverRate
,
mutationAmount
);
double
[]
totalFitness
=
new
double
[
popSize
];
for
(
int
j
=
0
;
j
<
popSize
;
j
++)
{
for
(
int
k
=
0
;
k
<
popSize
;
k
++)
{
if
(
j
!=
k
)
{
double
[][]
results
=
runFightSimulation
(
population
[
j
],
population
[
k
],
0
,
0
,
false
,
i
);
totalFitness
[
j
] +=
results
[
0
][
0
];
totalFitness
[
k
] +=
results
[
1
][
0
];
// System.out.println("Score for Red: " + results[0][0]);
// System.out.println("Score for Blue: " + results[1][0]);
}
}
}
population
=
sortPopulation
(
population
,
totalFitness
);
for
(
int
j
=
0
;
j
<
totalFitness
.
length
;
j
++)
{
totalFitness
[
j
] =
0
;
}
System
.
out
.
println
(
"Finished round: "
+
i
+
" of the GA"
);
}
return
population
[
0
];
}
/**
* This method sorts the population based on their fitness values
*
* @param initialPop
* @param fitness
* @return
*/
public
static
double
[][][]
sortPopulation
(
double
[][][]
initialPop
,
double
[]
fitness
)
{
double
[][][]
sortedPop
=
new
double
[
initialPop
.
length
][][];
for
(
int
i
=
0
;
i
<
sortedPop
.
length
;
i
++)
{
sortedPop
[
i
] =
initialPop
[
i
];
}
// bubble sort FTW!!!!
for
(
int
i
=
0
;
i
<
fitness
.
length
;
i
++)
{
for
(
int
j
=
i
+
1
;
j
<
fitness
.
length
;
j
++)
{
if
(
fitness
[
j
] >
fitness
[
i
])
{
double
temp
=
fitness
[
j
];
fitness
[
j
] =
fitness
[
i
];
fitness
[
i
] =
temp
;
double
[][]
temp2
=
sortedPop
[
j
];
sortedPop
[
j
] =
sortedPop
[
i
];
sortedPop
[
i
] =
temp2
;
}
}
}
return
sortedPop
;
}
/**
* This method takes an initial population and runs the GA to evolve a superior poputation
*
* Note: this method assumes that the initial population has been sorted from best to worst
*
* @param initialPop
* @param mutationRate
* @param crossOverRate
* @param mutationAmount
* @return
*/
public
static
double
[][][]
runTheGA
(
double
[][][]
initialPop
,
double
mutationRate
,
double
crossOverRate
,
double
mutationAmount
)
{
double
[][][]
evolvedPop
=
new
double
[
initialPop
.
length
][][];
int
currentIndex
=
0
;
int
len
=
initialPop
.
length
;
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
double
[][]
selected
=
null
;
while
(
selected
==
null
)
{
if
(
Math
.
random
() < (((
double
) ((
len
+
1
) -
currentIndex
) / (
len
+
2
)) /
2
))
{
selected
=
initialPop
[
currentIndex
];
}
currentIndex
= (
currentIndex
+
1
) %
len
;
}
// Cross Over
if
(
Math
.
random
() <
crossOverRate
)
{
double
[][]
selected2
=
null
;
while
(
selected2
==
null
)
{
if
(
Math
.
random
() < (((
double
) ((
len
+
1
) -
currentIndex
) / (
len
+
2
)) /
2
))
{
selected2
=
initialPop
[
currentIndex
];
}
currentIndex
= (
currentIndex
+
1
) %
len
;
}
for
(
int
j
=
0
;
j
<
selected
.
length
;
j
++)
{
int
crossOverPoint
= (
int
) (
Math
.
random
() *
selected
[
j
].
length
);
for
(
int
k
=
crossOverPoint
;
k
<
selected
[
j
].
length
;
k
++)
{
selected
[
j
] =
selected2
[
j
];
}
}
}
// Mutation
for
(
int
j
=
0
;
j
<
selected
.
length
;
j
++)
{
for
(
int
k
=
0
;
k
<
selected
[
j
].
length
;
k
++)
{
if
(
Math
.
random
() <
mutationRate
)
{
selected
[
j
][
k
] += (
Math
.
random
() *
2
*
mutationAmount
) -
mutationAmount
;
}
}
}
evolvedPop
[
i
] =
selected
;
}
return
evolvedPop
;
}
/**
* This method returns an array with the total fitness values of all units
* for both teams in the form
*
* [
* Team 1: [
* Soldiers: [
*
* ]
* Tanks: [
*
* ]
* etc...
* ]
*
* Team 2: [
*
* ]
* ]
*
*
* @param team1Inputs
* @param team2Inputs
* @return
*/
public
static
double
[][]
runFightSimulation
(
double
[][]
team1Inputs
,
double
[][]
team2Inputs
,
int
teamA
,
int
teamB
,
boolean
verbose
,
int
round
)
{
if
(
verbose
)
{
System
.
out
.
println
(
"Simulating a match"
);
}
Game
game
=
new
Game
(
team1Inputs
,
team2Inputs
,
verbose
);
String
map
=
"FightMicroGA/Simulation/simulationMaps/onetower.xml"
;
if
(
round
%
4
==
1
)
{
map
=
"FightMicroGA/Simulation/simulationMaps/barren.xml"
;
}
else
if
(
round
%
4
==
2
)
{
map
=
"FightMicroGA/Simulation/simulationMaps/frontlines.xml"
;
}
// else if (round % 4 == 3)
// {
// map = "FightMicroGA/Simulation/simulationMaps/noeffort.xml";
// }
game
.
runMatch
(
map
,
teamA
,
teamB
);
double
[][]
results
=
new
double
[
2
][];
results
[
0
] =
game
.
getTeamResults
(
0
);
results
[
1
] =
game
.
getTeamResults
(
1
);
return
results
;
}
}
Back
|
FazBrowse Home
|
New Git URL