Private Const MAX_CLUSTERS As Integer = 2 Private Const VECTORS As Integer = 4 Private Const VEC_LEN As Integer = 4 Private Const DECAY_RATE As Double = 0.96 Private Const MIN_ALPHA As Double = 0.01 Private Const CR As String = Chr(13) + Chr(10) Private Alpha As Double = 0.6 Private D(MAX_CLUSTERS) As Double Private w As Double(,) = New Double(,) {{0.2, 0.6, 0.5, 0.9}, _ {0.8, 0.4, 0.7, 0.3}} Private Pattern As Integer(,) = New Integer(,) {{1, 1, 0, 0}, _ {0, 0, 0, 1}, _ {1, 0, 0, 0}, _ {0, 0, 1, 1}} Private Tests As Integer(,) = New Integer(,) {{1, 0, 0, 1}, _ {0, 1, 1, 0}, _ {1, 0, 1, 0}, _ {0, 1, 0, 1}} Private Sub Training() Dim Iterations As Integer = 0 Dim i As Integer Dim VecNum As Integer Dim DMin As Integer Do While Alpha > MIN_ALPHA Iterations += 1 For VecNum = 0 To VECTORS - 1 'Compute input. ComputeInput(VecNum) 'See which is smaller, D(0) or D(1)? DMin = Minimum(D(0), D(1)) 'Text1.Text += "Closest is D(" + CStr(DMin) + ")" + CR 'Update the weights on the winning unit. For i = 0 To VECTORS - 1 w(DMin, i) = w(DMin, i) + (Alpha * (Pattern(VecNum, i) - w(DMin, i))) 'Text1.Text += " w(" + CStr(i) + ")= " + CStr(w(DMin, i)) Next i Next VecNum 'Reduce the learning rate. Alpha = DECAY_RATE * Alpha Loop Text1.Text += "Iterations: " + CStr(Iterations) + CR + CR End Sub Private Sub Testing() Dim i, j As Integer Dim VecNum As Integer Dim DMin As Integer 'Print clusters created. Text1.Text += "Clusters for training input:" + CR For VecNum = 0 To VECTORS - 1 'Compute input. ComputeInput(VecNum) 'See which is smaller, D(0) or D(1)? DMin = Minimum(D(0), D(1)) Text1.Text += CR + "Vector (" For i = 0 To VECTORS - 1 Text1.Text += CStr(Pattern(VecNum, i)) + ", " Next i Text1.Text += ") fits into category " + CStr(DMin) + CR Next VecNum 'Print weight matrix. Text1.Text += CR For i = 0 To MAX_CLUSTERS - 1 Text1.Text += "Weights for Node " + CStr(i) + " connections:" + CR For j = 0 To VEC_LEN - 1 Text1.Text += Microsoft.VisualBasic.Format(w(i, j), "#.000") + ", " Next Text1.Text += CR + CR Next 'Print post-training tests. Text1.Text += "Categorized test input:" + CR For VecNum = 0 To VECTORS - 1 'Compute input. ComputeInput(VecNum) 'See which is smaller, D(0) or D(1)? DMin = Minimum(D(0), D(1)) Text1.Text += CR + "Vector (" For i = 0 To VECTORS - 1 Text1.Text += CStr(Tests(VecNum, i)) + ", " Next i Text1.Text += ") fits into category " + CStr(DMin) + CR Next VecNum End Sub Private Sub ComputeInput(ByVal VectorNumber As Integer) Dim i, j As Integer D(0) = 0.0 D(1) = 0.0 For i = 0 To MAX_CLUSTERS - 1 For j = 0 To VECTORS - 1 D(i) += (w(i, j) - Tests(VectorNumber, j)) ^ 2 'Text1.Text += "D= " + CStr(D(i)) + CR Next j Next i End Sub Private Function Minimum(ByVal ValueA As Double, ByVal ValueB As Double) As Integer If ValueA > ValueB Then Minimum = 1 ElseIf ValueA < ValueB Then Minimum = 0 End If End Function Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles Button1.Click Training() Testing() End Sub