Public Class Form2
    Public Pole(Uzke_misto.PocetZakazek - 1) As Integer
    Public txtOutput As String
    Public Vysledne_pole(100) As Integer
    Public pom_pole(100) As Single
    Public zpozdeni_zakazek(100) As Single
    Public min As Single
    Public prace_stroje As Short
    Public Shared Vyroba_zakazky(Uzke_misto.PocetZakazek - 1) As Single
    Public Shared Termin_zakazky(Uzke_misto.PocetZakazek - 1) As Single
    Public PrvniReseni, FirstTime As Boolean
    Public Den_vyhotoveni As Single
    Public celkovy_presecas As Single
    Public minimum As Single
    Dim radek, sloupec As Integer
    Dim zadani_hodnot As Boolean
    Public Dynamicke_pole(,) As Single
    Public rozmer As Long
    Dim Graf_only, Optim_only As Boolean
    Dim TSP_pole(Uzke_misto.PocetZakazek - 1, 1) As Single
    Dim PPole(Uzke_misto.PocetZakazek - 1) As Integer
    Dim Soucet_preserizeni_min As Single
    Dim pole_zakazek(Uzke_misto.PocetZakazek - 1) As Zakazka
    Shared Pocet_zakazek As Integer
    Private _rnd As New Random()

    Private Structure Zakazka
        Public jmeno As Integer
        Public termin As Single
        Public vyrobni_doba As Single
        Public rozdil As Single
        Public zpozdeni As Single
    End Structure

    Public Pole_vysledku(,) As Single


    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Graf_only = False
        PrvniReseni = True
        Optim_only = False
        'Dim Poloha As Point
        'Dim Velikost As Size
        'Poloha.X = 100
        'Poloha.Y = 100
        'Velikost.Height = 900
        'Velikost.Width = 900
        'Kombinace_zakazek.Location = Poloha
        'Kombinace_zakazek.Size = Velikost

        Pocet_zakazek = Uzke_misto.PocetZakazek
        If Pocet_zakazek < 8 Then


            rozmer = Faktorial(Uzke_misto.PocetZakazek)
            ReDim Pole_vysledku(rozmer - 1, Uzke_misto.PocetZakazek)
            ReDim Dynamicke_pole(rozmer - 1, Uzke_misto.PocetZakazek)


        End If
        minimum = 0
        GetDobaVyroby()
        Min_cas()



    End Sub


    Private Sub Min_cas()
        Dim i, j As Integer
        Dim pom As Integer
        'Dim text As String
        'text = "vysledek" & Environment.NewLine

        pom = 1

        If Uzke_misto.PocetZakazek < 8 Then


            txtOutput = "Kombinace Zakázek:" & Environment.NewLine

            Vysledne_pole(1) = 1
            radek = 0
            sloupec = 0
            Kombinace(1, Uzke_misto.PocetZakazek)

            'For i = 0 To Faktorial(Uzke_misto.PocetZakazek) - 1
            '    For j = 0 To Uzke_misto.PocetZakazek
            '        text &= Convert.ToString(Pole_vysledku(i, j)) & " "
            '    Next
            '    text &= Environment.NewLine
            'Next


            txtOutput &= Environment.NewLine & "Idealní pořadí zakázek je: " & Environment.NewLine
            For i = 1 To Uzke_misto.PocetZakazek
                txtOutput &= pom_pole(i) & ". "
            Next
            txtOutput &= Environment.NewLine & Environment.NewLine & "Celkový minimální přeseřizovací čas je " & min & " min." & Environment.NewLine
        Else
            Spocitat_nejkratsi_preserizeni()
            Usporadat_TSP_pole()
            txtOutput &= Environment.NewLine & "Min čas 2.možnost " & Soucet_preserizeni_min & Environment.NewLine
            For i = 0 To Uzke_misto.PocetZakazek - 1
                txtOutput &= PPole(i) & " "
                'txtOutput &= TSP_pole(i, 0) & " "
            Next


        End If
        Kombinace_zakazek.Text = txtOutput

    End Sub


    Private Sub Kombinace(Pocatek As Integer, Pocet_Druhu As Integer)
        Dim i, j As Integer
        Dim soucet As Single
        Dim first As Boolean



        Do
            Vysledne_pole(Pocatek + 1) = 1
            If (Pocatek + 1) < Pocet_Druhu Then
                Kombinace(Pocatek + 1, Pocet_Druhu)
            Else
                Do
                    For i = 1 To Pocet_Druhu
                        For j = i + 1 To Pocet_Druhu
                            If Vysledne_pole(i) = Vysledne_pole(j) Then
                                GoTo Skok1
                            End If



                        Next
                    Next
                    soucet = 0
                    For i = 1 To Pocet_Druhu
                        txtOutput &= Vysledne_pole(i) & " "
                        Pole_vysledku(radek, sloupec) = Vysledne_pole(i)
                        sloupec += 1
                    Next

                    For i = 1 To Pocet_Druhu - 1
                        soucet += Uzke_misto.MaticePreserizeni(Vysledne_pole(i) - 1, Vysledne_pole(i + 1) - 1)
                    Next

                    first = True
                    For i = 1 To Pocet_Druhu
                        If Vysledne_pole(i) <> i Then
                            first = False
                        End If
                    Next
                    If first Then
                        min = soucet
                        For i = 1 To Pocet_Druhu
                            pom_pole(i) = Vysledne_pole(i)
                        Next
                    ElseIf soucet < min Then
                        min = soucet
                        For i = 1 To Pocet_Druhu
                            pom_pole(i) = Vysledne_pole(i)
                        Next

                    End If
                    txtOutput &= " = " & soucet
                    txtOutput &= Environment.NewLine
                    sloupec = 0
                    Pole_vysledku(radek, Pocet_Druhu) = soucet
                    radek += 1
