Skip to content

Environment update #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,35 @@
'''

import numpy as np
from hospital import Hospital
from school import School
'''
This class is used to save all environment change operations,
such as adding hospitals, adding residences, etc.
'''
class Environment:
def __init__(self):
self.building = []

def build_hospital(xmin, xmax, ymin, ymax, plt, addcross=True):
'''builds hospital

Defines hospital and returns wall coordinates for
the hospital, as well as coordinates for a red cross
above it

Keyword arguments
-----------------
xmin : int or float
lower boundary on the x axis

xmax : int or float
upper boundary on the x axis

ymin : int or float
lower boundary on the y axis

ymax : int or float
upper boundary on the y axis

plt : matplotlib.pyplot object
the plot object to which to append the hospital drawing
if None, coordinates are returned
#This function could create the building base on the input(building name, *location)
def create_building(self, building_type, xmin, xmax, ymin, ymax):

if building_type == 'hospital':
hospital = Hospital(xmin, xmax, ymin, ymax)
self.building.append(hospital)
elif building_type == 'school':
school = School(xmin, xmax, ymin, ymax)
self.building.append(school)

# Drawing the building here
def building_applied(self, plt, addcross):
if self.building:
for building in self.building:
building.display(plt,addcross)


Returns
-------
None
'''

#plot walls
plt.plot([xmin, xmin], [ymin, ymax], color = 'black')
plt.plot([xmax, xmax], [ymin, ymax], color = 'black')
plt.plot([xmin, xmax], [ymin, ymin], color = 'black')
plt.plot([xmin, xmax], [ymax, ymax], color = 'black')

#plot red cross
if addcross:
xmiddle = xmin + ((xmax - xmin) / 2)
height = np.min([0.3, (ymax - ymin) / 5])
plt.plot([xmiddle, xmiddle], [ymax, ymax + height], color='red',
linewidth = 3)
plt.plot([xmiddle - (height / 2), xmiddle + (height / 2)],
[ymax + (height / 2), ymax + (height / 2)], color='red',
linewidth = 3)


93 changes: 93 additions & 0 deletions hospital.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from path_planning import set_destination
import numpy as np

'''
builds hospital

Defines hospital and returns wall coordinates for
the hospital, as well as coordinates for a red cross
above it

Keyword arguments
-----------------
xmin : int or float
xmax : int or float
ymin : int or float
ymax : int or float
location showing below:

(xmin,ymax)----------(xmax,ymax)
| |
| Hosipital |
| |
(xmin,ymin)----------(xmax,ymin)

plt : matplotlib.pyplot object
the plot object to which to append the hospital drawing
if None, coordinates are returned

Returns
-------
None
'''
class Hospital:

def __init__(self,xmin, xmax, ymin, ymax):

self.destinations_location = []
self.xmin = xmin
self.xmax = xmax
self.ymin = ymin
self.ymax = ymax

def display(self, plt,addcross):
#plot walls
plt.plot([self.xmin, self.xmin], [self.ymin, self.ymax], color = 'black')
plt.plot([self.xmax, self.xmax], [self.ymin, self.ymax], color = 'black')
plt.plot([self.xmin, self.xmax], [self.ymin, self.ymin], color = 'black')
plt.plot([self.xmin, self.xmax], [self.ymax, self.ymax], color = 'black')
#TODO: setting the hosptial location
#plot red cross
if addcross:
xmiddle = self.xmin + ((self.xmax - self.xmin) / 2)
height = np.min([0.3, (self.ymax - self.ymin) / 5])
plt.plot([xmiddle, xmiddle], [self.ymax, self.ymax + height], color='red',
linewidth = 3)
plt.plot([xmiddle - (height / 2), xmiddle + (height / 2)],
[self.ymax + (height / 2), self.ymax + (height / 2)], color='red',
linewidth = 3)



def go_to_Hospital(self, population, pop_tracker):
#check infection
if len(pop_tracker.infectious) > 0:
#some people who infected will go to the hospital
active_dests = np.unique(population[:,6][population[:,6] == 1])


#Go to hospital:
for d in active_dests:
dest_x = np.random.random(self.xmin,self.xmax)
dest_y = np.random.random(self.ymin,self.ymax)

#compute new headings
head_x = dest_x - population[:,1]
head_y = dest_y - population[:,2]

#head_x = head_x / np.sqrt(head_x)
#head_y = head_y / np.sqrt(head_y)

#reinsert headings into population of those not at destination yet
population[:,3][(population[:,11] == d) &
(population[:,12] == 0)] = head_x[(population[:,11] == d) &
(population[:,12] == 0)]
population[:,4][(population[:,11] == d) &
(population[:,12] == 0)] = head_y[(population[:,11] == d) &
(population[:,12] == 0)]
#set speed to 0.01
population[:,5][(population[:,11] == d) &
(population[:,12] == 0)] = 0.02

return population

1 change: 1 addition & 0 deletions population.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def initialize_population(Config, mean_age=45, max_age=105,
12 : at destination: whether arrived at destination (0=traveling, 1=arrived)
13 : wander_range_x : wander ranges on x axis for those who are confined to a location
14 : wander_range_y : wander ranges on y axis for those who are confined to a location


Keyword arguments
-----------------
Expand Down
91 changes: 91 additions & 0 deletions school.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from path_planning import set_destination
import numpy as np

'''
builds School

