def estimate(self, *args, **kargs):
     agent_set = kargs["agent_set"]
     data_objects = kargs.get("data_objects", {})
     data_objects[agent_set.get_dataset_name()] = agent_set
     estimate_config = kargs.get("estimate_config", {})
     self.units_full_name = "%s.%s.%s" % (estimate_config.get("agent_variables_package", "urbansim"),
                                          agent_set.get_dataset_name(), self.units)
     agent_set.compute_variables(self.units_full_name, dataset_pool=self.dataset_pool, resources=Resources(data_objects))
     #logger.log_status("Buildings size: %d" % (agent_set.get_attribute_by_index(self.units_full_name).sum()))
     return AgentLocationChoiceModelMember.estimate(self, *args, **kargs)
    def get_sampling_weights(self, config, agent_set=None, agents_index=None, **kwargs):
        where_developable = AgentLocationChoiceModelMember.apply_filter(self, self.filter, None, agent_set,
                                                                        agents_index)
        if self.developable_maximum_unit_variable:
            varlist = [self.developable_maximum_unit_variable]
        else:
            varlist = []
        if self.developable_minimum_unit_variable is not None:
            varlist.append(self.developable_minimum_unit_variable)
        self.choice_set.compute_variables(varlist, dataset_pool = self.dataset_pool)
        if self.developable_maximum_unit_variable is not None:
            max_capacity = clip_to_zero_if_needed(self.choice_set.get_attribute_by_index(
                                 self.developable_maximum_unit_variable, where_developable),
                              "get_weights_for_sampling_locations - computing developable_maximum_unit_variable")
        else:
            max_capacity = ones(where_developable.size) * agent_set.get_attribute(self.units).max()

        if self.developable_minimum_unit_variable is not None:
            min_capacity = clip_to_zero_if_needed(self.choice_set.get_attribute_by_index(
                             self.developable_minimum_unit_variable, where_developable),
                              "get_weights_for_sampling_locations - computing developable_minimum_unit_variable")
        else:
            min_capacity = zeros(max_capacity.size)

        if max_capacity.sum(dtype="float32") == 0:
            raise RuntimeError, "There are no choices with any capacity for %s buildings." % agent_set.what

        #how many buildings fit in each developable location
        max_building_size = sort(max_capacity)[-max(50,int(where_developable.size*0.001))]
        proposed_building_sizes = agent_set.get_attribute_by_index(self.units_full_name, agents_index)
        if proposed_building_sizes.max() > max_building_size: # change the size if there is not enough space
            logger.log_status("Maximum building size: %s" % proposed_building_sizes.max())
            self.modify_agents_size(agent_set, minimum(proposed_building_sizes,max_building_size), agents_index)
            proposed_building_sizes = agent_set.get_attribute_by_index(self.units_full_name, agents_index)
            if self.run_config.get("agent_units_string", None): # needs to be corrected
                if VariableName(self.run_config["agent_units_string"]).get_alias() not in agent_set.get_known_attribute_names():
                    agent_set.compute_variables(self.run_config["agent_units_string"])
                self.run_config["agent_units_all"] = agent_set.get_attribute_by_index(self.run_config["agent_units_string"], agents_index)
            logger.log_warning("Not enough building capacity. Large building sizes reduced to %s" % max_building_size)
        proposed_building_sizes = reshape(proposed_building_sizes, (proposed_building_sizes.size,1))
        weight_array = logical_and(proposed_building_sizes >= reshape(min_capacity, (1, min_capacity.size)),
                        (reshape(max_capacity, (1, max_capacity.size)) >=
                             proposed_building_sizes))

        # for memory reasons, discard columns that have only zeros
        keep = where(weight_array.sum(axis=0, dtype=int32))[0]
        where_developable = where_developable[keep]
        weight_array = take(weight_array, keep, axis=1)
        if where_developable.size <= 0:
            logger.log_warning("No developable locations available.")
            
        self.filter_index = where_developable
         
        return weight_array
 def run(self, *args, **kargs):
     agent_set = kargs["agent_set"]
     run_config = kargs.get("run_config", {})
     self.units_full_name = "%s.%s.%s" % (run_config.get("agent_variables_package", "urbansim"),
                                          agent_set.get_dataset_name(), self.units)
     data_objects = kargs.get("data_objects",{})
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     agent_set.compute_variables(self.units_full_name, dataset_pool=self.dataset_pool)
     #logger.log_status("Buildings size: %d" % (agent_set.get_attribute_by_index(self.units_full_name).sum()))
     return AgentLocationChoiceModelMember.run(self, *args, **kargs)
Example #4
0
 def estimate(self, *args, **kargs):
     agent_set = kargs["agent_set"]
     data_objects = kargs.get("data_objects", {})
     data_objects[agent_set.get_dataset_name()] = agent_set
     estimate_config = kargs.get("estimate_config", {})
     self.units_full_name = "%s.%s.%s" % (estimate_config.get(
         "agent_variables_package",
         "urbansim"), agent_set.get_dataset_name(), self.units)
     agent_set.compute_variables(self.units_full_name,
                                 dataset_pool=self.dataset_pool,
                                 resources=Resources(data_objects))
     #logger.log_status("Buildings size: %d" % (agent_set.get_attribute_by_index(self.units_full_name).sum()))
     return AgentLocationChoiceModelMember.estimate(self, *args, **kargs)
Example #5
0
 def run(self, *args, **kargs):
     agent_set = kargs["agent_set"]
     run_config = kargs.get("run_config", {})
     self.units_full_name = "%s.%s.%s" % (run_config.get(
         "agent_variables_package",
         "urbansim"), agent_set.get_dataset_name(), self.units)
     data_objects = kargs.get("data_objects", {})
     if data_objects is not None:
         self.dataset_pool.add_datasets_if_not_included(data_objects)
     agent_set.compute_variables(self.units_full_name,
                                 dataset_pool=self.dataset_pool)
     #logger.log_status("Buildings size: %d" % (agent_set.get_attribute_by_index(self.units_full_name).sum()))
     return AgentLocationChoiceModelMember.run(self, *args, **kargs)
    def prepare_for_estimate_alt(self, agent_set, add_member_prefix=True, specification_dict=None, specification_storage=None,
                              specification_table=None, events_for_estimation_storage=None,
                              events_for_estimation_table=None, urbansim_constant=None, base_year=0,
                              building_categories=None,
                              location_id_variable=None, dataset_pool=None, **kwargs):
        """Remove new buildings of this type and add new projects from event history instead."""
        type = self.group_member.get_member_name()
        agent_set.resources["building_categories"] = building_categories
        agent_set.compute_variables(["urbansim.%s.building_age" % agent_set.get_dataset_name(),
                                     "urbansim.%s.is_building_type_%s" % (agent_set.get_dataset_name(),
                                     type)], dataset_pool = dataset_pool)
        idx_new_buildings = where(logical_and(where(ma.filled(agent_set.get_attribute("building_age"),
                                         urbansim_constant['recent_years'] + 1) <= urbansim_constant['recent_years'], 1, 0),
                                  agent_set.get_attribute("is_building_type_%s" % type)))[0]
        # create agents for estimation
        if events_for_estimation_storage is not None:
            agent_set.remove_elements(idx_new_buildings)
            event_set = DevelopmentEventDataset(urbansim_constant,
                                            in_storage = events_for_estimation_storage,
                                            in_table_name= events_for_estimation_table)
            event_set.remove_non_recent_data(base_year, urbansim_constant['recent_years'])
            BuildingCreator().add_events_from_history_to_existing_buildings(agent_set,
                                               event_set, type,
                                               self.group_member.get_member_code(),
                                               self.units, building_categories, dataset_pool=dataset_pool)
        if location_id_variable:
            agent_set.compute_variables(location_id_variable, dataset_pool=dataset_pool)

        if events_for_estimation_storage is None:
            agent_set.compute_variables(["urbansim.%s.size_category_%s" % (agent_set.get_dataset_name(),
                                                                           self.group_member.get_member_name()),
                                         "urbansim.%s.building_age" % agent_set.get_dataset_name()],
                           dataset_pool = dataset_pool)
        idx_new_buildings = where(ma.filled(agent_set.get_attribute("building_age"),
                                         urbansim_constant['recent_years']+1) <= urbansim_constant['recent_years'])[0]
        if (specification_dict is not None) or (specification_storage is not None):
            specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(self, add_member_prefix,
                                                               specification_dict, specification_storage,
                                                               specification_table,
                                                               location_id_variable=location_id_variable,
                                                               data_objects=dataset_pool.datasets_in_pool(), **kwargs)
        else:
            specification = None
        return (specification, idx_new_buildings)
Example #7
0
 def prepare_for_estimate(self,
                          agent_set,
                          add_member_prefix=True,
                          specification_dict=None,
                          specification_storage=None,
                          specification_table=None,
                          urbansim_constant=None,
                          building_categories=None,
                          location_id_variable=None,
                          dataset_pool=None,
                          **kwargs):
     """Return index of buildings that are younger than 'recent_years'+2"""
     type = self.group_member.get_member_name()
     if location_id_variable:
         agent_set.compute_variables(location_id_variable,
                                     dataset_pool=dataset_pool)
     agent_set.resources["building_categories"] = building_categories
     agent_set.compute_variables("urbansim.%s.size_category_%s" %
                                 (agent_set.get_dataset_name(),
                                  self.group_member.get_member_name()),
                                 dataset_pool=dataset_pool)
     agent_set.compute_variables([
         "urbansim.%s.building_age" % agent_set.get_dataset_name(),
         "urbansim.%s.is_building_type_%s" %
         (agent_set.get_dataset_name(), type)
     ],
                                 dataset_pool=dataset_pool)
     index = where(
         logical_and(
             ma.filled(agent_set.get_attribute("building_age"),
                       urbansim_constant['recent_years'] + 2) <=
             urbansim_constant['recent_years'] + 1,
             agent_set.get_attribute("is_building_type_%s" % type)))[0]
     specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(
         self,
         add_member_prefix,
         specification_dict,
         specification_storage,
         specification_table,
         location_id_variable=location_id_variable,
         data_objects=dataset_pool.datasets_in_pool(),
         **kwargs)
     return (specification, index)
 def prepare_for_estimate(self, agent_set, add_member_prefix=True, specification_dict=None, specification_storage=None,
                           specification_table=None, urbansim_constant=None, building_categories=None,
                           location_id_variable=None, dataset_pool=None, **kwargs):
     """Return index of buildings that are younger than 'recent_years'+2"""
     type = self.group_member.get_member_name()
     if location_id_variable:
         agent_set.compute_variables(location_id_variable, dataset_pool=dataset_pool)
     agent_set.resources["building_categories"] = building_categories
     agent_set.compute_variables("urbansim.%s.size_category_%s" % (agent_set.get_dataset_name(), self.group_member.get_member_name()),
                        dataset_pool=dataset_pool)
     agent_set.compute_variables(["urbansim.%s.building_age" % agent_set.get_dataset_name(),
                                  "urbansim.%s.is_building_type_%s" % (agent_set.get_dataset_name(),
                                  type)], dataset_pool=dataset_pool)
     index = where(logical_and(ma.filled(agent_set.get_attribute("building_age"),
                                      urbansim_constant['recent_years']+2) <= urbansim_constant['recent_years']+1,
                               agent_set.get_attribute("is_building_type_%s" % type)))[0]
     specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(self, add_member_prefix,
                                                            specification_dict, specification_storage,
                                                            specification_table,
                                                            location_id_variable=location_id_variable,
                                                            data_objects=dataset_pool.datasets_in_pool(),
                                                                                **kwargs)
     return (specification, index)
Example #9
0
    def __init__(
            self,
            group_member,
            location_set,
            agents_grouping_attribute='job.building_type',
            sampler="opus_core.samplers.weighted_sampler",
            utilities="opus_core.linear_utilities",
            choices="opus_core.random_choices",
            probabilities="opus_core.mnl_probabilities",
            estimation="opus_core.bhhh_mnl_estimation",
            capacity_string="vacant_SSS_job_space",
            estimation_weight_string="total_number_of_possible_SSS_jobs",
            simulation_weight_string=None,  # if this is None, weights are proportional to the capacity 
            number_of_agents_string="number_of_SSS_jobs",
            number_of_units_string="total_number_of_possible_SSS_jobs",
            sample_proportion_locations=None,
            sample_size_locations=30,
            estimation_size_agents=1.0,
            compute_capacity_flag=True,
            filter=None,
            submodel_string="sector_id",
            location_id_string=None,
            demand_string=None,  # if not None, the aggregate demand for locations will be stored in this attribute
            run_config=None,
            estimate_config=None,
            debuglevel=0,
            dataset_pool=None,
            variable_package="urbansim"):
        """ 'group_member' is of type ModelGroupMember. All SSS in variable names are replaced by the group member name.
        """
        group_member_name = group_member.get_member_name()
        if capacity_string:
            capacity_string = re.sub('SSS', group_member_name, capacity_string)
        if estimation_weight_string:
            estimation_weight_string = re.sub('SSS', group_member_name,
                                              estimation_weight_string)
        if simulation_weight_string:
            simulation_weight_string = re.sub('SSS', group_member_name,
                                              simulation_weight_string)
        if number_of_agents_string:
            number_of_agents_string = re.sub('SSS', group_member_name,
                                             number_of_agents_string)
        if number_of_units_string:
            number_of_units_string = re.sub('SSS', group_member_name,
                                            number_of_units_string)
        if demand_string:
            demand_string = re.sub('SSS', group_member_name, demand_string)

        run_config = merge_resources_if_not_None(
            run_config,
            [("sample_proportion_locations", sample_proportion_locations),
             ("sample_size_locations", sample_size_locations),
             ("compute_capacity_flag", compute_capacity_flag),
             ("capacity_string", capacity_string),
             ("number_of_agents_string", number_of_agents_string),
             ("number_of_units_string", number_of_units_string),
             ("weights_for_simulation_string", simulation_weight_string),
             ("demand_string", demand_string)])

        estimate_config = merge_resources_if_not_None(
            estimate_config,
            [("estimation", estimation),
             ("sample_proportion_locations", sample_proportion_locations),
             ("sample_size_locations", sample_size_locations),
             ("estimation_size_agents", estimation_size_agents),
             ("weights_for_estimation_string", estimation_weight_string)])

        AgentLocationChoiceModelMember.__init__(
            self,
            group_member,
            location_set,
            agents_grouping_attribute,
            model_name="Employment Location Choice Model",
            short_name="ELCM",
            sampler=sampler,
            utilities=utilities,
            probabilities=probabilities,
            choices=choices,
            filter=filter,
            submodel_string=submodel_string,
            location_id_string=location_id_string,
            run_config=run_config,
            estimate_config=estimate_config,
            debuglevel=debuglevel,
            dataset_pool=dataset_pool,
            variable_package=variable_package)
    def __init__(self, group_member, location_set, 
            agents_grouping_attribute = 'job.building_type',
            sampler = "opus_core.samplers.weighted_sampler", 
            utilities = "opus_core.linear_utilities", 
            choices = "opus_core.random_choices", 
            probabilities = "opus_core.mnl_probabilities", 
            estimation = "opus_core.bhhh_mnl_estimation", 
            capacity_string = "vacant_SSS_job_space",
            estimation_weight_string = "total_number_of_possible_SSS_jobs",
            simulation_weight_string = None, # if this is None, weights are proportional to the capacity 
            number_of_agents_string = "number_of_SSS_jobs",
            number_of_units_string = "total_number_of_possible_SSS_jobs",
            sample_proportion_locations = None, 
            sample_size_locations = 30, 
            estimation_size_agents = 1.0, 
            compute_capacity_flag = True, 
            filter = None,
            submodel_string = "sector_id", location_id_string = None,
            demand_string = None, # if not None, the aggregate demand for locations will be stored in this attribute
            run_config = None, estimate_config=None, debuglevel=0, dataset_pool=None,
            variable_package="urbansim"):
        """ 'group_member' is of type ModelGroupMember. All SSS in variable names are replaced by the group member name.
        """
        group_member_name = group_member.get_member_name()
        if capacity_string:
            capacity_string = re.sub('SSS', group_member_name, capacity_string)
        if estimation_weight_string:
            estimation_weight_string = re.sub('SSS', group_member_name, estimation_weight_string)
        if simulation_weight_string:
            simulation_weight_string = re.sub('SSS', group_member_name, simulation_weight_string)
        if number_of_agents_string:
            number_of_agents_string = re.sub('SSS', group_member_name, number_of_agents_string)
        if number_of_units_string:
            number_of_units_string = re.sub('SSS', group_member_name, number_of_units_string)
        if demand_string:
            demand_string = re.sub('SSS', group_member_name, demand_string)
            
        run_config = merge_resources_if_not_None(run_config, [ 
            ("sample_proportion_locations", sample_proportion_locations), 
            ("sample_size_locations", sample_size_locations), 
            ("compute_capacity_flag", compute_capacity_flag),
            ("capacity_string", capacity_string),
            ("number_of_agents_string", number_of_agents_string),
            ("number_of_units_string", number_of_units_string),
            ("weights_for_simulation_string", simulation_weight_string),
            ("demand_string", demand_string)                                                  
            ])
        
        estimate_config = merge_resources_if_not_None(estimate_config, [ 
                    ("estimation", estimation), 
                    ("sample_proportion_locations", sample_proportion_locations), 
                    ("sample_size_locations", sample_size_locations), 
                    ("estimation_size_agents", estimation_size_agents),
                    ("weights_for_estimation_string", estimation_weight_string)])

        AgentLocationChoiceModelMember.__init__(self, group_member, location_set, 
                                        agents_grouping_attribute, 
                                        model_name = "Employment Location Choice Model", 
                                        short_name = "ELCM", 
                                        sampler=sampler, 
                                        utilities=utilities, 
                                        probabilities=probabilities, 
                                        choices=choices,
                                        filter=filter, 
                                        submodel_string=submodel_string,   
                                        location_id_string=location_id_string,
                                        run_config=run_config, 
                                        estimate_config=estimate_config, 
                                        debuglevel=debuglevel, dataset_pool=dataset_pool,
                                        variable_package=variable_package)
Example #11
0
    def prepare_for_estimate_alt(self,
                                 agent_set,
                                 add_member_prefix=True,
                                 specification_dict=None,
                                 specification_storage=None,
                                 specification_table=None,
                                 events_for_estimation_storage=None,
                                 events_for_estimation_table=None,
                                 urbansim_constant=None,
                                 base_year=0,
                                 building_categories=None,
                                 location_id_variable=None,
                                 dataset_pool=None,
                                 **kwargs):
        """Remove new buildings of this type and add new projects from event history instead."""
        type = self.group_member.get_member_name()
        agent_set.resources["building_categories"] = building_categories
        agent_set.compute_variables([
            "urbansim.%s.building_age" % agent_set.get_dataset_name(),
            "urbansim.%s.is_building_type_%s" %
            (agent_set.get_dataset_name(), type)
        ],
                                    dataset_pool=dataset_pool)
        idx_new_buildings = where(
            logical_and(
                where(
                    ma.filled(agent_set.get_attribute("building_age"),
                              urbansim_constant['recent_years'] + 1) <=
                    urbansim_constant['recent_years'], 1, 0),
                agent_set.get_attribute("is_building_type_%s" % type)))[0]
        # create agents for estimation
        if events_for_estimation_storage is not None:
            agent_set.remove_elements(idx_new_buildings)
            event_set = DevelopmentEventDataset(
                urbansim_constant,
                in_storage=events_for_estimation_storage,
                in_table_name=events_for_estimation_table)
            event_set.remove_non_recent_data(base_year,
                                             urbansim_constant['recent_years'])
            BuildingCreator().add_events_from_history_to_existing_buildings(
                agent_set,
                event_set,
                type,
                self.group_member.get_member_code(),
                self.units,
                building_categories,
                dataset_pool=dataset_pool)
        if location_id_variable:
            agent_set.compute_variables(location_id_variable,
                                        dataset_pool=dataset_pool)

        if events_for_estimation_storage is None:
            agent_set.compute_variables([
                "urbansim.%s.size_category_%s" %
                (agent_set.get_dataset_name(),
                 self.group_member.get_member_name()),
                "urbansim.%s.building_age" % agent_set.get_dataset_name()
            ],
                                        dataset_pool=dataset_pool)
        idx_new_buildings = where(
            ma.filled(agent_set.get_attribute(
                "building_age"), urbansim_constant['recent_years'] +
                      1) <= urbansim_constant['recent_years'])[0]
        if (specification_dict is not None) or (specification_storage
                                                is not None):
            specification, dummy = AgentLocationChoiceModelMember.prepare_for_estimate(
                self,
                add_member_prefix,
                specification_dict,
                specification_storage,
                specification_table,
                location_id_variable=location_id_variable,
                data_objects=dataset_pool.datasets_in_pool(),
                **kwargs)
        else:
            specification = None
        return (specification, idx_new_buildings)
