//+------------------------------------------------------------------+ //| NeuroTrend_Include.mqh | //| Copyright © 2008, Arun Kumar Raj | //| avoruganti@gmail.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Arun Kumar Raj" #property link "avoruganti@gmail.com" /**************************** Usage ******************************* SetInput(Input); PropagateNet(); GetOutput(Output); **********************************************************************/ #define LOGISTIC 0 #define TANH 1 int gmLayers = 0; /* number of layers in the net */ int gmUnitsTotal = 0; /* total number of units in the net */ int gmConnections = 0; /* total number of connections int the net */ int gmGain = 1; /* Gain of the net*/ int gmUnits[]; /* number of units in each layer */ double gmOutput[]; /* output of ith unit of nth layer*/ double gmBias[]; /* bias of ith unit of nth layer*/ double gmWeight[]; /* weight of jth connection of ith unit & nth layer */ /*------------------------------------------------------------------------*/ /* Allocate memory for Neural Network of geometry described by layers, units[]. */ void GenerateNetwork(int layers_no, int units[]) { int l; gmLayers = layers_no; Print(ArrayResize(gmUnits, gmLayers)+" network layers:"); for(l=0; l0; j--) { sum += gmOutput[lub+j-1] * gmWeight[wts]; //if(l==2)Print("output: "+(lub+j-1)+" weight: "+wts); //if(l==2)Print((lub+j-1)+"X"+gmWeight[wts]); wts++; }*/ sum += gmBias[uub+i]; gmOutput[uub+i] = Activation(sum, TANH); }//for all units of a layer }// for all layers } /*-----------------------------------------------------------------------*/ double Activation(double sum, int type) { double ret; switch(type) { case 0: ret = 1.0 / (1.0 + MathExp(-gmGain * sum)); break; case 1: ret = (2.0 / (1.0 + MathExp(-2.0 * sum))) - 1.0; break; default: Print("Undefined activation type\n"); break; } return(ret); } /*------------------------------------------------------------------------*/ /* sets the network weights from the input array */ bool SetWeights(double weights[]) { if(ArraySize(weights) != gmConnections) { Print("Input weights not equal to connections"); return (0); } Print(ArrayCopy(gmWeight, weights,0,0,WHOLE_ARRAY)+" weights initialized"); return(true); } /*------------------------------------------------------------------------*/ /* Copies Input to the input layer of the net */ bool SetBias(double bias[]) { if(ArraySize(bias) != gmUnitsTotal) { Print("Input bias not equal to total units"); return (0); } Print(ArrayCopy(gmBias, bias,0,0,WHOLE_ARRAY)+" bias initialized"); return(true); } /*------------------------------------------------------------------------*/ int ReadNetwork(string nfile, string ofile, bool out = false) { int handle = -1, ohandle = -1; handle = FileOpen(nfile, FILE_CSV | FILE_READ); if(handle < 0) { Print("Error reading network file, code:"+ GetLastError()); return (0); } if(out) { ohandle = FileOpen(ofile, FILE_CSV|FILE_WRITE); if(ohandle < 0)Print("Error opening file to write, code:"+ GetLastError()); } string str, substr; int pos = -1, units = 0, connections = 0, index = 0; bool bias = false, weights = false; bool loop = true; //read no of units while(loop) { str = FileReadString(handle); if(GetLastError() == 4051) break; if(units <= 0) { pos = StringFind(str,"no. of units :",0); if(pos != -1) { units = StrToInteger(StringSubstr(str, 14,StringLen(str))); str = FileReadString(handle);//read next line connections = StrToInteger(StringSubstr(str, 20,StringLen(str))); if((units != gmUnitsTotal) || (connections != gmConnections)) { Print("read units/connections not equal to network units/connections"); return (0); } } }else { if(bias == false) { //find bias and write to file pos = StringFind(str,"| sites",0); if(pos != -1) { if(out && (ohandle > 0)) FileWrite(ohandle, "**********\n BIAS\n**********"); str = FileReadString(handle); index = 0; while(index < units) { str = FileReadString(handle); for(int i = 1; i < 5; i++) { pos = StringFind(str,"|",0); if(pos != -1)str = StringSubstr( str, pos+1); } pos = StringFind(str,"|",0); if(pos != -1) substr = StringSubstr( str, 0, pos); StringTrimLeft(substr); StringTrimRight(substr); gmBias[index] = StrToDouble(substr); index++; if(out && (ohandle > 0)) FileWrite(ohandle, substr); }//end while bias = true; }//if pos }else if(weights == false) { //find weights and write to file pos = StringFind(str,"source:weight",0); if(pos != -1) { if(out && (ohandle > 0)) FileWrite(ohandle, "\n\n**********\n WEIGHTS\n**********"); str = FileReadString(handle); index = 0; while(index < connections) { str = FileReadString(handle); while(true) { pos = StringFind(str,":",0); if(pos == -1) break; substr = StringSubstr(str, pos+1, 8); StringTrimLeft(substr); StringTrimRight(substr); gmWeight[index] = StrToDouble(substr); index++; if(out && (ohandle > 0)) FileWrite(ohandle, substr); str = StringSubstr(str, pos+8,0); } //end while true }//end while index < connections weights = true; loop = false; }//end if pos }else //if not bias or weights { loop = false; } }// end else if units } // end while loop if(ohandle > 0) FileClose(ohandle); if(handle > 0) FileClose(handle); return (1); } /*------------------------------------------------------------------------*/