コード例 #1
0
 def _are_targets_reached(self, column_value):
     if (column_value[self.column_names_index['building_type_id']] not in self.same_demand_group) or (self.current_year >= 2015):
         return (DevelopmentProjectProposalSamplingModelWithMinimum._is_target_reached(self, column_value), True)
     is_target_reached = DevelopmentProjectProposalSamplingModelWithMinimum._is_target_reached(self, column_value)
     result = []
     names_index = ones(len(self.column_names), dtype='bool8')
     names_index[self.column_names_index['building_type_id']] = False
     arr_column_value = array(column_value)
     for colvalue, accounting in self.accounting.items():
         if (not (array(colvalue)[names_index] == arr_column_value[names_index]).all()) or (colvalue[self.column_names_index['building_type_id']] not in self.same_demand_group):
             continue
         result.append((accounting.get("target_spaces",0) <= ( accounting.get("total_spaces",0) + accounting.get("proposed_spaces",0) - 
                                                         accounting.get("demolished_spaces",0) )) and (
                      accounting.get("proposed_spaces",0) >= accounting.get("minimum_spaces",0)))
     return (is_target_reached, all(result))            
コード例 #2
0
    def run(self, n=500, 
            realestate_dataset_name = 'building',
            current_year=None,
            **kwargs):

        target_vacancy = self.dataset_pool.get_dataset('target_vacancy')

        if current_year is None:
            year = SimulationState().get_current_time()
        else:
            year = current_year
        self.current_year = year
        this_year_index = where(target_vacancy['year']==year)[0]
        target_vacancy_for_this_year = DatasetSubset(target_vacancy, this_year_index)
        if target_vacancy_for_this_year.size() == 0:
            raise IOError, 'No target vacancy defined for year %s.' % year
        self.all_btypes_size = target_vacancy_for_this_year.size()
        return DevelopmentProjectProposalSamplingModelWithMinimum.run(self, n=n, realestate_dataset_name=realestate_dataset_name,
                                                                      current_year=current_year, **kwargs)
コード例 #3
0
 def _is_target_reached(self, column_value=()):
     if column_value and column_value[self.column_names_index['building_type_id']] not in self.same_demand_group:
         return DevelopmentProjectProposalSamplingModelWithMinimum._is_target_reached(self, column_value)
     if len(self.accounting) < self.all_btypes_size:
         return False
     return DevelopmentProjectProposalSamplingModelWithMinimum._is_target_reached(self, column_value)
