Hi..
Code: Select all
;
; ------------------------------------------------------------
;
; Neural network example
; (c) 2003 - Pedro Gil (Balrog Software)
;
; ------------------------------------------------------------
;
;
;
NumberBugs=5 ; number of bugs
Dim W(NumberBugs,10,3)
Dim Net(NumberBugs,10)
Dim Action(NumberBugs,10)
Dim x(NumberBugs)
Dim y(NumberBugs)
Dim xant(NumberBugs)
Dim yant(NumberBugs)
Dim Et.f(NumberBugs+1)
For Bug=1 To NumberBugs
For Funtion=0 To 7
For Weights=0 To 2
W(Bug,Funtion,Weights)=Random(5)-3
Next Weights
Next Funtion
Next Bug
LoadImage(0,"Black.bmp")
LoadImage(1,"A1.bmp")
LoadImage(2,"A2.bmp")
LoadImage(3,"A3.bmp")
LoadImage(4,"A4.bmp")
LoadImage(5,"ZBN.bmp")
LoadImage(6,"ZBS.bmp")
LoadImage(7,"ZBO.bmp")
LoadImage(8,"ZBE.bmp")
Dim WORLD(50,50)
If OpenWindow(0, 100, 140, 430, 314, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget, "Virtual Life")
For y=0 To 30
For x=0 To 30
If Random(5)>3
COM=Random(1)
WORLD(x,y)=Random(4)
EndIf
Next x
Next y
For Bug=1 To NumberBugs
x(Bug)=Random(28)+1
y(Bug)=Random(28)+1
xant(Bug)=x(Bug)
yant(Bug)=y(Bug)
Et.f(Bug)=10
Next Bug
Ta.f=0.8 ; Learning constant recommended a value between 0 to 1
Count=0
For YC=1 To 30
For XC=1 To 30
Count+1
CL=WORLD(XC,YC)
UseImage(CL)
StartDrawing(WindowOutput())
DrawImage(ImageID(),XC*9,YC*9)
StopDrawing()
Next XC
Next YC
Repeat
EventID.l = WindowEvent()
If EventID = #PB_EventCloseWindow ; If the uBug has pressed on the close button
Quit = 1
EndIf
Timer=Timer+1
If Timer=10000
Timer=0
EndIf
If Timer=0
If Et.f(Bug)<=0
For Bug=1 To NumberBugs
x=x(Bug)
y=y(Bug)
xant=xant(Bug)
yant=yant(Bug)
If xant=x
COM=Random(2)-1
Comp=Random(2)-1
If Comp=0
If COM=1
x+1
este=1
norte=0
sur=0
oeste=0
EndIf
If COM=-1
x-1
oeste=1
norte=0
sur=0
este=0
EndIf
EndIf
If Comp=1
If COM=1
y+1
sur=1
norte=0
este=0
oeste=0
EndIf
If COM=-1
y-1
norte=1
sur=0
este=0
oeste=0
EndIf
EndIf
EndIf
If x=0
x=1
EndIf
If x=31
x=30
EndIf
If y=0
y=1
EndIf
If y=31
y=30
EndIf
If norte=1
IncY=-1
Else
IncY=0
EndIf
If sur=1
IncY=1
Else
IncY=0
EndIf
If oeste=1
IncX=-1
Else
IncX=0
EndIf
If este=1
IncX=1
Else
IncX=0
EndIf
Et.f(Bug)-0.01
If WORLD(x+IncX,y+IncY)>0
If WORLD(x+IncX,y+IncY)=1
Smell=0
MouthTaste=0
ParticleEnergy=0
EndIf
If WORLD(x+IncX,y+IncY)=2
Smell=0
MouthTaste=1
ParticleEnergy=0
EndIf
If WORLD(x+IncX,y+IncY)=3
Smell=1
MouthTaste=0
ParticleEnergy=0
EndIf
If WORLD(x+IncX,y+IncY)=4
Smell=1
MouthTaste=1
ParticleEnergy=1
EndIf
Net(Bug,1)=(Smell*W(Bug,1,1))+(MouthTaste*W(Bug,1,2))+W(Bug,1,0)
If Net(Bug,1)=>0
Action(Bug,1)=1
Else
Action(Bug,1)=0
EndIf
Increment=ParticleEnergy-Action(Bug,1)
W(Bug,1,1)=W(Bug,1,1)+Ta.f*Increment*Smell
W(Bug,1,2)=W(Bug,1,2)+Ta.f*Increment*MouthTaste
W(Bug,1,0)=W(Bug,1,0)+Ta.f*Increment
If Action(Bug,1)=1
If ParticleEnergy=1
Et.f(Bug)+1
buenas+1
EndIf
If ParticleEnergy=0
Et.f(Bug)-1
malas+1
EndIf
UseImage(0)
StartDrawing(WindowOutput())
DrawImage(ImageID(),(x+IncX)*9,(y+IncY)*9)
StopDrawing()
Ali=WORLD(x+IncX,y+IncY)
WORLD(x+IncX,y+IncY)=0
Repeat
Xrnd=Random(29)+1
Yrnd=Random(29)+1
Until WORLD(Xrnd,Yrnd)=0
WORLD(Xrnd,Yrnd)=Ali
UseImage(Ali)
StartDrawing(WindowOutput())
DrawImage(ImageID(),Xrnd*9,Yrnd*9)
StopDrawing()
EndIf
StartDrawing(WindowOutput())
BackColor(200,200,200)
Locate(285,10)
DrawText("Energy: "+Str(Et.f)+" Value(int): "+Str(Ta.f)+" ")
Locate(285,30)
DrawText("Particles eated")
Locate(285,50)
DrawText("Good - Bad")
Locate(285,70)
DrawText(" "+Str(buenas)+" "+Str(malas)+" ")
StopDrawing()
EndIf
StartDrawing(WindowOutput())
UseImage(WORLD(xant,yant))
DrawImage(ImageID(),xant*9,yant*9)
StopDrawing()
StartDrawing(WindowOutput())
If norte=1
Img=5
EndIf
If sur=1
Img=6
EndIf
If oeste=1
Img=7
EndIf
If este=1
Img=8
EndIf
UseImage(Img)
DrawImage(ImageID(),x*9,y*9)
StopDrawing()
x(Bug)=x
y(Bug)=y
xant(Bug)=x
yant(Bug)=y
Next Bug
EndIf
Delay(40)
EndIf
Until Quit = 1
EndIf
End
But this or next weekend i submit to my web a little dll to make simple perceptron neural networks that i want to use with my game Tanks War II, with the source code and information about it.
Bye...