Public MM As Integer '染色体条数
Public NN As Integer '染色体位数
Public M1 As Integer '第M1条染色体
Public M2 As Integer '第M2条染色体
Public char()
Public N As Integer '在第N位交叉
Public A() As Integer '染色体
Public B() As Integer '适应度
Public MarkTemp As Integer '暂时标记最优
Public Mark As Integer '标记最优
Public Sub Exchange_A() '交叉
Dim i, j As Integer
Dim Temp() As Integer
ReDim Temp(NN)
For i = N To NN
Temp(i) = A(M1, i)
Next i
For i = N To NN
A(M1, i) = A(M2, i)
Next i
For i = N To NN
A(M2, i) = Temp(i)
Next i
End Sub
Public Sub Calculate_B() '计算适应度
Dim i, j As Integer
For i = 1 To MM '计算适应度
B(i) = 0
For j = 1 To NN
If A(i, j) = char(j) Then
B(i) = B(i) + 1
End If
If B(i) = NN Then
MarkTemp = i
MsgBox "适应度已达到最大值", vbOKCancel + vbDefaultButton2, "确认"
Exit For
End If
Next j
Next i
End Sub
Public Sub Find_Optimum()
Dim i As Integer
Dim Optimum As Integer
For i = 1 To MM
If Optimum < B(i) Then
Optimum = B(i)
MarkTemp = i
End If
Next i
End Sub
以上为MODULE里面的
Dim gene, cross, vary
Dim crossnumber, changenumber, ganumber
Private Sub Command1_Click() '--------------------------------初始化
Dim i, j As Integer
MM = Val(Text3.Text) '染色体个数
NN = Len(Text12.Text) '染色体位数
words = Text12.Text
Label13.Caption = NN
ReDim A(1 To MM, 1 To NN) '染色体种群
ReDim B(1 To MM) '适应度
ReDim char(1 To NN)
For i = 1 To NN
If Mid(words, i, 1) = " " Then
char(i) = 0
Else
char(i) = Asc(Mid(words, i, 1)) - 96
End If
Next i
Randomize
For i = 1 To MM
For j = 1 To NN
Randomize
A(i, j) = Int(Rnd * 27)
Next j
Next i
gene = 0 ''''''''''''''''''''进化的次数
cross = 0 '''''''''''''''''''交叉的次数
vary = 0 ''''''''''''''''''''变异的次数
Call Write_A
Call Calculate_B
Call Write_Calculate
End Sub
还有好多代码 放不下了 。。。我不知道怎么补充。。。
我用VB做遗传算法字符串学习,每个基因的适应度应该越来越大,可是却一直变小,不知道为什么
答案:2 悬赏:0 手机版
解决时间 2021-02-05 10:42
- 提问者网友:那叫心脏的地方装的都是你
- 2021-02-05 04:50
最佳答案
- 五星知识达人网友:等灯
- 2021-02-05 06:19
没有全部代码,无法测试,无法解析。所以,不知道在什么地方补充什么方面的代码
全部回答
- 1楼网友:你可爱的野爹
- 2021-02-05 07:46
'lpstring 是你要处理的字符串
'代码放在某一个过程或者函数中
'先解决双字,"不好"里面包含"好"
public sub convertstring(lpstring as string)as string
lpstring = replace(lpstring,"不好","!@#$")
lpstring = replace(lpstring,"不错","!#%&")
lpstring = replace(lpstring,"好","不错")
lpstring = replace(lpstring,"坏","不好")
lpstring = replace(lpstring,"!@#$","坏")
lpstring = replace(lpstring,"!#%&","好")
convertstring = lpstring
end sub
';举例,你的文本在text1中
'一个按钮 command1
private sub command1_click()
text1.text = convertstring(text1.text)
end sub
------------------------------------------
'你这新手也太新了把
for ...
'''
next ..
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