Unit BaseFunc; (*base function is a boolean function {0,1}^n -> {0,1} where n is an input width, n<=15 this is the function which we'll want to approximate by neural network *) Interface uses FileInp, Basic, Daxel; type tInput = object {input of a base function or a neural network} {input is a boolean vector. for the purpose of neural networks, boolean values need to be converted to real numbers and vice versa, for example true=0.9, false=0.1; real<0.5=false, real>0.5=true for the purpose of storing of base function, boolean vector needs to be compressed into a 16-bit value} {maximal input width is 15} {} {base function parameters} cInputWidth: Integer; {cardinality of a base function} cZeroValue: Real; {Boolean->Real conversion} cOneValue: Real; {Boolean->Real conversion} cThreshold: Real; {Real->Boolean conversion} {} cData: array [1..15] of Real; {} function Compress: Word; procedure Decompress( pw: Word ); function BoolToReal( pb: Boolean ): Real; function RealToBool( pr: Real ): Boolean; function ReadParams( psFileName: String ): Boolean; end; t_BaseFunction = ^tBaseFunction; tBaseFunction = object( tInput ) {base function contains a tInput object} cResult: array [0..32767] of Boolean; procedure GenerateRandom; function Evaluate: Boolean; procedure Save( psFileName: String ); procedure Load( psFileName: String ); end; Implementation function tInput.Compress: Word; var nRet: Word; i: Integer; begin nRet := 0; for i := 1 to cInputWidth do begin nRet := nRet shl 1; if cData[i]>cThreshold then nRet := nRet + 1; end; Compress := nRet; end; procedure tInput.Decompress( pw: Word ); var i: Integer; begin for i := cInputWidth downto 1 do begin if pw mod 2 = 1 then cData[i] := cOneValue else cData[i] := cZeroValue; pw := pw shr 1; end; end; function tInput.BoolToReal( pb: Boolean ): Real; begin if pb then BoolToReal := cOneValue else BoolToReal := cZeroValue; end; function tInput.RealToBool( pr: Real ): Boolean; begin if pr>cThreshold then RealToBool := True else RealToBool := False; end; function tInput.ReadParams( psFileName: String ): Boolean; var f: tFile; begin if f.Open( psFileName ) and f.SeekSection( '[BASEFUNCTION]' ) then begin cInputWidth := StrToInt( f.GetValue ); cZeroValue := StrToReal( f.GetValue ); cOneValue := StrToReal( f.GetValue ); cThreshold := StrToReal( f.GetValue ); end else ReadParams := False; f.close; end; procedure tBaseFunction.GenerateRandom; var i: Integer; n: Word; begin n := 1; for i := 1 to cInputWidth do n := n shl 1; for i := 0 to n-1 do begin if Random < 0.5 then cResult[i] := True else cResult[i] := False; end; end; function tBaseFunction.Evaluate: Boolean; begin Evaluate := cResult[ Compress ]; end; procedure tBaseFunction.Save( psFileName: String ); var a: tDaxel; begin a.AssignVar( Self, SizeOf( Self ) ); a.Save( psFileName ); end; procedure tBaseFunction.Load( psFileName: String ); var a: tDaxel; begin a.AssignVar( Self, SizeOf( Self ) ); a.ReLoad( psFileName ); end; begin end.