modcalculatefitnessvalue.bas
来自「用vb编写的一个遗传算法的程序」· BAS 代码 · 共 25 行
BAS
25 行
Attribute VB_Name = "ModCalculateFitnessValue"
'计算适应度
Sub CalculateFitnessValue()
Dim i As Integer
Dim x1 As Double, x2 As Double
For i = 0 To PopSize - 1
x1 = DecodeChromosome(Population(i).chrom, 0, 10, -2.048, 2.048)
x2 = DecodeChromosome(Population(i).chrom, 10, 10, -2.048, 2.048)
Population(i).fitness = 100 * (x1 ^ 2 - x2) ^ 2 + (1 - x1) ^ 2
Next i
End Sub
'解码
'参数:软色体串,串的起始位置,串长,所对应变量的下限,上限
Function DecodeChromosome(ch() As Integer, point As Integer, length As Integer, lowerlimit As Double, upperlimit As Double) As Double
Dim i As Integer
Dim temp As Double
temp = 0#
For i = point To point + length - 1
temp = temp + ch(i) * 2 ^ (i - point)
Next i
DecodeChromosome = lowerlimit + temp * (upperlimit - lowerlimit) / (2 ^ length - 1)
End Function
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?