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

