Created
July 15, 2021 00:59
-
-
Save mattn/d14dbf39b5e5701b161ffb54af222692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <memory.h> | |
#include <time.h> | |
#include "genann.h" | |
static void | |
bin(int n, double* d) { | |
int i; | |
memset(d, 0, sizeof(double)*10); | |
for (i = 0; i < 10; i++) { | |
d[i] = (double)((n >> i) & 1); | |
} | |
} | |
static int | |
dec(const double* d) { | |
int i, mi = 0; | |
double m = 0; | |
for (i = 0; i < 4; i++) { | |
if (m < d[i]) { | |
m = d[i]; | |
mi = i; | |
} | |
} | |
return mi; | |
} | |
static void | |
fizz_buzz(int n, double* d) { | |
memset(d, 0, sizeof(double)*4); | |
if (n%15 == 0) { | |
d[0] = 1; | |
} else if (n%3 == 0) { | |
d[1] = 1; | |
} else if (n%5 == 0) { | |
d[2] = 1; | |
} else { | |
d[3] = 1; | |
} | |
} | |
int | |
main() { | |
int i; | |
srand(time(0)); | |
// Initialize | |
genann *ann = genann_init(10, 1, 10, 4); | |
// Learn output with input. input is encoded with 10 floating-point numbers. | |
// output is encoded with 4 floating-point numbers. | |
for (i = 0; i < 500000; ++i) { | |
double input[10]; | |
double output[4]; | |
bin(i%100, input); | |
fizz_buzz(i%100+1, output); | |
genann_train(ann, input, output, 3); | |
} | |
// Output FizzBuzz | |
for (i = 0; i < 200; i++) { | |
double input[10]; | |
bin(i, input); | |
const double* f = genann_run(ann, input); | |
int n = dec(f); | |
switch (n) { | |
case 0: | |
puts("FizzBuzz"); | |
break; | |
case 1: | |
puts("Fizz"); | |
break; | |
case 2: | |
puts("Buzz"); | |
break; | |
case 3: | |
printf("%d\n", i+1); | |
break; | |
} | |
} | |
genann_free(ann); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment