コード例 #1
0
    def tstep(self):
        """
        takes a time step in the simulation
        """

        # check destinations if active
        # define motion vectors if destinations active and not everybody is at destination
        active_dests = len(
            self.population[self.population[:, 11] != 0]
        )  # look op this only once

        if active_dests > 0 and len(self.population[self.population[:, 12] == 0]) > 0:
            self.population = set_destination(self.population, self.destinations)
            self.population = check_at_destination(
                self.population,
                self.destinations,
                wander_factor=self.Config.wander_factor_dest,
                speed=self.Config.speed,
            )

        if active_dests > 0 and len(self.population[self.population[:, 12] == 1]) > 0:
            # keep them at destination
            self.population = keep_at_destination(
                self.population, self.destinations, self.Config.wander_factor
            )

        # out of bounds
        # define bounds arrays, excluding those who are marked as having a custom destination
        if len(self.population[:, 11] == 0) > 0:
            _xbounds = np.array(
                [[self.Config.xbounds[0] + 0.02, self.Config.xbounds[1] - 0.02]]
                * len(self.population[self.population[:, 11] == 0])
            )
            _ybounds = np.array(
                [[self.Config.ybounds[0] + 0.02, self.Config.ybounds[1] - 0.02]]
                * len(self.population[self.population[:, 11] == 0])
            )
            self.population[self.population[:, 11] == 0] = out_of_bounds(
                self.population[self.population[:, 11] == 0], _xbounds, _ybounds
            )

        # set randoms
        if self.Config.lockdown:
            if len(self.pop_tracker.infectious) == 0:
                mx = 0
            else:
                mx = np.max(self.pop_tracker.infectious)

            if len(self.population[self.population[:, 6] == 1]) >= len(
                self.population
            ) * self.Config.lockdown_percentage or mx >= (
                len(self.population) * self.Config.lockdown_percentage
            ):
                # reduce speed of all members of society
                self.population[:, 5] = np.clip(
                    self.population[:, 5], a_min=None, a_max=0.001
                )
                # set speeds of complying people to 0
                self.population[:, 5][self.Config.lockdown_vector == 0] = 0
            else:
                # update randoms
                self.population = update_randoms(
                    self.population, self.Config.pop_size, self.Config.speed
                )
        else:
            # update randoms
            self.population = update_randoms(
                self.population, self.Config.pop_size, self.Config.speed
            )

        # for dead ones: set speed and heading to 0
        self.population[:, 3:5][self.population[:, 6] == 3] = 0

        # update positions
        self.population = update_positions(self.population)

        # find new infections
        self.population, self.destinations = infect(
            self.population,
            self.Config,
            self.frame,
            send_to_location=self.Config.self_isolate,
            location_bounds=self.Config.isolation_bounds,
            destinations=self.destinations,
            location_no=1,
            location_odds=self.Config.self_isolate_proportion,
        )

        # recover and die
        self.population = recover_or_die(self.population, self.frame, self.Config)

        # send cured back to population if self isolation active
        # perhaps put in recover or die class
        # send cured back to population
        self.population[:, 11][self.population[:, 6] == 2] = 0

        # update population statistics
        self.pop_tracker.update_counts(self.population)

        # visualise
        if self.Config.visualise:
            draw_tstep(
                self.Config,
                self.population,
                self.pop_tracker,
                self.frame,
                self.fig,
                self.spec,
                self.ax1,
                self.ax2,
            )

        # report stuff to console
        sys.stdout.write("\r")
        sys.stdout.write(
            "%i: healthy: %i, infected: %i, immune: %i, in treatment: %i, \
dead: %i, of total: %i"
            % (
                self.frame,
                self.pop_tracker.susceptible[-1],
                self.pop_tracker.infectious[-1],
                self.pop_tracker.recovered[-1],
                len(self.population[self.population[:, 10] == 1]),
                self.pop_tracker.fatalities[-1],
                self.Config.pop_size,
            )
        )

        # save popdata if required
        if self.Config.save_pop and (self.frame % self.Config.save_pop_freq) == 0:
            save_population(self.population, self.frame, self.Config.save_pop_folder)
        # run callback
        self.callback()

        # update frame
        self.frame += 1
