cleaned all trailing white space from source files.
[sdk] / samples / eC / neural / neural.ec
1 import "neurons"
2
3 enum Behavior
4 {
5    RUN,
6    HIDE,
7    WANDER,
8    ATTACK
9 };
10
11 enum Health
12 {
13    POOR,
14    OK,
15    GOOD
16 };
17
18 struct Example
19 {
20    Health health;
21    bool hasKnife;
22    bool hasGun;
23    int ennemies;
24    Behavior result;
25 };
26
27 static Example examples[] =
28 {
29    { GOOD, false,false, 0, WANDER },
30    { GOOD, false,false, 1, WANDER },
31    { GOOD, false,true,  1, ATTACK },
32    { GOOD, false,true,  2, ATTACK },
33    { GOOD, true, false, 2, HIDE   },
34    { GOOD, true, false, 1, ATTACK },
35    { OK,   false,false, 0, WANDER },
36    { OK,   false,false, 1, HIDE   },
37    { OK,   false,true,  1, ATTACK },
38    { OK,   false,true,  2, HIDE   },
39    { OK,   true, false, 2, HIDE   },
40    { OK,   true, false, 1, HIDE   },
41    { POOR, false,false, 0, WANDER },
42    { POOR, false,false, 1, HIDE   },
43    { POOR, false,true,  1, HIDE   },
44    { POOR, false,true,  2, RUN    },
45    { POOR, true, false, 2, RUN    },
46    { POOR, true, false, 1, HIDE   }
47 };
48
49 char * behaviors[4] = { "Run", "Hide", "Wander", "Attack" };
50
51 #define NUM_EXAMPLES (sizeof(examples) / sizeof(Example))
52
53 static Example tests[] =
54 {
55    { GOOD, true, true, 1 },
56    { OK,   true, true, 2 },
57    { POOR, false,false,0 },
58    { POOR, true, true, 1 },
59    { GOOD, false,true, 3 },
60    { GOOD, true, false,3 },
61    { POOR, true, false,3 }
62 };
63
64 #define NUM_TESTS (sizeof(tests) / sizeof(Example))
65
66 #define NUM_ITERATIONS  6000
67
68 #define LEARN_RATE   0.2
69
70 #define NUM_INPUT    4
71 #define NUM_HIDDEN   3
72 #define NUM_OUTPUT   4
73
74 static Neuron inputNeurons[NUM_INPUT];
75 static Neuron hiddenNeurons[NUM_HIDDEN];
76 static Neuron outputNeurons[NUM_OUTPUT];
77
78 class NeuralApp : Application
79 {
80    void Main()
81    {
82       // ********** CONSTRUCT NEURAL NETWORK *****************
83       int i,h,o;
84       int c;
85
86       RandomSeed((int)(GetTime() * 1000));
87       // Input to hidden cells synapses
88       for(i = 0; i<NUM_HIDDEN; i++)
89          hiddenNeurons[i].Init();
90       for(i = 0; i<NUM_OUTPUT; i++)
91          outputNeurons[i].Init();
92       for(i = 0; i<NUM_INPUT; i++)
93       {
94          Neuron * input = &inputNeurons[i];
95
96          input->Init();
97          input->axons.size = NUM_HIDDEN;
98          for(h = 0; h<NUM_HIDDEN; h++)
99          {
100             Neuron * hidden = &hiddenNeurons[h];
101             Synapse * synapse = &input->axons._[h];
102
103             if(!hidden->dendrons.size)
104                hidden->dendrons.size = NUM_INPUT;
105             hidden->dendrons._[i] = synapse;
106
107             synapse->dendron = input;
108             synapse->axon = hidden;
109             synapse->weight = GetRandDouble(-0.5, 0.5);
110          }
111       }
112
113       // Hidden to output cells synapses
114       for(h = 0; h<NUM_HIDDEN; h++)
115       {
116          Neuron * hidden = &hiddenNeurons[h];
117
118          hidden->axons.size = NUM_OUTPUT;
119          for(o = 0; o<NUM_OUTPUT; o++)
120          {
121             Neuron * output = &outputNeurons[o];
122             Synapse * synapse = &hidden->axons._[o];
123
124             if(!output->dendrons.size)
125                output->dendrons.size = NUM_HIDDEN;
126             output->dendrons._[h] = synapse;
127
128             synapse->dendron = hidden;
129             synapse->axon = output;
130             synapse->weight = GetRandDouble(-0.5, 0.5);
131          }
132          hidden->bias = GetRandDouble(-0.5, 0.5);
133       }
134
135       // Output cells
136       for(o = 0; o<NUM_OUTPUT; o++)
137       {
138          Neuron * output = &outputNeurons[o];
139          output->bias = GetRandDouble(-0.5, 0.5);
140       }
141
142       // ********** TRAIN NEURAL NETWORK *****************
143       for(i = 0; i<NUM_ITERATIONS; i++)
144       {
145          int e;
146          for(e = 0; e<NUM_EXAMPLES; e++)
147          {
148             Example * example = &examples[e];
149
150             for(c = 0; c<NUM_OUTPUT; c++)
151                outputNeurons[c].Unactivate();
152
153             inputNeurons[0].activation = (double)example->health;
154             inputNeurons[1].activation = example->hasKnife;
155             inputNeurons[2].activation = example->hasGun;
156             inputNeurons[3].activation = example->ennemies;
157             for(c = 0; c<NUM_OUTPUT; c++)
158                outputNeurons[c].Activate();
159
160             for(c = 0; c<NUM_OUTPUT; c++)
161                outputNeurons[c].error = ((double)example->result == c) - outputNeurons[c].activation;
162
163             for(c = 0; c<NUM_INPUT; c++)
164                inputNeurons[c].BackPropagate();
165             for(c = 0; c<NUM_OUTPUT; c++)
166                outputNeurons[c].Teach(LEARN_RATE);
167          }
168       }
169
170       // ********** TEST NEURAL NETWORK *****************
171       for(c = 0; c<NUM_EXAMPLES; c++)
172       {
173          Example * example = &examples[c];
174          int winner;
175
176          for(o = 0; o<NUM_OUTPUT; o++)
177             outputNeurons[o].Unactivate();
178
179          inputNeurons[0].activation = (double)example->health;
180          inputNeurons[1].activation = example->hasKnife;
181          inputNeurons[2].activation = example->hasGun;
182          inputNeurons[3].activation = example->ennemies;
183
184          for(o = 0; o<NUM_OUTPUT; o++)
185             outputNeurons[o].Activate();
186
187          winner = Neuron_Winner(outputNeurons, NUM_OUTPUT);
188          printf("%s\n", behaviors[winner]);
189       }
190
191       // ********** UNKNOWN TEST CASES *****************
192       printf("\n\nUnknown Cases:\n\n");
193       for(c = 0; c<NUM_TESTS; c++)
194       {
195          Example * example = &tests[c];
196          int winner;
197
198          for(o = 0; o<NUM_OUTPUT; o++)
199             outputNeurons[o].Unactivate();
200
201          inputNeurons[0].activation = (double)example->health;
202          inputNeurons[1].activation = example->hasKnife;
203          inputNeurons[2].activation = example->hasGun;
204          inputNeurons[3].activation = example->ennemies;
205
206          for(o = 0; o<NUM_OUTPUT; o++)
207             outputNeurons[o].Activate();
208
209          winner = Neuron_Winner(outputNeurons, NUM_OUTPUT);
210          printf("%s\n", behaviors[winner]);
211       }
212       getch();
213    }
214 }