Public Class Neuron
    Public Weights As Double()
    Public X As Integer
    Public Y As Integer
    Private length As Integer
    Private nf As Double

    Public Sub New(ByVal x__1 As Integer, ByVal y__2 As Integer, ByVal length As Integer)
        X = x__1
        Y = y__2
        Me.length = length
        nf = 1000 / Math.Log(length)
    End Sub

    Private Function Gauss(ByVal win As Neuron, ByVal it As Integer) As Double
        Dim distance As Double = Math.Sqrt(Math.Pow(win.X - X, 2) + Math.Pow(win.Y - Y, 2))
        Return Math.Exp(-Math.Pow(distance, 2) / (Math.Pow(Strength(it), 2)))
    End Function

    Private Function LearningRate(ByVal it As Integer) As Double
        Return Math.Exp(-it / 1000) * 0.1
    End Function

    Private Function Strength(ByVal it As Integer) As Double
        Return Math.Exp(-it / nf) * length
    End Function

    Public Function UpdateWeights(ByVal pattern As Double(), ByVal winner As Neuron, ByVal it As Integer) As Double
        Dim sum As Double = 0

        For i As Integer = 0 To Weights.Length - 1
            Dim delta As Double = LearningRate(it) * Gauss(winner, it) * (pattern(i) - Weights(i))
            Weights(i) += delta
            sum += delta
        Next
        Return sum / Weights.Length
    End Function
End Class