コード例 #2
0
    def tstep(self):
        '''
        takes a time step in the simulation
        '''
        start = time.time()  # start clock
        #======================================================================================#
        #check destinations if active
        #define motion vectors if destinations active and not everybody is at destination
        active_dests = len(self.population[
            self.population[:, 11] != 0])  # look op this only once

        if active_dests > 0 and len(
                self.population[self.population[:, 12] == 0]) > 0:
            self.population = set_destination(self.population,
                                              self.destinations)
            self.population = check_at_destination(
                self.population,
                self.destinations,
                wander_factor=self.Config.wander_factor_dest)

        if active_dests > 0 and len(
                self.population[self.population[:, 12] == 1]) > 0:
            #keep them at destination
            self.population = keep_at_destination(self.population,
                                                  self.Config.isolation_bounds)

        #======================================================================================#
        #gravity wells
        if self.Config.gravity_strength > 0:
            [self.population, self.last_step_change] = update_gravity_forces(
                self.population, self.time, self.last_step_change,
                self.Config.wander_step_size, self.Config.gravity_strength,
                self.Config.wander_step_duration)

        #======================================================================================#
        #activate social distancing above a certain infection threshold
        if not self.above_act_thresh and self.Config.social_distance_threshold_on > 0:
            # If not previously above infection threshold activate when threshold reached
            if self.Config.thresh_type == 'hospitalized':
                self.above_act_thresh = sum(
                    self.population[:, 11] ==
                    1) >= self.Config.social_distance_threshold_on
            elif self.Config.thresh_type == 'infected':
                self.above_act_thresh = sum(
                    self.population[:, 6] ==
                    1) >= self.Config.social_distance_threshold_on
        elif self.Config.social_distance_threshold_on == 0:
            self.above_act_thresh = True

        #deactivate social distancing after infection drops below threshold after using social distancing
        if self.above_act_thresh and not self.above_deact_thresh and self.Config.social_distance_threshold_off > 0:
            # If previously went above infection threshold deactivate when threshold reached
            self.above_deact_thresh = sum(self.population[:,6][self.population[:,11] == 0] == 1) <= \
                                       self.Config.social_distance_threshold_off

        # activate social distancing at the onset of infection
        if not self.Config.SD_act_onset:
            act_social_distancing = self.above_act_thresh and not self.above_deact_thresh and sum(
                self.population[:, 6] == 1) > 0
        # activate social distancing from start of simulation
        elif self.Config.SD_act_onset:
            act_social_distancing = self.above_act_thresh and not self.above_deact_thresh

        #activate social distancing only for compliant individuals
        if self.Config.social_distance_factor > 0 and act_social_distancing:
            self.population[(self.population[:,17] == 0) &\
                            (self.population[:,11] == 0)] = update_repulsive_forces(self.population[(self.population[:,17] == 0) &\
                                                                                                    (self.population[:,11] == 0)], self.Config.social_distance_factor)
        #======================================================================================#
        #out of bounds
        #define bounds arrays, excluding those who are marked as having a custom destination
        if len(self.population[:, 11] == 0) > 0:
            buffer = 0.0
            _xbounds = np.array([[
                self.Config.xbounds[0] + buffer,
                self.Config.xbounds[1] - buffer
            ]] * len(self.population[self.population[:, 11] == 0]))
            _ybounds = np.array([[
                self.Config.ybounds[0] + buffer,
                self.Config.ybounds[1] - buffer
            ]] * len(self.population[self.population[:, 11] == 0]))

            self.population[self.population[:, 11] == 0] = update_wall_forces(
                self.population[self.population[:, 11] == 0], _xbounds,
                _ybounds)

        #======================================================================================#
        #update velocities
        self.population[(self.population[:,11] == 0) |\
                        (self.population[:,12] == 1)] = update_velocities(self.population[(self.population[:,11] == 0) |\
                                                                                          (self.population[:,12] == 1)],
                                                                                          self.Config.max_speed,self.Config.dt)

        #for dead ones: set velocity and social distancing to 0 for dead ones
        self.population[:, 3:5][self.population[:, 6] == 3] = 0
        self.population[:, 17][self.population[:, 6] == 3] = 1

        #update positions
        self.population = update_positions(self.population, self.Config.dt)

        #======================================================================================#
        #find new infections

        if not self.above_test_thresh and self.Config.testing_threshold_on > 0:
            # If not previously above infection threshold activate when threshold reached
            self.above_test_thresh = sum(
                self.population[:, 6] == 1) >= self.Config.testing_threshold_on
            # self.above_test_thresh = sum(self.population[:,6] == 1) >= self.Config.social_distance_threshold_on
        elif self.Config.testing_threshold_on == 0:
            self.above_test_thresh = True

        act_testing = self.above_test_thresh and sum(
            self.population[:, 6] == 1) > 0

        self.population, self.destinations = infect(
            self.population,
            self.Config,
            self.frame,
            send_to_location=self.Config.self_isolate,
            location_bounds=self.Config.isolation_bounds,
            destinations=self.destinations,
            location_no=1,
            location_odds=self.Config.self_isolate_proportion,
            test_flag=act_testing)

        #recover and die
        self.population = recover_or_die(self.population, self.frame,
                                         self.Config)

        #======================================================================================#
        #send cured back to population if self isolation active
        #perhaps put in recover or die class
        #send cured back to population
        self.population[:, 11][self.population[:, 6] == 2] = 0

        #======================================================================================#
        #update population statistics
        self.pop_tracker.update_counts(self.population, self.frame)

        #======================================================================================#
        #visualise
        if self.Config.visualise and (
                self.frame % self.Config.visualise_every_n_frame) == 0:
            draw_tstep(self.Config, self.population, self.pop_tracker,
                       self.frame, self.fig, self.spec, self.ax1, self.ax2,
                       self.tight_bbox)

        #report stuff to console
        if (self.Config.verbose) and ((self.frame % self.Config.report_freq)
                                      == 0):
            end = time.time()
            time_elapsed = end - start  # elapsed time

            sys.stdout.write('\r')
            sys.stdout.write(
                '%i: S: %i, I: %i, R: %i, in treatment: %i, F: %i, of total: %i, D: %.5f, GC: %.5f, time: %.5f'
                % (self.frame, self.pop_tracker.susceptible[-1],
                   self.pop_tracker.infectious[-1],
                   self.pop_tracker.recovered[-1],
                   len(self.population[self.population[:, 10] == 1]),
                   self.pop_tracker.fatalities[-1], self.Config.pop_size,
                   self.pop_tracker.distance_travelled[-1],
                   self.pop_tracker.mean_perentage_covered[-1], time_elapsed))

        #save popdata if required
        if self.Config.save_pop and (self.frame %
                                     self.Config.save_pop_freq) == 0:
            save_population(self.population, self.frame,
                            self.Config.save_pop_folder)
        #run callback
        self.callback()

        #======================================================================================#
        #update frame
        self.frame += 1
        self.time += self.Config.dt
