# Created By Michal Furmanek, @2019-2020 # Created for Bachelor Thesis at the Czech University of Life Sciences in Prague # Mathematical model of population prediction of wild board # import modules from tkinter import * from tkinter import messagebox import tkinter as tk import matplotlib.pyplot as plt from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from numpy import log as ln import matplotlib.ticker as ticker from math import * import pandas as pd # main function - calculate results def calculate_result(): global k global s global v global n global m global r global k_input global male global female global kids global min_n global result global graph global kids_hunt global kidsTotal global coeficient_boar global final_hunt k_input = (production_coeficient.get()) s = (ratio.get()) v = (final_age.get()) r = (kids_hunt_percentage.get()) m = (n_hunt_in_final_age.get()) print(s) print(v) # print(m) print(r) # old # k = (1 - (r / 100)) * k_input k = k_input print(k) if (k <= 3.2): coeficient_boar = 97.6 / 38.4 elif (k > 3.2 and k <= 3.3): coeficient_boar = 99.75 / 39.6 elif (k > 3.3 and k <= 3.4): coeficient_boar = 103 / 41.65 elif (k > 3.4 and k <= 3.5): coeficient_boar = 106.25 / 43.75 elif (k > 3.5 and k <= 3.6): coeficient_boar = 107.2 / 45.9 elif (k > 3.6 and k <= 3.7): coeficient_boar = 110.9 / 48.1 elif (k > 3.7 and k <= 3.8): coeficient_boar = 111.7 / 51.3 elif (k > 3.8 and k <= 3.9): coeficient_boar = 114.85 / 53.625 elif (k > 3.9 and k <= 4): coeficient_boar = 116 / 56 elif (k > 4 and k <= 4.1): coeficient_boar = 119.1 / 58.425 elif (k > 4.1 and k <= 4.2): coeficient_boar = 120.1 / 60.9 elif (k > 4.2 and k <= 4.3): coeficient_boar = 123.15 / 63.425 elif (k > 4.3 and k <= 4.4): coeficient_boar = 126.2 / 66 elif (k > 4.4 and k <= 4.5): coeficient_boar = 127.5 / 67.5 elif (k > 4.5 and k <= 4.6): coeficient_boar = 130.5 / 70.15 elif (k > 4.6 and k <= 4.7): coeficient_boar = 131.65 / 71.675 elif (k > 4.7 and k <= 4.8): coeficient_boar = 134.6 / 74.4 elif (k > 4.8 and k <= 4.9): coeficient_boar = 135.1 / 77.175 elif (k > 4.9 and k <= 5): coeficient_boar = 138 / 80 elif (k > 5 and k <= 5.1): coeficient_boar = 141.4 / 81.6 elif (k > 5.1 and k <= 5.2): coeficient_boar = 144.8 / 83.2 elif (k > 5.2 and k <= 5.3): coeficient_boar = 148.2 / 84.8 elif (k > 5.3 and k <= 5.4): coeficient_boar = 151.6 / 86.4 elif (k > 5.4 and k <= 5.5): coeficient_boar = 155 / 88 else: coeficient_boar = 106.25 / 43.75 if (options_radio.get() == 2): final_hunt = round((m * (v - ln(2 * v + 1)) * (s + 1 + ((2 * k) / (k * coeficient_boar + 2)))) / (((k / (k * coeficient_boar + 2)) * (1 - ln(2 * v + 1))) + (s * (1 - 1 / v)))) n = final_hunt print("M SE ROVNA %.0f" % final_hunt) # this part will be hidden if A Option is selected i9 = Label(calculation_screen, text=" Normovaný stav zvěře (N)", bg="#55ea50") i9.grid(row=8, column=1, columnspan=3, sticky="W") i10 = Label(calculation_screen, text="%.0f" % n, bg="#55ea50") i10.grid(row=8, column=4, sticky="W") arrWidgetInput.append(i9) arrWidgetInput.append(i10) # arrWidgetResult.append(s15) # arrWidgetResult.append(s16) else: n = (total_population.get()) # calculate number of kids in population kids = (((2 * k) / (k * coeficient_boar + 2)) * n) / (s + 1 + ((2 * k) / (k * coeficient_boar + 2))) # calculate number of males in population male = (n * s) / (s + 1 + ((2 * k) / (k * coeficient_boar + 2))) # calculate number of females in population female = n / (s + 1 + ((2 * k) / (k * coeficient_boar + 2))) kidsTotal = k * female kids_hunt = ceil(kidsTotal - kids) i11 = Label(calculation_screen, text=" Koeficient odlovu selat (R)", bg="#55ea50") i11.grid(row=7, column=1, columnspan=3, sticky="W") i12 = Label(calculation_screen, text="%.2f" % coeficient_boar, bg="#55ea50") i12.grid(row=7, column=4, sticky="W") arrWidgetInput.append(i11) arrWidgetInput.append(i12) s5 = Label(calculation_screen, text="Normované stavy:", bg="#55ea50") s5.grid(row=10, column=2, columnspan=2, sticky="W") s7 = Label(calculation_screen, text="Počet samců", bg="#55ea50", borderwidth=1, relief="solid") s7.grid(row=11, column=2, sticky="nsew", padx=0, pady=0) s8 = Label(calculation_screen, text="Počet samic", bg="#55ea50", borderwidth=1, relief="solid") s8.grid(row=12, column=2, sticky="nsew", padx=0, pady=0) s9 = Label(calculation_screen, text="Počet selat", bg="#55ea50", borderwidth=1, relief="solid") s9.grid(row=13, column=2, sticky="nsew", padx=0, pady=0) s6 = Label(calculation_screen, text="%.0f" % male, bg="#55ea50", borderwidth=1, relief="solid") s6.grid(row=11, column=3, sticky="nsew", padx=0, pady=0) s10 = Label(calculation_screen, text="%.0f" % female, bg="#55ea50", borderwidth=1, relief="solid") s10.grid(row=12, column=3, sticky="nsew", padx=0, pady=0) s11 = Label(calculation_screen, text="%.0f" % kids, bg="#55ea50", borderwidth=1, relief="solid") s11.grid(row=13, column=3, sticky="nsew", padx=0, pady=0) s12 = Label(calculation_screen, text="Počet samců dle grafu:", bg="#55ea50", ) s12.grid(row=15, column=2, columnspan=2, sticky="W") s1 = Label(calculation_screen, text="Celkový počet selat", bg="#55ea50", borderwidth=1, relief="solid") s1.grid(row=6, column=7, sticky="nsew", padx=0, pady=0) s2 = Label(calculation_screen, text="%.0f" % kidsTotal, bg="#55ea50", borderwidth=1, relief="solid") s2.grid(row=6, column=8, sticky="nsew", padx=0, pady=0) s3 = Label(calculation_screen, text="Celkovy počet selat k odlovu", bg="#55ea50", borderwidth=1, relief="solid") s3.grid(row=7, column=7, sticky="nsew", padx=0, pady=0) s4 = Label(calculation_screen, text="%.0f" % kids_hunt, bg="#55ea50", borderwidth=1, relief="solid") s4.grid(row=7, column=8, sticky="nsew", padx=0, pady=0) s15 = Label(calculation_screen, text=" ", bg="#55ea50") s15.grid(row=7, column=9, sticky="W") s13 = Label(calculation_screen, text="Informace k selatům:", bg="#55ea50") s13.grid(row=5, column=7, columnspan=2, sticky="W") s14 = Label(calculation_screen, text="Každoroční odlov jedinců:", bg="#55ea50") s14.grid(row=9, column=7, columnspan=2, sticky="W") print(coeficient_boar) # cycle to display values in table print("calculation Result") i = 1 arrAge = [] arrResult = [] arrWidgetResult.append(s1) arrWidgetResult.append(s2) arrWidgetResult.append(s3) arrWidgetResult.append(s4) arrWidgetResult.append(s5) arrWidgetResult.append(s6) arrWidgetResult.append(s7) arrWidgetResult.append(s8) arrWidgetResult.append(s9) arrWidgetResult.append(s10) arrWidgetResult.append(s11) arrWidgetResult.append(s12) arrWidgetResult.append(s13) arrWidgetResult.append(s14) arrWidgetResult.append(s15) while i <= v: if (i == 1): titleA = Label(calculation_screen, text="Věk", font=("calibri", 11), bg="#55ea50", borderwidth=2, relief="solid") titleA.grid(row=16, column=2, sticky="nsew", padx=0, pady=0) titleB = Label(calculation_screen, text="Počet Jedinců", font=("calibri", 11), bg="#55ea50", borderwidth=2, relief="solid") titleB.grid(row=16, column=3, sticky="nsew", padx=0, pady=0) res = round((1 / (v - ln(2 * v + 1))) * (n / (s + 1 + ((2 * k) / (k * coeficient_boar + 2)))) * ( (k / (k * coeficient_boar + 2)) * (v / i - ln(2 * v + 1)) + (s * (1 - 1 / i)))) if (res < 1): res = 0 a = Label(calculation_screen, text=i, font=("calibri", 11), bg="#55ea50", borderwidth=1, relief="solid") a.grid(row=16 + i, column=2, sticky="nsew", padx=0, pady=0) b = Label(calculation_screen, text="%.0f" % res, font=("calibri", 11), bg="#55ea50", borderwidth=1, relief="solid") b.grid(row=16 + i, column=3, sticky="nsew", padx=0, pady=0) arrAge.append(i) arrResult.append(res) arrWidgetResult.append(a) arrWidgetResult.append(b) arrWidgetResult.append(titleA) arrWidgetResult.append(titleB) i += 1 print(res) xx = 1 while xx <= v: if (xx == 1): # table in the right part titleC = Label(calculation_screen, text="Věk", font=("calibri", 11), bg="#55ea50", borderwidth=2, relief="solid") titleC.grid(row=10, column=7, sticky="nsew", padx=0, pady=0) titleD = Label(calculation_screen, text="Počet Jedinců", font=("calibri", 11), bg="#55ea50", borderwidth=2, relief="solid") titleD.grid(row=10, column=8, sticky="nsew", padx=0, pady=0) c = Label(calculation_screen, text=xx, font=("calibri", 11), bg="#55ea50", borderwidth=1, relief="solid") c.grid(row=10 + xx, column=7, sticky="nsew", padx=0, pady=0) if xx < v: res = arrResult[xx - 1] - arrResult[xx] else: res = arrResult[xx - 1] pass d = Label(calculation_screen, text="%.0f" % res, font=("calibri", 11), bg="#55ea50", borderwidth=1, relief="solid") d.grid(row=10 + xx, column=8, sticky="nsew", padx=0, pady=0) arrWidgetResult.append(c) arrWidgetResult.append(d) arrWidgetResult.append(titleC) arrWidgetResult.append(titleD) xx += 1 data = {'Age': arrAge, 'Result': arrResult } df = pd.DataFrame(data, columns=['Age', 'Result']) # plot plt.style.use('seaborn-whitegrid') plotResult = plt.Figure(figsize=(8, 5), dpi=100) plotResult.set_facecolor('#55ea50') ax3 = plotResult.add_subplot(111) ax3.plot(df['Result'], df['Age'], color='g', marker='o', markerfacecolor='black') loc = plt.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals # ax3.set_xticks(ax3.get_xticks()[::1]) ax3.xaxis.set_major_locator(ticker.MultipleLocator(1)) ax3.yaxis.set_major_locator(loc) graph = FigureCanvasTkAgg(plotResult, calculation_screen) # scatter3.get_tk_widget().pack(side=LEFT, fill=BOTH) # ax3.legend() ax3.invert_xaxis() ax3.set_ylim(bottom=0) ax3.set_xlim(right=0) ax3.yaxis.set_label_position("right") ax3.yaxis.tick_right() ax3.set_xlabel('Počet jedinců') ax3.set_ylabel('Věk') ax3.set_title('Počet jedinců v jednotlivých letech života') graph.get_tk_widget().grid(row=1, column=5, rowspan=25) arrWidgetResult.append(graph.get_tk_widget()) print(male) print(female) print(kids) return def close(): main_screen.destroy() def delete_row(): # clear previous rows if arrWidgetResult is not None: for widget in arrWidgetResult: print("widget-delted") widget.destroy() arrWidgetResult.clear() def delete_row_input(): # clear previous rows if arrWidgetInput is not None: for widget in arrWidgetInput: print("widget-input-delted") widget.destroy() arrWidgetInput.clear() # calculation screen definition def start_calculation(): global calculation_screen calculation_screen = Toplevel(main_screen) main_screen.withdraw() # close the top level will close the program calculation_screen.protocol("WM_DELETE_WINDOW", close) # scrollbar = Scrollbar(calculation_screen) # scrollbar.pack( side = RIGHT, fill = Y ) global total_population global production_coeficient global ratio global final_age global final_number global number_in_final_age global kids_hunt_percentage global n_hunt_in_final_age global arrWidgetResult global arrWidgetInput global options_radio options_radio = IntVar(value=1) total_population = DoubleVar(value=50) production_coeficient = DoubleVar(value=3.5) ratio = DoubleVar(value=1) final_age = DoubleVar(value=9) # number_in_final_age = DoubleVar(value=1) kids_hunt_percentage = DoubleVar(value=75) n_hunt_in_final_age = DoubleVar(value=2) arrWidgetResult = [] arrWidgetInput = [] calculation_screen.configure(background="#55ea50") calculation_screen.title("Variabilní model populace černé zvěře") calculation_screen.geometry("1500x600") Label(calculation_screen, text=" Prosím vložte koeficienty", bg="#55ea50", font=("Calibri", 13, 'bold')).grid(row=1, column=1, pady=5, columnspan=2, sticky="W") Button(calculation_screen, text="Nápověda", command=help).grid(row=1, column=3) Label(calculation_screen, text="", bg="#55ea50", ).grid(row=2, column=1) # Button(calculation_screen, text="Close", command=close).grid(row=0, column=7) def drawInput(): delete_row_input() i1 = Label(calculation_screen, text=" Vložte prosím poměr pohlaví (S) ", bg="#55ea50") i1.grid(row=3, column=1, columnspan=3, sticky="W") i2 = Label(calculation_screen, text=" Vložte prosím koeficient očekávané produkce (K)", bg="#55ea50") i2.grid(row=4, column=1, sticky="W", columnspan=3) i3 = Label(calculation_screen, text=" Vložte cílový věk kňoura (V)", bg="#55ea50") i3.grid(row=5, column=1, columnspan=3, sticky="W") if (options_radio.get() == 1): i4 = Label(calculation_screen, text=" Vložte normovaný stav zvěře (N)", bg="#55ea50") i4.grid(row=6, column=1, columnspan=3, sticky="W") else: i4 = Label(calculation_screen, text=" Vložte počet jedinců v cílovém věku (M)", bg="#55ea50") i4.grid(row=6, column=1, columnspan=3, sticky="W") i5 = Entry(calculation_screen, textvariable=ratio) i5.grid(row=3, column=4) i6 = Entry(calculation_screen, textvariable=production_coeficient) i6.grid(row=4, column=4) i7 = Entry(calculation_screen, textvariable=final_age) i7.grid(row=5, column=4) if (options_radio.get() == 1): i8 = Entry(calculation_screen, textvariable=total_population) i8.grid(row=6, column=4) else: i8 = Entry(calculation_screen, textvariable=n_hunt_in_final_age) i8.grid(row=6, column=4) arrWidgetInput.append(i1) arrWidgetInput.append(i2) arrWidgetInput.append(i3) arrWidgetInput.append(i4) arrWidgetInput.append(i5) arrWidgetInput.append(i6) arrWidgetInput.append(i7) arrWidgetInput.append(i8) # arrWidgetInput.append(i9) drawInput() def choicePicked(): delete_row() drawInput() Radiobutton(calculation_screen, text="Máme daný normovaný stav", padx=5, variable=options_radio, command=choicePicked, bg="#55ea50", value=1).grid(row=2, column=1, columnspan=2, sticky="W") Radiobutton(calculation_screen, text="Chceme lovit M jedinců v cílovém věku V", variable=options_radio, command=choicePicked, bg="#55ea50", value=2).grid(row=2, column=3, columnspan=2, sticky="W") # Entry(calculation_screen, textvariable=kids_hunt_percentage).grid(row=6, column=4) Button(calculation_screen, text="Vypočítat", command=validateInput).grid(row=9, column=2) calculation_screen.mainloop() def validateInput(): n = total_population m = n_hunt_in_final_age k = production_coeficient s = ratio v = final_age # r = kids_hunt_percentage delete_row() if (numIsNotValid(n) and options_radio.get() == 1): # print("dodo") setResultMessage("Normovaný stav zvěře musí být vyplněn") elif (numIsNotValid(m) and options_radio.get() == 2): # print("dede") setResultMessage("Počet kňourů M v cílovém věku musí být vyplněn") elif (Coeficient(k)): setResultMessage( "Koeficient očekávané produkce musí být větší nebo roven 3.2 a menší než 5.5, jako desetinnou čárku prosím použijte tečku") elif (numIsNotValid(s)): setResultMessage("Poměr pohlaví musí být vyplněn") elif (CoeficientV(v)): setResultMessage("Cílový věk kňoura musí být větší než 5") else: print("calculate result") calculate_result() def CoeficientV(arg): try: arg.get() return (arg.get() < 5) except Exception as e: return True def setResultMessage(arg): a = Label(calculation_screen, text=arg, bg="#55ea50", font=("calibri", 11), fg="red") a.grid(row=10, column=1, columnspan=6) arrWidgetResult.append(a) def Coeficient(arg): try: arg.get() return (arg.get() > 5.5 or arg.get() < 3.2) except Exception as e: return True def numIsNotValid(arg): try: arg.get() return (arg.get() == "" or arg.get() <= 0) except Exception as e: return True # Help Dialog def help(): global help_screen help_screen = Toplevel(main_screen) help_screen.geometry("750x400") help_screen.configure(background="#55ea50") help_screen.title("Nápověda") # create a Form label Label(help_screen, text="Vložení koeficientů - v případě použití desetinné čárky, použijte prosím jako oddělovač tečku", bg="#55ea50", anchor="w", font=("Calibri", 10, "bold")).pack(fill='both') Label(help_screen, text="Poměr pohlaví (S):", bg="#55ea50", anchor="w", height="1", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text="Vložte prosím jako číslo - příklady následovně:", bg="#55ea50", anchor="w", height="1", font=("Calibri", 10)).pack(fill='both') Label(help_screen, text=" V případě, že je poměr pohlaví 1:1 (samec:samice) je S=1.", bg="#55ea50", anchor="w", font=("Calibri", 10)).pack(fill='both') Label(help_screen, text=" V případě, že je poměr pohlaví 0,5:1 (samec:samice) je S=0.5. ", bg="#55ea50", anchor="w", font=("Calibri", 10)).pack(fill='both') Label(help_screen, text=" V případě, že je poměr pohlaví 1:0,5 (samec:samice) je S=2. ", bg="#55ea50", anchor="w", font=("Calibri", 10)).pack(fill='both') Label(help_screen, text="Koeficient očekávané produkce (K): ", bg="#55ea50",anchor="w", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text=" Koeficient očekáváné produkce musí být roven nebo vyšší 3.2 a menší než 5.", bg="#55ea50", anchor="w",font=("Calibri", 10)).pack(fill='both') Label(help_screen, text="Cílový věk kňoura(V) ", bg="#55ea50",anchor="w", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text=" Určuje cílový věk kňoura, kterého chceme lovit.", bg="#55ea50", anchor="w",font=("Calibri", 10)).pack(fill='both') Label(help_screen, text="Normovaný stav zvěře (N): ", bg="#55ea50",anchor="w", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text=" Určuje počet jedinců v populaci.", bg="#55ea50", anchor="w",font=("Calibri", 10)).pack(fill='both') Label(help_screen, text="Koeficient odlovu selat (R): ", bg="#55ea50",anchor="w", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text=" Koeficient je dopočítán automaticky programem, není nutno zde dělat výpočty.", bg="#55ea50", anchor="w",font=("Calibri", 10)).pack(fill='both') Label(help_screen, text="Počet jedinců v cílovém věku (M): ", bg="#55ea50",anchor="w", font=("Calibri", 10,"bold")).pack(fill='both') Label(help_screen, text=" Určuje počet jedinců, které chceme v cílovém věku V lovit.", bg="#55ea50", anchor="w",font=("Calibri", 10)).pack(fill='both') # create a Form label Label(help_screen, text="", bg="#55ea50").pack() # main welcome screen def main(): global main_screen main_screen = Tk() main_screen.geometry("750x400") main_screen.configure(background="#55ea50") main_screen.title("Variabilní model populace černé zvěře") # create a Form label Label(main_screen, text="Variabilní model populace černé zvěře", bg="#55ea50", width="300", height="2", font=("Calibri", 15)).pack() Label(main_screen, text="Bakalářská práce", bg="#55ea50", width="300", height="2", font=("Calibri", 13)).pack() Label(main_screen, text="Michal Furmánek", bg="#55ea50", width="300", height="2", font=("Calibri", 11)).pack() Label(main_screen, text="Vedoucí práce: doc. Ing. Vladimír Hanzal, CSc.", bg="#55ea50", width="300", height="2", font=("Calibri", 11)).pack() Label(main_screen, bg="#55ea50", text="").pack() # create a Form label Label(main_screen, text="Planování odstřelu prasete divokého při zohlednění počtu jedinců v populaci ", bg="#55ea50", width="300", height="1", font=("Calibri", 13)).pack() Label(main_screen, text="poměru pohlaví,koeficientu očekávané produkce a cílového věku trofejových kňourů ", bg="#55ea50", width="300", height="1", font=("Calibri", 13)).pack() Label(main_screen, text="", bg="#55ea50").pack() # create Start Button Button(main_screen, text="Začít", height="2", width="30", bg='green', fg='white', font=('helvetica', 9, 'bold'), command=start_calculation).pack() Label(main_screen, bg="#55ea50", text="").pack() main_screen.mainloop() if __name__ == '__main__': main()