📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 3090
ClientLeft = 60
ClientTop = 450
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 3090
ScaleWidth = 4680
StartUpPosition = 3 '窗口缺省
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Left = 2520
TabIndex = 0
Top = 1560
Width = 1815
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Base 1
Dim T() As Variant, V() As Variant
Private Sub Form_Load()
T = Array(233.4025, 239.35995, 243.59725, 249.9011, 254.20285, 258.0566, 262.54575, 268.73452, 272.85824, 278.15304, 283.27835, 288.66575, 292.992, 297.68075, 303.3721, 309.73835, 314.0148, 319.24615, 323.76605, 327.75165, 332.7726, 339.0916)
V = Array(4.202, 3.73, 3.493, 3.252, 3.155, 3.11, 3.086, 3.151, 3.218, 3.357, 3.53, 3.75, 3.966, 4.21, 4.527, 4.912, 5.168, 5.469, 5.713, 5.905, 6.122, 6.356)
End Sub
Private Sub Command1_Click()
Dim a() As Double, b() As Double, VaryNum%, Popsize%, MaxGeneration%, ObjectValue As Integer, M As Double
'a() 变量的取值下限 b() 变量的取值上限
'VaryNum 变量数 MaxGeneration 最大迭代数目
'Popsize 微粒群规模
VaryNum = 6 '微粒群算法中变量总数
ReDim a(VaryNum) As Double, b(VaryNum) As Double
a(1) = 0: a(2) = 0: a(3) = 0: a(4) = 3000: a(5) = 4000: a(6) = -0.5
b(1) = 1: b(2) = 1: b(3) = 1: b(4) = 5000: b(5) = 5500: b(6) = 0.5
Popsize = 200 '微粒群算法中个体总数
MaxGeneration = 10000 '微粒群算法运行代数
ObjectValue = 0 '判断目标函数取最小值还是最大值;当取最小值时取0,取最大值时取1
M = Pso(a(), b(), VaryNum, Popsize, MaxGeneration, ObjectValue)
End Sub
Public Function Pso(amin() As Double, amax() As Double, VaryNum As Integer, Popsize As Integer, MaxGeneration As Integer, ObjectValue As Integer)
'amin 变量的取值下限 amax 变量的取值上限
'VaryNum 变量数 MaxGeneration 最大迭代数目
'Popsize 微粒群规模
Dim RealValue(DataNum) As Double
Dim i%, j%, Generation%, PNum%
Dim c1#, c2#, Rnd1#, Rnd2#, w#
'##########重定义数组变量##############
Dim InBird() As Individual, IndividualBest() As Individual, GlobalBest As Individual
ReDim GlobalBest.V(VaryNum) As Double
ReDim GlobalBest.x(VaryNum) As Double
ReDim InBird(MaxGeneration, Popsize) As Individual, IndividualBest(Popsize) As Individual
For i = 1 To MaxGeneration
For j = 1 To Popsize
ReDim InBird(i, j).V(VaryNum) As Double
ReDim InBird(i, j).x(VaryNum) As Double
Next j
Next i
For i = 1 To Popsize
ReDim IndividualBest(i).V(VaryNum) As Double
ReDim IndividualBest(i).x(VaryNum) As Double
Next i
'##########重定义数组变量##############
Randomize
'##########初始化微粒群##############
For i = 1 To Popsize
For j = 1 To VaryNum
InBird(1, i).x(j) = amin(j) + Rnd * (amax(j) - amin(j)) '初始化变量值
Next j
Next i
For i = 1 To Popsize
InBird(1, i).Value = FunctionObject(InBird(1, i).x()) '通过调用函数计算目标函数值
IndividualBest(i) = InBird(1, i) '初始化时将初始值作为最优值
Next i
GlobalBest = IndividualBest(1)
For i = 1 To Popsize
If ObjectValue = 1 Then '目标函数取最大值为目的
If GlobalBest.Value < IndividualBest(i).Value Then
GlobalBest = IndividualBest(i) '寻求初始群体中的最优位置
End If
ElseIf ObjectValue = 0 Then '目标函数取最小值为目的
If GlobalBest.Value > IndividualBest(i).Value Then
GlobalBest = IndividualBest(i) '寻求初始群体中的最优位置
End If
End If
Next i
'##########初始化微粒群##############
For Generation = 2 To MaxGeneration
w = 0.4 - Generation * (0.8 - 0.4) / 1000 '线性调整权重因子w的值
c1 = 2: c2 = 2 '比例参数
For i = 1 To Popsize
For j = 1 To VaryNum
Recalculation: '重新计算
Rnd1 = Rnd: Rnd2 = Rnd '产生随机数
'产生下一代微粒移动的距离
InBird(Generation, i).V(j) = w * InBird(Generation - 1, i).V(j) + c1 * Rnd1 * (IndividualBest(i).x(j) - InBird(Generation - 1, i).x(j)) + c2 * Rnd2 * (GlobalBest.x(j) - InBird(Generation - 1, i).x(j))
InBird(Generation, i).x(j) = InBird(Generation - 1, i).x(j) + InBird(Generation, i).V(j)
If InBird(Generation, i).x(j) > amax(j) Or InBird(Generation, i).x(j) < amin(j) Then
PNum = PNum + 1
If PNum <= 10 Then
GoTo Recalculation
Else
InBird(Generation, i).V(j) = Rnd
InBird(Generation, i).x(j) = amin(j) + (amax(j) - amin(j)) * InBird(Generation, i).V(j)
End If
End If
PNum = 0
Next j
Next i
For i = 1 To Popsize
InBird(Generation, i).Value = FunctionObject(InBird(Generation, i).x()) '通过调用函数计算目标函数值
Next i
For i = 1 To Popsize '寻求每一个微粒群个体运行至某一代时发现的最优位置
If ObjectValue = 1 Then '目标函数取最大值为目的
If IndividualBest(i).Value < InBird(Generation, i).Value Then
IndividualBest(i) = InBird(Generation, i)
End If
ElseIf ObjectValue = 0 Then '目标函数取最小值为目的
If IndividualBest(i).Value > InBird(Generation, i).Value Then
IndividualBest(i) = InBird(Generation, i)
End If
End If
Next i
For i = 1 To Popsize
If ObjectValue = 1 Then '目标函数取最大值为目的
If GlobalBest.Value < IndividualBest(i).Value Then
GlobalBest = IndividualBest(i) '寻求整个微粒群体运行至某一代时发现的最优位置
End If
ElseIf ObjectValue = 0 Then '目标函数取最小值为目的
If GlobalBest.Value > IndividualBest(i).Value Then
GlobalBest = IndividualBest(i) '寻求整个微粒群体运行至某一代时发现的最优位置
End If
End If
Next i
Debug.Print "The Current Generation is at"; Generation - 1; " ";
Debug.Print "GlobalBest.Value="; GlobalBest.Value
Next Generation
For i = 1 To VaryNum
Debug.Print "GlobalBest.x("; i; ")="; GlobalBest.x(i)
Next i
'Call SubValue(GlobalBest.x(), RealValue())
End Function
Public Function FunctionObject(Vary() As Double) As Double
Dim Dimension As Integer, a() As Double, i%
Dimension = UBound(Vary)
ReDim a(Dimension) As Double
For i = 1 To Dimension
a(i) = Vary(i)
Next i
Dim Y1#, Y2#, Y3#, Y(DataNum) As Double
For i = 1 To DataNum
Y1 = 12 * (a(2) + a(6) * Exp(a(4) * (1 / T(i) - 1 / 293)))
Y2 = a(3) * Exp(a(5) * (1 / T(i) - 1 / 293)) / (a(3) + Exp(a(5) * (1# / T(i) - 1 / 293)))
Y3 = a(6) * Exp(a(4) * (1# / T(i) - 1 / 293))
Y(i) = Y1 / (a(1) + Y2 + a(2) + Y3)
Y1 = 0: Y2 = 0: Y3 = 0
Next i
FunctionObject = 0
For i = 1 To DataNum
FunctionObject = FunctionObject + (V(i) - Y(i)) ^ 2 '目标函数
Next i
'FunctionObject = 100 * (x(1) ^ 2 - x(2)) * (x(1) * x(2) - x(2)) + (1 - x(1)) ^ 2 '目标函数
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -