-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddr-radar-feet
executable file
·586 lines (513 loc) · 18.7 KB
/
ddr-radar-feet
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename qw(basename);
use AI::FANN qw(:all);
my $SCALE = {
S => {low=>0, high=>200},
V => {low=>0, high=>200},
A => {low=>0, high=>200},
F => {low=>0, high=>200},
C => {low=>0, high=>200},
FEET => {low=>0, high=>20},
};
my $DESIRED_ERROR = 0.00033;
my $MAX_EPOCHS = 50000;
my $HIDDEN_NODES = 8;
my $SHUFFLE_DATA = 1;
my $ACTIVATION_FUNCTION = FANN_SIGMOID_SYMMETRIC;
my $CACHE_DIR = "$ENV{HOME}/.cache/ddr-radar-feet";
my $RADAR_CACHE = "$ENV{HOME}/.cache/ddr-radar-feet/radar-training-data";
my $EXEC_SIMFILE_RADAR = "simfile-radar";
my $SONGS_BASE_DIR = "$ENV{HOME}/.stepmania/Songs";
my $DDRNAME_SONGLIST = "$ENV{HOME}/.config/ddrname-songlist";
my $TRAINING_SIMFILES_PATTERN = "$SONGS_BASE_DIR/DDR_*/*/*.sm";
my @ALL_GAMES = qw(singles doubles);
my @ALL_DIFFS = qw(beginner basic difficult expert challenge);
my $FORCE_NEURAL_NET_GAME = "singles"; #default if game is unknown (e.g.: dance-solo)
my $FORCE_NEURAL_NET_DIFF = "expert"; #default if diff is unknown (e.g.: edit)
my $REPLACE_GAME_DIFF_KEYS = {
"doubles-beginner" => "doubles-basic",
};
my $FMT_REPLACE_GAME_DIFF_KEYS = join "\n ",
map {"$_ => $$REPLACE_GAME_DIFF_KEYS{$_}"} sort keys %$REPLACE_GAME_DIFF_KEYS;
my $FMT_SKIP_GAME_DIFF_KEYS = join " ", sort keys %$REPLACE_GAME_DIFF_KEYS;
sub runAllGameDiffs($);
sub runNeuralNet($$$$$$$);
sub ensureRadarCache();
sub generateRadarCache();
sub addSimfileToRadarCache($);
sub readRadarCache();
sub runSimfileRadar($@);
sub parseRadarLine($);
sub readZeniusFeetFromSongList();
sub ensureNeuralNet($$);
sub createNeuralNet($$);
sub loadNeuralNet($$);
sub loadNeuralNetTrainingData($$);
sub getScaledValue($$);
sub getUnscaledValue($$);
sub roundFeetFormat($);
sub run(@);
sub readProcLine(@);
my $EXEC = basename $0;
my $usage = "Usage:
Creates a neural network with groove radar values as input nodes,
and DDR \"feet\" difficulty as the output node.
A different neuralnet is created for each game-type and difficulty-type.
Training input data (radar) is read from $EXEC_SIMFILE_RADAR, using simfiles that match:
$TRAINING_SIMFILES_PATTERN
Training output data (feet) from zenius is read from $DDRNAME_SONGLIST
Only radar-values that have a matching feet-value (by SONG_NAME_ID, GAME, and DIFF) are used.
$EXEC -h|--help
show this message
$EXEC --ensure | --cache
-ensure training radar data is cached ($EXEC --ensure-training-radar-data)
-for each GAME (@ALL_GAMES) and DIFF (@ALL_DIFFS):
-skip if GAME-DIFF is: $FMT_SKIP_GAME_DIFF_KEYS
-ensure neuralnet is built ($EXEC --ensure-neuralnet GAME DIFF)
$EXEC GAME DIFF STREAM VOLTAGE AIR FREEZE CHAOS
$EXEC --run-inputs GAME DIFF STREAM VOLTAGE AIR FREEZE CHAOS
-ensure neuralnet for GAME+DIFF ($EXEC --ensure-neuralnet GAME DIFF)
-write input nodes (STREAM/VOLTAGE/AIR/FREEZE/CHAOS) nodes and read output node (FEET)
-format FEET as %04.1f
-print \"FEET\" (e.g.; 15.3)
$EXEC GAME DIFF SIMFILE
$EXEC --run-simfile GAME DIFF SIMFILE
-extract STREAM/VOLTAGE/AIR/FREEZE/CHAOS from SIMFILE with $EXEC_SIMFILE_RADAR
-calculate FEET (same as: $EXEC --run-inputs GAME DIFF STREAM VOLTAGE AIR FREEZE CHAOS)
-format FEET as %04.1f
-print \"FEET\" (e.g.; 15.3)
$EXEC SIMFILE
$EXEC --run-simfile-all SIMFILE
-extract all GAME/DIFF from SIMFILE
-for each GAME DIFF:
-extract STREAM/VOLTAGE/AIR/FREEZE/CHAOS from SIMFILE with $EXEC_SIMFILE_RADAR
-calculate FEET (same as: $EXEC --run-inputs GAME DIFF STREAM VOLTAGE AIR FREEZE CHAOS)
-format FEET as %04.1f
-print \"GAME-DIFF FEET\" (e.g.: singles-expert 15.3)
$EXEC --ddrsonglist SIMFILE
-calculate FEET for each GAME-DIFF as in `$EXEC --run-simfile-all SIMFILE`
-get FEET values for each of the following 9 GAME-DIFFs:
singles-beginner singles-basic singles-difficult singles-expert singles-challenge
doubles-basic doubles-difficult doubles-expert doubles-challenge
-round each to the nearest integer half-up, format as two digits (as in `printf '%02d'`)
-fill in missing values with '--'
-print the 9 values in a single line, formatted as:
| ##| ##| ##| ##| ##| | ##| ##| ##| ##|
$EXEC --calculate-training-radar-data
-remove cached radar training data
$RADAR_CACHE
-extract input radar training data with $EXEC_SIMFILE_RADAR for simfiles that match this pattern:
$TRAINING_SIMFILES_PATTERN
-format each training input data set as:
<GAME> | <DIFF> | S<STREAM> | V<VOLTAGE> | A<AIR> | F<FREEZE> | C<CHAOS> | <SONG_NAME_ID>
-store the values in $RADAR_CACHE
NOTE: this is computationally and i/o intensive, and is by far the slowest part
currently takes about 780s without $EXEC_SIMFILE_RADAR cached
(about 90s with $EXEC_SIMFILE_RADAR cached)
$EXEC --ensure-training-radar-data
if cache file $RADAR_CACHE exists:
do nothing
otherwise:
same as: $EXEC --calculate-training-radar-data
$EXEC --build-neuralnet GAME DIFF
-replace GAME-DIFF keys that have no training data:
$FMT_REPLACE_GAME_DIFF_KEYS
-remove cached neuralnet: $CACHE_DIR/neuralnet-GAME-DIFF.ann
-ensure training radar data ($EXEC --ensure-training-radar-data)
-extract output data from $DDRNAME_SONGLIST
-create a new FANN neural net:
-input nodes = 5 (STREAM, VOLTAGE, AIR, FREEZE, CHAOS)
-output nodes = 1 (FEET)
-hidden notes = $HIDDEN_NODES
-activation fct = $ACTIVATION_FUNCTION
-error = $DESIRED_ERROR
-max epochs = $MAX_EPOCHS
-shuffle data = " . ($SHUFFLE_DATA ? "yes" : "no") . "
-train on the data set
-store the neuralnet in $CACHE_DIR/neuralnet-GAME-DIFF.ann
NOTE: this is cpu-intensive, but fairly fast- currently takes 1s ~ 15s
$EXEC --ensure-neuralnet GAME DIFF
if neuralnet $CACHE_DIR/neuralnet-GAME-DIFF.ann exists:
do nothing
otherwise:
same as: $EXEC --build-neuralnet GAME DIFF
";
my $CMD_ENSURE = "ensure";
my $CMD_RUN_INPUTS = "run-inputs";
my $CMD_RUN_SIMFILE = "run-simfile";
my $CMD_RUN_SIMFILE_ALL = "run-simfile-all";
my $CMD_DDRSONGLIST = "ddrsonglist";
my $CMD_CALCULATE_TRAINING_RADAR_DATA = "calculate-training-radar-data";
my $CMD_ENSURE_TRAINING_RADAR_DATA = "ensure-training-radar-data";
my $CMD_BUILD_NEURAL_NET = "build-neuralnet";
my $CMD_ENSURE_NEURAL_NET = "ensure-neuralnet";
my @ALL_CMDS = (
$CMD_ENSURE,
$CMD_RUN_INPUTS,
$CMD_RUN_SIMFILE,
$CMD_RUN_SIMFILE_ALL,
$CMD_DDRSONGLIST,
$CMD_CALCULATE_TRAINING_RADAR_DATA,
$CMD_ENSURE_TRAINING_RADAR_DATA,
$CMD_BUILD_NEURAL_NET,
$CMD_ENSURE_NEURAL_NET,
);
my $OK_CMDS = join "|", @ALL_CMDS;
my $OK_GAMES = join "|", @ALL_GAMES;
my $OK_DIFFS = join "|", @ALL_DIFFS;
sub main(@){
my $cmd = undef;
my $game = undef;
my $diff = undef;
my $simfile = undef;
my @inputVals;
while(@_ > 0){
my $arg = shift;
if($arg =~ /^(-h|--help)$/){
print $usage;
exit 0;
}elsif($arg =~ /^--($OK_CMDS)$/){
$cmd = $1
}elsif($arg =~ /^(--cache)$/){
$cmd = $CMD_ENSURE;
}elsif($arg =~ /^($OK_GAMES)$/){
$game = $1;
}elsif($arg =~ /^($OK_DIFFS)$/){
$diff = $1;
}elsif($arg =~ /^(\d+|\d*\.\d+)$/){
push @inputVals, $1;
}elsif(-f $arg){
$simfile = $arg;
}else{
die "$usage\nERROR: unknown arg \"$arg\"\n";
}
}
my $inputCount = @inputVals;
if(not defined $cmd){
my $gameAndDiff = (defined $game and defined $diff) ? 1 : 0;
$cmd = $CMD_RUN_INPUTS if $inputCount==5 and $gameAndDiff;
$cmd = $CMD_RUN_SIMFILE if defined $simfile and $gameAndDiff;
$cmd = $CMD_RUN_SIMFILE_ALL if defined $simfile and not $gameAndDiff;
}
die $usage if not defined $cmd;
if($cmd eq $CMD_ENSURE){
ensureRadarCache();
my %okSkips = map {$_=>1} sort keys %$REPLACE_GAME_DIFF_KEYS;
for my $game(@ALL_GAMES){
for my $diff(@ALL_DIFFS){
next if defined $okSkips{"$game-$diff"};
ensureNeuralNet($game, $diff);
}
}
}elsif($cmd eq $CMD_CALCULATE_TRAINING_RADAR_DATA){
generateRadarCache();
}elsif($cmd eq $CMD_ENSURE_TRAINING_RADAR_DATA){
ensureRadarCache();
}elsif($cmd eq $CMD_BUILD_NEURAL_NET){
die "ERROR: GAME is required for $cmd\n" if not defined $game;
die "ERROR: DIFF is required for $cmd\n" if not defined $diff;
createNeuralNet($game, $diff);
}elsif($cmd eq $CMD_ENSURE_NEURAL_NET){
die "ERROR: GAME is required for $cmd\n" if not defined $game;
die "ERROR: DIFF is required for $cmd\n" if not defined $diff;
ensureNeuralNet($game, $diff);
}elsif($cmd eq $CMD_RUN_INPUTS){
die "ERROR: GAME is required for $cmd\n" if not defined $game;
die "ERROR: DIFF is required for $cmd\n" if not defined $diff;
die "ERROR: insufficient radar inputs (expected 5, got $inputCount)\n" if $inputCount != 5;
my ($s, $v, $a, $f, $c) = @inputVals;
my $feet = runNeuralNet($game, $diff, $s, $v, $a, $f, $c);
printf "%05.2f\n", $feet;
}elsif($cmd eq $CMD_RUN_SIMFILE){
die "ERROR: GAME is required for $cmd\n" if not defined $game;
die "ERROR: DIFF is required for $cmd\n" if not defined $diff;
die "ERROR: SIMFILE is required for $cmd\n" if not defined $simfile;
my @radars = map {parseRadarLine($_)} runSimfileRadar($simfile, "--$game", "--$diff");
die "ERROR: too many $EXEC_SIMFILE_RADAR results for $game-$diff for $simfile\n" if @radars > 1;
die "ERROR: no $EXEC_SIMFILE_RADAR results for $game-$diff for $simfile\n" if @radars < 1;
my $r = $radars[0];
my $feet = runNeuralNet($game, $diff, $$r{S}, $$r{V}, $$r{A}, $$r{F}, $$r{C});
printf "%05.2f\n", $feet;
}elsif($cmd eq $CMD_RUN_SIMFILE_ALL){
die "ERROR: SIMFILE is required for $cmd\n" if not defined $simfile;
my @entries = runAllGameDiffs($simfile);
for my $entry(@entries){
printf "%-17s %05.2f\n", "$$entry{game}-$$entry{diff}", $$entry{feet};
}
}elsif($cmd eq $CMD_DDRSONGLIST){
die "ERROR: SIMFILE is required for $cmd\n" if not defined $simfile;
my @entries = runAllGameDiffs($simfile);
my %feetByGameDiff = map {$$_{game} . "-" . $$_{diff} => $$_{feet}} @entries;
my $fmt = sprintf("| %s| %s| %s| %s| %s| | %s| %s| %s| %s|",
roundFeetFormat($feetByGameDiff{"singles-beginner"}),
roundFeetFormat($feetByGameDiff{"singles-basic"}),
roundFeetFormat($feetByGameDiff{"singles-difficult"}),
roundFeetFormat($feetByGameDiff{"singles-expert"}),
roundFeetFormat($feetByGameDiff{"singles-challenge"}),
roundFeetFormat($feetByGameDiff{"doubles-basic"}),
roundFeetFormat($feetByGameDiff{"doubles-difficult"}),
roundFeetFormat($feetByGameDiff{"doubles-expert"}),
roundFeetFormat($feetByGameDiff{"doubles-challenge"}),
);
print "$fmt\n";
}else{
die "ERROR: unknown cmd \"$cmd\"\n";
}
}
sub runAllGameDiffs($){
my ($simfile) = @_;
my @radars = map {parseRadarLine($_)} runSimfileRadar($simfile);
my @entries;
for my $r(@radars){
my ($game, $diff) = ($$r{GAME}, $$r{DIFF});
$game = "singles" if $game =~ /^(dance-single)$/;
$game = "doubles" if $game =~ /^(dance-double)$/;
my $neuralnetGame = $game =~ /^($OK_GAMES)$/ ? $game : $FORCE_NEURAL_NET_GAME;
my $neuralnetDiff = $diff =~ /^($OK_DIFFS)$/ ? $diff : $FORCE_NEURAL_NET_DIFF;
if($game ne $neuralnetGame or $diff ne $neuralnetDiff){
my $actual = "$game-$diff";
my $replacement = "$neuralnetGame-$neuralnetDiff";
print STDERR " WARNING: forced neuralnet \"$replacement\" for unknown \"$actual\"\n";
}
my $feet = runNeuralNet($neuralnetGame, $neuralnetDiff,
$$r{S}, $$r{V}, $$r{A}, $$r{F}, $$r{C});
push @entries, {
game => $game,
diff => $diff,
radar => $r,
feet => $feet,
};
}
return @entries;
}
sub runNeuralNet($$$$$$$){
my ($game, $diff, $s, $v, $a, $f, $c) = @_;
my $origGameDiffKey = "$game-$diff";
my $replaceGameDiffKey = $$REPLACE_GAME_DIFF_KEYS{$origGameDiffKey};
if(defined $replaceGameDiffKey){
print STDERR "WARNING: using '$replaceGameDiffKey' neuralnet instead of '$origGameDiffKey'\n";
if($replaceGameDiffKey !~ /^(\w+)-(\w+)$/){
die "ERROR: replacement game-diff key malformed \"$replaceGameDiffKey\"\n";
}else{
($game, $diff) = ($1, $2);
}
}
my $ann = loadNeuralNet($game, $diff);
my $input = [
getScaledValue($s, $$SCALE{S}),
getScaledValue($v, $$SCALE{V}),
getScaledValue($a, $$SCALE{A}),
getScaledValue($f, $$SCALE{F}),
getScaledValue($c, $$SCALE{C}),
];
my $out = $ann->run($input);
my $feet = getUnscaledValue($$out[0], $$SCALE{FEET});
return $feet;
}
sub ensureRadarCache(){
generateRadarCache() if not -f $RADAR_CACHE;
}
sub generateRadarCache(){
run "rm", "-f", $RADAR_CACHE;
my @simfiles = glob $TRAINING_SIMFILES_PATTERN;
for my $simfile(@simfiles){
addSimfileToRadarCache($simfile);
}
}
sub addSimfileToRadarCache($){
my ($simfile) = @_;
my @lines = runSimfileRadar($simfile);
open FH, ">> $RADAR_CACHE" or die "ERROR: could not append to $RADAR_CACHE\n$!\n";
print FH $_ foreach @lines;
close FH;
}
sub readRadarCache(){
open FH, "< $RADAR_CACHE" or die "ERROR: could not read $RADAR_CACHE\n$!\n";
my @lines = <FH>;
close FH;
my $radar = {};
for my $line(@lines){
my $r = parseRadarLine($line);
die "ERROR: malformed line in $RADAR_CACHE\n$line" if not defined $r;
my $songNameId = $$r{SONG_NAME_ID};
my $gameDiffKey = $$r{GAME} . "-" . $$r{DIFF};
$$radar{$songNameId} = {} if not defined $$radar{$songNameId};
$$radar{$songNameId}{$gameDiffKey} = $r;
}
return $radar;
}
sub runSimfileRadar($@){
my ($simfile, @args) = @_;
my @lines = readProcLines(
"$EXEC_SIMFILE_RADAR",
"--format="
. "%-7s-GAME"
. " | %-10s-DIFF"
. " | S%-3d-STREAM | V%-3d-VOLTAGE | A%-3d-AIR | F%-3d-FREEZE | C%-3d-CHAOS"
. " | %SONG_NAME_ID"
. "%n",
$simfile,
@args,
);
return @lines;
}
sub parseRadarLine($){
my ($line) = @_;
if($line =~ /^
(?<game> [a-zA-Z0-9\-]+)
\s*\|\s* (?<diff> \w+)
\s*\|\s* S(?<S> \d+)
\s*\|\s* V(?<V> \d+)
\s*\|\s* A(?<A> \d+)
\s*\|\s* F(?<F> \d+)
\s*\|\s* C(?<C> \d+)
\s*\|\s* (?<songNameId> [a-z0-9\-]+)
$/x){
return {
SONG_NAME_ID => $+{songNameId},
GAME => $+{game},
DIFF => $+{diff},
S => $+{S},
V => $+{V},
A => $+{A},
F => $+{F},
C => $+{C},
};
}else{
return undef;
}
}
sub readZeniusFeetFromSongList(){
open FH, "<", $DDRNAME_SONGLIST or die "ERROR: could not read $DDRNAME_SONGLIST\n$!\n";
my @lines = <FH>;
close FH;
my $zeniusFeet = {};
for my $line(@lines){
$line =~ s/#.*//;
next if $line =~ /^\s*$/;
if($line =~ /^
(?<songNameId> [a-z0-9\-]+)
\s*\|\s* (?<abbrev> \w+)
\s*\|\s* (?<feetSinglesBeginner> \d+|-+)
\s*\|\s* (?<feetSinglesBasic> \d+|-+)
\s*\|\s* (?<feetSinglesDifficult> \d+|-+)
\s*\|\s* (?<feetSinglesExpert> \d+|-+)
\s*\|\s* (?<feetSinglesChallenge> \d+|-+)
\s*\|\s* \s*
\s*\|\s* (?<feetDoublesBasic> \d+|-+)
\s*\|\s* (?<feetDoublesDifficult> \d+|-+)
\s*\|\s* (?<feetDoublesExpert> \d+|-+)
\s*\|\s* (?<feetDoublesChallenge> \d+|-+)
\s*\|\s*
$/x){
$$zeniusFeet{$+{songNameId}} = {
"singles-beginner" => $+{feetSinglesBeginner},
"singles-basic" => $+{feetSinglesBasic},
"singles-difficult" => $+{feetSinglesDifficult},
"singles-expert" => $+{feetSinglesExpert},
"singles-challenge" => $+{feetSinglesChallenge},
"doubles-basic" => $+{feetDoublesBasic},
"doubles-difficult" => $+{feetDoublesDifficult},
"doubles-expert" => $+{feetDoublesExpert},
"doubles-challenge" => $+{feetDoublesChallenge},
};
}else{
die "ERROR: malformed line in $DDRNAME_SONGLIST\n$line";
}
}
return $zeniusFeet;
}
sub ensureNeuralNet($$){
my ($game, $diff) = @_;
my $neuralNetCacheFile = "$CACHE_DIR/neuralnet-$game-$diff.ann";
if(not -f $neuralNetCacheFile){
createNeuralNet($game, $diff);
}
}
sub createNeuralNet($$){
my ($game, $diff) = @_;
my $neuralNetCacheFile = "$CACHE_DIR/neuralnet-$game-$diff.ann";
run "rm", "-f", $neuralNetCacheFile;
my $ann = AI::FANN->new_standard(5, $HIDDEN_NODES, 1);
$ann->hidden_activation_function($ACTIVATION_FUNCTION);
$ann->output_activation_function($ACTIVATION_FUNCTION);
my $data = loadNeuralNetTrainingData($game, $diff);
if(@$data == 0){
print STDERR "WARNING: no data found for $game-$diff (cannot create neuralnet)\n";
return;
}
my $trainData = AI::FANN::TrainData->new(@$data);
$trainData->shuffle() if $SHUFFLE_DATA;
$ann->train_on_data($trainData, $MAX_EPOCHS, 1000, $DESIRED_ERROR);
$ann->save($neuralNetCacheFile);
}
sub loadNeuralNet($$){
my ($game, $diff) = @_;
my $neuralNetCacheFile = "$CACHE_DIR/neuralnet-$game-$diff.ann";
ensureNeuralNet($game, $diff);
return AI::FANN->new_from_file($neuralNetCacheFile);
}
sub loadNeuralNetTrainingData($$){
my ($game, $diff) = @_;
my $data = [];
ensureRadarCache();
my $radar = readRadarCache();
my $zeniusFeet = readZeniusFeetFromSongList();
for my $songNameId(sort keys %$radar){
for my $gameDiffKey(sort keys %{$$radar{$songNameId}}){
my $r = $$radar{$songNameId}{$gameDiffKey};
next if not defined $$zeniusFeet{$songNameId};
next if not defined $$zeniusFeet{$songNameId}{$gameDiffKey};
next if $$r{GAME} !~ /^$game$/i;
next if $$r{DIFF} !~ /^$diff$/i;
my $feet = $$zeniusFeet{$songNameId}{$gameDiffKey};
next if $feet !~ /^\d+$/;
my $input = [
getScaledValue($$r{S}, $$SCALE{S}),
getScaledValue($$r{V}, $$SCALE{V}),
getScaledValue($$r{A}, $$SCALE{A}),
getScaledValue($$r{F}, $$SCALE{F}),
getScaledValue($$r{C}, $$SCALE{C}),
];
my $output = [
getScaledValue($feet, $$SCALE{FEET}),
];
push @$data, $input;
push @$data, $output;
}
}
return $data;
}
sub getScaledValue($$){
my ($val, $valScale) = @_;
my $low = $$valScale{low};
my $high = $$valScale{high};
return ($val-$low) / ($high - $low);
}
sub getUnscaledValue($$){
my ($scaledVal, $valScale) = @_;
my $low = $$valScale{low};
my $high = $$valScale{high};
return ($scaledVal*($high-$low)) + $low;
}
sub roundFeetFormat($){
my ($feet) = @_;
return defined $feet ? sprintf("%02d", int($feet + 0.5)) : "--";
}
sub run(@){
print "@_\n";
system @_;
if($? != 0){
die "error running \"@_\"\n";
}
}
sub readProcLines(@){
my @cmd = @_;
open CMD, "-|", @cmd or die "ERROR: \"@cmd\" failed\n$!\n";
my @lines = <CMD>;
close CMD;
return @lines;
}
&main(@ARGV);