Skok1:
                    Vysledne_pole(Pocatek + 1) += 1

                Loop Until Vysledne_pole(Pocatek + 1) > Pocet_Druhu
            End If

            Vysledne_pole(Pocatek) += 1
        Loop Until Vysledne_pole(Pocatek) > Pocet_Druhu


    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If IsNumeric(Stroj_pracdoba.Text) = False Then
            MsgBox("Neplatná hodnota!")
            Exit Sub
        Else
            prace_stroje = Convert.ToInt16(Stroj_pracdoba.Text)
            If prace_stroje <= 0 OrElse prace_stroje > 24 Then
                MsgBox("Neplatná hodnota!")
                Exit Sub
            Else
                GetDobaVyroby()
            End If
        End If
    End Sub


    Private Sub GetDobaVyroby()
        Dim i As Integer

        DobaVyroby.Enabled = True
        DobaVyroby.Visible = True
        'Doba_termin_OK.Enabled = True
        'Doba_termin_OK.Visible = True
        DobaVyroby.Columns.Add(1, "Zakázka")
        DobaVyroby.Columns.Add(2, "Doba výroby zakázky [h]")
        DobaVyroby.Columns.Add(3, "Termín vyhotovení zakázky [h]")
        DobaVyroby.Columns(0).Width = 120
        For i = 1 To Uzke_misto.PocetZakazek
            DobaVyroby.Rows.Add()
        Next

        For i = 0 To Uzke_misto.PocetZakazek - 1
            DobaVyroby.Rows(i).Cells(0).Value = "Zakázka číslo " & (i + 1)
        Next



    End Sub

    Private Sub Doba_termin_OK_Click(sender As Object, e As EventArgs) Handles Doba_termin_OK.Click
        Dim i As Integer
        Dim termin, vyroba As Double
        Dim Text As String
        Text = "Vysledek" & Environment.NewLine

        For i = 1 To Uzke_misto.PocetZakazek
            termin = DobaVyroby.Rows(i - 1).Cells(2).Value
            vyroba = DobaVyroby.Rows(i - 1).Cells(1).Value

            If IsNumeric(termin) And IsNumeric(vyroba) And termin <> 0 And vyroba <> 0 Then
                Vyroba_zakazky(i - 1) = Convert.ToSingle(vyroba)
                Termin_zakazky(i - 1) = Convert.ToSingle(termin)
            Else
                MsgBox("Neplatná hodnota! Zadávajte pouze čísla.")
                zadani_hodnot = False
                Exit Sub
            End If
        Next
        zadani_hodnot = True
        If Graf_only Then
            Dynamicka_mezera()
            Exit Sub
        ElseIf Optim_only Then
            Vytvoreni_objektu_zakazek()
            Exit Sub
        End If

        'For i = 0 To Faktorial(Uzke_misto.PocetZakazek) - 1
        '    Text &= Dynamicke_pole(i) & Environment.NewLine
        'Next
        'MsgBox(Text)

        Splnitelnost()

    End Sub

    Private Sub Splnitelnost()
        Dim i As Short
        Dim upraveni As Boolean

        upraveni = False


        For i = 0 To Uzke_misto.PocetZakazek - 1
            If Vyroba_zakazky(i) > Termin_zakazky(i) Then
                MsgBox((i + 1) & ". zakázku nelze vyhotovit v požadovaném termínu!")
                upraveni = True


            End If
        Next
        If upraveni Then
            Upraveni_zakazek()
        Else
            Vypocitat_cas_vyhotoveni()
        End If



    End Sub

    Private Sub Upraveni_zakazek()
        Dim ret As String

        ret = "(Při zvolení možnosti NE se zakázky uspořádají tak, aby zpoždění bylo co nejmenší.)"

        Dim result As Integer = MessageBox.Show("Chcete zadat termíny vyhotovení zakázek znovu? " & ret, "Upozornění", MessageBoxButtons.YesNo)

        If result = DialogResult.Yes Then
            Exit Sub
        ElseIf result = DialogResult.No Then
            Minimalizace()
        End If

    End Sub

    Private Sub Minimalizace()
        Dim index As Integer
        Dim i As Integer


        index = 1
        Vysledne_pole(index) = index



        FirstTime = True
        Vypocet_minzpozdeni(index)
        Tisk_Usporadani()

        celkovy_presecas = 0
        For i = 1 To Uzke_misto.PocetZakazek - 1
            celkovy_presecas += Uzke_misto.MaticePreserizeni(pom_pole(i) - 1, pom_pole(i + 1) - 1)
        Next
        txtOutput &= Environment.NewLine & Environment.NewLine & "Celkový přeseřizovací čas mezi nově uspořádanými zakázkami je " & celkovy_presecas

        Kombinace_zakazek.Text = txtOutput

    End Sub

    Private Sub Vypocitat_cas_vyhotoveni()
        Dim i As Integer
        Dim preuspor As Boolean

        preuspor = False
        Dim cas_preserizeni, cas, cas_predani As Single

        For i = 1 To Uzke_misto.PocetZakazek
            If i = 1 Then
                cas_preserizeni = 0
            Else
                cas_preserizeni = Uzke_misto.MaticePreserizeni(pom_pole(i - 1) - 1, pom_pole(i) - 1) / 60
            End If
            cas += cas_preserizeni + Vyroba_zakazky(pom_pole(i) - 1)
            cas_predani = cas

            If cas_predani > Termin_zakazky(pom_pole(i) - 1) Then
                '*Tisk_Usporadani()
                'Kombinace_zakazek.Text = txtOutput
                preuspor = True
            End If
        Next
        If preuspor Then
            Preusporadani()
        Else
            Tisk_Usporadani()
        End If
        '**Tisk_Usporadani()
        Kombinace_zakazek.Text = txtOutput

    End Sub

    Private Sub Preusporadani()
        ' MsgBox("__Preusporadani")
        MsgBox("Pro dodržení termínu musí být zakázky přeskládány.")
        Vysledne_pole(1) = 1
        PrvniReseni = True

        NovePoradiZakazek(1)

        Preusporadani2()

    End Sub

    Private Sub Preusporadani2()
        Dim i As Integer



        If PrvniReseni = False Then
            Tisk_Usporadani()

            celkovy_presecas = 0
            For i = 1 To Uzke_misto.PocetZakazek - 1
                celkovy_presecas += Uzke_misto.MaticePreserizeni(pom_pole(i) - 1, pom_pole(i + 1) - 1)
            Next
            txtOutput &= Environment.NewLine & "Celkový přeseřizovací čas mezi nově uspořádanými zakázkami je " & celkovy_presecas & " min." & Environment.NewLine

        ElseIf PrvniReseni Then
            MsgBox("Zakázky nelze uspořádat tak, aby byly všechny včas hotovy!")
            Dotaz_upraveni_terminu.ShowDialog()
            If Dotaz_upraveni_terminu.rozhodnuti Then
                Upraveni_zakazek()
            Else
                Minimalizace()
            End If


        End If
        'Kombinace_zakazek.Text = txtOutput

    End Sub

    Private Sub NovePoradiZakazek(index As Integer)
        Dim i, j As Integer
        Dim cas_preserizeni, cas As Single
        Dim pom As Single


        Do
            Vysledne_pole(index + 1) = 1
            If (index + 1) < Uzke_misto.PocetZakazek Then
                NovePoradiZakazek(index + 1)
            Else
                Do
                    For i = 1 To Uzke_misto.PocetZakazek
                        For j = i + 1 To Uzke_misto.PocetZakazek
                            If Vysledne_pole(i) = Vysledne_pole(j) Then
                                GoTo Skok2
                            End If
                        Next
                    Next
                    cas = 0

                    For i = 1 To Uzke_misto.PocetZakazek
                        If i = 1 Then
                            cas_preserizeni = 0
                        Else
                            cas_preserizeni = Uzke_misto.MaticePreserizeni(Vysledne_pole(i - 1) - 1, Vysledne_pole(i) - 1) / 60
                        End If
                        cas += cas_preserizeni + Vyroba_zakazky(Vysledne_pole(i) - 1)
                        Den_vyhotoveni = cas
                        If Den_vyhotoveni > Termin_zakazky(Vysledne_pole(i) - 1) Then
                            GoTo Skok2
                        End If

                    Next

                    If PrvniReseni Then
                        pom = Den_vyhotoveni
                        For i = 1 To Uzke_misto.PocetZakazek
                            pom_pole(i) = Vysledne_pole(i)
                        Next
                        PrvniReseni = False
                    ElseIf Den_vyhotoveni < pom Then
                        pom = Den_vyhotoveni
                        For i = 1 To Uzke_misto.PocetZakazek
                            pom_pole(i) = Vysledne_pole(i)
                        Next
                    End If

