diff --git a/environment.py b/environment.py index e48068b..11511fe 100644 --- a/environment.py +++ b/environment.py @@ -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) \ No newline at end of file + + \ No newline at end of file diff --git a/hospital.py b/hospital.py new file mode 100644 index 0000000..ba6dd56 --- /dev/null +++ b/hospital.py @@ -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 + \ No newline at end of file diff --git a/population.py b/population.py index 2879a8b..5bb6c8d 100644 --- a/population.py +++ b/population.py @@ -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 ----------------- diff --git a/school.py b/school.py new file mode 100644 index 0000000..67f39b5 --- /dev/null +++ b/school.py @@ -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 + + + + + + + \ No newline at end of file diff --git a/simulation.py b/simulation.py index 9f6055f..5113e44 100644 --- a/simulation.py +++ b/simulation.py @@ -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,\ diff --git a/visualiser.py b/visualiser.py index fee9195..8f52183 100644 --- a/visualiser.py +++ b/visualiser.py @@ -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): @@ -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]