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]
Esempio n. 2
0
    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])
Esempio n. 4
0
    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])
Esempio n. 5
0
    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()
Esempio n. 6
0
    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()
Esempio n. 7
0
                     values=array([0.5,      0.2,       -5.0,     1.3]))

# LCM
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,
 def __init__(self, location_set, **kwargs):
     UrbansimHouseholdLocationChoiceModel.__init__(self, location_set,
                                                   **kwargs)
     location_set.compute_variables(["urbansim_parcel.building.zone_id"],
                                    dataset_pool=self.dataset_pool)
Esempio n. 9
0
 def __init__(self, location_set, **kwargs): 
     UrbansimHouseholdLocationChoiceModel.__init__(self, location_set, **kwargs)
     location_set.compute_variables(["urbansim_parcel.building.zone_id"], dataset_pool = self.dataset_pool)   
Esempio n. 10
0
    def run_chunk(self, agents_index, agent_set, specification, coefficients):
        """
        This method overwrites the run_chunk method in LocationChoiceModel by introducing iterative adjustment
        to the price attribute of choice_set according to its demand
        """
        CLOSE = 0.05
        maxiter = 30
        #submarkets = define_submarket(self.choice_set, submarket_id_expression="urbansim_parcel.building.zone_id * 100 + building.building_type_id")
        submarkets = define_submarket(self.choice_set, submarket_id_expression="building.building_id")
        price_attribute_name = "average_value_per_unit"
        capacity_string = self.run_config.get("capacity_string", None)
        demand_string = self.run_config.get('demand_string', 'demand')

        ##set demand_string, so the demand for each submodel will be added as a choice_set attribute
        choices = HouseholdLocationChoiceModel.run_chunk(self, agents_index, agent_set, specification, coefficients)
        #upc = self.upc_sequence
        sdratio_submarket, sdratio_choice = self.compute_sdratio(submarkets, self.choice_set, capacity_string, demand_string)

        data_for_plot = {}
        data_for_plot['sdratio'] = sdratio_submarket[:, newaxis]
        data_for_plot[price_attribute_name] = self.choice_set.get_attribute(price_attribute_name)[:, newaxis]
        residential_building_types = {1:'SFR',2:'MFR'}
        for i in range(1, maxiter+1, 1):
            submarkets_w_demand = where(submarkets.get_attribute('demand')>0)[0]
            logger.start_block("HLCM with bidding choice iteration %s" % i)
            logger.log_status("submarket sdratio:")
            self.log_descriptives(data_array=sdratio_submarket[submarkets_w_demand])
            logger.log_status("building %s by building_type_id:" % price_attribute_name)
            self.log_descriptives(dataset=self.choice_set, attribute=price_attribute_name, by='building_type_id', 
                                  show_values=residential_building_types)
            logger.log_status("excess demand by building_type_id:")
            demand_submarket = submarkets.get_attribute('demand')
            demand_supply = demand_submarket - sdratio_submarket * demand_submarket
            w_excess_demand = where(demand_supply > 0)[0]
            #submarket_building_type_id = submarkets.get_id_attribute() % 100
            submarket_building_type_id = self.choice_set.get_attribute_by_id("building_type_id", submarkets.get_id_attribute())
            self.log_descriptives(data_array=demand_supply[w_excess_demand],
                                  by=submarket_building_type_id[w_excess_demand], 
                                  show_values=residential_building_types)

            #if allclose(sdratio_submarket, 1, atol=CLOSE):
            if alltrue(sdratio_submarket[submarkets_w_demand] > 1):
                logger.log_status("Convergence achieved.")
                break

            ##cap price adjustment to +/- 10%
            price_adj = 1 / sdratio_choice
            price_adj[price_adj == 0] = 1.0            
            price_adj[price_adj > 1.10] = 1.10
            price_adj[price_adj < 0.9] = 0.9

            price_attribute = self.choice_set.get_attribute(price_attribute_name)

            self.choice_set.set_values_of_one_attribute(price_attribute_name, price_attribute * price_adj)
            self.choice_set.set_values_of_one_attribute(demand_string, zeros(self.choice_set.size(), dtype="float32"))
            choices = HouseholdLocationChoiceModel.run_chunk(self, agents_index, agent_set, specification, coefficients)

            sdratio_submarket, sdratio_choice = self.compute_sdratio(submarkets, self.choice_set, capacity_string, demand_string)

            data_for_plot['sdratio'] = concatenate( (data_for_plot['sdratio'], sdratio_submarket[:,newaxis]), axis=1)
            data_for_plot[price_attribute_name] = concatenate( (data_for_plot[price_attribute_name], self.choice_set.get_attribute(price_attribute_name)[:,newaxis]),
                                                       axis = 1)

            logger.end_block()

        self.plot_data(data_for_plot['sdratio'], main='sdratio_submkt')

        building_types = self.choice_set.get_attribute('building_type_id')
        data_for_plot[price_attribute_name] = concatenate( (self.choice_set.get_id_attribute()[:,newaxis], data_for_plot[price_attribute_name] ),
                                                   axis = 1)        
        for building_type in [4, 12, 19]:
            self.plot_data(data_for_plot[price_attribute_name][building_types==building_type,:],
                           main='%s_bldg_type_%s' % (price_attribute_name, building_type) )

        return choices
Esempio n. 11
0
    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()
Esempio n. 12
0
                     values=array([0.5,      0.2,       -5.0,     1.3]))

# LCM
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,