Module Optimalizace_záloha
    'Public Class Optimalizace
    '    Private _rnd As New Random()
    '    Dim Kombinace() As Integer
    '    Dim PocetZakazek As Integer
    '    Dim Pole_nazvu(,) As Integer
    '    Dim PocetPopulace As Integer
    '    Dim potomek1(), potomek2() As Integer
    '    Dim mutant() As Integer
    '    Dim konecGA As Boolean
    '    Dim zpozdeni_pole() As Single
    '    Dim vahap, vahaz As Single
    '    Dim Pocet_kroku As Integer
    '    Dim pocet_nejlepsich As Integer


    '    Private Structure Prvek
    '        Public jmeno() As Integer
    '        Public kritfce As Single
    '    End Structure
    '    Dim Populace As New List(Of Prvek)
    '    Dim Populace_new As New List(Of Prvek)

    '    Private Sub Start_optim_Click(sender As Object, e As EventArgs) Handles Start_optim.Click
    '        Dim i, j As Integer
    '        Dim txt As String
    '        Populace.Clear()
    '        Populace_new.Clear()
    '        vahap = vaha_preserizeni.Text
    '        vahaz = vaha_zpozdeni.Text
    '        Pocet_kroku = Kroky_textbox.Text
    '        PocetPopulace = Populace_textbox.Text
    '        Generovat_nahodnou_populaci(PocetPopulace)
    '        Vytvorit_objekty_populace()
    '        Seradit_pole_populace()
    '        For j = 0 To PocetPopulace - 1
    '            For i = 0 To PocetZakazek - 1
    '                txt &= Populace(j).jmeno(i) & " "
    '            Next
    '            txt &= " * " & Spocitat_celkove_zpozdeni(Populace(j).jmeno) & " * "
    '            txt &= Populace(j).kritfce & Environment.NewLine
    '        Next


    '        Run_genetic_algorithm()

    '        txt &= Environment.NewLine & "GA" & Environment.NewLine

    '        If konecGA Then
    '            Naplnit_tabulku(Populace_new)
    '            For j = 0 To PocetPopulace - 1
    '                For i = 0 To PocetZakazek - 1
    '                    txt &= Populace_new(j).jmeno(i) & " . "
    '                Next
    '                txt &= Populace_new(j).kritfce & Environment.NewLine
    '            Next
    '        Else
    '            Naplnit_tabulku(Populace)
    '            For j = 0 To PocetPopulace - 1
    '                For i = 0 To PocetZakazek - 1
    '                    txt &= Populace(j).jmeno(i) & " * "
    '                Next
    '                txt &= Populace(j).kritfce & Environment.NewLine
    '            Next
    '        End If

    '        'Output_textbox.Text &= Spocitat_hodnotu_kritfce({2, 1, 4, 5, 3}) & Environment.NewLine
    '        'Output_textbox.Text &= Spocitat_hodnotu_kritfce({2, 1, 5, 4, 3}) & Environment.NewLine
    '        'Output_textbox.Text &= Spocitat_hodnotu_kritfce({1, 4, 2, 3, 5}) & Environment.NewLine
    '        'Output_textbox.Text &= Spocitat_hodnotu_kritfce({1, 3, 4, 5, 2}) & Environment.NewLine
    '        Output_textbox.Text = txt

    '    End Sub

    '    Private Sub Optimalizace_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    '        pocet_nejlepsich = 0
    '        PocetZakazek = Uzke_misto.PocetZakazek
    '        ReDim Kombinace(PocetZakazek - 1)
    '    End Sub

    '    Private Sub Generovat_nahodnou_populaci(Pocet As Short)
    '        Dim i, j, k As Integer
    '        Dim pom As Integer
    '        Dim konec As Boolean

    '        ReDim Pole_nazvu(Pocet - 1, PocetZakazek - 1)

    '        For k = 0 To Pocet - 1
    '            ReDim Kombinace(PocetZakazek - 1)
    '            For i = 0 To PocetZakazek - 1
    '                Do
    '                    konec = True
    '                    pom = RandomNumber(1, PocetZakazek + 1)
    '                    For j = 0 To i
    '                        If pom = Kombinace(j) Then
    '                            konec = False
    '                            Exit For
    '                        End If
    '                    Next
    '                Loop Until konec
    '                Kombinace(i) = pom
    '                Pole_nazvu(k, i) = pom
    '            Next
    '        Next
    '    End Sub

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

    '    Private Function Spocitat_hodnotu_kritfce(kombi() As Integer) As Single
    '        Dim preserizeni, zpozdeni As Single
    '        preserizeni = Spocitat_dobu_preserizeni(kombi) / 60
    '        zpozdeni = Spocitat_celkove_zpozdeni(kombi)
    '        Spocitat_hodnotu_kritfce = (vahap * preserizeni) + (vahaz * zpozdeni)
    '    End Function

    '    Private Function Spocitat_dobu_preserizeni(kombi() As Integer) As Single
    '        Dim i As Integer
    '        Dim doba_preserizeni As Single

    '        doba_preserizeni = 0
    '        For i = 0 To kombi.Length - 2
    '            doba_preserizeni += Uzke_misto.MaticePreserizeni(kombi(i) - 1, kombi(i + 1) - 1)
    '        Next
    '        Spocitat_dobu_preserizeni = doba_preserizeni
    '    End Function

    '    Private Function Spocitat_celkove_zpozdeni(kombi() As Integer) As Single
    '        Dim i As Integer
    '        Dim doba As Single
    '        Dim pzpozdeni As Single
    '        Dim celkovezpozdeni As Single
    '        Dim preserizeni As Single

    '        ReDim zpozdeni_pole(kombi.Length - 1)
    '        For i = 0 To kombi.Length - 1
    '            zpozdeni_pole(i) = 0
    '        Next
    '        doba = Form2.Vyroba_zakazky(kombi(0) - 1)
    '        For i = 0 To kombi.Length - 1
    '            pzpozdeni = doba - Form2.Termin_zakazky(kombi(i) - 1)
    '            If pzpozdeni < 0 Then
    '                celkovezpozdeni += 0
    '                zpozdeni_pole(i) = pzpozdeni
    '            Else
    '                celkovezpozdeni += pzpozdeni
    '                zpozdeni_pole(i) = pzpozdeni
    '            End If
    '            If i <> (kombi.Length - 1) Then
    '                preserizeni = Uzke_misto.MaticePreserizeni(kombi(i) - 1, kombi(i + 1) - 1) / 60
    '                doba += preserizeni + Form2.Vyroba_zakazky(kombi(i + 1) - 1)
    '            End If

    '        Next
    '        Spocitat_celkove_zpozdeni = celkovezpozdeni
    '    End Function

    '    Private Sub Vytvorit_objekty_populace()
    '        Dim Node As New Prvek
    '        Dim i, j As Integer
    '        Dim pompole(PocetZakazek - 1) As Integer


    '        For i = 0 To PocetPopulace - 1
    '            ReDim Node.jmeno(PocetZakazek - 1)
    '            For j = 0 To PocetZakazek - 1
    '                Node.jmeno(j) = Pole_nazvu(i, j)
    '                pompole(j) = Pole_nazvu(i, j)
    '            Next
    '            Node.kritfce = Spocitat_hodnotu_kritfce(pompole)

    '            Populace.Add(Node)

    '        Next

    '    End Sub

    '    Private Sub Seradit_pole_populace()
    '        Populace.Sort(Function(x, y) x.kritfce.CompareTo(y.kritfce))
    '        Populace_new.Sort(Function(x, y) x.kritfce.CompareTo(y.kritfce))
    '        'Populace = Populace.OrderBy(Function(x) x.kritfce).ToList()
    '    End Sub

    '    Private Sub Krizeni(Uzel1 As Prvek, Uzel2 As Prvek)
    '        Dim rez1, rez2 As Integer
    '        Dim i, j As Integer

    '        rez1 = RandomNumber(0, PocetZakazek - 3)
    '        Do
    '            rez2 = RandomNumber(rez1 + 2, PocetZakazek)
    '        Loop Until (rez2 - rez1) <= CInt(PocetZakazek / 2)

    '        ReDim potomek1(PocetZakazek - 1)
    '        ReDim potomek2(PocetZakazek - 1)
    '        For i = 0 To PocetZakazek - 1
    '            potomek1(i) = 0
    '            potomek2(i) = 0
    '        Next
    '        For i = rez1 To rez2 - 1
    '            potomek1(i) = Uzel2.jmeno(i)
    '            potomek2(i) = Uzel1.jmeno(i)
    '        Next

    '        For i = 0 To PocetZakazek - 1
    '            If potomek1(i) = 0 Then
    '                If Not Retezec_obsahuje_prvek(Uzel1.jmeno(i), potomek1) Then        'pokud retezec neobsahuje cislo na i-te pozici rodice
    '                    potomek1(i) = Uzel1.jmeno(i)
    '                End If
    '            End If
    '            If potomek2(i) = 0 Then
    '                If Not Retezec_obsahuje_prvek(Uzel2.jmeno(i), potomek2) Then
    '                    potomek2(i) = Uzel2.jmeno(i)
    '                End If
    '            End If
    '        Next

    '        For i = 0 To PocetZakazek - 1
    '            If potomek1(i) = 0 Then
    '                For j = 1 To PocetZakazek
    '                    If Not Retezec_obsahuje_prvek(j, potomek1) Then
    '                        potomek1(i) = j
    '                    End If
    '                Next
    '            End If
    '            If potomek2(i) = 0 Then
    '                For j = 1 To PocetZakazek
    '                    If Not Retezec_obsahuje_prvek(j, potomek2) Then
    '                        potomek2(i) = j
    '                    End If
    '                Next
    '            End If
    '        Next

    '    End Sub

    '    Private Sub Mutace(Uzel As Prvek)
    '        Dim ZakazaneHodnoty() As Integer
    '        Dim i As Integer
    '        Dim vymena As Short
    '        Dim nahrada As Integer

    '        ReDim ZakazaneHodnoty(PocetZakazek - 1)
    '        ReDim mutant(PocetZakazek - 1)
    '        For i = 0 To PocetZakazek - 1
    '            ZakazaneHodnoty(i) = 0
    '            mutant(i) = 0
    '        Next

    '        For i = 0 To PocetZakazek - 1
    '            vymena = RandomNumber(0, 2)
    '            If vymena = 1 Then
    '                Do
    '                    nahrada = RandomNumber(1, PocetZakazek + 1)
    '                Loop Until Retezec_obsahuje_prvek(nahrada, ZakazaneHodnoty) = False
    '                mutant(i) = nahrada
    '            ElseIf vymena = 0 Then
    '                If Retezec_obsahuje_prvek(Uzel.jmeno(i), ZakazaneHodnoty) Then
    '                    Do
    '                        nahrada = RandomNumber(1, PocetZakazek + 1)
    '                    Loop Until Retezec_obsahuje_prvek(nahrada, ZakazaneHodnoty) = False
    '                    mutant(i) = nahrada
    '                Else
    '                    mutant(i) = Uzel.jmeno(i)
    '                End If

    '            End If
    '            ZakazaneHodnoty(i) = mutant(i)
    '        Next
    '    End Sub

    '    Private Function Retezec_obsahuje_prvek(cislo As Integer, Retezec() As Integer) As Boolean
    '        Dim i As Integer
    '        For i = 0 To Retezec.Length - 1
    '            If Retezec(i) = cislo Then
    '                Retezec_obsahuje_prvek = True
    '                Exit Function
    '            End If
    '        Next
    '        Retezec_obsahuje_prvek = False
    '    End Function

    '    Private Sub Button_save_best_Click(sender As Object, e As EventArgs) Handles Button_save_best.Click
    '        Dim i As Integer
    '        Dim pocetzakazek As Integer

    '        If Tabulka_reseni.Rows.Count = 0 Then
    '            MsgBox("Nejdříve je potřeba spustit genetický algoritmus a vytvořit tak konečnou populaci!")
    '            Exit Sub
    '        Else
    '            pocetzakazek = Tabulka_reseni.Columns.Count - 5
    '            If pocet_nejlepsich = 0 Then
    '                Tabulka_nejlepsich.Columns.Add("Jmeno", "Kombinace")
    '                Tabulka_nejlepsich.Columns.Add("Preserizeni", "Celkový přeseřizovací čas [min]")
    '                For i = 0 To pocetzakazek - 1
    '                    Tabulka_nejlepsich.Columns.Add("Zpozdeni" & i, "Zpoždění " & i + 1 & ". zakázky [h]")
    '                Next
    '                Tabulka_nejlepsich.Columns.Add("Dynamicka_mezera", "Celková dynamická mezera [h]")
    '                Tabulka_nejlepsich.Columns.Add("Soucet_zpozdeni", "Celkové zpoždění [h]")
    '                Tabulka_nejlepsich.Columns.Add("Kriterialni funkce", "Hodnota kriteriální fuknce [-]")
    '                Tabulka_nejlepsich.Rows.Add()
    '                For i = 0 To Tabulka_reseni.Columns.Count - 1
    '                    Tabulka_nejlepsich.Rows(pocet_nejlepsich).Cells(i).Value = Tabulka_reseni.Rows(0).Cells(i).Value
    '                Next
    '                Tabulka_nejlepsich.Rows(pocet_nejlepsich).HeaderCell.Value = pocet_nejlepsich + 1 & "."
    '            Else
    '                Tabulka_nejlepsich.Rows.Add()
    '                For i = 0 To Tabulka_reseni.Columns.Count - 1
    '                    Tabulka_nejlepsich.Rows(pocet_nejlepsich).Cells(i).Value = Tabulka_reseni.Rows(0).Cells(i).Value
    '                Next
    '                Tabulka_nejlepsich.Rows(pocet_nejlepsich).HeaderCell.Value = pocet_nejlepsich + 1 & "."
    '            End If
    '            pocet_nejlepsich += 1
    '        End If
    '    End Sub

    '    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    '        Tabulka_nejlepsich.Rows.Clear()
    '        Tabulka_nejlepsich.Columns.Clear()
    '        pocet_nejlepsich = 0
    '    End Sub

    '    Private Sub Run_genetic_algorithm()
    '        Dim Node, Node1, Node2 As New Prvek
    '        Dim i, j, konec, citac, pocet As Integer
    '        Dim hranice1, hranice2 As Short

    '        If PocetPopulace Mod 2 = 0 Then
    '            pocet = PocetPopulace
    '        Else
    '            pocet = PocetPopulace - 1
    '        End If
    '        hranice1 = (pocet / 5) - 1
    '        hranice2 = pocet / 2
    '        If hranice2 Mod 2 = 1 Then
    '            hranice2 += 1
    '        End If
    '        citac = 1
    '        konec = 0
    '        Do
    '            If (citac Mod 2) = 1 Then


    '                For i = 0 To hranice1
    '                    ReDim Node.jmeno(PocetZakazek - 1)
    '                    Node.jmeno = Populace(i).jmeno
    '                    Node.kritfce = Populace(i).kritfce
    '                    Populace_new.Add(Node)
    '                Next

    '                For i = 0 To (hranice2 / 2) - 1
    '                    j = hranice2 + hranice1 - i
    '                    Krizeni(Populace(hranice1 + 1 + i), Populace(j))
    '                    ReDim Node1.jmeno(PocetZakazek - 1)
    '                    ReDim Node2.jmeno(PocetZakazek - 1)
    '                    Node1.jmeno = potomek1
    '                    Node2.jmeno = potomek2
    '                    Node1.kritfce = Spocitat_hodnotu_kritfce(potomek1)
    '                    Node2.kritfce = Spocitat_hodnotu_kritfce(potomek2)
    '                    Populace_new.Add(Node1)
    '                    Populace_new.Add(Node2)
    '                Next

    '                For i = hranice1 + hranice2 + 1 To PocetPopulace - 1
    '                    ReDim Node.jmeno(PocetZakazek - 1)
    '                    Mutace(Populace(i))
    '                    Node.jmeno = mutant
    '                    Node.kritfce = Spocitat_hodnotu_kritfce(mutant)
    '                    Populace_new.Add(Node)
    '                Next
    '            ElseIf (citac Mod 2) = 0 Then

    '                For i = 0 To hranice1
    '                    ReDim Node.jmeno(PocetZakazek - 1)
    '                    Node.jmeno = Populace_new(i).jmeno
    '                    Node.kritfce = Populace_new(i).kritfce
    '                    Populace.Add(Node)
    '                Next

    '                For i = 0 To (hranice2 / 2) - 1
    '                    j = hranice2 + hranice1 - i
    '                    Krizeni(Populace_new(hranice1 + 1 + i), Populace_new(j))
    '                    ReDim Node1.jmeno(PocetZakazek - 1)
    '                    ReDim Node2.jmeno(PocetZakazek - 1)
    '                    Node1.jmeno = potomek1
    '                    Node2.jmeno = potomek2
    '                    Node1.kritfce = Spocitat_hodnotu_kritfce(potomek1)
    '                    Node2.kritfce = Spocitat_hodnotu_kritfce(potomek2)
    '                    Populace.Add(Node1)
    '                    Populace.Add(Node2)
    '                Next

    '                For i = hranice1 + hranice2 + 1 To PocetPopulace - 1
    '                    ReDim Node.jmeno(PocetZakazek - 1)
    '                    Mutace(Populace_new(i))
    '                    Node.jmeno = mutant
    '                    Node.kritfce = Spocitat_hodnotu_kritfce(mutant)
    '                    Populace.Add(Node)
    '                Next
    '            End If
    '            If Math.Abs(Populace(0).kritfce - Populace_new(0).kritfce) = 0 Then
    '                konec += 1
    '            Else
    '                konec = 0
    '            End If

    '            If (citac Mod 2) = 1 Then
    '                konecGA = True
    '                Populace.Clear()
    '            ElseIf (citac Mod 2) = 0 Then
    '                konecGA = False
    '                Populace_new.Clear()
    '            End If
    '            citac += 1
    '            Seradit_pole_populace()
    '        Loop Until konec > Pocet_kroku
    '    End Sub

    '    Private Sub Naplnit_tabulku(Seznam As List(Of Prvek))
    '        Dim i, j, k As Integer
    '        Dim celk_zpozdeni As Single
    '        Dim Soucet As Single
    '        Dim nazev As String

    '        Tabulka_reseni.Rows.Clear()
    '        Tabulka_reseni.Columns.Clear()
    '        nazev = ""

    '        Tabulka_reseni.Columns.Add("Jmeno", "Kombinace")
    '        Tabulka_reseni.Columns.Add("Preserizeni", "Celkový přeseřizovací čas [min]")
    '        'Tabulka_hodnot.Columns.Add("Dynamicka_mezera", "Celková dynamická mezera [den]")
    '        k = 0
    '        For i = 0 To Seznam.Count - 1
    '            Soucet = 0
    '            For j = 0 To Seznam(i).jmeno.Length - 1
    '                If j = 0 Then
    '                    nazev &= Convert.ToString(Seznam(i).jmeno(j))
    '                Else
    '                    nazev &= "-" & Convert.ToString(Seznam(i).jmeno(j))
    '                End If

    '            Next
    '            Tabulka_reseni.Rows.Add()
    '            Tabulka_reseni.Rows(i).HeaderCell.Value = i + 1 & "."
    '            Tabulka_reseni.Rows(i).Cells(0).Value = nazev
    '            Tabulka_reseni.Rows(i).Cells(1).Value = Spocitat_dobu_preserizeni(Seznam(i).jmeno)
    '            celk_zpozdeni = Spocitat_celkove_zpozdeni(Seznam(i).jmeno)
    '            For j = 0 To Seznam(i).jmeno.Length - 1
    '                If i = 0 Then
    '                    Tabulka_reseni.Columns.Add("Zpozdeni" & j, "Zpoždění " & j + 1 & ". zakázky [h]")
    '                End If
    '                Tabulka_reseni.Rows(i).Cells(2 + j).Value = zpozdeni_pole(j)
    '                Soucet += zpozdeni_pole(j)
    '            Next

    '            If i = 0 Then
    '                Tabulka_reseni.Columns.Add("Dynamicka_mezera", "Celková dynamická mezera [h]")
    '                Tabulka_reseni.Columns.Add("Soucet_zpozdeni", "Celkové zpoždění [h]")
    '                Tabulka_reseni.Columns.Add("Kriterialni funkce", "Hodnota kriteriální fuknce [-]")
    '            End If
    '            Tabulka_reseni.Rows(i).Cells(Seznam(i).jmeno.Length + 2).Value = Soucet
    '            Tabulka_reseni.Rows(i).Cells(Seznam(i).jmeno.Length + 3).Value = celk_zpozdeni
    '            Tabulka_reseni.Rows(i).Cells(Seznam(i).jmeno.Length + 4).Value = Seznam(i).kritfce
    '            nazev = ""
    '            celk_zpozdeni = 0
    '        Next

    '    End Sub
    'End Class
End Module
