def run(self, person_set, household_set, resources=None):
        index = AgentRelocationModel.run(self, person_set, resources=resources)

        person_ds_name, person_id_name = person_set.get_dataset_name(
        ), person_set.get_id_name()[0]
        hh_ds_name, hh_id_name = person_set.get_dataset_name(
        ), household_set.get_id_name()[0]

        max_hh_id = household_set.get_attribute(hh_id_name).max() + 1
        new_hh_id = arange(max_hh_id, max_hh_id + index.size)
        person_set.modify_attribute(hh_id_name, new_hh_id, index=index)
        household_set.add_elements({hh_id_name: new_hh_id},
                                   require_all_attributes=False)

        logger.log_status("%s children leave home and form new %s." %
                          (index.size, hh_ds_name))

        ##remove records from household_set that have no persons left
        persons = household_set.compute_variables("%s.number_of_agents(%s)" %
                                                  (hh_ds_name, person_ds_name),
                                                  resources=resources)
        index_hh0persons = where(persons == 0)[0]
        if index_hh0persons.size > 0:
            logger.log_status(
                "Removing %s records without %s from %s dataset" %
                (index_hh0persons.size, person_ds_name, hh_ds_name))
            household_set.remove_elements(index_hh0persons)
    def run(self, person_set, household_set, resources=None):
        index = AgentRelocationModel.run(self, person_set, resources=resources)

        person_ds_name, person_id_name = person_set.get_dataset_name(
        ), person_set.get_id_name()[0]
        hh_ds_name, hh_id_name = person_set.get_dataset_name(
        ), household_set.get_id_name()[0]

        max_person_id = person_set.get_attribute(person_id_name).max() + 1
        new_person_id = arange(max_person_id, max_person_id + index.size)

        new_born = {}
        new_born[person_id_name] = new_person_id
        new_born[hh_id_name] = person_set.get_attribute(hh_id_name)[index]
        new_born['age'] = ones(index.size, dtype="int32")
        ##TODO: give better default values
        new_born['sex'] = randint(
            2, size=index.size)  ##TODO: better way to assign sex?
        #new_born['education']
        #new_born['job_id']
        #new_born['race_id']
        #new_born['relation']

        logger.log_status("Adding %s records to %s dataset" %
                          (index.size, person_set.get_dataset_name()))
        person_set.add_elements(data=new_born,
                                require_all_attributes=False,
                                change_ids_if_not_unique=True)
 def test_return_unplaced_if_no_probabilities(self):
     """If no probabilities are passed, the model should only return indices of unplaced agents."""
     storage = StorageFactory().get_storage('dict_storage')
     
     storage.write_table(table_name = 'households',
         table_data = {
             'household_id': arange(1500)+1,
             'grid_id': array(1000*[1] + 500*[0]), # 500 households unplaced
             }
         )
     hh_set = HouseholdDataset(in_storage=storage, in_table_name='households')
     
     arm = AgentRelocationModel(location_id_name="grid_id")
     result = arm.run(hh_set)
     
     should_be = arange(1000, 1500)
     
     self.assertEqual(ma.allequal(result, should_be), True, msg = "Error " )
