so i tryed it whit a GA ANN
error :
it don't learn
it stops before showing best net working
Code: Select all
'' bluatigro 18 sept 2017
'' ga ann
const as integer inmax = 3
const as integer layers = 3
const as integer hidmax = 3
const as integer uitmax = 1
function signoid( x as double ) as double
return 1 / ( 1 + exp( -x ) )
end function
type t_net
public :
dim as double in( inmax )
dim as double win( inmax , hidmax )
dim as double hidden( layers , hidmax )
dim as double wh( layers , hidmax , hidmax )
dim as double wuit( hidmax , uitmax )
dim as double uit( uitmax )
dim as double fout
declare constructor()
declare sub run_net()
declare sub mutate( r as double )
end type
constructor t_net()
dim as integer i , l , h1 , h2 , u
in( 0 ) = 1
for i = 0 to inmax
for h1 = 0 to hidmax
win( i , h1 ) = rnd - rnd
next h1
next i
for l = 0 to layers
hidden( l , 0 ) = 1
for h1 = 0 to hidmax
for h2 = 0 to hidmax
wh( l , h1 , h2 ) = rnd - rnd
next h2
next h1
next l
for h1 = 0 to hidmax
for u = 0 to uitmax
wuit( h1 , u ) = rnd - rnd
next u
next h1
end constructor
sub t_net.run_net()
dim as integer i , l , h1 , h2 , u
dim as double sum
for i = 0 to inmax
sum = 0
for h1 = 0 to hidmax
sum += in( i ) * win( i , h1 )
next h1
hidden( 0 , h1 ) = signoid( sum / inmax )
next i
for l = 1 to layers
for h1 = 0 to hidmax
sum = 0
for h2 = 0 to hidmax
sum += hidden( l - 1 , h1 ) * wh( l - 1 , h1 , h2 )
next h2
hidden( l , h1 ) = signoid( sum / hidmax )
next h1
next l
for u = 0 to uitmax
sum = 0
for h1 = 0 to hidmax
sum += hidden( layers , h1 ) * wuit( h1 , u )
next h1
uit( u ) = signoid( sum / hidmax )
next u
end sub
function mutate_dbl( x as double ) as double
dim as long t
t = clng( x * 2 ^ 30 )
t = t xor ( 2 ^ int( rnd * 30 ) )
if rnd < .5 then return -cdbl( t / 2 ^ 30 )
return cdbl( t / 2 ^ 30 )
end function
sub t_net.mutate( r as double )
dim as integer i , l , h1 , h2 , u
for i = 0 to inmax
for h1 = 0 to hidmax
if rnd < r then
win( i , h1 ) = mutate_dbl( win( i , h1 ) )
end if
next h1
next i
for l = 0 to layers
for h1 = 0 to hidmax
for h2 = 0 to hidmax
if rnd < r then
wh( l , h1 , h2 ) = mutate_dbl( wh( l , h1 , h2 ) )
end if
next h2
next h1
next l
for u = 0 to uitmax
for h1 = 0 to hidmax
if rnd < r then
wuit( h1 , u ) = mutate_dbl( wuit( h1 , u ) )
end if
next h1
next u
end sub
function crosover(a as t_net,b as t_net)as t_net
dim as integer i , l , h1 , h2 , u
dim as t_net q
for i = 0 to inmax
for h1 = 0 to hidmax
if rnd < .5 then
q.win( i , h1 ) = a.win( i , h1 )
else
q.win( i , h1 ) = b.win( i , h1 )
end if
next h1
next i
for l = 0 to layers
for h1 = 0 to hidmax
for h2 = 0 to hidmax
if rnd < .5 then
q.wh( l , h1 , h2 ) = a.wh( l , h1 , h2 )
else
q.wh( l , h1 , h2 ) = b.wh( l , h1 , h2 )
end if
next h2
next h1
next l
for u = 0 to uitmax
for h1 = 0 to hidmax
if rnd < .5 then
q.wuit( h1 , u ) = a.wuit( h1 , u )
else
q.wuit( h1 , u ) = b.wuit( h1 , u )
end if
next h1
next u
return q
end function
dim as t_net net( 200 )
dim as integer tel , n
dim as double a , b , c
for tel = 0 to 2000
'' first calc output every net
for n = 0 to ubound( net )
net(n).fout=0
for a = 0 to 1
for b = 0 to 1
for c = 0 to 1
net( n ).in( 1 ) = a
net( n ).in( 2 ) = b
net( n ).in( 3 ) = c
net( n ).run_net
net( n ).fout += abs( net( n ).uit( 1 ) _
- ( cint( a ) xor cint( b ) xor cint( c ) ) )
next c
next b
next a
next n
'' sort nets on error
for a = 1 to ubound( net )
for b = 0 to a - 1
if net( a ).fout < net( b ).fout then
swap net( a ) , net( b )
end if
next b
next a
print "Generation " ; tel ; " : error = " ; net( 0 ).fout
for a = 20 to ubound( net )
b = int( rnd * 20 )
c = int( rnd * 20 )
net( a ) = crosover( net( b ) , net( c ) )
net( a ).mutate .05
next a
next tel
for a = 0 to 1
for b = 0 to 1
for c = 0 to 1
net( n ).in( 1 ) = a
net( n ).in( 2 ) = b
net( n ).in( 3 ) = c
net( n ).run_net
print str(a)+str(b)+str(c) , net( 0 ).uit( 1 ) , ( cint( a ) xor cint( b ) xor cint( c ) )
next c
next b
next a
print "[ game over ]"
sleep