Const k = 2
Const total = 50
Dim t As Integer
Dim s As Integer
Dim q As Integer
Public s1 As Integer
Dim ID(total) As Single
Dim px(total) As Single
Dim py(total) As Single
Dim zx(2) As Double
Dim zy(2) As Double
Dim z0x(2) As Double
Dim z0y(2) As Double
Dim dx(total) As Single
Dim dy(total) As Single
Dim d(total) As Single
Dim dx1() As Single
Dim dy1() As Single
Dim tp() As Single
Private Sub Command1_Click()
'按了取消按钮
OK = False
Me.Hide
End SubPrivate Sub Form_Load() Dim i As Integer
Dim strSQL As String
Dim RecordDate As Recordset '保存SQL语句搜索结果的记录集
Set g_Conn = New Connection
'连接到数据库
With g_Conn
.CursorLocation = adUseClient
.CommandTimeout = 10
' 连接到ACCESS数据库
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Password='';" & _
"Data Source=" & App.Path & "\Database\ManageClient.mdb"
.Open
End With
Adoana.ConnectionString = g_Conn.ConnectionString
strSQL = "select id,sco1 ,sco2 from score"
Set RecordDate = g_Conn.Execute(strSQL)
While Not RecordDate.EOF
If (RecordDate("sco1").Value <> 0) And (RecordDate("sco2").Value <> 0) Then
ID(i) = RecordDate("id").Value
px(i) = RecordDate("sco1").Value
py(i) = RecordDate("sco2").Value
i = i + 1
s1 = s1 + 1
RecordDate.MoveNext
End If
WendReDim tp(s1 - 1)'选k个初始聚类中心 z[i]
For i = 0 To 2
z0x(i) = px(i)
z0y(i) = py(i)
' MsgBox "随机选取的第{" & i & "}类中心为" & z0x(i) & "," & z0y(i) & ""
Next i
For i = 0 To s1 - 1
tp(i) = 0
Next i
t = 0 '记录point[i]暂时在哪个类中
For i = 0 To s1 - 1
For j = 0 To k
dx(i) = px(i) - z0x(j)
dy(i) = py(i) - z0y(j)
dx(t) = px(i) - z0x(t)
dy(t) = py(i) - z0y(t)Dim r
Dim fr = distance(dx(t), dy(t))
r = q
f = distance(dx(i), dy(i))
f = qIf (r > f) Then
t = j
End If
Next j
tp(i) = t
'MsgBox "经比较后,点(" & px(i) & " ," & py(i) & ")属于 " & t & " 类"Next iFor i = 0 To k '计算新的聚类中心Call newcentre(i)
' MsgBox "初始时第{" & i & "}类中心为" & zx(i) & "," & zy(i) & ""If ((zx(i) = z0x(i)) And (zy(i) = z0y(i))) Then '对前后两次的聚类中心进行比较
test = test + 1
Else
z0x(i) = zx(i)
z0y(i) = zy(i)
End If
Next i
While (test < 3) '进行迭代,对total个样本根据聚类中心进行分类
For i = 0 To s1 - 1
For j = 0 To k
dx(i) = px(i) - z0x(j)
dy(i) = py(i) - z0y(j)
dx(t) = px(i) - z0x(t)
dy(t) = py(i) - z0y(t)
Dim s
Dim m
Dim nr = distance(dx(t), dy(t))
r = q
f = distance(dx(i), dy(i))
f = qIf r > f Then
t = j
End If
Next j
tp(i) = t
'MsgBox "经比较后,点(" & px(i) & " ," & py(i) & ")属于 " & t & " 类"Next itest = 0For i = 0 To k
Call newcentre(i)
'MsgBox "第{" & i & "}类中心为" & zx(i) & "," & zy(i) & ""
If (zx(i) = z0x(i) And zy(i) = z0y(i)) Then test = test + 1
Else
z0x(i) = zx(i)
z0y(i) = zy(i)
End IfNext i
s = s + 1
WendDim msgtext As String
For i = 0 To s1 - 1
msgtext = "update score set sort =" & tp(i) & " where id = " & ID(i) & " and sco1 = " & px(i) & " and sco2 = " & py(i) & ""
g_Conn.Execute msgtext
Next i这是一个k均值算法,我把程序运行的结果存储在"score"这张表中(操作就如上面哪个sql语句),我把这张表的内容用mschart和MSHFlexGrid把结果表示出来,都是用adodc连接的,但是当我在score表中加一组数据时,就不能立即表示出来,非要先运行一边,再运行的时候就可以把结果表示出来了.大家救救我吧
“如果你要刷新的话,你就在程序中添加数据,添加完后,重新读取记录集,就可以刷新了”
什么意思,请明示!谢谢!
Adoana.Refresh
Dim msgtext As String
For i = 0 To s1 - 1
msgtext = "update score set sort =" & tp(i) & " where id = " & ID(i) & " and sco1 = " & px(i) & " and sco2 = " & py(i) & ""
g_Conn.Execute msgtext
Next iAdoana.Refresh
是这样刷新吗?