我根據程式框圖編的程式一運行就沒相應,題目為:f(x)=(x1-2)^2+(x2-1)^2,約束條件:g1(x)=x1^2-x2^2<=0
g2(x)=x1+x2-2<=0,初始點x=(3,3)f(x0)=5 最優解 x=(1,1) f(x)=1
框圖

編程如下:
Private Sub Command1_Click()
Dim r1(8) As Single, r2(8) As Single
Dim e1(8) As Single, e2(8) As Single
Dim x1(8) As Single, x2(8) As Single
Dim g1(8) As Single, g2(8) As Single
Dim f(8) As Single
x01 = 3: x02 = 3: t0 = 0.1: c = 0.01
fx0 = 5
f0 = fx0
fl = fx0
m1: t = t0
For j = 1 To 8
Randomize
r1(j) = 2 * Rnd - 1
r2(j) = 2 * Rnd - 1
e1(j) = r1(j) / (Sqr(Abs(r1(j) + r2(j))))
e2(j) = r2(j) / (Sqr(Abs(r1(j) + r2(j))))
x1(j) = x01 + t * e1(j)
x2(j) = x01 + t * e2(j)
If g1(j) = x1(j) ^ 2 - x2(j) <= 0 And g2(j) = x1(j) + x2(j) - 2 <= 0 Then
f(j) = (x1(j) - 2) ^ 2 + (x2(j) - 1) ^ 2
If f(j) < fl Then
fl = f(j)
d1 = e1(j)
d2 = e2(j)
xl1 = x1(j)
xl2 = x2(j)
End If
End If
Next j
a = xl1
b = xl2
m2: t = 1.3 * t
a = a + t * d1
b = b + t * d2
If (a ^ 2 - b) <= 0 And (a + b - 2) <= 0 Then
y = (a - 2) ^ 2 + (b - 1) ^ 2
If y < fl Then
fl = y
GoTo m2
End If
m3: a = a - t * d1
b = b - t * d2
t = 0.7 * t
a = a + t * d1
b = b + t * d2
If (a ^ 2 - b) <= 0 And (a + b - 2) <= 0 Then
y = (a - 2) ^ 2 + (b - 1) ^ 2
If y < fl Then
If Abs((f0 - y) / f0) < c Then
Print a, b
Print y
Else
f0 = y
fl = y
GoTo m1
End If
Else
GoTo m3
End If
Else
GoTo m3
End If
Else: GoTo m3
End If
End Sub
運行后沒回應,也出不來結果
uj5u.com熱心網友回復:
除錯了一下,這應該是演算法設計問題,這代碼寫得也相當的原始,問之前你應該先要了解清楚這個流程圖的含義uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
這不會是打算我幫你寫吧,除錯的時候程式困在M3里出不來,在那段可能有問題吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/110973.html
標籤:網絡編程