Skok2:
                    Vysledne_pole(index + 1) += +1

                Loop Until Vysledne_pole(index + 1) > Uzke_misto.PocetZakazek

            End If
            Vysledne_pole(index) += 1

        Loop Until Vysledne_pole(index) > Uzke_misto.PocetZakazek



    End Sub

    Private Sub Tisk_Usporadani()
        Dim i As Integer
        Dim Predchozi_cas, cas_preserizeni, celkovy_cas, hotova, zpozdeni As Single


        txtOutput &= Environment.NewLine & Environment.NewLine & "Pořadí a čas vyhotovení zakázek: " & Environment.NewLine
        Predchozi_cas = 0
        For i = 1 To Uzke_misto.PocetZakazek
            If i = 1 Then
                cas_preserizeni = 0
            Else
                cas_preserizeni = Uzke_misto.MaticePreserizeni(pom_pole(i - 1) - 1, pom_pole(i) - 1) / 60
            End If
            'celkovy_cas += cas_preserizeni + Vyroba_zakazky(pom_pole(i) - 1)
            Predchozi_cas += cas_preserizeni + Vyroba_zakazky(pom_pole(i) - 1)
            'hotova = celkovy_cas / prace_stroje
            hotova = Predchozi_cas / prace_stroje
            txtOutput &= Environment.NewLine & pom_pole(i) & ". zakázka hotová za " & hotova & " dne, požadována " & Termin_zakazky(pom_pole(i) - 1) & " den"

            If hotova > Termin_zakazky(pom_pole(i) - 1) Then
                zpozdeni = hotova - Termin_zakazky(pom_pole(i) - 1)
                txtOutput &= " , zpoždění " & zpozdeni & " dne"
            End If
        Next

    End Sub

    Private Sub Vypocet_minzpozdeni(index As Integer)
        Dim i, j As Integer
        Dim soucet, zpozdeni, preserizeni, cas_davek As Single

        Do
            Vysledne_pole(index + 1) = 1
            If (index + 1) < Uzke_misto.PocetZakazek Then
                Vypocet_minzpozdeni(index + 1)
            Else
                Do
                    For i = 1 To Uzke_misto.PocetZakazek
                        For j = i + 1 To Uzke_misto.PocetZakazek
                            If Vysledne_pole(i) = Vysledne_pole(j) Then
                                GoTo Skok3
                            End If
                        Next

                    Next
                    soucet = 0
                    zpozdeni = 0
                    For i = 1 To Uzke_misto.PocetZakazek
                        If i = 1 Then
                            preserizeni = 0
                        Else
                            preserizeni = Uzke_misto.MaticePreserizeni(Vysledne_pole(i - 1) - 1, Vysledne_pole(i) - 1) / 60
                        End If
                        soucet += preserizeni + Vyroba_zakazky(Vysledne_pole(i) - 1)
                        cas_davek = soucet
                        zpozdeni_zakazek(Vysledne_pole(i)) = cas_davek - Termin_zakazky(Vysledne_pole(i) - 1)
                        If zpozdeni_zakazek(Vysledne_pole(i)) < 0 Then
                            zpozdeni_zakazek(Vysledne_pole(i)) = 0
                        End If
                    Next

                    If FirstTime Then
                        FirstTime = False
                        For i = 1 To Uzke_misto.PocetZakazek
                            minimum += zpozdeni_zakazek(Vysledne_pole(i))
                        Next

                    Else
                        For i = 1 To Uzke_misto.PocetZakazek
                            zpozdeni += zpozdeni_zakazek(Vysledne_pole(i))
                        Next

                        If zpozdeni > minimum Then
                            GoTo Skok3
                        End If
                        minimum = zpozdeni
                        For i = 1 To Uzke_misto.PocetZakazek
                            pom_pole(i) = Vysledne_pole(i)
                        Next
                    End If

