Commit 7ccb987d authored by vektoririna's avatar vektoririna

Результаты

parents
Pipeline #6983 failed with stages
in 34 seconds
File added
import PySimpleGUI as sg
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import random
# функция выбора направления движения
def choosingWay():
way_list = ['north', 'south', 'west', 'east']
return random.choice(way_list)
# функция построения пути
def moving(time):
x, y = 0, 0
for _ in range(time):
way = choosingWay()
if way == 'north':
y += 1
elif way == 'south':
y -= 1
elif way == 'west':
x -= 1
else:
x += 1
return max(abs(x), abs(y))
# функция проведения эксперимента
def experiment(numberOfExp, pastTime):
s = 0
for _ in range(numberOfExp):
s += moving(pastTime)
s = round(s / numberOfExp)
result = 'Вероятнее всего крокодила нужно искать в ' + str(s) + '-х кварталах'
d = []
for __ in range(10):
t_x, t_y = [0], [0]
x, y = 0, 0
for _ in range(pastTime):
way = choosingWay()
if way == 'north':
y += 1
elif way == 'south':
y -= 1
elif way == 'west':
x -= 1
else:
x += 1
t_x.append(x)
t_y.append(y)
d.append(t_x)
d.append(t_y)
fig = draw_map(pastTime, s, d)
# fig.show()
return fig
def draw_map(n, s, d):
fig, ax = plt.subplots(figsize=(5, 5))
ax.plot(*d, marker='o')
ax.plot((-s, s, s, -s, -s), (s, s, -s, -s, s), linestyle='dotted', color='red')
ax.set_xlim(-n, n)
ax.set_ylim(-n, n)
ax.set_title('СЕВЕР')
ax.set_xlabel('ЮГ')
ax.set_ylabel('ЗАПАД')
ax.legend('0123456789', loc='upper left')
ax.grid()
ax.annotate('ZOO', xy=(0, 0), xytext=(0.1, 0.1))
ax.text(-s + 0.1, s - 0.4, 'Район поиска')
ax.text(-n + 0.5, -n + 0.5, 'Крокодила нужно искать в ' + str(s) + '-х кварталах')
return fig
# experiment(1000, 5)
matplotlib.use('TkAgg')
def draw_figure(canvas, figure):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure_canvas_agg
def delete_fig_agg(agg):
agg.get_tk_widget().forget()
plt.close('all')
sg.theme('Material2')
frame_left = [
[sg.Text('01'), sg.Text('Сколько времени прошло?')],
[sg.Text('Часов:'), sg.InputText(size=(5, 1), key='hours'), sg.Text('Минут:'),
sg.InputText(size=(5, 1), key='minutes')],
[sg.Text('02'), sg.Text('Сколько раз вы хотите провести эксперимент?', justification='center')],
[sg.Text('Сделать'), sg.InputText(size=(5, 1), key='number'), sg.Text('повторений')],
[sg.Submit('Запустить'), sg.Cancel('Закрыть')]
]
frame_right = [
[sg.Canvas(key='-CANVAS-', size=(450, 650))]
]
layout = [
[sg.Frame('Форма ввода данных', frame_left),
sg.Frame('Результаты', frame_right)],
]
# Create the Window
window = sg.Window('Wandering model', layout, finalize=True, size=(850, 600))
# Event Loop to process "events" and get the "values" of the inputs
fig_agg = None
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Закрыть': # if user closes window or clicks cancel
break
if event == 'Запустить':
pastTime = int(values['hours'])
numberOfExp = int(values['number'])
fig = experiment(numberOfExp, pastTime)
if fig_agg is not None:
delete_fig_agg(fig_agg)
canvas_elem = window['-CANVAS-'].TKCanvas
canvas_elem.Size = (100, 100)
fig_agg = draw_figure(canvas_elem, fig)
window.close()
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment