import plotly.graph_objects as go import random from plotly.subplots import make_subplots from Agent import Agent from World import World # create a room for Agents world = World() world.populate() world.savePositions() world.lessMovement() frames = [] for step in range(world.steps): # movement for Agents for agent1 in range(world.number_of_agents): world.agents_list[agent1].move() world.updatePostitions(agent1) # check for collision; infection; quarantine for agent2 in range(agent1,world.number_of_agents): world.changeCondition1(world.agents_list[agent1], world.agents_list[agent2], probability = 0.3) world.changeCondition2(world.agents_list[agent1], quarantine = 0) world.updateLists() # save current attributes of Agents (positions, size, color, shape) frames.append(go.Frame(data=[go.Scatter(x=world.x, y=world.y, \ mode = "markers", \ marker=dict(size = 10, color = [world.agents_list[i].color for i in range(world.number_of_agents)], \ )), go.Scatter(y = world.infected_series),go.Scatter(y = world.susceptible_series),go.Scatter(y = world.recovered_series)] )) # ======= VISUALIZATION ======= # properties of the motion field and graphs fig = make_subplots( rows=2, cols=1, row_heights=[4, 2], \ subplot_titles=('Agents Simulation', 'Data Visualization')) # add main square for agents fig.add_shape(type="rect", x0=-Agent.radius, y0=-Agent.radius, x1=world.width+Agent.radius, y1=world.height+Agent.radius, line=dict(color="black") ) # add square for quarantine fig.add_shape(type="rect", x0=world.width+Agent.radius +10, y0=-Agent.radius, x1=world.width+Agent.radius+30, y1=(world.height+Agent.radius)/2, line=dict(color="black") ) # add Agents at the right positions in the main square fig.add_trace(go.Scatter(x = [world.agents_list[i].xstartpos for i in range(world.number_of_agents)], y = [world.agents_list[i].ystartpos for i in range(world.number_of_agents)], name = 'Agents', mode = "markers", \ marker=dict(size = 10, color='cornflowerblue', \ )), row=1, col=1) # add traces for infected, susceptible, recovered fig.add_trace(go.Scatter(y=[0], marker_color = "red", marker_size=1, name = 'Infected'), row=2, col=1) fig.add_trace(go.Scatter(y=[0], marker_color = "cornflowerblue", marker_size=1, name = 'Susceptible'), row=2, col=1) fig.add_trace(go.Scatter(y=[0], marker_color = "gray", marker_size=1, name = 'Recovered'), row=2, col=1) fig.frames=frames # create start button button = dict(label='Start', method='animate', args=[None, dict(frame=dict(duration=50, redraw=False), transition=dict(duration=0), fromcurrent=True, mode='immediate')]) fig.update_layout(updatemenus=[dict(type='buttons', showactive=False, y=0, x=1.05, xanchor='left', yanchor='bottom', buttons=[button] )], width=800, height=500) # set axes properties fig.update_layout(xaxis1_showgrid = False, xaxis1_showticklabels = False, yaxis1_showgrid = False, yaxis1_showticklabels = False, \ xaxis2_range=[0, world.steps], xaxis2_autorange=False, xaxis2_gridcolor = 'lightgrey', xaxis2_linecolor = 'black', yaxis2_range=[0, world.number_of_agents + 1], yaxis2_autorange=False, yaxis2_gridcolor = 'lightgrey', yaxis2_linecolor = 'black') # adjust background fig.update_layout(autosize=False, width=1000, height=1000, paper_bgcolor="white", plot_bgcolor = 'white') # add titel for the quarantine square fig.add_annotation(text ="Quarantine", xref = "paper", yref = "paper", x=0.965, y=0.8, showarrow = False, font = {'size' : 15}) # show the simulation fig.show()