コード例 #4
0
    def run(self, zones, zone_ids_to_process=None, type=None, year=2000, **kwargs):
        """If 'type' is None, the model runs for both, residential and non-residential space. Alternatively,
        it can be set to 'residential' or 'non_residential'.
        """

        self.type = {"residential": False,
                     "non_residential": False}
        if (type is None) or (type == 'residential'):
            self.type["residential"] = True
        if (type is None) or (type == 'non_residential'):
            self.type["non_residential"] = True

        self.proposal_set.id_planned = 99999 # to switch processing of planned proposals of
        
        target_vacancies = self.dataset_pool.get_dataset('target_vacancy')
        tv_building_types = unique(target_vacancies.get_attribute('building_type_id'))
        
        bldgs = self.dataset_pool.get_dataset('building')
        bts = self.dataset_pool.get_dataset('building_type')
        all_building_types = bts.get_id_attribute()
        
        self.bt_do_not_count = array(map(lambda x: x not in tv_building_types, all_building_types))
        self.bt_do_not_count =  all_building_types[self.bt_do_not_count]
        self.do_not_count_residential_units  = self.get_do_not_count_residential_units(zones)
        
        zones.compute_variables(["placed_households = zone.aggregate(building.number_of_agents(household))",
                                 "occupied_spaces = zone.aggregate(psrc_parcel.building.occupied_spaces * (urbansim_parcel.building.is_residential == 0), [parcel])"
                                 ], dataset_pool=self.dataset_pool)
        bldgs.compute_variables(["urbansim_parcel.building.zone_id"], dataset_pool=self.dataset_pool)
        self.occuppied_estimate = {}
        self.occupied_estimate_residential = 0
        self.occupied_estimate_nonresidential = 0
        if self.type["residential"]: 
            occupied_residential_units = zones.compute_variables(["total_number_of_households = zone.number_of_agents(household)",
                                     ], dataset_pool=self.dataset_pool) - self.do_not_count_residential_units
            to_be_placed_households = zones["total_number_of_households"] - zones["placed_households"]
            hhdistr = self.compute_household_building_type_distribution()
            for ibt in range(all_building_types.size):
                bt = all_building_types[ibt]
                if bt in self.bt_do_not_count or not bts['is_residential'][ibt]:
                    continue
                self.occuppied_estimate[(bt,)] = zones.sum_over_ids(bldgs['zone_id'], 
                        bldgs['occupied_spaces']*(bldgs['building_type_id']==bt)) + round(hhdistr[ibt]*to_be_placed_households)
                self.occupied_estimate_residential = self.occupied_estimate_residential + self.occuppied_estimate[(bt,)]
                
        if self.type["non_residential"]:    
            zones.compute_variables(["number_of_all_nhb_jobs = zone.aggregate(job.home_based_status==0)"],
                                 dataset_pool=self.dataset_pool)
            job_building_type_distribution = self.compute_job_building_type_distribution()
            to_be_placed_jobs = zones.get_attribute("number_of_all_nhb_jobs") - zones['occupied_spaces']                                
            for ibt in range(all_building_types.size):
                bt = all_building_types[ibt]
                if bt in self.bt_do_not_count or bts['is_residential'][ibt]:
                    continue
                self.occuppied_estimate[(bt,)] = zones.sum_over_ids(bldgs['zone_id'], 
                        bldgs['occupied_spaces']*(bldgs['building_type_id']==bt)) + round(to_be_placed_jobs * job_building_type_distribution[ibt])
                self.occupied_estimate_nonresidential = self.occupied_estimate_nonresidential + self.occuppied_estimate[(bt,)]
                      
        self.is_residential_bt = {}
        for ibt in range(all_building_types.size):
            self.is_residential_bt[(all_building_types[ibt],)] = bts['is_residential'][ibt]
            
        zone_ids_in_proposals = self.proposal_set.compute_variables("zone_id = development_project_proposal.disaggregate(urbansim_parcel.parcel.zone_id)", 
                                                dataset_pool=self.dataset_pool)
        zone_ids = zones.get_id_attribute()
        # keep copy of the weights
        original_weight = self.weight.copy()
        # this is a hack: we want buildings to be built in 2000, but the simulation is running for different year
        #start_year = SimulationState().get_current_time() - 1
        start_year = year
        self.proposal_set.modify_attribute(name="start_year", data=array(self.proposal_set.size()*[start_year]))
        status = self.proposal_set.get_attribute("status_id")
        self.proposal_set.add_primary_attribute(name='original_status_id', data=status.copy())
        for zone_index in range(zone_ids.size):
            self.zone = zone_ids[zone_index]
            self.zone_index = zone_index
            if (zone_ids_to_process is not None) and (self.zone not in zone_ids_to_process):
                continue
            self.build_in_zone = {"residential": False, "non_residential": False}
            if self.type["residential"]:
                if to_be_placed_households[zone_index] > 0:
                    self.build_in_zone["residential"] = True
            if self.type["non_residential"]:
                if to_be_placed_jobs[zone_index] > 0:
                    self.build_in_zone["non_residential"] = True
            if not self.build_in_zone["residential"] and not self.build_in_zone["non_residential"]:
                continue
            where_zone = zone_ids_in_proposals == self.zone
            idx_zone = where(where_zone)[0]
            if (self.proposal_set.id_active in status[idx_zone]) or (self.proposal_set.id_refused in status[idx_zone]):
                continue # this zone was handled previously
            if idx_zone.size <= 0:
                logger.log_status("No proposals for zone %s" % self.zone)
                continue
            idx_out_zone_not_active_not_refused = where(logical_and(logical_and(status != self.proposal_set.id_active, 
                                                                               status != self.proposal_set.id_refused),
                                                                    logical_not(where_zone)))[0]
            status[idx_zone] = self.proposal_set.id_tentative
            status[idx_out_zone_not_active_not_refused] = self.proposal_set.id_not_available
            self.proposal_set.modify_attribute(name="status_id", data=status)
            
            logger.log_status("\nDPSM for zone %s" % self.zone)
            self.weight[:] = original_weight[:]
            if self.weight[idx_zone].sum() <= 0:
                logger.log_status("No non-zero weights for zone %s." % self.zone)
            while isinf(self.weight[idx_zone].sum()):
                self.weight[idx_zone] = self.weight[idx_zone]/10.
            self.second_pass = {}
            DevelopmentProjectProposalSamplingModel.run(self, **kwargs)                
            status = self.proposal_set.get_attribute("status_id")
            self.proposal_set.modify_attribute(name="original_status_id", data=status[idx_zone], index=idx_zone)
            where_not_active = where(status[idx_zone] != self.proposal_set.id_active)[0]
            status[idx_zone[where_not_active]] = self.proposal_set.id_refused
            self.proposal_set.modify_attribute(name="status_id", data=status)
            if ((zone_index+1) % 50) == 0: # flush every 50th zone 
                self.proposal_set.flush_dataset()
                
        self.proposal_set.set_values_of_one_attribute("status_id", self.proposal_set.id_not_available, where(status != self.proposal_set.id_active)[0])
        return (self.proposal_set, [])
