Advertisement
Infiniti_Inter

123

May 23rd, 2022
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.40 KB | None | 0 0
  1. import PySimpleGUI as sg
  2.  
  3. import math
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6.  
  7. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
  8.  
  9.  
  10. def draw_figure_w_toolbar(canvas, fig, canvas_toolbar):
  11.     if canvas.children:
  12.         for child in canvas.winfo_children():
  13.             child.destroy()
  14.     if canvas_toolbar.children:
  15.         for child in canvas_toolbar.winfo_children():
  16.             child.destroy()
  17.     figure_canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
  18.     figure_canvas_agg.draw()
  19.     toolbar = Toolbar(figure_canvas_agg, canvas_toolbar)
  20.     toolbar.update()
  21.     figure_canvas_agg.get_tk_widget().pack(side='right', fill='both', expand=1)
  22.  
  23.  
  24. class Toolbar(NavigationToolbar2Tk):
  25.     def __init__(self, *args, **kwargs):
  26.         super(Toolbar, self).__init__(*args, **kwargs)
  27.  
  28.  
  29. def f(x):
  30.     return math.exp(1)**-lambd*lambd*x/math.factorial(x)
  31.  
  32. def get_t_c(s):
  33.     return t_m + t_vis + t_p + t_r + t_t
  34.    
  35. def get_L_q():
  36.     return lambd**2*1./(mu*(mu-lambd))
  37.  
  38. #0.5^2/(0.4*(0.4-0.5)
  39. def get_L():
  40.     return get_L_q() + lambd/mu
  41.  
  42. def get_w_q():
  43.     return get_L_q()/lambd
  44.  
  45. def get_w():
  46.     return get_w_q() + 1/mu
  47.  
  48. def get_C_p(_s):
  49.     return C_w*get_L() + C_b * get_w() + C_r * get_w_q() + C_i * get_L_q()+ C_s * _s
  50.  
  51.  
  52. def calc():
  53.     ro = lambd/mu
  54.     #print('ro = ', ro)
  55.     L_q = get_L_q()
  56.     L = get_L()
  57.     w_q = get_w_q()
  58.     w = get_w()
  59.     #print('L_q = ',L_q)
  60.     #print('L = ',L)
  61.     #print('w_q = ',w_q)
  62.     #print('w = ', w)      
  63.     cost = get_C_p(s)
  64.     return cost
  65.     #print('cost = ', cost)
  66.    
  67. def drow(x, y, title, xname = 'x', yname = 'y'):
  68.     plt.plot(arr_s, arr_cost)
  69.     plt.title(title)
  70.     plt.xlabel(xname)
  71.     plt.ylabel(yname)
  72.     #plt.show()
  73.  
  74.    
  75.  
  76. def layout2():
  77.     global cost
  78.     global fl
  79.     global mu
  80.     global lambd
  81.     global s
  82.     global val
  83.     sg.theme('DarkAmber')
  84.         # Window layout
  85.     layout = [[sg.Text('Зависимость функции стоимости от значений  λ, μ', justification='center', size=(50, 1), relief=sg.RELIEF_SUNKEN)],
  86.                   [sg.Text(f'Значение функции стоимости {cost}', size=(40, 1), key="-OUT-")],
  87.                   [sg.Text('λ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER-')],
  88.                   [sg.Text('μ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER2-')],
  89.                     [sg.Button('Закрыть'), sg.B('0'), sg.Button('1'), sg.Button('2')],
  90.                    ]
  91.                  
  92.        
  93.     window = sg.Window('Window Title', layout)
  94.        
  95.     while True:
  96.         event, values = window.read()
  97.         if event == sg.WIN_CLOSED or event == 'Закрыть':
  98.             fl = False
  99.             break
  100.         if (event == '0'):
  101.             val = 0
  102.             break
  103.         if (event == '1'):
  104.             val = 1
  105.             break
  106.                
  107.         lambd = values['-SLIDER-']
  108.         mu = values['-SLIDER2-']
  109.         if (lambd == mu):
  110.             window["-OUT-"].update('Деление на 0')
  111.         else:
  112.             cost = calc()
  113.             window["-OUT-"].update(f'Значение функции стоимости {cost}')
  114.     window.close()
  115.  
  116. def layout1():
  117.     global cost
  118.     global fl
  119.     global mu
  120.     global lambd
  121.     global s
  122.     global val
  123.     sg.theme('DarkBlue')
  124.     layout = [
  125.             [sg.Text(f'Значение функции стоимости : {cost}',size=(40, 1), key="-OUT-")],
  126.             [sg.Text('Введите количество роботов'), sg.InputText()],
  127.             [sg.Button('Ok'), sg.Button('Закрыть'), sg.B('0'), sg.Button('1'), sg.Button('2')]
  128.             ]
  129.        
  130.  
  131.        
  132.     window = sg.Window('Window Title', layout)
  133.     s = 1
  134.     while True:
  135.         event, values = window.read()
  136.         if event == sg.WIN_CLOSED or event == 'Закрыть':
  137.             fl = False
  138.             break
  139.         if (event == '0'):
  140.             val = 0
  141.             break
  142.         if (event == '2'):
  143.             val = 2
  144.             break
  145.        #sg.Output(val)        
  146.         if event == 'Ok':
  147.             if (len(values[0]) > 0):
  148.                 s = int(values[0])
  149.                 cost = calc()
  150.                 window["-OUT-"].update(f'Значение функции стоимости : {cost}')
  151.             else:
  152.                 window["-OUT-"].update('Введите количество')
  153.            
  154.     window.close()
  155.  
  156.  
  157.  
  158.  
  159.  
  160. def layout0():
  161.     global cost
  162.     global fl
  163.     global mu
  164.     global lambd
  165.     global val
  166.  
  167.     sg.theme('Default1')
  168.     layout = [
  169.          [sg.Text(f'Значение функции стоимости {cost}', size=(40, 1), key="-OUT-")],
  170.          [sg.Text('λ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER-')],
  171.          [sg.Text('μ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER2-')],
  172.          [sg.Text('s'), sg.Slider((1, 15), orientation='h',  resolution = 1, enable_events=True, key='-SLIDER3-')],
  173.         [sg.B('Посторить график'), sg.B('Закрыть'), sg.T('стр.: '), sg.B('0'), sg.B('1'), sg.B('2')],
  174.         [sg.T('Панель управления:')],
  175.         [sg.Canvas(key='controls_cv')],
  176.         [sg.T('График:')],
  177.         [sg.Column(
  178.             layout=[
  179.                 [sg.Canvas(key='fig_cv',
  180.                            # it's important that you set this size
  181.                            size=(400 * 2, 400)
  182.                            )]
  183.             ],
  184.             background_color='#DAE0E6',
  185.             pad=(0, 0)
  186.         )],
  187.         ]
  188.    
  189.     window = sg.Window('Интерактивный график функции стоимости', layout)    
  190.     while True:
  191.         event, values = window.read()        
  192.         if event == sg.WIN_CLOSED or event == 'Закрыть':
  193.             fl = False
  194.             break
  195.        
  196.         if (event == '1'):
  197.             val = 1
  198.             break
  199.         if (event == '2'):
  200.             val = 2
  201.             break
  202.        
  203.         lambd = values['-SLIDER-']
  204.         mu = values['-SLIDER2-']
  205.         s = values['-SLIDER3-']
  206.         if (lambd == mu):
  207.             window["-OUT-"].update('Деление на 0')
  208.         else:
  209.             cost = calc()
  210.             window["-OUT-"].update(f'Значение функции стоимости {cost}')
  211.        
  212.        
  213.         if event in (sg.WIN_CLOSED, 'Закрыть'):  # always,  always give a way out!
  214.             fl = True
  215.             break
  216.         elif event == 'Посторить график' and lambd != mu:
  217.             plt.clf()
  218.             # ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE
  219.             plt.figure(1)
  220.             fig = plt.gcf()
  221.             DPI = fig.get_dpi()
  222.             # ------------------------------- you have to play with this size to reduce the movement error when the mouse hovers over the figure, it's close to canvas size
  223.             fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
  224.             # -------------------------------
  225.             x = []
  226.             y = []
  227.             for s in range(1, 20):
  228.                 C_p = get_C_p(s)
  229.                 y.append(C_p)
  230.                 x.append(s)
  231.             plt.plot(x, y)
  232.             plt.title('cost = f(robots)')
  233.             plt.xlabel('Роботы')
  234.             plt.ylabel('Стоимость')
  235.             plt.grid()
  236.             #print(x)
  237.             #print(y)
  238.    
  239.             # ------------------------------- Instead of plt.show()
  240.             draw_figure_w_toolbar(window['fig_cv'].TKCanvas, fig, window['controls_cv'].TKCanvas)
  241.            
  242.    
  243.     window.close()
  244.            
  245.  
  246.  
  247.  
  248. lambd = 0.5
  249. mu = 0.4
  250.  
  251. C_w = 50
  252. C_b = 0.1
  253. C_r = 60
  254. C_i = 10
  255. C_s = 0.5
  256.  
  257.  
  258. t_m = 0.13
  259. t_vis = 0.1
  260. t_p = 0.12
  261. t_r = 0.125
  262. t_t = 0.15
  263.  
  264. ####
  265.  
  266. t_c = 0
  267. C_p = 0
  268. p_n = []
  269. L_q = 0
  270. L = 0
  271. w_q = 0
  272. w_a = 0
  273. t_c = get_t_c(1)
  274.  
  275. _min = 1e10
  276. arr_cost = []
  277. arr_s = np.linspace(0, 15, 16)
  278.  
  279. cost = 1
  280. s = 1
  281. fl = True
  282. val = 0
  283.  
  284.  
  285.  
  286.  
  287.        
  288. L_q = get_L_q()
  289. #print(L_q)
  290.  
  291.  
  292. pi = math.acos(-1)
  293. n = 340
  294. w = 250
  295. d = 14.4
  296. a = d/2
  297. #a = 95.5428
  298.  
  299.  
  300.  
  301.  
  302. #rb = 1.49681
  303. #Nf = 1.12468
  304. #c = 2.628867911(посчитано)
  305. #2.628867911
  306.  
  307.  
  308. v = 256
  309. p_b = (w - d - w * a)**2/(w - d)**2
  310. c = w*pi*(a+d)**2/(w-d)**2/v
  311. r_b = 1/c
  312. print('p_b = ', p_b)
  313. print('r_b = ',  r_b)
  314. print('c = ', c)
  315.  
  316.  
  317. Nf = (w/v)*r_b*p_b*math.exp(1)**(-r_b*c)
  318. Nf = Nf // 1
  319. print('Nf = ', Nf)
  320.  
  321. bot = (w/v)+t_p+Nf*t_c
  322. bot = bot // 1
  323. T_r = Nf/(bot)
  324.  
  325. print('T_r = ', T_r)
  326.    
  327.    
  328.    
  329.  
  330.  
  331. while (fl == True):
  332.     #print(val)        
  333. ############################## 0 ##############################  
  334.     if (val == 0):        
  335.         layout0()
  336. ###############################################################                    
  337.            
  338.            
  339. ############################## 1 ##############################        
  340.     if (val == 1):
  341.         layout1()
  342. ###############################################################
  343.    
  344.    
  345. ############################## 2 ##############################  
  346.     if (val == 2):
  347.         layout2()
  348. ###############################################################
  349.  
  350.  
  351.        
  352.    
  353.    
  354.  
  355.    
  356.    
  357. #print(arr_s)  
  358. #print(len(arr_cost))
  359. #print(arr_cost)
  360. #print(len(arr_s))
  361.  
  362.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement