Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import PySimpleGUI as sg
- import math
- import matplotlib.pyplot as plt
- import numpy as np
- from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
- def draw_figure_w_toolbar(canvas, fig, canvas_toolbar):
- if canvas.children:
- for child in canvas.winfo_children():
- child.destroy()
- if canvas_toolbar.children:
- for child in canvas_toolbar.winfo_children():
- child.destroy()
- figure_canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
- figure_canvas_agg.draw()
- toolbar = Toolbar(figure_canvas_agg, canvas_toolbar)
- toolbar.update()
- figure_canvas_agg.get_tk_widget().pack(side='right', fill='both', expand=1)
- class Toolbar(NavigationToolbar2Tk):
- def __init__(self, *args, **kwargs):
- super(Toolbar, self).__init__(*args, **kwargs)
- def f(x):
- return math.exp(1)**-lambd*lambd*x/math.factorial(x)
- def get_t_c(s):
- return t_m + t_vis + t_p + t_r + t_t
- def get_L_q():
- return lambd**2*1./(mu*(mu-lambd))
- #0.5^2/(0.4*(0.4-0.5)
- def get_L():
- return get_L_q() + lambd/mu
- def get_w_q():
- return get_L_q()/lambd
- def get_w():
- return get_w_q() + 1/mu
- def get_C_p(_s):
- return C_w*get_L() + C_b * get_w() + C_r * get_w_q() + C_i * get_L_q()+ C_s * _s
- def calc():
- ro = lambd/mu
- #print('ro = ', ro)
- L_q = get_L_q()
- L = get_L()
- w_q = get_w_q()
- w = get_w()
- #print('L_q = ',L_q)
- #print('L = ',L)
- #print('w_q = ',w_q)
- #print('w = ', w)
- cost = get_C_p(s)
- return cost
- #print('cost = ', cost)
- def drow(x, y, title, xname = 'x', yname = 'y'):
- plt.plot(arr_s, arr_cost)
- plt.title(title)
- plt.xlabel(xname)
- plt.ylabel(yname)
- #plt.show()
- def layout2():
- global cost
- global fl
- global mu
- global lambd
- global s
- global val
- sg.theme('DarkAmber')
- # Window layout
- layout = [[sg.Text('Зависимость функции стоимости от значений λ, μ', justification='center', size=(50, 1), relief=sg.RELIEF_SUNKEN)],
- [sg.Text(f'Значение функции стоимости {cost}', size=(40, 1), key="-OUT-")],
- [sg.Text('λ'), sg.Slider((0.05, 10), orientation='h', resolution = 0.05, enable_events=True, key='-SLIDER-')],
- [sg.Text('μ'), sg.Slider((0.05, 10), orientation='h', resolution = 0.05, enable_events=True, key='-SLIDER2-')],
- [sg.Button('Закрыть'), sg.B('0'), sg.Button('1'), sg.Button('2')],
- ]
- window = sg.Window('Window Title', layout)
- while True:
- event, values = window.read()
- if event == sg.WIN_CLOSED or event == 'Закрыть':
- fl = False
- break
- if (event == '0'):
- val = 0
- break
- if (event == '1'):
- val = 1
- break
- lambd = values['-SLIDER-']
- mu = values['-SLIDER2-']
- if (lambd == mu):
- window["-OUT-"].update('Деление на 0')
- else:
- cost = calc()
- window["-OUT-"].update(f'Значение функции стоимости {cost}')
- window.close()
- def layout1():
- global cost
- global fl
- global mu
- global lambd
- global s
- global val
- sg.theme('DarkBlue')
- layout = [
- [sg.Text(f'Значение функции стоимости : {cost}',size=(40, 1), key="-OUT-")],
- [sg.Text('Введите количество роботов'), sg.InputText()],
- [sg.Button('Ok'), sg.Button('Закрыть'), sg.B('0'), sg.Button('1'), sg.Button('2')]
- ]
- window = sg.Window('Window Title', layout)
- s = 1
- while True:
- event, values = window.read()
- if event == sg.WIN_CLOSED or event == 'Закрыть': # if user closes window or clicks cancel
- fl = False
- break
- if (event == '0'):
- val = 0
- break
- if (event == '2'):
- val = 2
- break
- sg.Output(val)
- s = int(values[0])
- cost = calc()
- window["-OUT-"].update(f'Значение функции стоимости : {cost}')
- window.close()
- def layout0():
- global cost
- global fl
- global mu
- global lambd
- global val
- sg.theme('Default1')
- layout = [
- [sg.Text(f'Значение функции стоимости {cost}', size=(40, 1), key="-OUT-")],
- [sg.Text('λ'), sg.Slider((0.05, 10), orientation='h', resolution = 0.05, enable_events=True, key='-SLIDER-')],
- [sg.Text('μ'), sg.Slider((0.05, 10), orientation='h', resolution = 0.05, enable_events=True, key='-SLIDER2-')],
- [sg.Text('s'), sg.Slider((1, 15), orientation='h', resolution = 1, enable_events=True, key='-SLIDER3-')],
- [sg.B('Посторить график'), sg.B('Выход'), sg.T('стр.: '), sg.B('0'), sg.B('1'), sg.B('2')],
- [sg.T('Панель управления:')],
- [sg.Canvas(key='controls_cv')],
- [sg.T('График:')],
- [sg.Column(
- layout=[
- [sg.Canvas(key='fig_cv',
- # it's important that you set this size
- size=(400 * 2, 400)
- )]
- ],
- background_color='#DAE0E6',
- pad=(0, 0)
- )],
- ]
- window = sg.Window('Интерактивный график функции стоимости', layout)
- while True:
- event, values = window.read()
- if event == sg.WIN_CLOSED or event == 'Закрыть':
- fl = False
- break
- if (event == '1'):
- val = 1
- break
- if (event == '2'):
- val = 2
- break
- lambd = values['-SLIDER-']
- mu = values['-SLIDER2-']
- s = values['-SLIDER3-']
- if (lambd == mu):
- window["-OUT-"].update('Деление на 0')
- else:
- cost = calc()
- window["-OUT-"].update(f'Значение функции стоимости {cost}')
- if event in (sg.WIN_CLOSED, 'Выход'): # always, always give a way out!
- break
- elif event == 'Посторить график' and lambd != mu:
- plt.clf()
- # ------------------------------- PASTE YOUR MATPLOTLIB CODE HERE
- plt.figure(1)
- fig = plt.gcf()
- DPI = fig.get_dpi()
- # ------------------------------- 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
- fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
- # -------------------------------
- x = []
- y = []
- for s in range(1, 20):
- C_p = get_C_p(s)
- y.append(C_p)
- x.append(s)
- plt.plot(x, y)
- plt.title('cost = f(robots)')
- plt.xlabel('Роботы')
- plt.ylabel('Стоимость')
- plt.grid()
- #print(x)
- #print(y)
- # ------------------------------- Instead of plt.show()
- draw_figure_w_toolbar(window['fig_cv'].TKCanvas, fig, window['controls_cv'].TKCanvas)
- window.close()
- lambd = 0.5
- mu = 0.4
- C_w = 50
- C_b = 0.1
- C_r = 60
- C_i = 10
- C_s = 0.5
- t_m = 0.13
- t_vis = 0.1
- t_p = 0.12
- t_r = 0.125
- t_t = 0.15
- ####
- t_c = 0
- C_p = 0
- p_n = []
- L_q = 0
- L = 0
- w_q = 0
- w_a = 0
- t_c = get_t_c(1)
- _min = 1e10
- arr_cost = []
- arr_s = np.linspace(0, 15, 16)
- cost = 1
- s = 1
- fl = True
- val = 0
- fl = True
- L_q = get_L_q()
- #print(L_q)
- pi = math.acos(-1)
- n = 340
- w = 250
- d = 14.4
- a = d/2
- #a = 95.5428
- #rb = 1.49681
- #Nf = 1.12468
- #c = 2.628867911(посчитано)
- #2.628867911
- v = 256
- p_b = (w - d - w * a)**2/(w - d)**2
- c = w*pi*(a+d)**2/(w-d)**2/v
- r_b = 1/c
- print('p_b = ', p_b)
- print('r_b = ', r_b)
- print('c = ', c)
- Nf = (w/v)*r_b*p_b*math.exp(1)**(-r_b*c)
- Nf = Nf // 1
- print('Nf = ', Nf)
- bot = (w/v)+t_p+Nf*t_c
- bot = bot // 1
- T_r = Nf/(bot)
- print('T_r = ', T_r)
- while (fl != True):
- #print(val)
- ############################## 0 ##############################
- if (val == 0):
- layout0()
- ###############################################################
- ############################## 1 ##############################
- if (val == 1):
- layout1()
- ###############################################################
- ############################## 2 ##############################
- if (val == 2):
- layout2()
- ###############################################################
- #print(arr_s)
- #print(len(arr_cost))
- #print(arr_cost)
- #print(len(arr_s))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement