def __init__( self, location_set, nested_structure=None, stratum=None, model_name=None, short_name=None, sampler="opus_core.samplers.stratified_sampler", utilities="opus_core.hierarchical_linear_utilities", probabilities="opus_core.nl_probabilities", **kwargs ): AgentLocationChoiceModel.__init__( self, location_set=location_set, model_name=model_name, short_name=short_name, sampler=sampler, utilities=utilities, probabilities=probabilities, **kwargs ) HierarchicalChoiceModel.create_nested_and_tree_structure(self, nested_structure, stratum, **kwargs) self.model_interaction = ModelInteractionHierLCM( self, kwargs.get("interaction_pkg", "urbansim"), self.choice_set )
def __init__(self, location_set, 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_residential_units", estimation_weight_string = "residential_units", simulation_weight_string = None, # if this is None, weights are proportional to the capacity number_of_agents_string = "number_of_households", number_of_units_string = "residential_units", sample_proportion_locations = None, sample_size_locations = 30, estimation_size_agents = 1.0, compute_capacity_flag = True, filter=None, submodel_string = None, 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", model_name=None, model_short_name=None, **kwargs): 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)]) if model_name is not None: self.model_name = model_name if model_short_name is not None: self.model_short_name = model_short_name AgentLocationChoiceModel.__init__(self, location_set, model_name=self.model_name, short_name=self.model_short_name, 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, **kwargs)
def __init__(self, location_set, nested_structure=None, stratum=None, model_name=None, short_name=None, sampler="opus_core.samplers.stratified_sampler", utilities="opus_core.hierarchical_linear_utilities", probabilities="opus_core.nl_probabilities", **kwargs): AgentLocationChoiceModel.__init__(self, location_set=location_set, model_name=model_name, short_name=short_name, sampler=sampler, utilities=utilities, probabilities=probabilities, **kwargs) HierarchicalChoiceModel.create_nested_and_tree_structure(self, nested_structure, stratum, **kwargs) self.model_interaction = ModelInteractionHierLCM(self, kwargs.get('interaction_pkg',"urbansim"), self.choice_set)
def __init__(self, group_member, location_set, agents_grouping_attribute, model_name, short_name, **kwargs): """ 'group_member' is of type ModelGroupMember. 'agents_grouping_attribute' is attribute of the agent_set (passed to the 'run' and 'estimate' method) that is used for grouping. """ self.group_member = group_member group_member_name = group_member.get_member_name() self.group_member.set_agents_grouping_attribute(agents_grouping_attribute) AgentLocationChoiceModel.__init__( self, location_set, model_name="%s %s" % (group_member_name.capitalize(), model_name), short_name="%s %s" % (group_member_name.capitalize(), short_name), **kwargs )
def __init__(self, group_member, location_set, agents_grouping_attribute, model_name, short_name, **kwargs): """ 'group_member' is of type ModelGroupMember. 'agents_grouping_attribute' is attribute of the agent_set (passed to the 'run' and 'estimate' method) that is used for grouping. """ self.group_member = group_member group_member_name = group_member.get_member_name() self.group_member.set_agents_grouping_attribute( agents_grouping_attribute) AgentLocationChoiceModel.__init__( self, location_set, model_name="%s %s" % (group_member_name.capitalize(), model_name), short_name="%s %s" % (group_member_name.capitalize(), short_name), **kwargs)
def estimate(self, specification, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) # filter out agents for this group new_agents_index = self.group_member.get_index_of_my_agents(agent_set, agents_index) return AgentLocationChoiceModel.estimate( self, specification, agent_set, agents_index=agents_index[new_agents_index], **kwargs )
def run(self, *args, **kwargs): if 'agent_set' in args and args['agent_set'] is None: logger.log_status("Development project dataset is empty. Skip DPLCM") return elif kwargs.has_key('agent_set') and kwargs['agent_set'] is None: logger.log_status("Development project dataset is empty. Skip DPLCM") return else: return AgentLocationChoiceModel.run(self, *args, **kwargs)
def get_model( self, location_set, 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", sample_proportion_locations=None, sample_size_locations=30, estimation_size_agents=1.0, compute_capacity_flag=True, filter=None, submodel_string=None, 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): 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)]) run_config = merge_resources_with_defaults( run_config, [("capacity_string", self.capacity_string_default), ("number_of_agents_string", self.number_of_agents_string_default), ("number_of_units_string", self.number_of_units_string_default)]) if demand_string: run_config["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)]) estimate_config = merge_resources_with_defaults( estimate_config, [("weights_for_estimation_string", self.estimation_weight_string_default)]) return AgentLocationChoiceModel( location_set, model_name="Household Location Choice Model", short_name="HLCM", 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)
def run(self, *args, **kwargs): if 'agent_set' in args and args['agent_set'] is None: logger.log_status( "Development project dataset is empty. Skip DPLCM") return elif kwargs.has_key('agent_set') and kwargs['agent_set'] is None: logger.log_status( "Development project dataset is empty. Skip DPLCM") return else: return AgentLocationChoiceModel.run(self, *args, **kwargs)
def estimate(self, specification, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) # filter out agents for this group new_agents_index = self.group_member.get_index_of_my_agents( agent_set, agents_index) return AgentLocationChoiceModel.estimate( self, specification, agent_set, agents_index=agents_index[new_agents_index], **kwargs)
def prepare_for_estimate(self, estimation_storage, agents_for_estimation_table, agent_set, agents_filter=None, households_for_estimation_table=None, **kwargs): estimation_set = Dataset(in_storage = estimation_storage, in_table_name=agents_for_estimation_table, id_name=agent_set.get_id_name(), dataset_name=agent_set.get_dataset_name()) self.dataset_pool.replace_dataset('person', estimation_set) if households_for_estimation_table is not None: hhs = HouseholdDataset(in_storage=estimation_storage, in_table_name='households_for_estimation') self.dataset_pool.replace_dataset('household', hhs) spec, index = AgentLocationChoiceModel.prepare_for_estimate(self, estimation_set, **kwargs) if agents_filter is not None: filter_condition = estimation_set.compute_variables(agents_filter, dataset_pool= self.dataset_pool) index = intersect1d(where(filter_condition)[0], index) return (spec, index, estimation_set)
def prepare_for_estimate( self, add_member_prefix=True, specification_dict=None, specification_storage=None, specification_table=None, **kwargs ): if add_member_prefix: specification_table = self.group_member.add_member_prefix_to_table_name(specification_table) if "movers_variable" in kwargs.keys(): kwargs["movers_variable"] = re.sub("SSS", self.group_member.get_member_name(), kwargs["movers_variable"]) return AgentLocationChoiceModel.prepare_for_estimate( self, specification_dict, specification_storage, specification_table, **kwargs )
def prepare_for_estimate(self, add_member_prefix=True, specification_dict=None, specification_storage=None, specification_table=None, **kwargs): if add_member_prefix: specification_table = self.group_member.add_member_prefix_to_table_name( specification_table) if 'movers_variable' in kwargs.keys(): kwargs['movers_variable'] = re.sub( 'SSS', self.group_member.get_member_name(), kwargs['movers_variable']) return AgentLocationChoiceModel.prepare_for_estimate( self, specification_dict, specification_storage, specification_table, **kwargs)
def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) data_objects = kwargs.get("data_objects", {}) if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) # filter out agents for this group new_agents_index = self.group_member.get_index_of_my_agents( agent_set, agents_index, dataset_pool=self.dataset_pool ) choices = AgentLocationChoiceModel.run( self, specification, coefficients, agent_set, agents_index=agents_index[new_agents_index], **kwargs ) result = resize(array([-1], dtype=choices.dtype), agents_index.size) result[new_agents_index] = choices return result
def prepare_for_run(self, add_member_prefix=True, specification_storage=None, specification_table=None, coefficients_storage=None, coefficients_table=None, **kwargs): if add_member_prefix: specification_table, coefficients_table = \ self.group_member.add_member_prefix_to_table_names([specification_table, coefficients_table]) return AgentLocationChoiceModel.prepare_for_run( self, specification_storage=specification_storage, specification_table=specification_table, coefficients_storage=coefficients_storage, coefficients_table=coefficients_table, **kwargs)
def get_model(self, location_set, 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", sample_proportion_locations=None, sample_size_locations=20, estimation_size_agents=1.0, compute_capacity_flag=False, filter=None, submodel_string="sector_id", run_config=None, estimate_config=None, debuglevel=0): 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)]) run_config = merge_resources_with_defaults( run_config, [("capacity_string", self.capacity_string_default)]) 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)]) estimate_config = merge_resources_with_defaults( estimate_config, [("weights_for_estimation_string", self.estimation_weight_string_default)]) return AgentLocationChoiceModel(location_set, agent_name="job", model_name="Employment Location Choice Model", \ short_name="ELCM", sampler=sampler, utilities=utilities, probabilities=probabilities, choices=choices, filter=filter, submodel_string=submodel_string, run_config=run_config, estimate_config=estimate_config, debuglevel=debuglevel)
def prepare_for_run( self, add_member_prefix=True, specification_storage=None, specification_table=None, coefficients_storage=None, coefficients_table=None, **kwargs ): if add_member_prefix: specification_table, coefficients_table = self.group_member.add_member_prefix_to_table_names( [specification_table, coefficients_table] ) return AgentLocationChoiceModel.prepare_for_run( self, specification_storage=specification_storage, specification_table=specification_table, coefficients_storage=coefficients_storage, coefficients_table=coefficients_table, **kwargs )
def run(self, specification, coefficients, agent_set, agents_index=None, **kwargs): if agents_index is None: agents_index = arange(agent_set.size()) data_objects = kwargs.get("data_objects", {}) if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) # filter out agents for this group new_agents_index = self.group_member.get_index_of_my_agents( agent_set, agents_index, dataset_pool=self.dataset_pool) choices = AgentLocationChoiceModel.run( self, specification, coefficients, agent_set, agents_index=agents_index[new_agents_index], **kwargs) result = resize(array([-1], dtype=choices.dtype), agents_index.size) result[new_agents_index] = choices return result
def create_interaction_datasets(self, agent_set, agents_index, config, submodels=[-2], **kwargs): """Like the parent method but it also deals with filtering by groups. """ def mywhere(x, index): i = where(index == x)[0] if i.size == 0: i=-1 return i nchoices = self.get_choice_set_size() if nchoices <> self.choice_set.size() or self.filter is None: return AgentLocationChoiceModel.create_interaction_datasets(self, agent_set, agents_index, config, submodels, **kwargs) # apply filter without doing sampling groups = [-2] group_values = None if config.get("filter_by_groups", False): group_var = config.get("group_definition_for_filtering_alternatives", None) if group_var is None: raise 'No group variable defined for filtering alternatives. Set "group_definition_for_filtering_alternatives" in run_config/estimate_config.' else: group_values = agent_set.compute_variables([group_var], dataset_pool=self.dataset_pool)[agents_index] groups = unique(group_values) where_group = {} choice_index = {} chosen_choice = {} maxsize = 0 for submodel in submodels: where_submodel = self.observations_mapping[submodel] is_submodel = zeros(agents_index.size, dtype='bool8') is_submodel[where_submodel] = True for group in groups: if group_values is None: where_group[(submodel, group)] = where_submodel else: where_group[(submodel, group)] = where(logical_and(group_values == group, is_submodel))[0] if where_group[(submodel, group)].size==0: continue agents_index_in_group = agents_index[where_group[(submodel, group)]] choice_index[(submodel, group)] = self.apply_filter(self.filter, agent_set=agent_set, agents_index=agents_index_in_group, submodel=submodel, replace_dict={'SUBMODEL': submodel, 'GROUP': group}) if choice_index[(submodel, group)] is not None and choice_index[(submodel, group)].size == 0: logger.log_error("There is no alternative that passes filter %s for SUBMODEL=%s and GROUP=%s; %s agents with id %s will remain unplaced." % \ (self.filter, submodel, group, agents_index_in_group.size, agent_set.get_id_attribute()[agents_index_in_group])) continue if config.get('include_chosen_choice', False): chosen = self.choice_set.get_id_index(id=agent_set.get_attribute_by_index(self.choice_set.get_id_name()[0], agents_index_in_group)) chosen_choice[(submodel, group)] = apply_along_axis(mywhere, 1, chosen[:,newaxis], choice_index[(submodel, group)]) maxsize = maximum(maxsize, choice_index[(submodel, group)].size) filter_index = -1 + zeros((agents_index.size, maxsize), dtype="int32") for submodel, group in choice_index.keys(): filter_index[where_group[(submodel, group)],0:choice_index[(submodel, group)].size] = choice_index[(submodel, group)][newaxis, :].repeat(where_group[(submodel, group)].size, axis=0) self.model_interaction.create_interaction_datasets(agents_index, filter_index) if config.get('include_chosen_choice', False): chosen_choice_attr = zeros((agents_index.size, maxsize), dtype="bool8") for submodel, group in chosen_choice.keys(): chosen_choice_attr[where_group[(submodel, group)], chosen_choice[(submodel, group)].flat] = True self.model_interaction.interaction_dataset.add_attribute(data=chosen_choice_attr, name="chosen_choice") self.update_choice_set_size(maxsize) return
def estimate(self, specification, *args, **kwargs): HierarchicalChoiceModel.init_membership_in_nests(self) HierarchicalChoiceModel.delete_logsum_from_specification(self, specification) return AgentLocationChoiceModel.estimate(self, specification, *args, **kwargs)
def __init__( self, location_set, 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_residential_units", estimation_weight_string="residential_units", simulation_weight_string=None, # if this is None, weights are proportional to the capacity number_of_agents_string="number_of_households", number_of_units_string="residential_units", sample_proportion_locations=None, sample_size_locations=30, estimation_size_agents=1.0, compute_capacity_flag=True, filter=None, submodel_string=None, 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"): 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)]) AgentLocationChoiceModel.__init__( self, location_set, model_name=self.model_name, short_name=self.model_short_name, 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 estimate_step(self): self.set_correct_for_sampling() result = AgentLocationChoiceModel.estimate_step(self) HierarchicalChoiceModel.add_logsum_to_coefficients(self, result) return result
def estimate(self, specification, *args, **kwargs): HierarchicalChoiceModel.init_membership_in_nests(self) HierarchicalChoiceModel.delete_logsum_from_specification(self, specification) return AgentLocationChoiceModel.estimate(self, specification, *args, **kwargs)
def run_chunk(self, agents_index, agent_set, specification, coefficients): HierarchicalChoiceModel.add_logsum_to_specification(self, specification, coefficients) HierarchicalChoiceModel.init_membership_in_nests(self) return AgentLocationChoiceModel.run_chunk(self, agents_index, agent_set, specification, coefficients)
def __init__(self, location_set, 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 = "clip_to_zero((parcel.parcel_acres*parcel.max_du_acre*(1-parcel.pct_undevelopable)) - (parcel.aggregate(building.non_residential_sqft)/1500.0) - parcel.aggregate(building.residential_units))", estimation_weight_string = "parcel_acres", simulation_weight_string = None, # if this is None, weights are proportional to the capacity ###number_of_agents_string = "building.residential_units", #number_of_units_string = "clip_to_zero((parcel.parcel_acres*parcel.max_du_acre*(1-parcel.pct_undevelopable)) - (parcel.aggregate(building.non_residential_sqft)/1500.0) - parcel.aggregate(building.residential_units))", agent_units_string = "building.residential_units", sample_proportion_locations = None, sample_size_locations = 250, estimation_size_agents = 1.0, compute_capacity_flag = True, filter=None, submodel_string = None, 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", model_name=None, model_short_name=None, **kwargs): 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), ("agent_units_string", agent_units_string), ("weights_for_simulation_string", simulation_weight_string), ("demand_string", demand_string), ("lottery_max_iterations", 20) ]) 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)]) if model_name is not None: self.model_name = model_name if model_short_name is not None: self.model_short_name = model_short_name AgentLocationChoiceModel.__init__(self, location_set, model_name=self.model_name, short_name=self.model_short_name, 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, **kwargs)
def run_chunk(self, agents_index, agent_set, specification, coefficients): HierarchicalChoiceModel.add_logsum_to_specification(self, specification, coefficients) HierarchicalChoiceModel.init_membership_in_nests(self) return AgentLocationChoiceModel.run_chunk(self, agents_index, agent_set, specification, coefficients)
def estimate_step(self): self.set_correct_for_sampling() result = AgentLocationChoiceModel.estimate_step(self) HierarchicalChoiceModel.add_logsum_to_coefficients(self, result) return result