Example #4
0
 def get_model(self, choices = "opus_core.random_choices",
               probabilities = "urbansim.employment_relocation_probabilities",
               location_id_name = "grid_id",
               debuglevel=0):
     return AgentRelocationModel(probabilities = probabilities,
                                 choices = choices,
                                 location_id_name = location_id_name,
                                 model_name="Employment Relocation Model",
                                 debuglevel=debuglevel)
    def run(self, person_set, household_set, resources=None):
        person_ds_name = person_set.get_dataset_name()
        hh_ds_name = household_set.get_dataset_name()
        
        index = AgentRelocationModel.run(self, person_set, resources=resources)

        logger.log_status("Removing %s records from %s dataset" % (index.size, person_set.get_dataset_name()) )
        person_set.remove_elements(index)
        
        ##remove records from household_set that have no persons left
        persons = household_set.compute_variables("%s.number_of_agents(%s)" % (hh_ds_name, person_ds_name), resources=resources)
        index_hh0persons = where(persons==0)[0]
        if index_hh0persons.size > 0:
            logger.log_status("Removing %s records without %s from %s dataset" % (index_hh0persons.size, person_ds_name, hh_ds_name) )
            household_set.remove_elements(index_hh0persons)
    def run(self, agent_set, **kwargs):

        large_areas = agent_set.get_attribute(self.large_area_id_name)
        valid_large_area = where(large_areas > 0)[0]
        if valid_large_area.size > 0:
            unique_large_areas = unique(large_areas[valid_large_area])
            cond_array = zeros(agent_set.size(), dtype="bool8")
            cond_array[valid_large_area] = True
            result = array([], dtype="int32")
            for area in unique_large_areas:
                new_index = where(logical_and(cond_array, large_areas == area))[0]
                agent_subset =  DatasetSubset(agent_set, new_index)
                logger.log_status("ARM for area %s (%s agents)" % (area, agent_subset.size()))
                this_result = AgentRelocationModel.run(self, agent_subset, **kwargs)
                result = concatenate((result, new_index[this_result]))
        no_large_area = where(large_areas <= 0)[0]
        result = concatenate((result, no_large_area))
        return result
    def run(self, person_set, household_set, resources=None):
        person_ds_name = person_set.get_dataset_name()
        hh_ds_name = household_set.get_dataset_name()

        index = AgentRelocationModel.run(self, person_set, resources=resources)

        logger.log_status("Removing %s records from %s dataset" %
                          (index.size, person_set.get_dataset_name()))
        person_set.remove_elements(index)

        ##remove records from household_set that have no persons left
        persons = household_set.compute_variables("%s.number_of_agents(%s)" %
                                                  (hh_ds_name, person_ds_name),
                                                  resources=resources)
        index_hh0persons = where(persons == 0)[0]
        if index_hh0persons.size > 0:
            logger.log_status(
                "Removing %s records without %s from %s dataset" %
                (index_hh0persons.size, person_ds_name, hh_ds_name))
            household_set.remove_elements(index_hh0persons)
    def run(self, agent_set, **kwargs):

        large_areas = agent_set.get_attribute(self.large_area_id_name)
        valid_large_area = where(large_areas > 0)[0]
        if valid_large_area.size > 0:
            unique_large_areas = unique(large_areas[valid_large_area])
            cond_array = zeros(agent_set.size(), dtype="bool8")
            cond_array[valid_large_area] = True
            result = array([], dtype="int32")
            for area in unique_large_areas:
                new_index = where(logical_and(cond_array,
                                              large_areas == area))[0]
                agent_subset = DatasetSubset(agent_set, new_index)
                logger.log_status("ARM for area %s (%s agents)" %
                                  (area, agent_subset.size()))
                this_result = AgentRelocationModel.run(self, agent_subset,
                                                       **kwargs)
                result = concatenate((result, new_index[this_result]))
        no_large_area = where(large_areas <= 0)[0]
        result = concatenate((result, no_large_area))
        return result
    def run(self, person_set, household_set, resources=None):
        index = AgentRelocationModel.run(self, person_set, resources=resources)

        person_ds_name, person_id_name = person_set.get_dataset_name(), person_set.get_id_name()[0]
        hh_ds_name, hh_id_name = person_set.get_dataset_name(), household_set.get_id_name()[0]

        max_hh_id = household_set.get_attribute(hh_id_name).max() + 1
        new_hh_id = arange(max_hh_id, max_hh_id + index.size)
        person_set.modify_attribute(hh_id_name, new_hh_id, index=index)
        household_set.add_elements({hh_id_name: new_hh_id}, require_all_attributes=False)

        logger.log_status("%s children leave home and form new %s." % (index.size, hh_ds_name))

        ##remove records from household_set that have no persons left
        persons = household_set.compute_variables(
            "%s.number_of_agents(%s)" % (hh_ds_name, person_ds_name), resources=resources
        )
        index_hh0persons = where(persons == 0)[0]
        if index_hh0persons.size > 0:
            logger.log_status(
                "Removing %s records without %s from %s dataset" % (index_hh0persons.size, person_ds_name, hh_ds_name)
            )
            household_set.remove_elements(index_hh0persons)
    def run(self, person_set, household_set, resources=None):
        index = AgentRelocationModel.run(self, person_set, resources=resources)

        person_ds_name, person_id_name = person_set.get_dataset_name(), person_set.get_id_name()[0]
        hh_ds_name, hh_id_name = person_set.get_dataset_name(), household_set.get_id_name()[0]
        
        max_person_id = person_set.get_attribute(person_id_name).max() + 1
        new_person_id = arange(max_person_id, max_person_id+index.size)
        
        new_born = {}
        new_born[person_id_name] = new_person_id
        new_born[hh_id_name] = person_set.get_attribute(hh_id_name)[index]
        new_born['age'] = ones(index.size, dtype="int32")
        ##TODO: give better default values
        new_born['sex'] = randint(2, size=index.size)  ##TODO: better way to assign sex?
        #new_born['education']
        #new_born['job_id']
        #new_born['race_id'] 
        #new_born['relation']
        
        logger.log_status("Adding %s records to %s dataset" % (index.size, person_set.get_dataset_name()) )
        person_set.add_elements(data=new_born, require_all_attributes=False, change_ids_if_not_unique=True)