def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) large_areas = agent_set.get_attribute(self.large_area_id_name) self.choice_set.compute_variables(["washtenaw.%s.%s" % (self.choice_set.get_dataset_name(), self.large_area_id_name)], dataset_pool=self.dataset_pool) valid_large_area = where(large_areas[agents_index] > 0)[0] if valid_large_area.size > 0: unique_large_areas = unique(large_areas[agents_index][valid_large_area]) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index[valid_large_area]] = True for area in unique_large_areas: new_index = where(logical_and(cond_array, large_areas == area))[0] self.filter = "%s.%s == %s" % (self.choice_set.get_dataset_name(), self.large_area_id_name, area) logger.log_status("ELCM for area %s" % area) EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_index_no_large_area = agents_index[ large_areas[agents_index] <= 0 ] if agent_index_no_large_area.size > 0: # run the ELCM for jobs that don't have assigned large_area self.filter = None logger.log_status("ELCM for jobs with no area assigned") choices = EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=agent_index_no_large_area, **kwargs) where_valid_choice = where(choices > 0)[0] choices_index = self.choice_set.get_id_index(choices[where_valid_choice]) chosen_large_areas = self.choice_set.get_attribute_by_index(self.large_area_id_name, choices_index) agent_set.modify_attribute(name=self.large_area_id_name, data=chosen_large_areas, index=agent_index_no_large_area[where_valid_choice])
def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) large_areas = agent_set.get_attribute(self.large_area_id_name) self.choice_set.compute_variables([ "washtenaw.%s.%s" % (self.choice_set.get_dataset_name(), self.large_area_id_name) ], dataset_pool=self.dataset_pool) valid_large_area = where(large_areas[agents_index] > 0)[0] if valid_large_area.size > 0: unique_large_areas = unique( large_areas[agents_index][valid_large_area]) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index[valid_large_area]] = True for area in unique_large_areas: new_index = where(logical_and(cond_array, large_areas == area))[0] self.filter = "%s.%s == %s" % ( self.choice_set.get_dataset_name(), self.large_area_id_name, area) logger.log_status("ELCM for area %s" % area) EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_index_no_large_area = agents_index[ large_areas[agents_index] <= 0] if agent_index_no_large_area.size > 0: # run the ELCM for jobs that don't have assigned large_area self.filter = None logger.log_status("ELCM for jobs with no area assigned") choices = EmploymentLocationChoiceModel.run( self, specification, coefficients, agent_set, agents_index=agent_index_no_large_area, **kwargs) where_valid_choice = where(choices > 0)[0] choices_index = self.choice_set.get_id_index( choices[where_valid_choice]) chosen_large_areas = self.choice_set.get_attribute_by_index( self.large_area_id_name, choices_index) agent_set.modify_attribute( name=self.large_area_id_name, data=chosen_large_areas, index=agent_index_no_large_area[where_valid_choice])
def run(self, specification, coefficients, agent_set, agents_index=None, agents_filter=None, flush_after_each_subarea=False, run_no_area=True, **kwargs): if agents_index is None: if agents_filter is None: agents_index = arange(agent_set.size()) else: agents_index = where(agent_set.compute_variables(agents_filter))[0] if self.location_id_string is not None: agent_set.compute_variables(self.location_id_string, dataset_pool=self.dataset_pool) if not self.subarea_id_name in agent_set.get_attribute_names(): agent_set.compute_one_variable_with_unknown_package(variable_name="%s" % (self.subarea_id_name), dataset_pool=self.dataset_pool) regions = agent_set.get_attribute(self.subarea_id_name) self.choice_set.compute_one_variable_with_unknown_package(variable_name="%s" % (self.subarea_id_name), dataset_pool=self.dataset_pool) valid_region = where(regions[agents_index] > 0)[0] filter0 = self.filter #keep a copy of the original self.filter if valid_region.size > 0: unique_regions = unique(regions[agents_index][valid_region]) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index[valid_region]] = True for area in unique_regions: new_index = where(logical_and(cond_array, regions == area))[0] #append subarea_id filter to the original filter string if it is set subarea_filter = "(%s.%s==%s)" % (self.choice_set.get_dataset_name(), self.subarea_id_name, area) if filter0: self.filter = "(" + filter0 + ")" + "*" + subarea_filter else: self.filter = subarea_filter logger.log_status("ELCM for area %s" % area) EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) if flush_after_each_subarea: agent_set.flush_dataset() self.choice_set.flush_dataset() if run_no_area: no_region = where(regions[agents_index] <= 0)[0] self.filter = filter0 if no_region.size > 0: # run the ELCM for jobs that don't have assigned region #self.filter = None logger.log_status("ELCM for jobs with no area assigned") choices = EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=agents_index[no_region], **kwargs) where_valid_choice = where(choices > 0)[0] choices_index = self.choice_set.get_id_index(choices[where_valid_choice]) chosen_regions = self.choice_set.get_attribute_by_index(self.subarea_id_name, choices_index) agent_set.modify_attribute(name=self.subarea_id_name, data=chosen_regions, index=no_region[where_valid_choice])
def test_agents_placed_in_appropriate_types(self): """Create 1000 unplaced industrial jobs and 1 commercial job. Allocate 50 commercial gridcells with enough space for 10 commercial jobs per gridcell. After running the EmploymentLocationChoiceModel, the 1 commercial job should be placed, but the 100 industrial jobs should remain unplaced """ storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='job_building_types', table_data = { 'id':array([2,1]), 'name': array(['commercial', 'industrial']) } ) job_building_types = JobBuildingTypeDataset(in_storage=storage, in_table_name='job_building_types') storage.write_table(table_name='jobs', table_data = { 'job_id': arange(1001)+1, 'grid_id': array([0]*1001), 'building_type': array([1]*1000 + [2]) } ) jobs = JobDataset(in_storage=storage, in_table_name='jobs') storage.write_table(table_name='gridcells', table_data = { 'grid_id': arange(50)+1, 'commercial_sqft': array([1000]*50), 'commercial_sqft_per_job': array([100]*50) } ) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') coefficients = Coefficients(names=("dummy",), values=(0.1,)) specification = EquationSpecification(variables=("gridcell.commercial_sqft",), coefficients=("dummy",)) compute_resources = Resources({"job":jobs, "job_building_type": job_building_types}) agents_index = where(jobs.get_attribute("grid_id") == 0) unplace_jobs = DatasetSubset(jobs, agents_index) agents_index = where(unplace_jobs.get_attribute("building_type") == 2)[0] gridcells.compute_variables(["urbansim.gridcell.number_of_commercial_jobs"], resources=compute_resources) commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs") gridcells.compute_variables(["urbansim.gridcell.number_of_industrial_jobs"], resources=compute_resources) industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs") model_group = ModelGroup(job_building_types, "name") elcm = EmploymentLocationChoiceModel(ModelGroupMember(model_group,"commercial"), location_set=gridcells, agents_grouping_attribute = "job.building_type", choices = "opus_core.random_choices_from_index", sample_size_locations = 30) elcm.run(specification, coefficients, agent_set = jobs, agents_index=agents_index, debuglevel=1) gridcells.compute_variables(["urbansim.gridcell.number_of_commercial_jobs"], resources=compute_resources) commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs") gridcells.compute_variables(["urbansim.gridcell.number_of_industrial_jobs"], resources=compute_resources) industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs") self.assertEqual(commercial_jobs.sum() == 1, True, "Error, there should only be a total of 1 commercial job") self.assertEqual(industrial_jobs.sum() == 0, True, "Error, there should be no industrial jobs because there's no space for them")
def test_agents_placed_in_appropriate_types(self): """Create 1000 unplaced industrial jobs and 1 commercial job. Allocate 50 commercial gridcells with enough space for 10 commercial jobs per gridcell. After running the EmploymentLocationChoiceModel, the 1 commercial job should be placed, but the 100 industrial jobs should remain unplaced """ storage = StorageFactory().get_storage('dict_storage') storage.write_table(table_name='job_building_types', table_data={ 'id': array([2, 1]), 'name': array(['commercial', 'industrial']) }) job_building_types = JobBuildingTypeDataset( in_storage=storage, in_table_name='job_building_types') storage.write_table(table_name='jobs', table_data={ 'job_id': arange(1001) + 1, 'grid_id': array([0] * 1001), 'building_type': array([1] * 1000 + [2]) }) jobs = JobDataset(in_storage=storage, in_table_name='jobs') storage.write_table(table_name='gridcells', table_data={ 'grid_id': arange(50) + 1, 'commercial_sqft': array([1000] * 50), 'commercial_sqft_per_job': array([100] * 50) }) gridcells = GridcellDataset(in_storage=storage, in_table_name='gridcells') coefficients = Coefficients(names=("dummy", ), values=(0.1, )) specification = EquationSpecification( variables=("gridcell.commercial_sqft", ), coefficients=("dummy", )) compute_resources = Resources({ "job": jobs, "job_building_type": job_building_types }) agents_index = where(jobs.get_attribute("grid_id") == 0) unplace_jobs = DatasetSubset(jobs, agents_index) agents_index = where( unplace_jobs.get_attribute("building_type") == 2)[0] gridcells.compute_variables( ["urbansim.gridcell.number_of_commercial_jobs"], resources=compute_resources) commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs") gridcells.compute_variables( ["urbansim.gridcell.number_of_industrial_jobs"], resources=compute_resources) industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs") model_group = ModelGroup(job_building_types, "name") elcm = EmploymentLocationChoiceModel( ModelGroupMember(model_group, "commercial"), location_set=gridcells, agents_grouping_attribute="job.building_type", choices="opus_core.random_choices_from_index", sample_size_locations=30) elcm.run(specification, coefficients, agent_set=jobs, agents_index=agents_index, debuglevel=1) gridcells.compute_variables( ["urbansim.gridcell.number_of_commercial_jobs"], resources=compute_resources) commercial_jobs = gridcells.get_attribute("number_of_commercial_jobs") gridcells.compute_variables( ["urbansim.gridcell.number_of_industrial_jobs"], resources=compute_resources) industrial_jobs = gridcells.get_attribute("number_of_industrial_jobs") self.assertEqual( commercial_jobs.sum() == 1, True, "Error, there should only be a total of 1 commercial job") self.assertEqual( industrial_jobs.sum() == 0, True, "Error, there should be no industrial jobs because there's no space for them" )
def __init__(self, group_member, location_set, **kwargs): UrbansimEmploymentLocationChoiceModel.__init__(self, group_member, location_set, **kwargs) location_set.compute_variables( ["urbansim_parcel.building.%s" % self.geography_id_name], dataset_pool=self.dataset_pool )
def __init__(self, group_member, location_set, **kwargs): UrbansimEmploymentLocationChoiceModel.__init__(self, group_member, location_set, **kwargs) location_set.compute_variables(["urbansim_parcel.building.%s" % self.geography_id_name], dataset_pool = self.dataset_pool)
def __init__(self, group_member, location_set, **kwargs): UrbansimEmploymentLocationChoiceModel.__init__(self, group_member, location_set, **kwargs) location_set.compute_one_variable_with_unknown_package("%s" % self.geography_id_name, dataset_pool = self.dataset_pool)
def run(self, specification, coefficients, agent_set, agents_index=None, agents_filter=None, **kwargs): if agents_index is None: if agents_filter is None: agents_index = arange(agent_set.size()) else: agents_index = where( agent_set.compute_variables(agents_filter))[0] if self.location_id_string is not None: agent_set.compute_variables(self.location_id_string, dataset_pool=self.dataset_pool) if not self.subarea_id_name in agent_set.get_attribute_names(): agent_set.compute_one_variable_with_unknown_package( variable_name="%s" % (self.subarea_id_name), dataset_pool=self.dataset_pool) regions = agent_set.get_attribute(self.subarea_id_name) self.choice_set.compute_one_variable_with_unknown_package( variable_name="%s" % (self.subarea_id_name), dataset_pool=self.dataset_pool) valid_region = where(regions[agents_index] > 0)[0] if valid_region.size > 0: unique_regions = unique(regions[agents_index][valid_region]) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index[valid_region]] = True for area in unique_regions: new_index = where(logical_and(cond_array, regions == area))[0] self.filter = "%s.%s == %s" % ( self.choice_set.get_dataset_name(), self.subarea_id_name, area) logger.log_status("ELCM for area %s" % area) EmploymentLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) no_region = where(regions[agents_index] <= 0)[0] if no_region.size > 0: # run the ELCM for jobs that don't have assigned region self.filter = None logger.log_status("ELCM for jobs with no area assigned") choices = EmploymentLocationChoiceModel.run( self, specification, coefficients, agent_set, agents_index=agents_index[no_region], **kwargs) where_valid_choice = where(choices > 0)[0] choices_index = self.choice_set.get_id_index( choices[where_valid_choice]) chosen_regions = self.choice_set.get_attribute_by_index( self.subarea_id_name, choices_index) agent_set.modify_attribute(name=self.subarea_id_name, data=chosen_regions, index=no_region[where_valid_choice])