コード例 #3
0
def tstep(config, vir, pop, pop_tracker, soc, fig, spec, ax1, ax2):
    '''
    takes a time step in the simulation
    '''

    #check destinations if active
    #define motion vectors if destinations active and not everybody is at destination
    active_dests = len(
        pop.population[pop.population[:, 11] != 0])  # look op this only once

    if active_dests > 0 and len(pop.population[pop.population[:,
                                                              12] == 0]) > 0:
        pop.population = pop.set_destination()
        pop.population = pop.check_at_destination()

    if active_dests > 0 and len(pop.population[pop.population[:,
                                                              12] == 1]) > 0:
        #keep them at destination
        pop.population = pop.keep_at_destination()

    #out of bounds
    #define bounds arrays, excluding those who are marked as having a custom destination
    if len(pop.population[:, 11] == 0) > 0:
        _xbounds = np.array(
            [[config.xbounds[0] + 0.02, config.xbounds[1] - 0.02]] *
            len(pop.population[pop.population[:, 11] == 0]))
        _ybounds = np.array(
            [[config.ybounds[0] + 0.02, config.ybounds[1] - 0.02]] *
            len(pop.population[pop.population[:, 11] == 0]))
        pop.population[pop.population[:, 11] == 0] = out_of_bounds(
            pop.population[pop.population[:, 11] == 0], _xbounds, _ybounds)

    #set randoms
    if soc.lockdown:
        if len(pop_tracker.infectious) == 0:
            mx = 0
        else:
            #mx = np.max(pop_tracker.infectious)
            mx = pop_tracker.infectious[-1]

        if len(pop.population[pop.population[:,6] == 1]) >= len(pop.population) * soc.lockdown_percentage or\
           mx >= (len(pop.population) * soc.lockdown_percentage) or soc.lockdown_act:
            soc.lockdown_act = True
            #reduce speed of all members of society
            pop.population[:, 5] = np.clip(pop.population[:, 5],
                                           a_min=None,
                                           a_max=0.00001)
            #set speeds of complying people to 0
            pop.population[:, 5][soc.lockdown_vector == 0] = 0
            if len(pop.population[pop.population[:, 6] == 1]) <= len(
                    pop.population) * soc.lockdown_percentage / 2:
                soc.lockdown_act = False
        else:
            #update randoms
            pop.population = update_randoms(pop.population, pop.pop_size,
                                            pop.speed)
    else:
        #update randoms
        pop.population = update_randoms(pop.population, pop.pop_size,
                                        pop.speed)

    #for dead ones: set speed and heading to 0
    pop.population[:, 3:5][pop.population[:, 6] == 3] = 0

    #update positions
    pop.population = update_positions(pop.population)

    #find new infections
    pop.population, pop.destinations = vir.infect(
        pop,
        soc,
        config,
        send_to_location=soc.self_isolate,
        location_bounds=soc.isolation_bounds,
        destinations=pop.destinations,
        location_no=1,
        location_odds=soc.self_isolate_proportion)

    #recover and die
    pop.population = vir.recover_or_die(pop, soc, config)

    #send cured back to population if self isolation active
    #perhaps put in recover or die class
    #send cured back to population
    pop.population[:, 11][pop.population[:, 6] == 2] = 0

    #update population statistics
    pop_tracker.update_counts(pop.population)

    #visualise
    if config.visualise:
        draw_tstep(config, soc, pop.pop_size, pop.population, pop_tracker,
                   config.frame, fig, spec, ax1, ax2)

    #report stuff to console
    sys.stdout.write('\r')
    sys.stdout.write(
        '%i: healthy: %i, infected: %i, immune: %i, in treatment: %i, \
dead: %i, of total: %i' %
        (config.frame, pop_tracker.susceptible[-1], pop_tracker.infectious[-1],
         pop_tracker.recovered[-1],
         len(pop.population[pop.population[:, 10] == 1]),
         pop_tracker.fatalities[-1], pop.pop_size))

    #save popdata if required
    if config.save_pop and (config.frame % config.save_pop_freq) == 0:
        pop.save_population(pop.population, config.frame,
                            config.save_pop_folder)
    #run callback
    callback(pop, config)

    #update frame
    config.frame += 1
コード例 #4
0
    def tstep(self):
        '''
        take time step as days
        '''
        active_dests = len(self.pop[self.pop[:, 11] != 0])
        # shows population matrix

        if active_dests > 0 and len(self.pop[self.pop[:, 12] == 0]) > 0:
            self.pop = set_destination(self.pop, self.destinations)
            self.pop = check_at_destination(
                self.pop,
                self.destinations,
                wander_factor=self.Config.wander_factor_dest,
                speed=self.Config.speed)

        if active_dests > 0 and len(self.pop[self.pop[:, 12] == 1]) > 0:
            #keep them at destination
            self.pop = keep_at_destination(self.pop, self.destinations,
                                           self.Config.wander_factor)

        if len(self.pop[:, 11] == 0) > 0:
            _xbounds = np.array([[
                self.Config.xbounds[0] + 0.02, self.Config.xbounds[1] - 0.02
            ]] * len(self.pop[self.pop[:, 11] == 0]))
            _ybounds = np.array([[
                self.Config.ybounds[0] + 0.02, self.Config.ybounds[1] - 0.02
            ]] * len(self.pop[self.pop[:, 11] == 0]))
            self.pop[self.pop[:, 11] == 0] = out_of_bounds(
                self.pop[self.pop[:, 11] == 0], _xbounds, _ybounds)

        #set randoms
        if self.Config.lockdown:
            if len(self.pop_tracker.infectious) == 0:
                mx = 0
            else:
                mx = np.max(self.pop_tracker.infectious)

            if len(self.pop[self.pop[:,6] == 1]) >= len(self.pop) * self.Config.lockdown_percentage or\
               mx >= (len(self.pop) * self.Config.lockdown_percentage):
                #reduce speed for all population
                self.pop[:, 5] = np.clip(self.pop[:, 5],
                                         a_min=None,
                                         a_max=0.001)
                #set speeds of complying people to 0
                self.pop[:, 5][self.Config.lockdown_vector == 0] = 0
            else:
                #random update
                self.pop = update_randoms(self.pop, self.Config.size_pop,
                                          self.Config.speed)
        else:
            self.pop = update_randoms(self.pop, self.Config.size_pop,
                                      self.Config.speed)

        #dead people: set speed and heading to 0
        self.pop[:, 3:5][self.pop[:, 6] == 3] = 0

        #update positions
        self.pop = update_positions(self.pop)

        #find new infections
        self.pop, self.destinations = infect(
            self.pop,
            self.Config,
            self.frame,
            send_to_location=self.Config.self_isolate,
            location_bounds=self.Config.isolation_bounds,
            destinations=self.destinations,
            location_no=1,
            location_odds=self.Config.self_isolate_proportion)

        #set self for recover and die
        self.pop = recover_or_die(self.pop, self.frame, self.Config)

        #send cured back to pop if self isolation active or  put in recover or die class
        self.pop[:, 11][self.pop[:, 6] == 2] = 0
        self.pop_tracker.update_counts(self.pop)

        #visualise
        if self.Config.visualise:
            draw_tstep(self.Config, self.pop, self.pop_tracker, self.frame,
                       self.figure, self.spec, self.ax1, self.ax2)

        #report stuff to console
        sys.stdout.write('\r')
        sys.stdout.write(
            '%i: healthy: %i, infected: %i, immune: %i, in treatment: %i, \
        dead: %i, of total: %i' %
            (self.frame, self.pop_tracker.susceptible[-1],
             self.pop_tracker.infectious[-1], self.pop_tracker.recovered[-1],
             len(self.pop[self.pop[:, 10] == 1]),
             self.pop_tracker.fatalities[-1], self.Config.size_pop))

        #save popdata if required
        if self.Config.save_pop and (self.frame %
                                     self.Config.save_pop_freq) == 0:
            save_population(self.pop, self.frame, self.Config.save_pop_folder)
        #run callback
        self.callback()
        self.frame += 1
コード例 #5
0
    def tstep(self):

        if self.frame == 0:
            self.fig, self.spec, self.ax1, self.ax2 = build_fig(self.Config)

        xbounds = np.array(
            [[self.Config.xbounds[0] + 0.02, self.Config.xbounds[1] - 0.02]] *
            self.Config.pop_size)
        ybounds = np.array(
            [[self.Config.ybounds[0] + 0.02, self.Config.ybounds[1] - 0.02]] *
            self.Config.pop_size)
        self.population = out_of_bounds(self.population, xbounds, ybounds)

        if self.Config.is_lockdown and self.Config.lockdown == False:
            if len(self.population[(self.population[:, 6] == 1)]
                   ) >= self.Config.lockdown_percentage * self.Config.pop_size:
                self.Config.lockdown = True
                print("\nLockdown Started")

        left_range = [
            self.Config.xbounds[0] + 0.02, self.Config.xbounds[1] / 3 - 0.02
        ]
        mid_range = [
            self.Config.xbounds[1] / 3 + 0.02,
            2 * self.Config.xbounds[1] / 3 - 0.02
        ]
        right_range = [
            2 * self.Config.xbounds[1] / 3 + 0.02,
            self.Config.xbounds[1] - 0.02
        ]

        bottom_range = [
            self.Config.ybounds[0] + 0.02, self.Config.ybounds[1] / 2 - 0.02
        ]
        top_range = [
            self.Config.ybounds[1] / 2 + 0.02, self.Config.ybounds[1] - 0.02
        ]

        left_condition = (self.population[:, 1] <= self.Config.xbounds[1] / 3)
        mid_condition = (
            self.population[:, 1] > self.Config.xbounds[1] / 3) & (
                self.population[:, 1] <= 2 * self.Config.xbounds[1] / 3)
        right_condition = (self.population[:, 1] >
                           2 * self.Config.xbounds[1] / 3)

        bottom_condition = (self.population[:, 2] <=
                            self.Config.ybounds[1] / 2)
        top_condition = (self.population[:, 2] > self.Config.ybounds[1] / 2)

        if self.Config.lockdown:

            x_left_bottom = np.array(
                [left_range] *
                len(self.population[left_condition & bottom_condition]))
            y_left_bottom = np.array(
                [bottom_range] *
                len(self.population[left_condition & bottom_condition]))
            self.population[left_condition & bottom_condition] = out_of_bounds(
                self.population[left_condition & bottom_condition],
                x_left_bottom, y_left_bottom)

            x_left_top = np.array(
                [left_range] *
                len(self.population[left_condition & top_condition]))
            y_left_top = np.array(
                [top_range] *
                len(self.population[left_condition & top_condition]))
            self.population[left_condition & top_condition] = out_of_bounds(
                self.population[left_condition & top_condition], x_left_top,
                y_left_top)

            x_mid_bottom = np.array(
                [mid_range] *
                len(self.population[mid_condition & bottom_condition]))
            y_mid_bottom = np.array(
                [bottom_range] *
                len(self.population[mid_condition & bottom_condition]))
            self.population[mid_condition & bottom_condition] = out_of_bounds(
                self.population[mid_condition & bottom_condition],
                x_mid_bottom, y_mid_bottom)

            x_mid_top = np.array(
                [mid_range] *
                len(self.population[mid_condition & top_condition]))
            y_mid_top = np.array(
                [top_range] *
                len(self.population[mid_condition & top_condition]))
            self.population[mid_condition & top_condition] = out_of_bounds(
                self.population[mid_condition & top_condition], x_mid_top,
                y_mid_top)

            x_right_bottom = np.array(
                [right_range] *
                len(self.population[right_condition & bottom_condition]))
            y_right_bottom = np.array(
                [bottom_range] *
                len(self.population[right_condition & bottom_condition]))
            self.population[right_condition
                            & bottom_condition] = out_of_bounds(
                                self.population[right_condition
                                                & bottom_condition],
                                x_right_bottom, y_right_bottom)

            x_right_top = np.array(
                [right_range] *
                len(self.population[right_condition & top_condition]))
            y_right_top = np.array(
                [top_range] *
                len(self.population[right_condition & top_condition]))
            self.population[right_condition & top_condition] = out_of_bounds(
                self.population[right_condition & top_condition], x_right_top,
                y_right_top)

        self.population = update_randoms(self.population, self.Config.pop_size,
                                         self.Config.speed)

        self.population[:, 5][self.population[:, 6] == 3] = 0

        self.population = update_positions(self.population)

        self.population = infect(self.population, self.Config, self.frame)

        self.population = recover_or_die(self.population, self.frame,
                                         self.Config)

        self.pop_tracker.update_counts(self.population)

        draw_tstep(self.Config, self.population, self.pop_tracker, self.frame,
                   self.fig, self.spec, self.ax1, self.ax2)

        self.peak_infections = max(
            self.peak_infections,
            len(self.population[self.population[:, 6] == 1]))

        if self.frame == 50:
            print('\ninfecting patient zero')
            self.population[0][6] = 1

        self.frame += 1