Esempio n. 1
0
class SchellingModel(Model):
    """Model class for the Schelling segregation model."""
    def __init__(self, density, minority_pc):
        self.density = density
        self.minority_pc = minority_pc

        self.schedule = RandomActivation(self)
        self.grid = GeoSpace(crs='epsg:4326')

        self.happy = 0
        self.datacollector = DataCollector(
            {"happy": lambda m: m.happy})  # Model-level count of happy agents

        self.running = True

        # Set up the grid with patches for every NUTS region
        regions = geojson.load(open('nuts_rg_60M_2013_lvl_2.geojson'))
        self.grid.create_agents_from_GeoJSON(regions,
                                             SchellingAgent,
                                             model=self,
                                             unique_id='NUTS_ID')

        # Set up agents
        for agent in self.grid.agents:
            if random.random() < self.density:
                if random.random() < self.minority_pc:
                    agent.atype = 1
                else:
                    agent.atype = 0
                self.schedule.add(agent)

        # Update the bounding box of the grid and create a new rtree
        self.grid.update_bbox()
        self.grid.create_rtree()

    def step(self):
        """Run one step of the model.

        If All agents are happy, halt the model.
        """
        self.happy = 0  # Reset counter of happy agents
        self.schedule.step()
        self.datacollector.collect(self)

        if self.happy == self.schedule.get_agent_count():
            self.running = False

        self.grid.create_rtree()
Esempio n. 2
0
class RecoveryModel(Model):
    def __init__(self,
                 locations,
                 N1,
                 scheduleClass,
                 initialization=None,
                 seed=None):
        if initialization == None:
            raise Exception("Initialization cannot be none")

        N = len(locations['features']
                ) * N1  #N1 =density of housholds in one building
        #print(N)
        self.landuse = locations['features'][0]['landuse']
        #print(self.landuse[0])
        self.running = True
        self.num_agents = N
        self.schedule = scheduleClass(self)
        self.G = nx.complete_graph(self.num_agents)
        self.nw = NetworkGrid(self.G)
        self.grid = GeoSpace(crs='epsg:4326')
        agent_kwargs = dict(model=self, unique_id='id')
        self.grid.create_agents_from_GeoJSON(locations,
                                             agent=LocationAgent,
                                             **agent_kwargs)
        self.locations = list(self.grid.agents)
        self.initialize(initialization)
        self.datacollector = DataCollector(
            agent_reporters={
                "NewShape": agent_loc,
                "Satisfaction": agent_satisfaction,
                "LandUse": agent_LU,
                "H**o": agent_homo,
                "WorkPlace": agent_workplace
            })
        self.grid.update_bbox()
        self.grid.create_rtree()

    def initialize(self, initialization):
        list_of_random_nodes = random.sample(self.G.nodes(), self.num_agents)

        for i in range(self.num_agents):
            agent, self.landuse1 = initialization.next('Household ' + str(i),
                                                       self)
            self.nw.place_agent(agent, list_of_random_nodes[i])
            self.schedule.add(agent)
            self.grid.add_agent(agent)

    def calculate_homophily(self):
        for i in range(self.G.number_of_nodes()):
            HH_homphily = 0
            total_homophily = 0
            c = 1
            for agent in self.G.node[i]['agent']:
                attr_self = [agent.workplace, agent.income, agent.education]
                agent_location = agent.shape

            for HH in self.locations:
                if HH.shape.contains(agent_location):
                    HH_polygon = HH.shape

            neighbor_nodes = self.nw.get_neighbors(i, include_center=False)
            for node in neighbor_nodes:
                for nbr in self.G.node[node]['agent']:
                    attr_neighbor = [nbr.workplace, nbr.income, nbr.education]
                    self.G[i][node]['weight'] = jaccard_similarity(
                        attr_self, attr_neighbor)
                    total_homophily = total_homophily + jaccard_similarity(
                        attr_self, attr_neighbor)
                    neighbor_point = nbr.shape
                    if HH_polygon.contains(neighbor_point):
                        c = c + 1
                        HH_homphily = HH_homphily + jaccard_similarity(
                            attr_self, attr_neighbor)
                    else:
                        pass

            agent.h**o = total_homophily
            agent.shomo = HH_homphily / c

    def step(self):
        #print (self.schedule.time)
        self.datacollector.collect(self)
        self.schedule.step()
        self.grid.create_rtree()
        self.calculate_homophily()