global maxin , hoog maxin = 1 hoog = 2 ^ 24 dim cel( 200 , maxin ) , w( 200 , maxin + 1 ) dim fout( 200 ) , ry( 200 ) ''set weights at random for i = 0 to 200 ry( i ) = i for j = 0 to maxin + 1 w( i , j ) = int( rnd(0) * hoog ) next j next i for gen = 0 to 100 ''let all cels live for i = 0 to 200 fout( i ) = 0 for x = -10 to 10 for y = -10 to 10 cel( i , 0 ) = x cel( i , 1 ) = y fout( i ) = fout( i ) _ + abs( sign( cel.uit( i ) ) - f( x , y ) ) next y next x next i ''sort cels on fitnes for h = 1 to 200 for l = 0 to h - 1 if fout(ry(l)) > fout(ry(h)) then i = ry(l) ry(l)=ry(h) ry(h)= i end if next l next h print gen , fout( ry( 0 ) ) ''let best cels make next generation for i = 20 to 200 a = int( rnd(0) * 20 ) b = int( rnd(0) * 20 ) call cel.mix ry( i ) , ry( a ) , ry( b ) if rnd(0) < 0.5 then call cel.mutate ry( i ) end if next i next gen q = 0 ''test the best cel for i = 0 to 99 x = int( rnd(0) * 20 ) - 10 y = int( rnd(0) * 20 ) - 10 cel( ry( 0 ) ) = x cel( ry( 0 ) ) = y if cel.uit( ry( 0 ) ) <> f( x , y ) then q = q + 1 end if print cel.uit( ry( 0 ) ) , f( x , y ) _ , "error = " ; q ; " %" next i end function sign( x ) uit = 0 if x < 0 then uit = -1 if x > 0 then uit = 1 sign = uit end function function f( x , y ) uit = -1 if x < y then uit = 1 f = uit end function function cel.uit( no ) sum = w( no , maxin + 1 ) / hoog - .5 for i = 0 to maxin sum = sum + cel(no,i)*w(no,i)/hoog - .5 next i cel.uit = sum end function sub cel.mutate no dw = int( rnd(0) * ( maxin + 1 ) ) m = int( rnd(0) * 24 ) w( no , dw ) = w( no , dw ) xor 2 ^ m end sub sub cel.mix uit , a , b for i = 0 to maxin w( uit , i ) = 0 for m = 0 to 24 if rnd(0) < .5 then w(uit,i)=w(uit,i)+w(a,i)and 2^m else w(uit,i)=w(uit,i)+w(b,i)and 2^m end if next m next i end sub
global maxin , hoog maxin = 1 hoog = 2 ^ 24 dim cel( 200 , maxin ) , w( 200 , maxin + 1 ) dim fout( 200 ) , ry( 200 ) ''set weights at random for i = 0 to 200 ry( i ) = i for j = 0 to maxin + 1 w( i , j ) = int( rnd(0) * hoog ) next j next i for gen = 0 to 10 scan ''let all cels live for i = 0 to 200 fout( i ) = 0 for x = -10 to 10 for y = -10 to 10 cel( i , 0 ) = x cel( i , 1 ) = y fout( i ) = fout( i ) _ + abs( sign( cel.uit( i ) ) - f( x , y ) ) next y next x next i ''sort cels on fitnes for h = 1 to 200 for l = 0 to h - 1 if fout(ry(l)) > fout(ry(h)) then i = ry(l) ry(l)=ry(h) ry(h)= i end if next l next h print gen , fout( ry( 0 ) ) ''let best cels make next generation for i = 20 to 200 a = int( rnd(0) * 20 ) b = int( rnd(0) * 20 ) call cel.mix ry( i ) , ry( a ) , ry( b ) if rnd(0) < 0.5 then call cel.mutate ry( i ) end if next i next gen q = 0 ''test the best cel for i = 0 to 999 x = int( rnd(0) * 20 ) - 10 y = int( rnd(0) * 20 ) - 10 cel( ry( 0 ) , 0 ) = x cel( ry( 0 ) , 1 ) = y if sign( cel.uit( ry( 0 ) ) ) <> f( x , y ) then q = q + 1 end if print sign( cel.uit( ry( 0 ) ) ) , f( x , y ) _ , "error = " ; q/10 ; " %" next i end function sign( x ) sign = ( x < 0 ) - ( x > 0 ) '' uit = 0 '' if x < 0 then uit = -1 '' if x > 0 then uit = 1 '' sign = uit end function function f( x , y ) f = ( x >= y ) - ( x < y ) '' uit = -1 '' if x < y then uit = 1 '' f = uit end function function cel.uit( no ) sum = w( no , maxin + 1 ) / hoog - .5 for i = 0 to maxin sum = sum + cel(no,i)*w(no,i)/hoog - .5 next i cel.uit = sum end function sub cel.mutate no dw = int( rnd(0) * ( maxin + 1 ) ) m = int( rnd(0) * 24 ) w( no , dw ) = w( no , dw ) xor 2 ^ m end sub sub cel.mix uit , a , b for i = 0 to maxin w( uit , i ) = 0 for m = 0 to 24 if rnd(0) < .5 then w(uit,i)=w(uit,i)+(w(a,i)and 2^m) else w(uit,i)=w(uit,i)+(w(b,i)and 2^m) end if next m next i end sub