Advertisement
Infiniti_Inter

code

May 19th, 2022
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.23 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 == 'Закрыть': # if user closes window or clicks cancel
  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.         s = int(values[0])
  147.         cost = calc()
  148.         window["-OUT-"].update(f'Значение функции стоимости : {cost}')
  149.     window.close()
  150.  
  151. def layout0():
  152.     global cost
  153.     global fl
  154.     global mu
  155.     global lambd
  156.     global val
  157.  
  158.     sg.theme('Default1')
  159.     layout = [
  160.          [sg.Text(f'Значение функции стоимости {cost}', size=(40, 1), key="-OUT-")],
  161.          [sg.Text('λ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER-')],
  162.          [sg.Text('μ'), sg.Slider((0.05, 10), orientation='h',  resolution = 0.05, enable_events=True, key='-SLIDER2-')],
  163.          [sg.Text('s'), sg.Slider((1, 15), orientation='h',  resolution = 1, enable_events=True, key='-SLIDER3-')],
  164.         [sg.B('Посторить график'), sg.B('Выход'), sg.T('стр.: '), sg.B('0'), sg.B('1'), sg.B('2')],
  165.         [sg.T('Панель управления:')],
  166.         [sg.Canvas(key='controls_cv')],
  167.         [sg.T('График:')],
  168.         [sg.Column(
  169.             layout=[
  170.                 [sg.Canvas(key='fig_cv',
  171.                            # it's important that you set this size
  172.                            size=(400 * 2, 400)
  173.                            )]
  174.             ],
  175.             background_color='#DAE0E6',
  176.             pad=(0, 0)
  177.         )],
  178.         ]
  179.    
  180.     window = sg.Window('Интерактивный график функции стоимости', layout)
  181.    
  182.     while True:
  183.         event, values = window.read()
  184.        
  185.        
  186.         if event == sg.WIN_CLOSED or event == 'Закрыть':
  187.             fl = False
  188.             break
  189.        
  190.         if (event == '1'):
  191.             val = 1
  192.             break
  193.         if (event == '2'):
  194.             val = 2
  195.             break
  196.        
  197.         lambd = values['-SLIDER-']
  198.         mu = values['-SLIDER2-']
  199.         s = values['-SLIDER3-']
  200.         if (lambd == mu):
  201.             window["-OUT-"].update('Деление на 0')
  202.         else:
  203.             cost = calc()
  204.             window["-OUT-"].update(f'Значение функции стоимости {cost}')
  205.        
  206.        
  207.         if event in (sg.WIN_CLOSED, 'Выход'):  # always,  always give a way out!
  208.             break
  209.         elif event == 'Посторить график' and lambd != mu:
  210.             plt.clf()
  211.             # ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE
  212.             plt.figure(1)
  213.             fig = plt.gcf()
  214.             DPI = fig.get_dpi()
  215.             # ------------------------------- 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
  216.             fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
  217.             # -------------------------------
  218.             x = []
  219.             y = []
  220.             for s in range(1, 20):
  221.                 C_p = get_C_p(s)
  222.                 y.append(C_p)
  223.                 x.append(s)
  224.             plt.plot(x, y)
  225.             plt.title('cost = f(robots)')
  226.             plt.xlabel('Роботы')
  227.             plt.ylabel('Стоимость')
  228.             plt.grid()
  229.             #print(x)
  230.             #print(y)
  231.    
  232.             # ------------------------------- Instead of plt.show()
  233.             draw_figure_w_toolbar(window['fig_cv'].TKCanvas, fig, window['controls_cv'].TKCanvas)
  234.            
  235.    
  236.     window.close()
  237.            
  238.  
  239.  
  240.  
  241. lambd = 0.5
  242. mu = 0.4
  243.  
  244. C_w = 50
  245. C_b = 0.1
  246. C_r = 60
  247. C_i = 10
  248. C_s = 0.5
  249.  
  250.  
  251. t_m = 0.13
  252. t_vis = 0.1
  253. t_p = 0.12
  254. t_r = 0.125
  255. t_t = 0.15
  256.  
  257. ####
  258.  
  259. t_c = 0
  260. C_p = 0
  261. p_n = []
  262. L_q = 0
  263. L = 0
  264. w_q = 0
  265. w_a = 0
  266. t_c = get_t_c(1)
  267.  
  268. _min = 1e10
  269. arr_cost = []
  270. arr_s = np.linspace(0, 15, 16)
  271.  
  272. cost = 1
  273. s = 1
  274. fl = True
  275. val = 0
  276.  
  277.  
  278. fl = True
  279.  
  280.  
  281.  
  282.        
  283. L_q = get_L_q()
  284. #print(L_q)
  285.  
  286.  
  287. pi = math.acos(-1)
  288. n = 340
  289. w = 250
  290. d = 14.4
  291. a = d/2
  292. #a = 95.5428
  293.  
  294.  
  295.  
  296.  
  297. #rb = 1.49681
  298. #Nf = 1.12468
  299. #c = 2.628867911(посчитано)
  300. #2.628867911
  301.  
  302.  
  303. v = 256
  304. p_b = (w - d - w * a)**2/(w - d)**2
  305. c = w*pi*(a+d)**2/(w-d)**2/v
  306. r_b = 1/c
  307. print('p_b = ', p_b)
  308. print('r_b = ',  r_b)
  309. print('c = ', c)
  310.  
  311.  
  312. Nf = (w/v)*r_b*p_b*math.exp(1)**(-r_b*c)
  313. Nf = Nf // 1
  314. print('Nf = ', Nf)
  315.  
  316. bot = (w/v)+t_p+Nf*t_c
  317. bot = bot // 1
  318. T_r = Nf/(bot)
  319.  
  320. print('T_r = ', T_r)
  321.    
  322.    
  323.    
  324.  
  325.  
  326. while (fl != True):
  327.     #print(val)    
  328.    
  329. ############################## 0 ##############################  
  330.     if (val == 0):        
  331.         layout0()
  332. ###############################################################
  333.                    
  334.            
  335.            
  336. ############################## 1 ##############################        
  337.     if (val == 1):
  338.         layout1()
  339. ###############################################################
  340.    
  341.    
  342. ############################## 2 ##############################  
  343.     if (val == 2):
  344.         layout2()
  345. ###############################################################
  346.  
  347.  
  348.        
  349.    
  350.    
  351.  
  352.    
  353.    
  354. #print(arr_s)  
  355. #print(len(arr_cost))
  356. #print(arr_cost)
  357. #print(len(arr_s))
  358.  
  359.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement