def run(self, specification, coefficients, agent_set, agents_index=None, *args, **kargs): if agent_set is None: logger.log_status('No developments need to be allocated') return if agents_index is None: agents_index = arange(agent_set.size()) if not isinstance(agents_index, ndarray): try: agents_index = array(agents_index) except: raise TypeError, "Argument agents_index is of wrong type (numpy array or list allowed.)" id_name = self.choice_set.get_id_name()[0] unplaced = arange(agents_index.size) for run in range(self.max_runs): choices = LocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index[unplaced], *args, **kargs) if run == 0: all_choices=choices else: all_choices[unplaced]=choices from opus_core.sampling_toolbox import find_duplicates unplaced = where(find_duplicates(all_choices))[0] if unplaced.size <= 0: break agent_set.set_values_of_one_attribute(id_name, -1, agents_index[unplaced]) return all_choices LocationChoiceModel.run(self, *args, **kargs)
def __init__(self, location_set, model_name=None, short_name=None, **kargs): if model_name is not None: self.model_name = model_name if short_name is not None: self.model_short_name = short_name LocationChoiceModel.__init__(self, location_set=location_set, **kargs)
def __init__(self, location_set, model_name=None, short_name=None, sampler="opus_core.samplers.weighted_sampler", utilities="opus_core.linear_utilities", probabilities="opus_core.mnl_probabilities", choices="opus_core.random_choices", filter=None, submodel_string=None, location_id_string = None, run_config=None, estimate_config=None, debuglevel=0, dataset_pool=None, variable_package = "urbansim", **kwargs): """ :number_of_units_string: number of units string is used to determine whether a choice is over-filled, by comparing it with number_of_agents_string in get_locations_vacancy(). TODO: How does it differ from capacity_string? """ if model_name is not None: self.model_name = model_name if short_name is not None: self.model_short_name = short_name if (run_config is not None) and not isinstance(run_config, Resources): run_config = Resources(run_config) if (estimate_config is not None) and not isinstance(estimate_config, Resources): estimate_config = Resources(estimate_config) self.add_prefix_to_variable_names(["capacity_string", "number_of_agents_string", "number_of_units_string"], location_set, variable_package, run_config) self.add_prefix_to_variable_names("weights_for_estimation_string", location_set, variable_package, estimate_config) LocationChoiceModel.__init__(self, location_set=location_set, 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, **kwargs)
def __init__(self, location_set, project_type, units, developable_maximum_unit_variable_full_name, developable_minimum_unit_variable_full_name=None, model_name=None, **kargs): """ 'project_type' is a string such as 'Residential', or 'Commercial'. """ self.project_type = project_type self.units = units if model_name is not None: self.model_name = model_name else: self.model_name = "%s %s" % (self.project_type, self.model_name) self.model_short_name = "%s %s" % (self.project_type[:3], self.model_short_name) self.developable_maximum_unit_full_name = developable_maximum_unit_variable_full_name self.developable_maximum_unit_short_name = VariableName( self.developable_maximum_unit_full_name).get_alias() self.developable_minimum_unit_full_name = developable_minimum_unit_variable_full_name if self.developable_minimum_unit_full_name is not None: self.developable_minimum_unit_short_name = VariableName( self.developable_minimum_unit_full_name).get_alias() else: self.developable_minimum_unit_short_name = None LocationChoiceModel.__init__(self, location_set=location_set, **kargs)
def create_interaction_dataset(self, agent_set, agents_index, config, *args, **kwargs): if config is not None and config.get("estimate", False): id_name = self.choice_set.get_id_name()[0] mod_id_name = "__%s__" % id_name if mod_id_name in agent_set.get_known_attribute_names(): agent_set.set_values_of_one_attribute(id_name, agent_set.get_attribute(mod_id_name)) result = LocationChoiceModel.create_interaction_dataset(self, agent_set, agents_index, config, **kwargs) # select randomly buildings to unplace ntounplace = int(agents_index.size/4.0) #ntounplace = 1 #self.dataset_pool.get_dataset("urbansim_constant")["recent_years"]) #idx = sample_noreplace(agents_index, ntounplace) tmp = randint(0, agents_index.size, ntounplace) utmp = unique(tmp) idx = agents_index[utmp] logger.log_status("Unplace %s buildings." % utmp.size) if (mod_id_name not in agent_set.get_known_attribute_names()): agent_set.add_attribute(name=mod_id_name, data=array(agent_set.get_attribute(id_name))) agent_set.set_values_of_one_attribute(id_name,-1.0*ones((idx.size,)), idx) return result return LocationChoiceModel.create_interaction_dataset(self, agent_set, agents_index, config, **kwargs)
def create_interaction_dataset(self, agent_set, agents_index, config, *args, **kwargs): if config is not None and config.get("estimate", False): id_name = self.choice_set.get_id_name()[0] mod_id_name = "__%s__" % id_name if mod_id_name in agent_set.get_known_attribute_names(): agent_set.set_values_of_one_attribute( id_name, agent_set.get_attribute(mod_id_name)) result = LocationChoiceModel.create_interaction_dataset( self, agent_set, agents_index, config, **kwargs) # select randomly buildings to unplace ntounplace = int(agents_index.size / 4.0) #ntounplace = 1 #self.dataset_pool.get_dataset("urbansim_constant")["recent_years"]) #idx = sample_noreplace(agents_index, ntounplace) tmp = randint(0, agents_index.size, ntounplace) utmp = unique(tmp) idx = agents_index[utmp] logger.log_status("Unplace %s buildings." % utmp.size) if (mod_id_name not in agent_set.get_known_attribute_names()): agent_set.add_attribute(name=mod_id_name, data=array( agent_set.get_attribute(id_name))) agent_set.set_values_of_one_attribute(id_name, -1.0 * ones( (idx.size, )), idx) return result return LocationChoiceModel.create_interaction_dataset( self, agent_set, agents_index, config, **kwargs)
def __init__(self, location_set, run_config=None, estimate_config=None, variable_package = "urbansim", *args, **kwargs): """ :number_of_units_string: number of units string is used to determine whether a choice is over-filled, by comparing it with number_of_agents_string in get_locations_vacancy(). TODO: How does it differ from capacity_string? """ if (run_config is not None) and not isinstance(run_config, Resources): run_config = Resources(run_config) if (estimate_config is not None) and not isinstance(estimate_config, Resources): estimate_config = Resources(estimate_config) self.add_prefix_to_variable_names(["capacity_string", "number_of_agents_string", "number_of_units_string"], location_set, variable_package, run_config) self.add_prefix_to_variable_names("weights_for_estimation_string", location_set, variable_package, estimate_config) LocationChoiceModel.__init__(self, location_set=location_set, run_config=run_config, estimate_config=estimate_config, *args, **kwargs )
def __init__(self, location_set, opus_package, model_name, **kargs): """ """ self.opus_package = opus_package self.model_name = model_name self.max_runs = 100 LocationChoiceModel.__init__(self, location_set=location_set, **kargs)
def run(self, *args, **kargs): """disable filter for simulation, since it's been handled by get_weights_for_sampling_locations method""" self.filter = None agent_set = kargs["agent_set"] if agent_set is None: logger.log_status("No development projects for this model") return None logger.log_status("project size: %d" % (agent_set.get_attribute(agent_set.get_attribute_name()).sum())) LocationChoiceModel.run(self, *args, **kargs)
def __init__(self, location_set, model_name=None, short_name=None, sampler="opus_core.samplers.weighted_sampler", utilities="opus_core.linear_utilities", probabilities="opus_core.mnl_probabilities", choices="opus_core.random_choices", filter=None, submodel_string=None, location_id_string=None, run_config=None, estimate_config=None, debuglevel=0, dataset_pool=None, variable_package="urbansim", **kwargs): """ :number_of_units_string: number of units string is used to determine whether a choice is over-filled, by comparing it with number_of_agents_string in get_locations_vacancy(). TODO: How does it differ from capacity_string? """ if model_name is not None: self.model_name = model_name if short_name is not None: self.model_short_name = short_name if (run_config is not None) and not isinstance(run_config, Resources): run_config = Resources(run_config) if (estimate_config is not None) and not isinstance(estimate_config, Resources): estimate_config = Resources(estimate_config) self.add_prefix_to_variable_names([ "capacity_string", "number_of_agents_string", "number_of_units_string" ], location_set, variable_package, run_config) self.add_prefix_to_variable_names("weights_for_estimation_string", location_set, variable_package, estimate_config) LocationChoiceModel.__init__(self, location_set=location_set, 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, **kwargs)
def run(self, *args, **kargs): """disable filter for simulation, since it's been handled by get_weights_for_sampling_locations method""" self.filter = None agent_set = kargs["agent_set"] if agent_set is None: logger.log_status("No development projects for this model") return None logger.log_status( "project size: %d" % (agent_set.get_attribute(agent_set.get_attribute_name()).sum())) LocationChoiceModel.run(self, *args, **kargs)
def create_interaction_dataset(self, agent_set, agents_index, config, **kwargs): if config is not None and config.get("estimate", False): id_name = self.choice_set.get_id_name()[0] mod_id_name = "__%s__" % id_name # This should be only true when reestimating, since the agents for estimation were unplaced # in the previous run and their original locations were stored in mod_id_name if mod_id_name in agent_set.get_known_attribute_names(): agent_set.set_values_of_one_attribute(id_name, agent_set.get_attribute(mod_id_name)) result = LocationChoiceModel.create_interaction_dataset(self, agent_set, agents_index, config, **kwargs) return result return LocationChoiceModel.create_interaction_dataset(self, agent_set, agents_index, config, **kwargs)
def create_interaction_dataset(self, agent_set, agents_index, config, **kwargs): if config is not None and config.get("estimate", False): id_name = self.choice_set.get_id_name()[0] mod_id_name = "__%s__" % id_name # This should be only true when reestimating, since the agents for estimation were unplaced # in the previous run and their original locations were stored in mod_id_name if mod_id_name in agent_set.get_known_attribute_names(): agent_set.set_values_of_one_attribute( id_name, agent_set.get_attribute(mod_id_name)) result = LocationChoiceModel.create_interaction_dataset( self, agent_set, agents_index, config, **kwargs) return result return LocationChoiceModel.create_interaction_dataset( self, agent_set, agents_index, config, **kwargs)
def apply_filter(self, filter, agent_set, agents_index, submodel=-2): """ apply filter comparing to mean project size by submodel instead of 0, by shifting self.filter """ project_size_filter = None if (filter is not None): if isinstance(filter, dict): submodel_filter = filter[submodel] else: submodel_filter = filter mean_project_size = agent_set.get_attribute( agent_set.get_attribute_name())[agents_index].mean() if isinstance(submodel_filter, str): resources = Resources({"debug": self.debug}) self.choice_set.compute_variables( [submodel_filter], dataset_pool=self.dataset_pool, resources=resources) filter_name = VariableName(submodel_filter) project_size_filter = self.choice_set.get_attribute( filter_name.get_alias()) - mean_project_size else: project_size_filter = submodel_filter - mean_project_size return LocationChoiceModel.apply_filter(self, project_size_filter, agent_set=agent_set, agents_index=agents_index, submodel=submodel)
def apply_filter(self, filter, submodel=-2): """ apply filter """ if submodel != -2: filter = filter + "_for_development_type_" + str(submodel) return LocationChoiceModel.apply_filter(self, filter, submodel=submodel)
def run(self, specification, coefficients, agent_set, agents_index=None, *args, **kargs): if agent_set is None: logger.log_status('No developments need to be allocated') return if agents_index is None: agents_index = arange(agent_set.size()) if not isinstance(agents_index, ndarray): try: agents_index = array(agents_index) except: raise TypeError, "Argument agents_index is of wrong type (numpy array or list allowed.)" id_name = self.choice_set.get_id_name()[0] unplaced = arange(agents_index.size) for run in range(self.max_runs): choices = LocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index[unplaced], *args, **kargs) if run == 0: all_choices = choices else: all_choices[unplaced] = choices from opus_core.sampling_toolbox import find_duplicates unplaced = where(find_duplicates(all_choices))[0] if unplaced.size <= 0: break agent_set.set_values_of_one_attribute(id_name, -1, agents_index[unplaced]) return all_choices LocationChoiceModel.run(self, *args, **kargs)
def __init__(self, location_set, project_type, units, developable_maximum_unit_variable_full_name, developable_minimum_unit_variable_full_name=None, model_name=None, **kargs): """ 'project_type' is a string such as 'Residential', or 'Commercial'. """ self.project_type = project_type self.units = units if model_name is not None: self.model_name = model_name else: self.model_name = "%s %s" % (self.project_type, self.model_name) self.model_short_name = "%s %s" % (self.project_type[:3], self.model_short_name) self.developable_maximum_unit_full_name = developable_maximum_unit_variable_full_name self.developable_maximum_unit_short_name = VariableName(self.developable_maximum_unit_full_name).get_alias() self.developable_minimum_unit_full_name = developable_minimum_unit_variable_full_name if self.developable_minimum_unit_full_name is not None: self.developable_minimum_unit_short_name = VariableName(self.developable_minimum_unit_full_name).get_alias() else: self.developable_minimum_unit_short_name = None LocationChoiceModel.__init__(self, location_set=location_set, **kargs)
def determine_capacity(self, capacity_string=None, agent_set=None, **kwargs): capacity = LocationChoiceModel.determine_capacity( self, capacity_string=capacity_string, agent_set=agent_set, **kwargs) # subtract locations taken in previous chunks taken_locations = self.choice_set.sum_over_ids( agent_set.get_attribute(self.choice_set.get_id_name()[0]), ones((agent_set.size(), ))) return capacity - taken_locations
def run(self, specification, coefficients, agent_set, agents_index=None, agents_filter=None, chunk_specification=None, data_objects=None, run_config=None, debuglevel=0, maximum_runs=10): if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) if agents_index is None: if agents_filter is not None: agent_set.compute_variables(agents_filter, dataset_pool=self.dataset_pool) agents_index = where(agent_set.get_attribute(VariableName(agents_filter).get_alias()))[0] else: agents_index = arange(agent_set.size()) if not isinstance(agents_index, ndarray): try: agents_index = array(agents_index) except: raise TypeError, "Argument agents_index is of wrong type (numpy array or list allowed.)" if agents_index.size <= 0: logger.log_status("Nothing to be done.") return array([], dtype='int32') if run_config == None: run_config = Resources() self.run_config = run_config.merge_with_defaults(self.run_config) self.number_of_units_string = self.run_config.get("number_of_units_string", None) self.number_of_agents_string = self.run_config.get( "number_of_agents_string", "%s.number_of_agents(%s)" % (self.choice_set.get_dataset_name(), agent_set.get_dataset_name())) if self.number_of_units_string is None: maximum_runs = 1 unplaced = arange(agents_index.size) id_name = self.choice_set.get_id_name()[0] for run in range(maximum_runs): unplaced_size_before_model = unplaced.size choices = LocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index[unplaced], chunk_specification, debuglevel=debuglevel) if run == 0: all_choices=choices else: all_choices[unplaced]=choices unplaced = self.get_movers_from_overfilled_locations(agent_set, agents_index, config=run_config) if (unplaced.size <= 0) or (unplaced_size_before_model == unplaced.size) or (unplaced.size == (unplaced_size_before_model - self.observations_mapping['mapped_index'].size)): break agent_set.set_values_of_one_attribute(id_name, -1, agents_index[unplaced]) return all_choices
def apply_filter(self, filter, agent_set, agents_index, submodel=-2): """ apply filter comparing to mean project size by submodel instead of 0, by shifting self.filter """ project_size_filter = None if (filter is not None): if isinstance(filter, dict): submodel_filter = filter[submodel] else: submodel_filter = filter mean_project_size = agent_set.get_attribute(agent_set.get_attribute_name())[agents_index].mean() if isinstance(submodel_filter, str): resources = Resources({"debug":self.debug}) self.choice_set.compute_variables([submodel_filter], dataset_pool=self.dataset_pool, resources=resources) filter_name = VariableName(submodel_filter) project_size_filter = self.choice_set.get_attribute(filter_name.get_alias()) - mean_project_size else: project_size_filter = submodel_filter - mean_project_size return LocationChoiceModel.apply_filter(self, project_size_filter, agent_set=agent_set, agents_index=agents_index, submodel=submodel)
def prepare_for_run(self, *args, **kwargs): spec, coef, dummy = LocationChoiceModel.prepare_for_run(self, *args, **kwargs) return (spec, coef)
def run(self, specification, coefficients, agent_set, agents_index=None, agents_filter=None, chunk_specification=None, data_objects=None, run_config=None, debuglevel=0, maximum_runs=10): if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) if agents_index is None: if agents_filter is not None: agent_set.compute_variables(agents_filter, dataset_pool=self.dataset_pool) agents_index = where( agent_set.get_attribute( VariableName(agents_filter).get_alias()))[0] else: agents_index = arange(agent_set.size()) if not isinstance(agents_index, ndarray): try: agents_index = array(agents_index) except: raise TypeError, "Argument agents_index is of wrong type (numpy array or list allowed.)" if agents_index.size <= 0: logger.log_status("Nothing to be done.") return array([], dtype='int32') if run_config == None: run_config = Resources() self.run_config = run_config.merge_with_defaults(self.run_config) self.number_of_units_string = self.run_config.get( "number_of_units_string", None) self.number_of_agents_string = self.run_config.get( "number_of_agents_string", "%s.number_of_agents(%s)" % (self.choice_set.get_dataset_name(), agent_set.get_dataset_name())) if self.number_of_units_string is None: maximum_runs = 1 unplaced = arange(agents_index.size) id_name = self.choice_set.get_id_name()[0] for run in range(maximum_runs): unplaced_size_before_model = unplaced.size choices = LocationChoiceModel.run(self, specification, coefficients, agent_set, agents_index[unplaced], chunk_specification, debuglevel=debuglevel) if run == 0: all_choices = choices else: all_choices[unplaced] = choices unplaced = self.get_movers_from_overfilled_locations( agent_set, agents_index, config=run_config) if (unplaced.size <= 0) or (unplaced_size_before_model == unplaced.size) or ( unplaced.size == (unplaced_size_before_model - self.observations_mapping['mapped_index'].size)): break agent_set.set_values_of_one_attribute(id_name, -1, agents_index[unplaced]) return all_choices
def determine_capacity(self, capacity_string=None, agent_set=None, **kwargs): capacity = LocationChoiceModel.determine_capacity(self, capacity_string=capacity_string, agent_set=agent_set, **kwargs) # subtract locations taken in previous chunks taken_locations = self.choice_set.sum_over_ids(agent_set.get_attribute(self.choice_set.get_id_name()[0]), ones((agent_set.size(),))) return capacity - taken_locations
def run(self, specification, coefficients, agent_set, agents_index=None, agents_filter=None, chunk_specification=None, data_objects=None, run_config=None, debuglevel=0, maximum_runs=10): if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) bindex = zeros(agent_set.size(), dtype='b') if agents_filter is not None: bfilter = agent_set.compute_variables(agents_filter, dataset_pool=self.dataset_pool) if agents_index is not None: bindex[agents_index] = True agents_index = where(bindex * bfilter)[0] else: agents_index = where(bfilter)[0] else: if agents_index is not None: agents_index = agents_index else: agents_index = arange(agent_set.size()) if not isinstance(agents_index, ndarray): try: agents_index = array(agents_index) except: raise TypeError, "Argument agents_index is of wrong type (numpy array or list allowed.)" if agents_index.size == 0: logger.log_status("Nothing to be done.") return array([], dtype='int32') if run_config == None: run_config = Resources() self.run_config = run_config.merge_with_defaults(self.run_config) #this is handled by choices module (in UPC sequence) self.number_of_units_string = self.run_config.get("number_of_units_string", None) #self.number_of_agents_string = self.run_config.get( # "number_of_agents_string", # "%s.number_of_agents(%s)" % (self.choice_set.get_dataset_name(), agent_set.get_dataset_name())) if self.number_of_units_string is None: maximum_runs = 1 unplaced = ones_like(agents_index).astype('bool') #boolean of the same shape as agents_index end_choices = -1 * ones_like(agents_index) id_name = self.choice_set.get_id_name()[0] demand_string = self.run_config.get("demand_string") supply_string = self.run_config.get("supply_string") for run in range(maximum_runs): unplaced_size_before = unplaced.sum() choices = LocationChoiceModel.run(self, specification=specification, coefficients=coefficients, agent_set=agent_set, agents_index=agents_index[unplaced], chunk_specification=chunk_specification, debuglevel=debuglevel) end_choices[unplaced] = choices if run > 0: ## delete demand_string and supply string for later iterations to ## avoid these variables being distorted by assigning overfilled agents if demand_string: del self.run_config["demand_string"] if supply_string: del self.run_config["supply_string"] unplaced = agent_set[id_name][agents_index] <= 0 ## these two lines are inside the loop because self.observations_mapping is ## not initialized before calling LocationChoiceModel.run agents_size_mapped = self.observations_mapping['mapped_index'].size agents_size_unmapped = agents_index.size - agents_size_mapped logger.log_status("Agent Location Choice Model iteration %s/%s: %s unplaced agents" % \ (run+1, maximum_runs, unplaced.sum())) if unplaced.sum() in (0, unplaced_size_before, agents_size_unmapped): logger.log_status("All agents placed or number of unplaced agents doesn't change; exit ALCM.") break agent_set.set_values_of_one_attribute(id_name, -1, agents_index[unplaced]) if demand_string: self.run_config["demand_string"] = demand_string if supply_string: self.run_config["supply_string"] = supply_string return end_choices
def prepare_for_run(self, *args, **kwargs): spec, coef, dummy = LocationChoiceModel.prepare_for_run( self, *args, **kwargs) return (spec, coef)