📄 classchaser.cls
字号:
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "ClassChaser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'the swift brown fox jumps over the lazy hare
Public fox As New ClassXYbot
Public hare As New ClassXYbot
Public GA As New ClassGApopulation
Public Sub Init()
fox.WorldWidth = 100
fox.WorldHeight = 100
hare.WorldWidth = 100
hare.WorldHeight = 100
Call GA.Init(50, 2, 4)
GA.MaxConnections = 20
GA.maxHiddenUnits = 10
End Sub
Public Sub Update(NoOfGenerations As Long, itterationsPerGeneration As Long, GenerationCtr As TextBox, MaxFitness As TextBox)
Dim generation As Long
For generation = 0 To NoOfGenerations
Call Evolve(itterationsPerGeneration)
GenerationCtr.Text = GA.generation
GenerationCtr.Refresh
MaxFitness.Text = GA.MeanFitness
MaxFitness.Refresh
Next
End Sub
Public Sub Evolve(NoOfItterations As Long)
Dim dist As Single
'start with the first individual
Call GA.FirstIndividual
Do
Call Test(NoOfItterations, False)
Loop While (GA.NextIndividual)
'go to the next generation
Call GA.NextGeneration
End Sub
Public Sub Test(NoOfItterations As Long, show As Boolean, Optional frm As Object, Optional shaFox As Object, Optional shaHare As Object)
Dim itteration As Long
Dim dist As Single
Dim d As Single
Dim dx As Single
Dim dy As Single
If (show) Then
Call GA.setBestIndividual
End If
'starting positions
fox.direction = 0
fox.vx = 0
fox.vy = 0
fox.x = CInt(Rnd * 100)
fox.y = CInt(Rnd * 100)
hare.direction = 3.1415927
hare.vx = 0
hare.vy = 0
hare.x = CInt(Rnd * 100)
hare.y = CInt(Rnd * 100)
hare.Accn = 0.1
hare.maxSpeed = 0.5
d = 0
For itteration = 0 To NoOfItterations - 1
'run rabbit run
Call HareMove
'get sensor inputs for fox
dx = (fox.x - hare.x) / 100
dy = (fox.y - hare.y) / 100
Call GA.setInput(0, dx)
Call GA.setInput(1, dy)
dist = Sqr(fox.DistSqr(hare)) / 100
d = d + dist
Call GA.Update
If (GA.getOutput(0) > GA.getOutput(1)) Then
fox.x = fox.x + 1
Else
fox.x = fox.x - 1
End If
If (GA.getOutput(2) > GA.getOutput(3)) Then
fox.y = fox.y + 1
Else
fox.y = fox.y - 1
End If
If (fox.x < 5) Then
fox.x = 5
End If
If (fox.x > 95) Then
fox.x = 95
End If
If (fox.y < 5) Then
fox.y = 5
End If
If (fox.y > 95) Then
fox.y = 95
End If
'Call fox.Move
'dist = dist + (Sqr(fox.DistSqr(hare)) / 100)
If (show) Then
shaFox.Left = fox.x
shaFox.Top = fox.y
shaHare.Left = hare.x
shaHare.Top = hare.y
frm.Refresh
End If
Next
d = d / NoOfItterations
Call GA.setFitness(1 - d)
End Sub
Private Sub HareMove()
Call hare.TurnRandom
Call hare.Move
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -