Homework5_A

Create – in your preferred language C# or VB.NET – a demonstrative program which computes the online arithmetic mean (or “running mean”) and distribution for a continuous variable (can use random simulated values). Make the code as general and reusable as possible, as it must be used in your next applications and exam.

How i worked

I tried to modulate the code, so as to have it more robust and readable. Also for this reason the class has been put in a separate file.

   Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
 
        Dim Dog As Dog = new_dataset()
        ListOfDog.Add(Dog)
        print_Database(Dog)
        online_mean(Dog.Weight)
        print_OnArMean()
        discrete_distribution(DicOfBreed, Dog.Breed)
        discrete_distribution(DicOfAgeDistributio, Dog.Age)
    End Sub

Vb.Net

Form1.vb

Public Class Form1
    Public R As New Random

    'List for store the dog
    Public ListOfDog As New List(Of Dog)

    'Array of breed
    Public PossibleBreed() As String = {"German Shepherd", "Alaskan Malamute", "Beauceron", "Bernese Mountain Dog",
                                        "Black Russian Terrier", "Weimaraner", "Pointer", "Rottweiler", "Siberian Husky",
                                        "Afghan Hound", "Boxer", "Collie", "Weimaraner", "Dalmatian", "Golden Retriver",
                                        "Irish Setter", "Labrador Retriver"}

    Public Count As Integer
    Public CurrentArMean As Double

    Dim listOfIntervals As New List(Of Interval)


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        inizialized()
    End Sub
    Public Sub inizialized()
        Count = 0
        CurrentArMean = 0
        ListOfDog.Clear()
        listOfIntervals.Clear()
        Me.RTxTDatabase.Text = ("Breed:".PadRight(25) & "Weight:".PadRight(10) & "Time:".PadRight(12) & "Age:" & Environment.NewLine & Environment.NewLine)
        Me.RTxtArOnMean.Text = ("Mean: " & Environment.NewLine & Environment.NewLine)
        Me.RTxtBreedDistribution.Text = "Distribution: " & Environment.NewLine & Environment.NewLine
    End Sub



    'Online algo for mean
    Public Sub online_mean(mean As Double)
        Count += 1
        CurrentArMean = CurrentArMean + (mean - CurrentArMean) / Count
    End Sub
    Public Overloads Sub discrete_distribution(dictionary As Dictionary(Of String, Integer),
                                     b As String)
        If (dictionary.ContainsKey(b)) Then
            dictionary(b) += 1
        Else
            dictionary.Add(b, 1)
        End If
    End Sub
    Public Overloads Sub discrete_distribution(dictionary As Dictionary(Of Integer, Integer), b As Integer)
        If (dictionary.ContainsKey(b)) Then
            dictionary(b) += 1
        Else
            dictionary.Add(b, 1)
        End If
    End Sub
    Public Sub print_OnArMean(type As String)
        Me.RTxtArOnMean.AppendText(type & ": " & CurrentArMean.ToString("N2") & Environment.NewLine)
    End Sub
    Public Sub print_Database(Dog As Dog)
        Me.RTxTDatabase.AppendText(Dog.Breed.PadRight(25) & Dog.Weight.ToString.PadRight(10) & Dog.Time.ToString.PadRight(12) & Dog.Age & Environment.NewLine)

    End Sub

    Public Sub print_continue_distr()
        Me.RTxtBreedDistribution.AppendText("Continue time Distribution: " & Environment.NewLine & Environment.NewLine)
        Me.RTxtBreedDistribution.AppendText("Count:".PadRight(10).PadLeft(25) & "Relative Freq:".PadRight(18) & "Percentage:" & Environment.NewLine & Environment.NewLine)
        For Each i In listOfIntervals
            Me.RTxtBreedDistribution.AppendText(i.lowerEnd & " - " & i.upperEnd.ToString.PadRight(15) & i.count.ToString.PadRight(13) & i.relativeFreq.ToString("N2").PadRight(10) & i.percentage.ToString("N2") & " %" & Environment.NewLine)
        Next
    End Sub
    Public Function new_dataset() As Dog
        Dim minTime As Integer = 10
        Dim maxTime As Integer = 120
        Dim maxWeight As Integer = 50
        Dim minWeight As Integer = 30
        'Time to complete a race can be between 10 and 120 minutes

        Dim Dog As New Dog With {
            .Breed = PossibleBreed(R.Next(0, PossibleBreed.Length)),
            .Time = Math.Round(R.NextDouble * (maxTime - minTime) + minTime, 2),
            .Weight = Math.Round(R.NextDouble * (maxWeight - minWeight) + minWeight, 2),
            .Age = R.Next(1, 13)
        }
        Return Dog
    End Function
    Private Sub BtnGo_Click(sender As Object, e As EventArgs) Handles BtnGo.Click
        Me.Timer.Start()
    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick

        Dim Dog As Dog = new_dataset()
        ListOfDog.Add(Dog)
        print_Database(Dog)
        online_mean(Dog.Weight)
        print_OnArMean("Weight mean")
        Count = 0
        CurrentArMean = 0
        online_mean(Dog.Time)
        print_OnArMean("Time mean")
        create_interval(Dog)
        continue_distribution()
    End Sub

    Public Sub continue_distribution()
        For Each i In listOfIntervals
            i.relativeFreq = i.count / listOfIntervals.Count
            i.percentage = i.relativeFreq * 100
        Next
    End Sub

    Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click
        Me.Timer.Stop()
        Me.RTxtArOnMean.AppendText(Environment.NewLine & ListOfDog.Count & " occurrences" & Environment.NewLine)
    End Sub



    Private Sub BtnDistribution_Click(sender As Object, e As EventArgs) Handles BtnDistribution.Click
        Me.RTxtBreedDistribution.Clear()
        print_continue_distr()
    End Sub

    Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
        inizialized()
    End Sub

    'interval
    Public Function interval_Correct(d As Dog, i As Interval) As Boolean
        Return d.Time > i.lowerEnd AndAlso d.Time <= i.upperEnd
    End Function

    Public Function interval_isLeft(d As Dog) As Boolean
        Return d.Time <= listOfIntervals(0).lowerEnd
    End Function
    Public Function interval_isRight(d As Dog) As Boolean
        Return d.Time > listOfIntervals(listOfIntervals.Count - 1).upperEnd
    End Function

    Public Sub inizialized_list(intervalSize As Double)
        Dim startingEndPoint As Double = 40
        Dim interval_0 As New Interval
        interval_0.lowerEnd = startingEndPoint
        interval_0.upperEnd = interval_0.lowerEnd + intervalSize
        listOfIntervals.Add(interval_0)
    End Sub
    Public Sub create_interval(d As Dog)
        Dim intervalSize As Double = 5

        If listOfIntervals.Count = 0 Then
            inizialized_list(intervalSize)
        End If


        Dim dogAlloc As Boolean = False

        For Each i In listOfIntervals
            If interval_Correct(d, i) Then
                i.count += 1
                dogAlloc = True
                Exit For
            End If
        Next
        If dogAlloc Then
            Exit Sub
        End If
        If interval_isLeft(d) Then

            newLeftInterval(intervalSize, d)

        ElseIf interval_isRight(d) Then
            newRightInterval(intervalSize, d)
        End If
    End Sub

    Public Sub newLeftInterval(intervalSize As Integer, d As Dog)

        Do
            Dim newLeftIn As New Interval
            newLeftIn.upperEnd = listOfIntervals(0).lowerEnd
            newLeftIn.lowerEnd = newLeftIn.upperEnd - intervalSize

            listOfIntervals.Insert(0, newLeftIn)

            If interval_Correct(d, newLeftIn) Then
                newLeftIn.count += 1
                Exit Do
            End If
        Loop
    End Sub

    Public Sub newRightInterval(intervalSize As Integer, d As Dog)
        Do
            Dim newRightIn As New Interval
            newRightIn.lowerEnd = listOfIntervals(listOfIntervals.Count - 1).upperEnd
            newRightIn.upperEnd = newRightIn.lowerEnd + intervalSize
            listOfIntervals.Add(newRightIn)

            If interval_Correct(d, newRightIn) Then
                newRightIn.count += 1
                Exit Do
            End If
        Loop
    End Sub
End Class

Dog.vb

Public Class Dog
    Public Breed As String
    Public Age As Integer
    Public Time As Double
    Public Weight As Double

    Public Overrides Function ToString() As String
        Return Breed & " " & Age & " " & Weight & " " & Time
    End Function
End Class

Interval.vb

Public Class Interval
    Public lowerEnd As Double
    Public upperEnd As Double
    Public count As Integer
    Public relativeFreq As Double
    Public percentage As Double
End Class

Lascia un commento