Defines school, Young people will go to the school at a fixed time,
and on the map you can see that the students will move in a normal
distribution at a set school location.

Keyword arguments
-----------------
xmin : int or float
xmax : int or float
ymin : int or float
ymax : int or float
location showing below:

(xmin,ymax)----------(xmax,ymax)
| |
| School |
| |
(xmin,ymin)----------(xmax,ymin)

plt : matplotlib.pyplot object
the plot object to which to append the hospital drawing
if None, coordinates are returned

Returns
-------
None
'''
class School:

def __init__(self,xmin, xmax, ymin, ymax):

self.destinations_location = []
self.xmin = xmin
self.xmax = xmax
self.ymin = ymin
self.ymax = ymax

def display(self, plt,addcross):
#plot walls
plt.plot([self.xmin, self.xmin], [self.ymin, self.ymax], color = 'black')
plt.plot([self.xmax, self.xmax], [self.ymin, self.ymax], color = 'black')
plt.plot([self.xmin, self.xmax], [self.ymin, self.ymin], color = 'black')
plt.plot([self.xmin, self.xmax], [self.ymax, self.ymax], color = 'black')
#TODO: setting the hosptial location

def go_to_school(self, population):
max_studying_age = 18

#If popluation exist people who age less than max studying age, then they need
#go to school.
if len(population[:7] < max_studying_age) > 0:
#some people who infected will go to the hospital
active_dests = np.unique(population[:,7][population[:,7] < max_studying_age])


#Go to hospital:
for d in active_dests:
dest_x = np.random.random(self.xmin,self.xmax)
dest_y = np.random.random(self.ymin,self.ymax)

#compute new headings
head_x = dest_x - population[:,1]
head_y = dest_y - population[:,2]

#head_x = head_x / np.sqrt(head_x)
#head_y = head_y / np.sqrt(head_y)

#reinsert headings into population of those not at destination yet
population[:,3][(population[:,11] == d) &
(population[:,12] == 0)] = head_x[(population[:,11] == d) &
(population[:,12] == 0)]
population[:,4][(population[:,11] == d) &
(population[:,12] == 0)] = head_y[(population[:,11] == d) &
(population[:,12] == 0)]
#set speed to 0.5
population[:,5][(population[:,11] == d) &
(population[:,12] == 0)] = 0.05

return population







2 changes: 1 addition & 1 deletion simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from matplotlib.animation import FuncAnimation

from config import Configuration, config_error
from environment import build_hospital

from infection import find_nearby, infect, recover_or_die, compute_mortality,\
healthcare_infection_correction
from motion import update_positions, out_of_bounds, update_randoms,\
Expand Down
14 changes: 10 additions & 4 deletions visualiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import matplotlib as mpl
import numpy as np

from environment import build_hospital
from environment import Environment
from utils import check_folder

def set_style(Config):
Expand Down Expand Up @@ -54,10 +54,16 @@ def draw_tstep(Config, population, pop_tracker, frame,
ax1.set_xlim(Config.x_plot[0], Config.x_plot[1])
ax1.set_ylim(Config.y_plot[0], Config.y_plot[1])

#All sourrouding will install here such as Hospital, working place
environment = Environment()

if Config.self_isolate and Config.isolation_bounds != None:
build_hospital(Config.isolation_bounds[0], Config.isolation_bounds[2],
Config.isolation_bounds[1], Config.isolation_bounds[3], ax1,
addcross = False)

environment.create_building( 'hospital', Config.isolation_bounds[0], Config.isolation_bounds[2],
Config.isolation_bounds[1], Config.isolation_bounds[3])

environment.building_applied(ax1, addcross = True)


#plot population segments
healthy = population[population[:,6] == 0][:,1:3]
Expand Down