| 1 | #include "brain.h"
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 |
|
|---|
| 4 | NN::NN()
|
|---|
| 5 | {
|
|---|
| 6 | for(int y=0; y<N_NEURON_LAYER; y++)
|
|---|
| 7 | for(int x=0; x<N_NEURON; x++)
|
|---|
| 8 | neurons[y][x] = 0;
|
|---|
| 9 | for(int z=0; z<N_NEURON; z++)
|
|---|
| 10 | for(int y=0; y<N_NEURON; y++)
|
|---|
| 11 | for(int x=0; x<N_NEURON_LAYER; x++)
|
|---|
| 12 | weight[x][y][z] = 0;
|
|---|
| 13 | return;
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | int* NN::step(int input[N_NEURON])
|
|---|
| 18 | {
|
|---|
| 19 | // ���֔��f
|
|---|
| 20 | for(int n=0; n<N_NEURON; n++)
|
|---|
| 21 | {
|
|---|
| 22 | neurons[0][n] = input[n];
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | // �e�w��Z
|
|---|
| 26 | for(int layer=1; layer<N_NEURON_LAYER; layer++)
|
|---|
| 27 | {
|
|---|
| 28 | for(int y=0; y<N_NEURON; y++)
|
|---|
| 29 | {
|
|---|
| 30 | int t = 0;
|
|---|
| 31 | for(int x=0; x<N_NEURON; x++)
|
|---|
| 32 | {
|
|---|
| 33 | t += (int)(weight[layer-1][y][x] * neurons[layer-1][x]);
|
|---|
| 34 | if(t - 0.5 >= 0)
|
|---|
| 35 | neurons[layer][y] = 1;
|
|---|
| 36 | else
|
|---|
| 37 | neurons[layer][y] = 0;
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | // �o�͑w��� |
|---|
| 43 | return neurons[N_NEURON_LAYER-1];
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | Brain::Brain()
|
|---|
| 47 | {
|
|---|
| 48 | for(int y=0; y<NN::N_NEURON_LAYER; y++)
|
|---|
| 49 | for(int x=0; x<NN::N_NEURON; x++)
|
|---|
| 50 | nn.neurons[y][x] = 0;
|
|---|
| 51 | for(int z=0; z<NN::N_NEURON; z++)
|
|---|
| 52 | for(int y=0; y<NN::N_NEURON; y++)
|
|---|
| 53 | for(int x=0; x<NN::N_NEURON_LAYER; x++)
|
|---|
| 54 | {
|
|---|
| 55 | nn.weight[x][y][z] = 0.5;
|
|---|
| 56 | for ( int i = 0 ; i < 5 ; i ++ )
|
|---|
| 57 | nn.weight[x][y][z] += rand() % 2 == 1 ? 0.1 : -0.1;
|
|---|
| 58 | if ( nn.weight[x][y][z] < 0 )
|
|---|
| 59 | nn.weight[x][y][z] = 0;
|
|---|
| 60 | if ( nn.weight[x][y][z] > 1 )
|
|---|
| 61 | nn.weight[x][y][z] = 1;
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | int Brain::input_and_output(int *input)
|
|---|
| 66 | {
|
|---|
| 67 | int *output = nn.step(input);
|
|---|
| 68 | return output[0] * 2 + output[1];
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void Brain::copy(Brain *b)
|
|---|
| 72 | {
|
|---|
| 73 | for(int y=0; y<NN::N_NEURON_LAYER; y++)
|
|---|
| 74 | for(int x=0; x<NN::N_NEURON; x++)
|
|---|
| 75 | nn.neurons[y][x] = b->nn.neurons[y][x];
|
|---|
| 76 | for(int z=0; z<NN::N_NEURON; z++)
|
|---|
| 77 | for(int y=0; y<NN::N_NEURON; y++)
|
|---|
| 78 | for(int x=0; x<NN::N_NEURON_LAYER; x++)
|
|---|
| 79 | nn.weight[x][y][z] = b->nn.weight[x][y][z];
|
|---|
| 80 |
|
|---|
| 81 | // �ˑR�ψ� |
|---|
| 82 | for ( int i = 0 ; i < 100 ; i ++ )
|
|---|
| 83 | nn.weight[rand()%NN::N_NEURON_LAYER][rand()%NN::N_NEURON][rand()%NN::N_NEURON] += 0.1 * ( rand()%2 == 0 ? -1 : 1 );
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|