source: liacs/ai/poker/nn.c.tex@ 298

Last change on this file since 298 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 21.8 KB
Line 
1% Generator: GNU source-highlight, by Lorenzo Bettini, http://www.gnu.org/software/src-highlite
2\noindent
3\mbox{}\texttt{001:} \textit{/*} \\
4\mbox{}\texttt{002:} \textit{\ *\ Rick\ van\ der\ Zwet} \\
5\mbox{}\texttt{003:} \textit{\ *\ 0433373} \\
6\mbox{}\texttt{004:} \textit{\ *\ OS\ Assigment\ 3} \\
7\mbox{}\texttt{005:} \textit{\ *\ Licence:\ BSD} \\
8\mbox{}\texttt{006:} \textit{\ *\ \$Id:\ nn.c\ 555\ 2008-04-07\ 21:59:55Z\ rick\ \$} \\
9\mbox{}\texttt{007:} \textit{*/} \\
10\mbox{}\texttt{008:} \\
11\mbox{}\texttt{009:} \textbf{\#include}\ \texttt{$<$sysexits.h$>$} \\
12\mbox{}\texttt{010:} \textbf{\#include}\ \texttt{$<$stdio.h$>$} \\
13\mbox{}\texttt{011:} \textbf{\#include}\ \texttt{$<$stdlib.h$>$} \\
14\mbox{}\texttt{012:} \textbf{\#include}\ \texttt{$<$math.h$>$} \\
15\mbox{}\texttt{013:} \textbf{\#include}\ \texttt{$<$time.h$>$} \\
16\mbox{}\texttt{014:} \\
17\mbox{}\texttt{015:} \textit{/*\ NOTE:\ All\ first\ knobs\ are\ bias\ knobs\ or\ hidden\ stale\ knobs} \\
18\mbox{}\texttt{016:} \textit{\ *\ -\ Validation\ is\ done\ using\ rounding,\ please\ make\ outputs\ discrete\ or} \\
19\mbox{}\texttt{017:} \textit{\ *\ alter\ validation\ function} \\
20\mbox{}\texttt{018:} \textit{\ */} \\
21\mbox{}\texttt{019:} \\
22\mbox{}\texttt{020:} \textit{/*\ Allow\ uniform\ and\ easy\ calls\ at\ functions\ */} \\
23\mbox{}\texttt{021:} \textbf{\#define}\ TRUE\ 1 \\
24\mbox{}\texttt{022:} \textbf{\#define}\ FALSE\ 0 \\
25\mbox{}\texttt{023:} \\
26\mbox{}\texttt{024:} \\
27\mbox{}\texttt{025:} \textit{/*\ Network\ variables\ */} \\
28\mbox{}\texttt{026:} \textit{/*NOTE:\ first\ node\ is\ 'hidden'\ bias\ knob\ */} \\
29\mbox{}\texttt{027:} \textbf{\#ifndef}\ INPUT$\_$SIZE \\
30\mbox{}\texttt{028:} \textbf{\#define}\ INPUT$\_$SIZE\ 11 \\
31\mbox{}\texttt{029:} \textbf{\#endif} \\
32\mbox{}\texttt{030:} \\
33\mbox{}\texttt{031:} \textit{/*NOTE:\ first\ node\ is\ 'hidden'\ bias\ knob\ */} \\
34\mbox{}\texttt{032:} \textbf{\#ifndef}\ HIDDEN$\_$SIZE \\
35\mbox{}\texttt{033:} \textbf{\#define}\ HIDDEN$\_$SIZE\ 11 \\
36\mbox{}\texttt{034:} \textbf{\#endif} \\
37\mbox{}\texttt{035:} \\
38\mbox{}\texttt{036:} \textit{/*NOTE:\ first\ node\ is\ 'hidden'\ 'lame'\ knob\ */} \\
39\mbox{}\texttt{037:} \textbf{\#ifndef}\ OUTPUT$\_$SIZE \\
40\mbox{}\texttt{038:} \textbf{\#define}\ OUTPUT$\_$SIZE\ 11 \\
41\mbox{}\texttt{039:} \textbf{\#endif} \\
42\mbox{}\texttt{040:} \\
43\mbox{}\texttt{041:} \textit{/*\ Learn\ speed\ alpha\ of\ network\ */} \\
44\mbox{}\texttt{042:} \textbf{\#ifndef}\ LEARN$\_$SPEED \\
45\mbox{}\texttt{043:} \textbf{\#define}\ LEARN$\_$SPEED\ 0.5 \\
46\mbox{}\texttt{044:} \textbf{\#endif} \\
47\mbox{}\texttt{045:} \\
48\mbox{}\texttt{046:} \textit{/*\ After\ QUALITY$\_$ROUND\ trainingset\ check\ quality\ of\ network\ */} \\
49\mbox{}\texttt{047:} \textbf{\#define}\ QUALITY$\_$ROUND\ 100 \\
50\mbox{}\texttt{048:} \\
51\mbox{}\texttt{049:} \textit{/*\ Training\ set,\ to\ be\ used\ to\ train\ network\ */} \\
52\mbox{}\texttt{050:} char\ *\ file$\_$training\ =\ \texttt{"{}data/training.txt"{}}; \\
53\mbox{}\texttt{051:} \textit{/*\ Validation\ set,\ to\ be\ used\ to\ test\ end\ result\ of\ network\ */} \\
54\mbox{}\texttt{052:} char\ *\ file$\_$validate\ =\ \texttt{"{}data/validate.txt"{}}; \\
55\mbox{}\texttt{053:} \textit{/*\ Quality\ set,\ to\ be\ used\ to\ do\ quick\ testing\ whether\ network\ is} \\
56\mbox{}\texttt{054:} \textit{\ *\ improving} \\
57\mbox{}\texttt{055:} \textit{\ */} \\
58\mbox{}\texttt{056:} char\ *\ file$\_$quality\ =\ \texttt{"{}data/quality.txt"{}}; \\
59\mbox{}\texttt{057:} \\
60\mbox{}\texttt{058:} \textit{/*\ Globally\ defined\ arrays,\ which\ represent\ the\ network\ */} \\
61\mbox{}\texttt{059:} double\ hidden[HIDDEN$\_$SIZE]; \\
62\mbox{}\texttt{060:} double\ input[INPUT$\_$SIZE]; \\
63\mbox{}\texttt{061:} double\ output[OUTPUT$\_$SIZE]; \\
64\mbox{}\texttt{062:} double\ target[OUTPUT$\_$SIZE]; \\
65\mbox{}\texttt{063:} double\ weight$\_$HtoO[HIDDEN$\_$SIZE][OUTPUT$\_$SIZE]; \\
66\mbox{}\texttt{064:} double\ weight$\_$ItoH[INPUT$\_$SIZE][HIDDEN$\_$SIZE]; \\
67\mbox{}\texttt{065:} \\
68\mbox{}\texttt{066:} \textbf{\#define}\ WEIGHT$\_$NOT$\_$USED\ -99999 \\
69\mbox{}\texttt{067:} \\
70\mbox{}\texttt{068:} void\ \textbf{stdInit}()\ \{ \\
71\mbox{}\texttt{069:} \ \ \ \ int\ i; \\
72\mbox{}\texttt{070:} \ \ \ \ \textit{/*\ Should\ never\ change,\ been\ using\ */} \\
73\mbox{}\texttt{071:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
74\mbox{}\texttt{072:} \ \ \ \ \ \ \ \ weight$\_$ItoH[i][0]\ =\ WEIGHT$\_$NOT$\_$USED; \\
75\mbox{}\texttt{073:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
76\mbox{}\texttt{074:} \ \ \ \ \ \ \ \ weight$\_$HtoO[i][0]\ =\ WEIGHT$\_$NOT$\_$USED; \\
77\mbox{}\texttt{075:} \} \\
78\mbox{}\texttt{076:} \\
79\mbox{}\texttt{077:} \\
80\mbox{}\texttt{078:} \textit{/*\ Random\ init\ of\ weights\ */} \\
81\mbox{}\texttt{079:} void\ \textbf{randInit}()\ \{ \\
82\mbox{}\texttt{080:} \ \ \ \ int\ i,j; \\
83\mbox{}\texttt{081:} \ \ \ \ \\
84\mbox{}\texttt{082:} \ \ \ \ \textit{/*\ Different\ numbers\ every\ call\ */} \\
85\mbox{}\texttt{083:} \ \ \ \ \textbf{srandom}(\textbf{time}(NULL)); \\
86\mbox{}\texttt{084:} \\
87\mbox{}\texttt{085:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
88\mbox{}\texttt{086:} \ \ \ \ \ \ \ \ \textbf{for}\ (\ j\ =\ 1;\ j\ $<$\ HIDDEN$\_$SIZE;\ j++)\ \{ \\
89\mbox{}\texttt{087:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$ItoH[i][j]\ =\ (double)(\textbf{random}()\ \%\ 100)\ /\ 100; \\
90\mbox{}\texttt{088:} \ \ \ \ \ \ \ \ \} \\
91\mbox{}\texttt{089:} \\
92\mbox{}\texttt{090:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
93\mbox{}\texttt{091:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 1;\ j\ $<$\ OUTPUT$\_$SIZE;\ j++) \\
94\mbox{}\texttt{092:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$HtoO[i][j]\ =\ (double)(\textbf{random}()\ \%\ 100)\ /\ 100; \\
95\mbox{}\texttt{093:} \\
96\mbox{}\texttt{094:} \ \ \ \ \textbf{stdInit}(); \\
97\mbox{}\texttt{095:} \} \\
98\mbox{}\texttt{096:} \\
99\mbox{}\texttt{097:} \textit{/*\ Fixed\ init\ of\ weights\ */} \\
100\mbox{}\texttt{098:} void\ \textbf{fixedInit}()\ \{ \\
101\mbox{}\texttt{099:} \ \ \ \ int\ i,j; \\
102\mbox{}\texttt{100:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
103\mbox{}\texttt{101:} \ \ \ \ \ \ \ \ \textbf{for}\ (\ j\ =\ 1;\ j\ $<$\ HIDDEN$\_$SIZE;\ j++)\ \{ \\
104\mbox{}\texttt{102:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$ItoH[i][j]\ =\ 0.5; \\
105\mbox{}\texttt{103:} \ \ \ \ \ \ \ \ \} \\
106\mbox{}\texttt{104:} \\
107\mbox{}\texttt{105:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
108\mbox{}\texttt{106:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 1;\ j\ $<$\ OUTPUT$\_$SIZE;\ j++) \\
109\mbox{}\texttt{107:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$HtoO[i][j]\ =\ 0.5; \\
110\mbox{}\texttt{108:} \\
111\mbox{}\texttt{109:} \ \ \ \ \textbf{stdInit}(); \\
112\mbox{}\texttt{110:} \} \\
113\mbox{}\texttt{111:} \\
114\mbox{}\texttt{112:} \textit{/*\ Define\ exact\ wights,\ used\ for\ debugging\ calculations} \\
115\mbox{}\texttt{113:} \textit{\ *\ other\ flags\ INPUT\ =\ 2,\ HIDDEN\ =\ 2,\ OUTPUT\ =\ 1} \\
116\mbox{}\texttt{114:} \textit{\ */} \\
117\mbox{}\texttt{115:} void\ \textbf{debugInit}()\ \{ \\
118\mbox{}\texttt{116:} \ \ \ \ \textbf{stdInit}(); \\
119\mbox{}\texttt{117:} \ \ \ \ weight$\_$ItoH[0][1]\ =\ 1; \\
120\mbox{}\texttt{118:} \ \ \ \ weight$\_$ItoH[0][2]\ =\ 1; \\
121\mbox{}\texttt{119:} \ \ \ \ weight$\_$ItoH[1][1]\ =\ 0.62; \\
122\mbox{}\texttt{120:} \ \ \ \ weight$\_$ItoH[1][2]\ =\ 0.42; \\
123\mbox{}\texttt{121:} \ \ \ \ weight$\_$ItoH[2][1]\ =\ 0.55; \\
124\mbox{}\texttt{122:} \ \ \ \ weight$\_$ItoH[2][2]\ =\ -0.17; \\
125\mbox{}\texttt{123:} \\
126\mbox{}\texttt{124:} \ \ \ \ weight$\_$HtoO[0][1]\ =\ 1; \\
127\mbox{}\texttt{125:} \ \ \ \ weight$\_$HtoO[1][1]\ =\ 0.35; \\
128\mbox{}\texttt{126:} \ \ \ \ weight$\_$HtoO[2][1]\ =\ 0.81; \\
129\mbox{}\texttt{127:} \} \\
130\mbox{}\texttt{128:} \\
131\mbox{}\texttt{129:} \textit{/*\ \ calculate\ Aj's\ and\ Ai's\ (outputs)\ */} \\
132\mbox{}\texttt{130:} void\ \textbf{nnCalc}()\ \{ \\
133\mbox{}\texttt{131:} \ \ \ \ int\ i,j; \\
134\mbox{}\texttt{132:} \ \ \ \ double\ total; \\
135\mbox{}\texttt{133:} \ \ \ \ \textbf{for}\ (i\ =\ 1;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++)\ \{ \\
136\mbox{}\texttt{134:} \ \ \ \ \ \ \ \ total\ =\ 0; \\
137\mbox{}\texttt{135:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 0;\ j\ $<$\ INPUT$\_$SIZE;\ j++) \\
138\mbox{}\texttt{136:} \ \ \ \ \ \ \ \ \ \ \ \ total\ +=\ weight$\_$ItoH[j][i]\ *\ input[j]; \\
139\mbox{}\texttt{137:} \ \ \ \ \ \ \ \ hidden[i]\ =\ 1\ /\ (\ 1\ +\ \textbf{exp}(total\ *\ (-1))); \\
140\mbox{}\texttt{138:} \ \ \ \ \} \\
141\mbox{}\texttt{139:} \\
142\mbox{}\texttt{140:} \ \ \ \ \textbf{for}\ (i\ =\ 1;\ i\ $<$\ OUTPUT$\_$SIZE;\ i++)\ \{ \\
143\mbox{}\texttt{141:} \ \ \ \ \ \ \ \ total\ =\ 0; \\
144\mbox{}\texttt{142:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 0;\ j\ $<$\ HIDDEN$\_$SIZE;\ j++) \\
145\mbox{}\texttt{143:} \ \ \ \ \ \ \ \ \ \ \ \ total\ +=\ weight$\_$HtoO[j][i]\ *\ hidden[j]; \\
146\mbox{}\texttt{144:} \ \ \ \ \ \ \ \ output[i]\ =\ 1\ /\ (\ 1\ +\ \textbf{exp}(total\ *\ (-1))); \\
147\mbox{}\texttt{145:} \ \ \ \ \} \\
148\mbox{}\texttt{146:} \\
149\mbox{}\texttt{147:} \} \\
150\mbox{}\texttt{148:} \\
151\mbox{}\texttt{149:} \textit{/*\ train\ network,\ NOTE:\ nnCalc\ needs\ to\ be\ called\ first\ */} \\
152\mbox{}\texttt{150:} void\ \textbf{nnTrain}()\ \{ \\
153\mbox{}\texttt{151:} \ \ \ \ int\ i,j; \\
154\mbox{}\texttt{152:} \ \ \ \ double\ hidden$\_$delta[HIDDEN$\_$SIZE]; \\
155\mbox{}\texttt{153:} \ \ \ \ double\ output$\_$delta[OUTPUT$\_$SIZE]; \\
156\mbox{}\texttt{154:} \ \ \ \ double\ output$\_$error[OUTPUT$\_$SIZE]; \\
157\mbox{}\texttt{155:} \ \ \ \ double\ hidden$\_$sum$\_$delta[HIDDEN$\_$SIZE]; \\
158\mbox{}\texttt{156:} \\
159\mbox{}\texttt{157:} \ \ \ \ \textbf{for}\ (i\ =\ 1;\ i\ $<$\ OUTPUT$\_$SIZE;\ i++)\ \{ \\
160\mbox{}\texttt{158:} \ \ \ \ \ \ \ \ output$\_$error[i]\ =\ target[i]\ -\ output[i]; \\
161\mbox{}\texttt{159:} \ \ \ \ \ \ \ \ output$\_$delta[i]\ =\ output$\_$error[i]\ *\ output[i]\ *\ (1\ -\ output[i]); \\
162\mbox{}\texttt{160:} \ \ \ \ \} \\
163\mbox{}\texttt{161:} \\
164\mbox{}\texttt{162:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++)\ \{ \\
165\mbox{}\texttt{163:} \ \ \ \ \ \ \ \ hidden$\_$sum$\_$delta[i]\ =\ 0; \\
166\mbox{}\texttt{164:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 1;\ j\ $<$\ OUTPUT$\_$SIZE;\ j++) \\
167\mbox{}\texttt{165:} \ \ \ \ \ \ \ \ \ \ \ \ hidden$\_$sum$\_$delta[i]\ +=\ weight$\_$HtoO[i][j]\ *\ output$\_$delta[j]; \\
168\mbox{}\texttt{166:} \ \ \ \ \ \ \ \ hidden$\_$delta[i]\ =\ hidden[i]\ *\ (1\ -\ hidden[i])\ * \\
169\mbox{}\texttt{167:} \ \ \ \ \ \ \ \ hidden$\_$sum$\_$delta[i]; \\
170\mbox{}\texttt{168:} \ \ \ \ \ \} \\
171\mbox{}\texttt{169:} \\
172\mbox{}\texttt{170:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
173\mbox{}\texttt{171:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 1;\ j\ $<$\ OUTPUT$\_$SIZE;\ j++)\ \{ \\
174\mbox{}\texttt{172:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$HtoO[i][j]\ =\ weight$\_$HtoO[i][j]\ +\ LEARN$\_$SPEED\ * \\
175\mbox{}\texttt{173:} \ \ \ \ \ \ \ \ \ \ \ \ hidden[i]\ *\ output$\_$delta[j]; \\
176\mbox{}\texttt{174:} \ \ \ \ \ \ \ \ \} \\
177\mbox{}\texttt{175:} \\
178\mbox{}\texttt{176:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
179\mbox{}\texttt{177:} \ \ \ \ \ \ \ \ \textbf{for}\ (j\ =\ 1;\ j\ $<$\ HIDDEN$\_$SIZE;\ j++)\ \{ \\
180\mbox{}\texttt{178:} \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$ItoH[i][j]\ =\ weight$\_$ItoH[i][j]\ +\ LEARN$\_$SPEED\ * \\
181\mbox{}\texttt{179:} \ \ \ \ \ \ \ \ \ \ \ \ input[i]\ *\ hidden$\_$delta[j]; \\
182\mbox{}\texttt{180:} \ \ \ \ \ \ \ \ \} \\
183\mbox{}\texttt{181:} \} \\
184\mbox{}\texttt{182:} \\
185\mbox{}\texttt{183:} \textit{/*\ Verify\ wether\ target,\ matches\ output\ */} \\
186\mbox{}\texttt{184:} int\ \textbf{nnValidate}()\ \{ \\
187\mbox{}\texttt{185:} \ \ \ \ int\ i; \\
188\mbox{}\texttt{186:} \ \ \ \ \textit{//printf\ ("{}Rounding:\ \%lf\ -\ \%lf\textbackslash{}n"{},output[1],\ target[1]);} \\
189\mbox{}\texttt{187:} \ \ \ \ \textbf{for}\ (i\ =\ 1;\ i\ $<$\ OUTPUT$\_$SIZE;\ i++) \\
190\mbox{}\texttt{188:} \ \ \ \ \ \ \ \ \textbf{if}\ (\textbf{round}(output[i])\ !=\ \textbf{round}(target[i])) \\
191\mbox{}\texttt{189:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{return}\ FALSE; \\
192\mbox{}\texttt{190:} \ \ \ \ \textbf{return}\ TRUE; \\
193\mbox{}\texttt{191:} \} \\
194\mbox{}\texttt{192:} \\
195\mbox{}\texttt{193:} \textit{/*\ Pretty\ print\ of\ output\ */} \\
196\mbox{}\texttt{194:} void\ \textbf{nnOutput}()\ \{ \\
197\mbox{}\texttt{195:} \ \ \ \ int\ i; \\
198\mbox{}\texttt{196:} \ \ \ \ \textbf{for}(i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
199\mbox{}\texttt{197:} \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}\%lf,\ "{}},\ input[i]); \\
200\mbox{}\texttt{198:} \ \ \ \ \textbf{printf}(\texttt{"{}=\ \%lf\ -\ \%lf\ -\ "{}},\ output[1],\ target[1]); \\
201\mbox{}\texttt{199:} \ \ \ \ \textbf{if}\ (\textbf{nnValidate}()\ ==\ TRUE) \\
202\mbox{}\texttt{200:} \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}OK"{}}); \\
203\mbox{}\texttt{201:} \ \ \ \ \textbf{else} \\
204\mbox{}\texttt{202:} \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}ERROR"{}}); \\
205\mbox{}\texttt{203:} \ \ \ \ \textbf{printf}(\texttt{"{}}\texttt{\textbackslash{}n}\texttt{"{}}); \\
206\mbox{}\texttt{204:} \} \\
207\mbox{}\texttt{205:} \\
208\mbox{}\texttt{206:} \textit{/*\ Pretty\ print\ of\ hidden\ knobs\ */} \\
209\mbox{}\texttt{207:} void\ \textbf{nnHiddenOutput}()\ \{ \\
210\mbox{}\texttt{208:} \ \ \ \ int\ i; \\
211\mbox{}\texttt{209:} \ \ \ \ \textbf{for}(i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
212\mbox{}\texttt{210:} \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}\%lf,\ "{}},\ hidden[i]); \\
213\mbox{}\texttt{211:} \ \ \ \ \textbf{printf}(\texttt{"{}\ -\ HIDDEN}\texttt{\textbackslash{}n}\texttt{"{}}); \\
214\mbox{}\texttt{212:} \} \\
215\mbox{}\texttt{213:} \\
216\mbox{}\texttt{214:} \\
217\mbox{}\texttt{215:} \textit{/*\ Pretty\ print\ of\ all\ weights\ */} \\
218\mbox{}\texttt{216:} void\ \textbf{nnNeuronOutput}()\ \{ \\
219\mbox{}\texttt{217:} \ \ \ \ int\ i,j; \\
220\mbox{}\texttt{218:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ INPUT$\_$SIZE;\ i++) \\
221\mbox{}\texttt{219:} \ \ \ \ \ \ \ \ \textbf{for}(j\ =\ 0;\ j\ $<$\ HIDDEN$\_$SIZE;\ j++) \\
222\mbox{}\texttt{220:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{if}\ (weight$\_$ItoH[i][j]\ !=\ WEIGHT$\_$NOT$\_$USED) \\
223\mbox{}\texttt{221:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}weight$\_$ItoH[\%i][\%i]\ =\ \%lf}\texttt{\textbackslash{}n}\texttt{"{}},\ i,\ j, \\
224\mbox{}\texttt{222:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$ItoH[i][j]); \\
225\mbox{}\texttt{223:} \ \ \ \ \textbf{printf}(\texttt{"{}-\/-\/-}\texttt{\textbackslash{}n}\texttt{"{}}); \\
226\mbox{}\texttt{224:} \ \ \ \ \textbf{for}\ (i\ =\ 0;\ i\ $<$\ HIDDEN$\_$SIZE;\ i++) \\
227\mbox{}\texttt{225:} \ \ \ \ \ \ \ \ \textbf{for}(j\ =\ 0;\ j\ $<$\ OUTPUT$\_$SIZE;\ j++) \\
228\mbox{}\texttt{226:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{if}\ (weight$\_$ItoH[i][j]\ !=\ WEIGHT$\_$NOT$\_$USED) \\
229\mbox{}\texttt{227:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}weight$\_$HtoO[\%i][\%i]\ =\ \%lf}\texttt{\textbackslash{}n}\texttt{"{}},\ i,\ j, \\
230\mbox{}\texttt{228:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ weight$\_$HtoO[i][j]); \\
231\mbox{}\texttt{229:} \} \\
232\mbox{}\texttt{230:} \\
233\mbox{}\texttt{231:} int\ \textbf{nnReadInput}(FILE\ *\ handle)\ \{ \\
234\mbox{}\texttt{232:} \ \ \ \ int\ i\ =\ 1; \\
235\mbox{}\texttt{233:} \ \ \ \ double\ finput; \\
236\mbox{}\texttt{234:} \ \ \ \ \textbf{while}\ (\textbf{fscanf}(handle,\ \texttt{"{}\%lf"{}},\ \&finput)\ !=\ EOF)\ \{ \\
237\mbox{}\texttt{235:} \ \ \ \ \ \ \ \ \textbf{if}\ (i\ $<$\ INPUT$\_$SIZE) \\
238\mbox{}\texttt{236:} \ \ \ \ \ \ \ \ \ \ \ \ input[i]\ =\ finput; \\
239\mbox{}\texttt{237:} \ \ \ \ \ \ \ \ \textbf{else}\ \textbf{if}\ (i\ $<$\ (INPUT$\_$SIZE\ +\ OUTPUT$\_$SIZE)) \\
240\mbox{}\texttt{238:} \ \ \ \ \ \ \ \ \ \ \ \ target[i\ -\ INPUT$\_$SIZE]\ =\ finput; \\
241\mbox{}\texttt{239:} \\
242\mbox{}\texttt{240:} \ \ \ \ \ \ \ \ \textit{/*\ Calc\ next\ input\ */} \\
243\mbox{}\texttt{241:} \ \ \ \ \ \ \ \ i++; \\
244\mbox{}\texttt{242:} \ \ \ \ \ \ \ \ \textit{/*\ Skip\ hidden\ output\ knob\ */} \\
245\mbox{}\texttt{243:} \ \ \ \ \ \ \ \ \textbf{if}\ (i\ ==\ INPUT$\_$SIZE) \\
246\mbox{}\texttt{244:} \ \ \ \ \ \ \ \ \ \ \ \ i++; \\
247\mbox{}\texttt{245:} \ \ \ \ \ \ \ \ \textbf{if}\ (i\ ==\ (INPUT$\_$SIZE\ +\ OUTPUT$\_$SIZE)) \\
248\mbox{}\texttt{246:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{return}\ TRUE; \\
249\mbox{}\texttt{247:} \ \ \ \ \} \\
250\mbox{}\texttt{248:} \\
251\mbox{}\texttt{249:} \ \ \ \ \textit{/*\ Input\ not\ complete\ */} \\
252\mbox{}\texttt{250:} \ \ \ \ \textbf{return}\ FALSE; \\
253\mbox{}\texttt{251:} \} \\
254\mbox{}\texttt{252:} \\
255\mbox{}\texttt{253:} \textit{/*\ Verify\ quality\ of\ current\ network\ */} \\
256\mbox{}\texttt{254:} double\ \textbf{nnQualityCheck}(char\ *\ file)\ \{ \\
257\mbox{}\texttt{255:} \ \ \ \ double\ validate$\_$total\ =\ 0; \\
258\mbox{}\texttt{256:} \ \ \ \ double\ validate$\_$ok\ =\ 0; \\
259\mbox{}\texttt{257:} \ \ \ \ double\ validate$\_$percent\ =\ 0; \\
260\mbox{}\texttt{258:} \ \ \ \ FILE\ *\ handle; \\
261\mbox{}\texttt{259:} \\
262\mbox{}\texttt{260:} \ \ \ \ handle\ =\ \textbf{fopen}(file,\texttt{"{}r"{}}); \\
263\mbox{}\texttt{261:} \ \ \ \ \textbf{while}\ (\textbf{nnReadInput}(handle)\ ==\ TRUE)\ \{ \\
264\mbox{}\texttt{262:} \ \ \ \ \ \ \ \ \ \ \ \ validate$\_$total++; \\
265\mbox{}\texttt{263:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{nnCalc}(); \\
266\mbox{}\texttt{264:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{if}\ (\textbf{nnValidate}()\ ==\ TRUE) \\
267\mbox{}\texttt{265:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ validate$\_$ok++; \\
268\mbox{}\texttt{266:} \ \ \ \ \ \ \ \ \ \ \ \ \textit{//else} \\
269\mbox{}\texttt{267:} \ \ \ \ \ \ \ \ \ \ \ \ \textit{//\ \ nnOutput();} \\
270\mbox{}\texttt{268:} \ \ \ \ \} \\
271\mbox{}\texttt{269:} \ \ \ \ \textbf{fclose}(handle); \\
272\mbox{}\texttt{270:} \ \ \ \ validate$\_$percent\ =\ (validate$\_$ok\ /\ validate$\_$total)\ *\ 100; \\
273\mbox{}\texttt{271:} \ \ \ \ \textbf{printf}(\texttt{"{}Validating:\ \%.0lf/\%.0lf\ -\ \%.2lf\ \%\%}\texttt{\textbackslash{}n}\texttt{"{}}, \\
274\mbox{}\texttt{272:} \ \ \ \ validate$\_$ok,validate$\_$total,validate$\_$percent); \\
275\mbox{}\texttt{273:} \\
276\mbox{}\texttt{274:} \ \ \ \ \textbf{return}(validate$\_$percent); \\
277\mbox{}\texttt{275:} \} \\
278\mbox{}\texttt{276:} \\
279\mbox{}\texttt{277:} \textit{/*\ Main\ program\ */} \\
280\mbox{}\texttt{278:} int\ \textbf{main\ }(int\ argc,\ char\ *\ argv[])\ \{ \\
281\mbox{}\texttt{279:} \ \ \ \ int\ i,training$\_$total,\ training$\_$best; \\
282\mbox{}\texttt{280:} \ \ \ \ double\ quality$\_$max,\ quality; \\
283\mbox{}\texttt{281:} \ \ \ \ FILE\ *\ handle; \\
284\mbox{}\texttt{282:} \\
285\mbox{}\texttt{283:} \ \ \ \ \textit{/*\ Set\ the\ bias\ knob\ */} \\
286\mbox{}\texttt{284:} \ \ \ \ input[0]\ =\ -1; \\
287\mbox{}\texttt{285:} \ \ \ \ hidden[0]\ =\ -1; \\
288\mbox{}\texttt{286:} \ \\
289\mbox{}\texttt{287:} \ \ \ \ \textit{/*\ Init\ set\ of\ all\ wights\ */} \\
290\mbox{}\texttt{288:} \ \ \ \ \textit{//debugInit();} \\
291\mbox{}\texttt{289:} \ \ \ \ \textbf{fixedInit}(); \\
292\mbox{}\texttt{290:} \ \ \ \ \textit{//randInit();} \\
293\mbox{}\texttt{291:} \\
294\mbox{}\texttt{292:} \ \ \ \ \textit{/*\ Set\ initial\ quality\ */} \\
295\mbox{}\texttt{293:} \ \ \ \ quality$\_$max\ =\ \textbf{nnQualityCheck}(file$\_$quality); \\
296\mbox{}\texttt{294:} \ \ \ \ training$\_$best\ =\ 0; \\
297\mbox{}\texttt{295:} \ \ \ \ training$\_$total\ =\ 0; \\
298\mbox{}\texttt{296:} \\
299\mbox{}\texttt{297:} \ \ \ \ \textbf{printf}(\texttt{"{}Running\ neural\ network\ with\ following\ parameters}\texttt{\textbackslash{}n}\texttt{"{}}); \\
300\mbox{}\texttt{298:} \ \ \ \ \textbf{printf}(\texttt{"{}Input\ \ nodes\ \ \ \ :\ \%i}\texttt{\textbackslash{}n}\texttt{"{}},\ INPUT$\_$SIZE); \\
301\mbox{}\texttt{299:} \ \ \ \ \textbf{printf}(\texttt{"{}Hidden\ nodes\ \ \ \ :\ \%i}\texttt{\textbackslash{}n}\texttt{"{}},\ HIDDEN$\_$SIZE); \\
302\mbox{}\texttt{300:} \ \ \ \ \textbf{printf}(\texttt{"{}Output\ nodes\ \ \ \ :\ \%i}\texttt{\textbackslash{}n}\texttt{"{}},\ OUTPUT$\_$SIZE); \\
303\mbox{}\texttt{301:} \ \ \ \ \textbf{printf}(\texttt{"{}Learning\ rate\ \ \ :\ \%lf}\texttt{\textbackslash{}n}\texttt{"{}},\ LEARN$\_$SPEED); \\
304\mbox{}\texttt{302:} \ \ \ \ \textbf{printf}(\texttt{"{}Quality\ check\ \ \ :\ \%i}\texttt{\textbackslash{}n}\texttt{"{}},\ QUALITY$\_$ROUND); \\
305\mbox{}\texttt{303:} \ \ \ \ \textbf{printf}(\texttt{"{}Initial\ quality\ :\ \%lf\ \%\%}\texttt{\textbackslash{}n}\texttt{"{}},\ quality$\_$max); \\
306\mbox{}\texttt{304:} \ \ \ \ \textit{/*\ Start\ training\ */} \\
307\mbox{}\texttt{305:} \ \ \ \ \textit{//nnNeuronOutput();} \\
308\mbox{}\texttt{306:} \ \ \ \ i\ =\ 1; \\
309\mbox{}\texttt{307:} \ \ \ \ handle\ =\ \textbf{fopen}(file$\_$training,\texttt{"{}r"{}}); \\
310\mbox{}\texttt{308:} \ \ \ \ \textbf{while}\ (\ \textbf{nnReadInput}(handle)\ ==\ TRUE)\ \{ \\
311\mbox{}\texttt{309:} \ \ \ \ \ \ \ \ training$\_$total++; \\
312\mbox{}\texttt{310:} \ \ \ \ \ \ \ \ \textbf{nnCalc}(); \\
313\mbox{}\texttt{311:} \ \ \ \ \ \ \ \ \textit{//nnOutput();} \\
314\mbox{}\texttt{312:} \ \ \ \ \ \ \ \ \textit{//nnHiddenOutput();} \\
315\mbox{}\texttt{313:} \\
316\mbox{}\texttt{314:} \ \ \ \ \ \ \ \ \textbf{if}\ (\textbf{nnValidate}()\ ==\ FALSE)\ \{ \\
317\mbox{}\texttt{315:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{nnTrain}(); \\
318\mbox{}\texttt{316:} \ \ \ \ \ \ \ \ \ \ \ \ \textit{//nnNeuronOutput();} \\
319\mbox{}\texttt{317:} \ \ \ \ \ \ \ \ \} \\
320\mbox{}\texttt{318:} \\
321\mbox{}\texttt{319:} \ \ \ \ \ \ \ \ \textit{/*\ Verifiy\ quality,\ stop\ training\ when\ quality\ is\ going\ down\ */} \\
322\mbox{}\texttt{320:} \ \ \ \ \ \ \ \ \textbf{if}\ ((training$\_$total\ \%\ QUALITY$\_$ROUND)\ ==\ 0)\ \{ \\
323\mbox{}\texttt{321:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{printf}(\texttt{"{}Learned:\ \%i\ -\ "{}},\ training$\_$total); \\
324\mbox{}\texttt{322:} \ \ \ \ \ \ \ \ \ \ \ \ quality\ =\ \textbf{nnQualityCheck}(file$\_$quality); \\
325\mbox{}\texttt{323:} \ \ \ \ \ \ \ \ \ \ \ \ \textbf{if}\ (quality\ $>$\ quality$\_$max)\ \{ \\
326\mbox{}\texttt{324:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ quality$\_$max\ =\ quality; \\
327\mbox{}\texttt{325:} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ training$\_$best\ =\ training$\_$total; \\
328\mbox{}\texttt{326:} \ \ \ \ \ \ \ \ \ \ \ \ \} \\
329\mbox{}\texttt{327:} \ \ \ \ \ \ \ \ \} \\
330\mbox{}\texttt{328:} \ \ \ \ \} \\
331\mbox{}\texttt{329:} \ \ \ \ \textbf{fclose}(handle); \\
332\mbox{}\texttt{330:} \ \ \ \ \textbf{printf}(\texttt{"{}Max\ quality:\ \%.2lf\%\%\ at\ training\ round:\ \%i}\texttt{\textbackslash{}n}\texttt{"{}},\ quality$\_$max, \\
333\mbox{}\texttt{331:} \ \ \ \ \ \ \ \ training$\_$best); \\
334\mbox{}\texttt{332:} \ \ \ \ quality\ =\ \textbf{nnQualityCheck}(file$\_$validate); \\
335\mbox{}\texttt{333:} \ \ \ \ \textbf{return}(EX$\_$OK); \\
336\mbox{}\texttt{334:} \} \\
337\mbox{}\texttt{335:} \\
338
Note: See TracBrowser for help on using the repository browser.