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("HLCM for area %s" % area) HouseholdLocationChoiceModel.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 HLCM for households that don't have assigned large_area self.filter = None logger.log_status("HLCM for households with no area assigned") choices = HouseholdLocationChoiceModel.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]) return agent_set[self.choice_set.get_id_name()[0]][agents_index]
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("HLCM for area %s" % area) HouseholdLocationChoiceModel.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 HLCM for households that don't have assigned large_area self.filter = None logger.log_status("HLCM for households with no area assigned") choices = HouseholdLocationChoiceModel.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 # this loop handles subarea_id_name households 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("HLCM for area %s" % area) HouseholdLocationChoiceModel.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() #potential to add another loop here to handle a secondary higher geography if run_no_area: no_region = where(regions[agents_index] <= 0)[0] self.filter = filter0 # this loop handles households w/out a subarea if no_region.size > 0: # run the HLCM for housseholds that don't have assigned region #self.filter = None logger.log_status("HLCM for households with no area assigned") choices = HouseholdLocationChoiceModel.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 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] # this loop handles subarea_id_name households 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("HLCM for area %s" % area) HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) no_region = where(regions[agents_index] <= 0)[0] #potential to add another loop here to handle a secondary higher geography # this loop handles households w/out a subarea if no_region.size > 0: # run the HLCM for housseholds that don't have assigned region self.filter = None logger.log_status("HLCM for households with no area assigned") choices = HouseholdLocationChoiceModel.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 run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): dataset_pool = SessionConfiguration().get_dataset_pool() submarket_set = dataset_pool.get_dataset('submarket') building_set = dataset_pool.get_dataset('building') building_set.add_attribute(name='submarket', data=building_set.compute_variables('drcog.building.submarket_id')) if agents_index is None: agents_index = arange(agent_set.size()) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index] = True submarket_ids = submarket_set.get_id_attribute() agents_submarkets = agent_set.get_attribute(submarket_set.get_id_name()[0]) for submarket_id in submarket_ids: new_index = where(logical_and(cond_array, agents_submarkets == submarket_id))[0] self.filter = "building.submarket == %s" % submarket_id logger.log_status("HLCM for submarket %s" % submarket_id) HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_set.flush_dataset()
def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): dataset_pool = SessionConfiguration().get_dataset_pool() #submarket_set = dataset_pool.get_dataset('employment_submarket') building_set = dataset_pool.get_dataset('building') parcel_set = dataset_pool.get_dataset('parcel') building_tract = building_set.get_attribute('neighborhood_board_id') building_parcel_id = building_set.get_attribute('parcel_id') agents_index = where(building_parcel_id<1)[0] if agents_index is None: agents_index = arange(agent_set.size()) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index] = True census_tracts = parcel_set.get_attribute('neighborhood_board_id') census_tracts = unique(census_tracts) #agents_submarkets = agent_set.get_attribute(submarket_set.get_id_name()[0]) for tract_id in census_tracts: new_index = where(logical_and(cond_array, building_tract == tract_id))[0] self.filter = "parcel.neighborhood_board_id == %s" % tract_id logger.log_status("Building allocation to parcel for census tract %s" % tract_id) HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_set.flush_dataset()
locations = Dataset(in_storage = storage, in_table_name = 'locations', id_name='location', dataset_name='gridcell') coefficients = Coefficients(names=("costcoef", ), values=(-0.01,)) specification = EquationSpecification(variables=("gridcell.cost", ), coefficients=("costcoef", )) from urbansim.models.household_location_choice_model import HouseholdLocationChoiceModel hlcm = HouseholdLocationChoiceModel( location_set = locations, sampler=None, compute_capacity_flag=False) seed(1) results = hlcm.run(specification, coefficients, agent_set=households) households.get_attribute("location") coef, results = hlcm.estimate(specification, agent_set=households) #hlcm.plot_choice_histograms(capacity=locations.get_attribute("capacity")) number_of_agents = "gridcell.number_of_agents(household)" vacant_capacity = "capacity - gridcell.number_of_agents(household)" hlcm2 = HouseholdLocationChoiceModel( location_set = locations, sampler=None, choices="urbansim.lottery_choices", compute_capacity_flag=True, capacity_string=vacant_capacity, number_of_agents_string=number_of_agents, number_of_units_string="capacity", run_config={"lottery_max_iterations":10})
def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): dataset_pool = SessionConfiguration().get_dataset_pool() #county_set = dataset_pool.get_dataset('county') building_set = dataset_pool.get_dataset('building') #building_set.add_attribute(name='submarket', data=building_set.compute_variables('bayarea.building.submarket_id')) household_zonetype = agent_set.compute_variables("((1000000 * (household.county_id == 1) + 2000000*(household.county_id==43) + 3000000*(household.county_id==49) + 4000000*(household.county_id==57) + 5000000 * (household.county_id==61)) + household.tract_id).astype('i4')") building_zonetype = building_set.compute_variables("((100 * building.disaggregate(parcel.tract_id)) + (1000000*building.disaggregate(parcel.county_id))).astype('i4')") agent_set.add_primary_attribute(name='zonetype', data=household_zonetype) building_set.add_primary_attribute(name='zonetype', data=building_zonetype) if agents_index is None: agents_index = arange(agent_set.size()) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index] = True #county_ids = county_set.get_id_attribute() agents_zonetypes = agent_set.get_attribute('zonetype') zonetype_ids = unique(building_set.get_attribute('zonetype'))#unique(agents_zonetypes) for zonetype_id in zonetype_ids: new_index = where(logical_and(cond_array, agents_zonetypes == zonetype_id))[0] self.filter = "building.zonetype == %s" % zonetype_id logger.log_status("HLCM for zonetype %s" % zonetype_id) logger.log_status("HLCM for households %s" % agent_set['household_id'][new_index]) HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_set.flush_dataset() # agents_index = where(agent_set.get_attribute("building_id") < 1)[0] # #agents_index = arange(agent_set.size()) # cond_array = zeros(agent_set.size(), dtype="bool8") # cond_array[agents_index] = True # #county_ids = county_set.get_id_attribute() # #household_unplaced = agent_set.compute_variables("(household.building_id<1).astype('i4')") # agents_zonetypes = agent_set.get_attribute('zone_id') # #agents_zonetypes = agents_zonetypes*household_unplaced # zonetype_ids = unique(building_set.get_attribute('zone_id'))#unique(agents_zonetypes) # for zonetype_id in zonetype_ids: # new_index = where(logical_and(cond_array, agents_zonetypes == zonetype_id))[0] # self.filter = "building.zone_id == %s" % zonetype_id # logger.log_status("HLCM for zone %s" % zonetype_id) # logger.log_status("HLCM for households %s" % agent_set['household_id'][new_index]) # HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, # agents_index=new_index, **kwargs) # agent_set.flush_dataset() # #self.choice_set.flush_dataset() # this (after a while) slows down the simulation considerably household_county = agent_set.compute_variables("((1 * (household.county_id == 1) + 2*(household.county_id==43) + 3*(household.county_id==49) + 4*(household.county_id==57) + 5 * (household.county_id==61))).astype('i4')") building_county = building_set.compute_variables("(building.disaggregate(parcel.county_id)).astype('i4')") agent_set.add_primary_attribute(name='county', data=household_county) building_set.add_primary_attribute(name='county', data=building_county) parcel_set = dataset_pool.get_dataset('parcel') agents_index = where(agent_set.get_attribute("building_id") < 1)[0] #agents_index = arange(agent_set.size()) cond_array = zeros(agent_set.size(), dtype="bool8") cond_array[agents_index] = True #county_ids = county_set.get_id_attribute() #household_unplaced = agent_set.compute_variables("(household.building_id<1).astype('i4')") agents_zonetypes = agent_set.get_attribute('county') #agents_zonetypes = agents_zonetypes*household_unplaced zonetype_ids = unique(parcel_set.get_attribute('county_id'))#unique(agents_zonetypes) for zonetype_id in zonetype_ids: new_index = where(logical_and(cond_array, agents_zonetypes == zonetype_id))[0] self.filter = "building.county == %s" % zonetype_id logger.log_status("HLCM for zone %s" % zonetype_id) logger.log_status("HLCM for households %s" % agent_set['household_id'][new_index]) HouseholdLocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index=new_index, **kwargs) agent_set.flush_dataset()