コード例 #5
0
    def run(self, location_ids_to_process=None, type=None, year=2000, n=500, 
            realestate_dataset_name = 'building', process_planned=False, modify_start_year=True, **kwargs):
        """If 'type' is None, the model runs for both, residential and non-residential space. Alternatively,
        it can be set to 'residential' or 'non_residential'.
        modify_start_year should be set to True if the model is run in a different year than start_year 
        of projects that should be processed.
        """

        self.type = {"residential": False,
                     "non_residential": False}
        if (type is None) or (type == 'residential'):
            self.type["residential"] = True
        if (type is None) or (type == 'non_residential'):
            self.type["non_residential"] = True
        
        regions = self.dataset_pool.get_dataset(self.subarea_name)
        self.subarea_id_name = regions.get_id_name()[0]
        if not process_planned:
            self.proposal_set.id_planned = 99999 # to switch processing of planned proposals of
        
        target_vacancies = self.dataset_pool.get_dataset('target_vacancy')
        tv_building_types = unique(target_vacancies.get_attribute('building_type_id'))
        
        bldgs = self.dataset_pool.get_dataset(realestate_dataset_name)
        bts = self.dataset_pool.get_dataset('building_type')
        all_building_types = bts.get_id_attribute()
        
        self.bt_do_not_count = array(map(lambda x: x not in tv_building_types, all_building_types))
        self.bt_do_not_count =  all_building_types[self.bt_do_not_count]
        
        regions.compute_variables(["placed_households = %s.aggregate(%s.number_of_agents(household), intermediates=[%s])" % (self.subarea_name, realestate_dataset_name, self.intermediates_to_realestate),
                                 "occupied_spaces = %s.aggregate(psrc_parcel.%s.occupied_spaces * (urbansim_parcel.%s.is_residential == 0), [%s])" % (self.subarea_name, realestate_dataset_name, realestate_dataset_name, self.intermediates_to_realestate)
                                 ], dataset_pool=self.dataset_pool)
        bldgs.compute_one_variable_with_unknown_package("%s" % self.subarea_id_name, dataset_pool=self.dataset_pool)
        self.occuppied_estimate = {}
        self.occupied_estimate_residential = 0
        self.occupied_estimate_nonresidential = 0
        btdistr = self.compute_building_type_distribution(bts, realestate_dataset_name, regions)
        btdistr_nonres = self.compute_job_building_type_distribution(bts, realestate_dataset_name, regions)
        if self.type["residential"]: 
            regions.compute_variables(["total_number_of_households = %s.number_of_agents(household)" % self.subarea_name
                                     ], dataset_pool=self.dataset_pool)
            to_be_placed_households = regions["total_number_of_households"] - regions["placed_households"]            
            for ibt in range(all_building_types.size):
                bt = all_building_types[ibt]
                if bt in self.bt_do_not_count or not bts['is_residential'][ibt]:
                    continue
                # serves as target
                self.occuppied_estimate[(bt,)] = regions.sum_over_ids(bldgs[self.subarea_id_name], 
                        bldgs['occupied_spaces']*(bldgs['building_type_id']==bt)) + round(btdistr[ibt]*to_be_placed_households)
                self.occupied_estimate_residential = self.occupied_estimate_residential + self.occuppied_estimate[(bt,)]
            # add 7%
            self.occupied_estimate_residential = self.occupied_estimate_residential*1.07
                
        if self.type["non_residential"]:    
            regions.compute_variables(["number_of_all_nhb_jobs = %s.aggregate(job.home_based_status==0)" % self.subarea_name],
                                 dataset_pool=self.dataset_pool)
            to_be_placed_jobs = regions.get_attribute("number_of_all_nhb_jobs") - regions['occupied_spaces']                                
            for ibt in range(all_building_types.size):
                bt = all_building_types[ibt]
                if bt in self.bt_do_not_count or bts['is_residential'][ibt]:
                    continue
                # serves as target
                self.occuppied_estimate[(bt,)] = regions.sum_over_ids(bldgs[self.subarea_id_name], 
                        bldgs['occupied_spaces']*(bldgs['building_type_id']==bt)) + round(to_be_placed_jobs * btdistr_nonres[ibt])
                self.occupied_estimate_nonresidential = self.occupied_estimate_nonresidential + self.occuppied_estimate[(bt,)]
            # add 2%
            self.occupied_estimate_nonresidential = self.occupied_estimate_nonresidential*1.02
                      
        self.is_residential_bt = {}
        for ibt in range(all_building_types.size):
            self.is_residential_bt[(all_building_types[ibt],)] = bts['is_residential'][ibt]
            
        region_ids_in_proposals = self.proposal_set.compute_variables("%s = development_project_proposal.disaggregate(urbansim_parcel.parcel.%s)" % (self.subarea_id_name, self.subarea_id_name), 
                                                dataset_pool=self.dataset_pool)
        region_ids = regions.get_id_attribute()
        # keep copy of the weights
        original_weight = self.weight.copy()
        if modify_start_year:
            start_year = year
            self.proposal_set.modify_attribute(name="start_year", data=array(self.proposal_set.size()*[start_year]))
        status = self.proposal_set.get_attribute("status_id")
        self.proposal_set.add_primary_attribute(name='original_status_id', data=status.copy())
        self.all_demolished_buildings = []
        for subarea_index in range(region_ids.size):
            self.subarea = region_ids[subarea_index]
            self.subarea_index = subarea_index
            if (location_ids_to_process is not None) and (self.subarea not in location_ids_to_process):
                continue
            self.build_in_subarea = {"residential": False, "non_residential": False}
            if self.type["residential"]:
                if to_be_placed_households[subarea_index] > 0:
                    self.build_in_subarea["residential"] = True
            if self.type["non_residential"]:
                if to_be_placed_jobs[subarea_index] > 0:
                    self.build_in_subarea["non_residential"] = True
            if not self.build_in_subarea["residential"] and not self.build_in_subarea["non_residential"]:
                continue
            where_subarea = region_ids_in_proposals == self.subarea
            idx_subarea_in_prop = where(where_subarea)[0]
            if (self.proposal_set.id_active in status[idx_subarea_in_prop]) or (self.proposal_set.id_refused in status[idx_subarea_in_prop]):
                continue # this subarea was handled previously
            if idx_subarea_in_prop.size <= 0:
                logger.log_status("No proposals for subarea %s" % self.subarea)
                continue
            idx_out_subarea_not_active_not_refused = where(logical_and(logical_and(status != self.proposal_set.id_active, 
                                                                               status != self.proposal_set.id_refused),
                                                                    logical_not(where_subarea)))[0]
            status[idx_subarea_in_prop] = self.proposal_set['original_status_id'][idx_subarea_in_prop]
            status[idx_out_subarea_not_active_not_refused] = self.proposal_set.id_not_available
            self.proposal_set.modify_attribute(name="status_id", data=status)
            
            logger.log_status("\nDPSM for subarea %s" % self.subarea)
            self.weight[:] = original_weight[:]
            if self.weight[idx_subarea_in_prop].sum() <= 0:
                logger.log_status("No non-zero weights for subarea %s." % self.subarea)
            while isinf(self.weight[idx_subarea_in_prop].sum()):
                self.weight[idx_subarea_in_prop] = self.weight[idx_subarea_in_prop]/10.
            # check building types that don't have any proposals
            no_space = []
            for ibt in range(all_building_types.size):
                bt = all_building_types[ibt]
                if bt in self.bt_do_not_count:
                    continue
                if btdistr[ibt][subarea_index] == 0:
                    no_space = no_space + [bt]
            if len(no_space) > 0:
                logger.log_warning('No developable space for building types: %s' % str(no_space).strip('[]'))
            self.second_pass = {}
            DevelopmentProjectProposalSamplingModel.run(self, n=n, realestate_dataset_name=realestate_dataset_name, **kwargs)                
            status = self.proposal_set.get_attribute("status_id")
            self.proposal_set.modify_attribute(name="original_status_id", data=status[idx_subarea_in_prop], index=idx_subarea_in_prop)
            where_not_active = where(status[idx_subarea_in_prop] != self.proposal_set.id_active)[0]
            status[idx_subarea_in_prop[where_not_active]] = self.proposal_set.id_refused
            self.proposal_set.modify_attribute(name="status_id", data=status)
            self.all_demolished_buildings.extend(self.demolished_buildings)
            if ((subarea_index+1) % 50) == 0: # flush every 50th subarea 
                self.proposal_set.flush_dataset()
                
        self.proposal_set.set_values_of_one_attribute("status_id", self.proposal_set.id_not_available, where(status != self.proposal_set.id_active)[0])
        self.proposal_set.set_values_of_one_attribute("status_id", self.proposal_set.id_planned, where(self.proposal_set["original_status_id"] == self.proposal_set.id_planned)[0])
        return (self.proposal_set, bldgs.get_id_attribute()[self.all_demolished_buildings])