Skok3:
                    Vysledne_pole(index + 1) = Vysledne_pole(index + 1) + 1

                Loop Until Vysledne_pole(index + 1) > Uzke_misto.PocetZakazek
            End If

            Vysledne_pole(index) = Vysledne_pole(index) + 1
        Loop Until Vysledne_pole(index) > Uzke_misto.PocetZakazek

    End Sub

    Private Sub Graf_show_button_Click(sender As Object, e As EventArgs) Handles Graf_show_button.Click
        Dim velikost1, velikost2 As Size
        Graf_only = True
        Doba_termin_OK_Click(sender, e)
        If zadani_hodnot = False Then
            Exit Sub
        End If
        velikost1.Height = 800
        velikost1.Width = 1050
        Graf.Size = velikost1
        velikost2.Height = 700
        velikost2.Width = 1000
        Graf.Graf1.Size = velikost2
        Graf.Show()

    End Sub

    Private Function Faktorial(cislo As Integer) As Long
        Dim i As Integer
        Dim vysledek As Long
        vysledek = 1

        For i = cislo To 1 Step -1
            vysledek = vysledek * i
        Next
        Faktorial = vysledek
    End Function

    Private Sub Dynamicka_mezera()
        Dim i As Integer

        Dim row As Integer
        Dim celkova_mezera As Single
        Dim doba_predesle, doba_aktualni As Single
        row = 0

        Dim cas_preserizeni, cas, cas_predani As Single
        For row = 0 To Faktorial(Uzke_misto.PocetZakazek) - 1
            cas = 0
            celkova_mezera = 0
            doba_predesle = 0
            For i = 1 To Uzke_misto.PocetZakazek
                doba_aktualni = 0
                If i = 1 Then
                    cas_preserizeni = 0
                Else
                    cas_preserizeni = Uzke_misto.MaticePreserizeni(Pole_vysledku(row, i - 2) - 1, Pole_vysledku(row, i - 1) - 1) / 60

                End If
                doba_aktualni = (Vyroba_zakazky(Pole_vysledku(row, i - 1) - 1)) + (cas_preserizeni)
                'MsgBox("aktualni doba = " & doba_aktualni)
                cas += cas_preserizeni + Vyroba_zakazky(Pole_vysledku(row, i - 1) - 1)
                Dynamicke_pole(row, i - 1) = doba_aktualni + doba_predesle - Termin_zakazky(Pole_vysledku(row, i - 1) - 1)
                celkova_mezera += doba_aktualni + doba_predesle - Termin_zakazky(Pole_vysledku(row, i - 1) - 1)
                'MsgBox("celkova mezera = " & celkova_mezera)
                doba_predesle += doba_aktualni



            Next
            cas_predani = cas
            Dynamicke_pole(row, Uzke_misto.PocetZakazek) = celkova_mezera
        Next

    End Sub

    Private Sub Spocitat_nejkratsi_preserizeni()
        Dim i, j, prvek As Integer
        Dim min As Single
        TSP_pole(0, 0) = 1
        For i = 0 To Uzke_misto.PocetZakazek - 2
            'MsgBox("i = " & i)
            min = 1000000
            For j = 0 To Uzke_misto.PocetZakazek - 1
                If Not (TSP_pole(i, 0) - 1) = j Then
                    If Uzke_misto.MaticePreserizeni(TSP_pole(i, 0) - 1, j) < min Then
                        If Kontrola_TSP_pole(j + 1) = False Then
                            min = Uzke_misto.MaticePreserizeni(TSP_pole(i, 0) - 1, j)
                            prvek = j + 1
                        End If

                    End If
                End If
            Next
            'MsgBox("Min = " & min)
            TSP_pole(i + 1, 0) = prvek
            TSP_pole(i, 1) = min



        Next
        TSP_pole(Uzke_misto.PocetZakazek - 1, 1) = Uzke_misto.MaticePreserizeni(TSP_pole(Uzke_misto.PocetZakazek - 1, 0) - 1, 0)
    End Sub

    Private Function Kontrola_TSP_pole(cislo As Integer) As Boolean
        Dim i As Integer
        For i = 0 To Uzke_misto.PocetZakazek - 1
            If cislo = TSP_pole(i, 0) Then
                Kontrola_TSP_pole = True         'Pokud cislo uz je obsazene v poli vrati True
                Exit Function
            End If
        Next
        Kontrola_TSP_pole = False            'Pokud pole zadane cislo neobsahuje vrati False
    End Function


    Private Sub Usporadat_TSP_pole()
        Dim i, clen As Integer
        Dim max As Single
        max = 0
        Soucet_preserizeni_min = 0
        For i = 0 To Uzke_misto.PocetZakazek - 1
            Soucet_preserizeni_min += TSP_pole(i, 1)
            If TSP_pole(i, 1) > max Then
                max = TSP_pole(i, 1)
                clen = i
            End If
        Next
        Soucet_preserizeni_min -= max
        For i = 0 To Uzke_misto.PocetZakazek - 1
            If (clen + i + 1) > Uzke_misto.PocetZakazek - 1 Then
                PPole(i) = TSP_pole(clen + i + 1 - Uzke_misto.PocetZakazek, 0)
            Else
                PPole(i) = TSP_pole(clen + i + 1, 0)
            End If

        Next
    End Sub

    Private Sub Button_optimalizace_Click(sender As Object, e As EventArgs) Handles Button_optimalizace.Click
        Dim i As Short
        Optim_only = True
        Doba_termin_OK_Click(sender, e)
        If zadani_hodnot = False Then
            Exit Sub
        End If
        'For i = 0 To Pocet_zakazek
        '    Optimalizace_uzkeho_mista()
        'Next
        Optimalizace.Show()
    End Sub

    Private Sub Optimalizace_uzkeho_mista()
        Dim i, citac As Integer
        Dim max, min As Single
        Dim startovni_prvek As Integer
        Dim prubezna_doba As Single
        Dim total_delay As Single
        Dim list_of_used_nodes As New List(Of Zakazka)
        Dim pom As Integer
        Dim dobavyr, bestdobavyr As Single
        Dim Node As Zakazka
        Dim krit_fce As Single
        Dim vaha1, vaha2, preserizeni As Single
        Dim nahoda, interval As Single
        Dim celkovezpozdenip, celkovezpozdeni As Single
        Dim koeficient_nahody As Integer


        celkovezpozdeni = 0
        nahoda = 0.75
        vaha1 = 0
        vaha2 = 1
        prubezna_doba = 0
        max = -10000
        For i = 0 To Pocet_zakazek - 1
            'koeficient_nahody = (1 - ((nahoda) * Rnd())) * 100
            koeficient_nahody = RandomNumber((1 - nahoda) * 100, 100)
            MsgBox("koef = " & (pole_zakazek(i).rozdil * koeficient_nahody))
            If (pole_zakazek(i).rozdil * koeficient_nahody) > max Then
                max = pole_zakazek(i).rozdil * koeficient_nahody
                startovni_prvek = pole_zakazek(i).jmeno

            End If
        Next

        interval = nahoda / Pocet_zakazek
        Node = pole_zakazek(startovni_prvek - 1)
        If Node.rozdil >= 0 Then
            celkovezpozdeni += Node.rozdil
        End If

        'MsgBox("Add First = " & pole_zakazek(startovni_prvek - 1).jmeno)
        'list_of_used_nodes.Add(pole_zakazek(startovni_prvek - 1))
        list_of_used_nodes.Add(Node)
        prubezna_doba = pole_zakazek(startovni_prvek - 1).vyrobni_doba / prace_stroje
        pole_zakazek(startovni_prvek - 1).zpozdeni = prubezna_doba - (pole_zakazek(startovni_prvek - 1).termin * prace_stroje)
        total_delay = pole_zakazek(startovni_prvek - 1).zpozdeni

        'MsgBox("zpozdeni start = " & celkovezpozdeni)
        citac = 1
        Do
            min = 10000
            For Each Item In pole_zakazek
                'MsgBox("Item = " & Item.jmeno)
                'MsgBox("Contains = " & list_of_used_nodes.Any(Function(Zakazka) Zakazka.jmeno = Item.jmeno))
                If Not list_of_used_nodes.Any(Function(Zakazka) Zakazka.jmeno = Item.jmeno) Then
                    preserizeni = (Uzke_misto.MaticePreserizeni(list_of_used_nodes.Last.jmeno - 1, Item.jmeno - 1) / 1440)
                    dobavyr = preserizeni + (Item.vyrobni_doba)


                    'If (prubezna_doba + dobavyr - Item.termin) < 0 Then
                    '    Item.zpozdeni = 0
                    'Else
                    Item.zpozdeni = prubezna_doba + dobavyr - Item.termin
                    'End If



                    'MsgBox("Jmeno = " & Item.jmeno)
                    'MsgBox("Zpozdeni = " & Item.zpozdeni)
                    krit_fce = (vaha1 * preserizeni) + (vaha2 * Item.zpozdeni)
                    'If Item.zpozdeni < min Then
                    '    min = Item.zpozdeni
                    '    pom = Item.jmeno
                    '    bestdobavyr = dobavyr
                    'End If
                    'koeficient_nahody = (1 - ((nahoda - (interval * citac)) * Rnd())) * 100
                    koeficient_nahody = RandomNumber(1 - (nahoda - (interval * citac)) * 100, 100)

                    If krit_fce * koeficient_nahody < min Then
                        min = krit_fce * koeficient_nahody
                        pom = Item.jmeno
                        bestdobavyr = dobavyr
                        If Item.zpozdeni < 0 Then
                            celkovezpozdenip = 0
                        Else
                            celkovezpozdenip = Item.zpozdeni
                        End If

                    End If
                End If

            Next
            'MsgBox("pom = " & pom)
            'MsgBox("Add = " & pole_zakazek(pom - 1).jmeno)
            list_of_used_nodes.Add(pole_zakazek(pom - 1))
            prubezna_doba += bestdobavyr
            celkovezpozdeni += celkovezpozdenip
            'MsgBox("zpozdeni = " & celkovezpozdeni)
            citac += 1
        Loop Until list_of_used_nodes.Count = pole_zakazek.Count

        For Each ite In list_of_used_nodes
            MsgBox(ite.jmeno)
        Next
        MsgBox("Celk zpo = " & celkovezpozdeni)
    End Sub

    Private Sub Vytvoreni_objektu_zakazek()

        Dim i As Integer
        Dim pom_rozdil As Single

        For i = 0 To Pocet_zakazek - 1
            pole_zakazek(i).jmeno = (i + 1)
            pole_zakazek(i).termin = Termin_zakazky(i)
            pole_zakazek(i).vyrobni_doba = Vyroba_zakazky(i)
            pom_rozdil = (pole_zakazek(i).vyrobni_doba) - pole_zakazek(i).termin
            'If pom_rozdil >= 0 Then
            pole_zakazek(i).rozdil = pom_rozdil
            'ElseIf pom_rozdil < 0 Then
            '    pole_zakazek(i).rozdil = 0
            'End If

        Next
    End Sub

    Function RandomNumber(ByVal low As Integer, ByVal high As Integer) As Integer
        Return _rnd.Next(low, high)
    End Function
End Class