Example #12
0
    def __init__(
            self,
            group_member,
            location_set,
            agents_grouping_attribute='building.building_type_id',
            sampler="opus_core.samplers.weighted_sampler",
            utilities="opus_core.linear_utilities",
            choices="urbansim.first_agent_first_choices",
            probabilities="opus_core.mnl_probabilities",
            estimation="opus_core.bhhh_mnl_estimation",
            capacity_string="is_developable_for_buildings_UNITS",
            estimation_weight_string=None,
            developable_maximum_unit_variable="developable_maximum_buildings_UNITS",
            developable_minimum_unit_variable="developable_minimum_UNITS",  # None means don't consider any minimum. For default, set it to empty string
            number_of_agents_string="buildings_SSS_space",
            number_of_units_string="total_maximum_development_SSS",
            sample_proportion_locations=None,
            sample_size_locations=30,
            estimation_size_agents=1.0,
            compute_capacity_flag=True,
            filter="developable_maximum_buildings_UNITS",  # None doesn't use any filter
            submodel_string="size_category_SSS",  # Put here None, if no submodels should be used.
            nrecords_per_chunk_for_estimation_sampling=1000,  # put here None, if everything in 1 chunk
            location_id_string=None,
            run_config=None,
            estimate_config=None,
            debuglevel=0,
            dataset_pool=None,
            variable_package="urbansim"):
        """ 'group_member' is of type ModelGroupMember.
        """
        group_member_name = group_member.get_member_name()
        units = group_member.get_attribute_value("units")[0]

        if capacity_string:
            capacity_string = re.sub("UNITS", units, capacity_string)
            capacity_string = re.sub("SSS", group_member_name, capacity_string)
        if filter:
            filter = re.sub("UNITS", units, filter)
            filter = re.sub("SSS", group_member_name, filter)
        if developable_maximum_unit_variable:
            developable_maximum_unit_variable = re.sub(
                "UNITS", units, developable_maximum_unit_variable)
            developable_maximum_unit_variable = re.sub(
                "SSS", group_member_name, developable_maximum_unit_variable)
        if developable_minimum_unit_variable == "":
            developable_minimum_unit_variable = "developable_minimum_%s" % units
        elif developable_minimum_unit_variable:
            developable_minimum_unit_variable = re.sub(
                "UNITS", units, developable_minimum_unit_variable)
            developable_minimum_unit_variable = re.sub(
                "SSS", group_member_name, developable_minimum_unit_variable)
        if submodel_string:
            submodel_string = re.sub("UNITS", units, submodel_string)
            submodel_string = re.sub("SSS", group_member_name, submodel_string)
        if number_of_agents_string:
            number_of_agents_string = re.sub('UNITS', units,
                                             number_of_agents_string)
            number_of_agents_string = re.sub('SSS', group_member_name,
                                             number_of_agents_string)
        if number_of_units_string:
            number_of_units_string = re.sub('UNITS', units,
                                            number_of_units_string)
            number_of_units_string = re.sub('SSS', group_member_name,
                                            number_of_units_string)

        # create full names from (possibly) short names
        tmpdict = Resources({
            "filter": filter,
            "max": developable_maximum_unit_variable,
            "min": developable_minimum_unit_variable
        })
        self.add_prefix_to_variable_names(["filter", "max", "min"],
                                          location_set,
                                          variable_package,
                                          resources=tmpdict)
        filter = tmpdict["filter"]
        if tmpdict["max"]:
            self.developable_maximum_unit_variable = VariableName(
                tmpdict["max"])
        else:
            self.developable_maximum_unit_variable = None

        if tmpdict["min"]:
            self.developable_minimum_unit_variable = VariableName(
                tmpdict["min"])
        else:
            self.developable_minimum_unit_variable = None

        self.project_type = group_member_name
        self.units = units

        run_config = merge_resources_with_defaults(run_config, [
            ("sample_proportion_locations", sample_proportion_locations),
            ("sample_size_locations", sample_size_locations),
            ("compute_capacity_flag", compute_capacity_flag),
            ("capacity_string", capacity_string),
            ("agent_units_string", "urbansim.building.building_size"),
            ("number_of_agents_string", number_of_agents_string),
            ("number_of_units_string", number_of_units_string),
        ])
        estimate_config = merge_resources_with_defaults(
            estimate_config,
            [("estimation", estimation),
             ("sample_proportion_locations", sample_proportion_locations),
             ("sample_size_locations", sample_size_locations),
             ("estimation_size_agents", estimation_size_agents),
             ("weights_for_estimation_string", estimation_weight_string),
             ("capacity_string", capacity_string),
             ("compute_capacity_flag", compute_capacity_flag)])
        if nrecords_per_chunk_for_estimation_sampling:
            estimate_config[
                "chunk_specification_for_estimation"] = ChunkSpecification({
                    "records_per_chunk":
                    nrecords_per_chunk_for_estimation_sampling
                })
        AgentLocationChoiceModelMember.__init__(
            self,
            group_member,
            location_set,
            agents_grouping_attribute,
            model_name="Building Location Choice Model",
            short_name="BLCM",
            sampler=sampler,
            utilities=utilities,
            probabilities=probabilities,
            choices=choices,
            filter=filter,
            submodel_string=submodel_string,
            location_id_string=location_id_string,
            run_config=run_config,
            estimate_config=estimate_config,
            debuglevel=debuglevel,
            dataset_pool=dataset_pool,
            variable_package=variable_package)
Example #13
0
    def get_sampling_weights(self,
                             config,
                             agent_set=None,
                             agents_index=None,
                             **kwargs):
        where_developable = AgentLocationChoiceModelMember.apply_filter(
            self, self.filter, None, agent_set, agents_index)
        if self.developable_maximum_unit_variable:
            varlist = [self.developable_maximum_unit_variable]
        else:
            varlist = []
        if self.developable_minimum_unit_variable is not None:
            varlist.append(self.developable_minimum_unit_variable)
        self.choice_set.compute_variables(varlist,
                                          dataset_pool=self.dataset_pool)
        if self.developable_maximum_unit_variable is not None:
            max_capacity = clip_to_zero_if_needed(
                self.choice_set.get_attribute_by_index(
                    self.developable_maximum_unit_variable, where_developable),
                "get_weights_for_sampling_locations - computing developable_maximum_unit_variable"
            )
        else:
            max_capacity = ones(
                where_developable.size) * agent_set.get_attribute(
                    self.units).max()

        if self.developable_minimum_unit_variable is not None:
            min_capacity = clip_to_zero_if_needed(
                self.choice_set.get_attribute_by_index(
                    self.developable_minimum_unit_variable, where_developable),
                "get_weights_for_sampling_locations - computing developable_minimum_unit_variable"
            )
        else:
            min_capacity = zeros(max_capacity.size)

        if max_capacity.sum(dtype="float32") == 0:
            raise RuntimeError, "There are no choices with any capacity for %s buildings." % agent_set.what

        #how many buildings fit in each developable location
        max_building_size = sort(
            max_capacity)[-max(50, int(where_developable.size * 0.001))]
        proposed_building_sizes = agent_set.get_attribute_by_index(
            self.units_full_name, agents_index)
        if proposed_building_sizes.max(
        ) > max_building_size:  # change the size if there is not enough space
            logger.log_status("Maximum building size: %s" %
                              proposed_building_sizes.max())
            self.modify_agents_size(
                agent_set, minimum(proposed_building_sizes, max_building_size),
                agents_index)
            proposed_building_sizes = agent_set.get_attribute_by_index(
                self.units_full_name, agents_index)
            if self.run_config.get("agent_units_string",
                                   None):  # needs to be corrected
                if VariableName(
                        self.run_config["agent_units_string"]).get_alias(
                        ) not in agent_set.get_known_attribute_names():
                    agent_set.compute_variables(
                        self.run_config["agent_units_string"])
                self.run_config[
                    "agent_units_all"] = agent_set.get_attribute_by_index(
                        self.run_config["agent_units_string"], agents_index)
            logger.log_warning(
                "Not enough building capacity. Large building sizes reduced to %s"
                % max_building_size)
        proposed_building_sizes = reshape(proposed_building_sizes,
                                          (proposed_building_sizes.size, 1))
        weight_array = logical_and(
            proposed_building_sizes >= reshape(min_capacity,
                                               (1, min_capacity.size)),
            (reshape(max_capacity,
                     (1, max_capacity.size)) >= proposed_building_sizes))

        # for memory reasons, discard columns that have only zeros
        keep = where(weight_array.sum(axis=0, dtype=int32))[0]
        where_developable = where_developable[keep]
        weight_array = take(weight_array, keep, axis=1)
        if where_developable.size <= 0:
            logger.log_warning("No developable locations available.")

        self.filter_index = where_developable

        return weight_array
    def __init__(self, group_member, location_set,
            agents_grouping_attribute = 'building.building_type_id',
            sampler = "opus_core.samplers.weighted_sampler",
            utilities = "opus_core.linear_utilities",
            choices = "urbansim.first_agent_first_choices",
            probabilities = "opus_core.mnl_probabilities",
            estimation = "opus_core.bhhh_mnl_estimation",
            capacity_string = "is_developable_for_buildings_UNITS",
            estimation_weight_string = None,
            developable_maximum_unit_variable = "developable_maximum_buildings_UNITS",
            developable_minimum_unit_variable = "developable_minimum_UNITS", # None means don't consider any minimum. For default, set it to empty string
            number_of_agents_string = "buildings_SSS_space",
            number_of_units_string = "total_maximum_development_SSS",
            sample_proportion_locations = None,
            sample_size_locations = 30,
            estimation_size_agents = 1.0,
            compute_capacity_flag = True,
            filter = "developable_maximum_buildings_UNITS", # None doesn't use any filter
            submodel_string = "size_category_SSS", # Put here None, if no submodels should be used.
            nrecords_per_chunk_for_estimation_sampling = 1000, # put here None, if everything in 1 chunk
            location_id_string = None,
            run_config = None, estimate_config=None, debuglevel=0, dataset_pool=None, variable_package = "urbansim"):
        """ 'group_member' is of type ModelGroupMember.
        """
        group_member_name = group_member.get_member_name()
        units = group_member.get_attribute_value("units")[0]

        if capacity_string:
            capacity_string = re.sub("UNITS", units, capacity_string)
            capacity_string = re.sub("SSS", group_member_name, capacity_string)
        if filter:
            filter = re.sub("UNITS", units, filter)
            filter = re.sub("SSS", group_member_name, filter)
        if developable_maximum_unit_variable:
            developable_maximum_unit_variable = re.sub("UNITS", units, developable_maximum_unit_variable)
            developable_maximum_unit_variable = re.sub("SSS", group_member_name, developable_maximum_unit_variable)
        if developable_minimum_unit_variable == "":
            developable_minimum_unit_variable = "developable_minimum_%s" % units
        elif developable_minimum_unit_variable:
            developable_minimum_unit_variable = re.sub("UNITS", units, developable_minimum_unit_variable)
            developable_minimum_unit_variable = re.sub("SSS", group_member_name, developable_minimum_unit_variable)
        if submodel_string:
            submodel_string = re.sub("UNITS", units, submodel_string)
            submodel_string = re.sub("SSS", group_member_name, submodel_string)
        if number_of_agents_string:
            number_of_agents_string = re.sub('UNITS', units, number_of_agents_string)
            number_of_agents_string = re.sub('SSS', group_member_name, number_of_agents_string)
        if number_of_units_string:
            number_of_units_string = re.sub('UNITS', units, number_of_units_string)
            number_of_units_string = re.sub('SSS', group_member_name, number_of_units_string)

        # create full names from (possibly) short names
        tmpdict = Resources({"filter": filter, "max": developable_maximum_unit_variable,
                             "min": developable_minimum_unit_variable})
        self.add_prefix_to_variable_names(["filter", "max", "min"], location_set, variable_package, resources=tmpdict)
        filter = tmpdict["filter"]
        if tmpdict["max"]:
            self.developable_maximum_unit_variable = VariableName(tmpdict["max"])
        else:
            self.developable_maximum_unit_variable = None

        if tmpdict["min"]:
            self.developable_minimum_unit_variable = VariableName(tmpdict["min"])
        else:
            self.developable_minimum_unit_variable = None

        self.project_type = group_member_name
        self.units = units

        run_config = merge_resources_with_defaults(run_config, [
            ("sample_proportion_locations", sample_proportion_locations),
            ("sample_size_locations", sample_size_locations),
            ("compute_capacity_flag", compute_capacity_flag),
            ("capacity_string", capacity_string),
            ("agent_units_string", "urbansim.building.building_size"),
            ("number_of_agents_string", number_of_agents_string),
            ("number_of_units_string", number_of_units_string),
            ])
        estimate_config = merge_resources_with_defaults(estimate_config, [
                    ("estimation", estimation),
                    ("sample_proportion_locations", sample_proportion_locations),
                    ("sample_size_locations", sample_size_locations),
                    ("estimation_size_agents", estimation_size_agents),
                    ("weights_for_estimation_string", estimation_weight_string),
                    ("capacity_string", capacity_string),
                    ("compute_capacity_flag", compute_capacity_flag)])
        if nrecords_per_chunk_for_estimation_sampling:
            estimate_config["chunk_specification_for_estimation"] = ChunkSpecification({
                                             "records_per_chunk": nrecords_per_chunk_for_estimation_sampling})
        AgentLocationChoiceModelMember.__init__(self, group_member, location_set,
                                        agents_grouping_attribute,
                                        model_name = "Building Location Choice Model",
                                        short_name = "BLCM",
                                        sampler=sampler,
                                        utilities=utilities,
                                        probabilities=probabilities,
                                        choices=choices,
                                        filter=filter,
                                        submodel_string=submodel_string,
                                        location_id_string=location_id_string,
                                        run_config=run_config,
                                        estimate_config=estimate_config,
                                        debuglevel=debuglevel,
                                        dataset_pool=dataset_pool,
                                        variable_package=variable_package)