def run(self, specification, coefficients, agent_set, agents_index=None, sync_persons=False, **kwargs): """Set sync_persons to True if the model is run on households level and the persons table should be synchronized. """ results = ChoiceModel.run(self, specification, coefficients, agent_set, agents_index=agents_index, **kwargs) if sync_persons: persons = self.dataset_pool.get_dataset('person') choice_id_name = self.choice_set.get_id_name()[0] values = persons.compute_variables( ['_tmp_ = person.disaggregate(%s.%s)' % (agent_set.get_dataset_name(), choice_id_name)], dataset_pool=self.dataset_pool) if agents_index==None: agents_index=arange(agent_set.size()) pers_idx = where(ismember(persons['%s' % agent_set.get_id_name()[0]], agent_set.get_id_attribute()[agents_index])) if choice_id_name not in persons.get_known_attribute_names(): persons.add_primary_attribute(data=zeros(persons.size(), dtype=values.dtype), name=choice_id_name) persons.modify_attribute(data=values, name=choice_id_name, index=pers_idx) persons.delete_one_attribute('_tmp_') agent_set.modify_attribute(data=results, name=self.choice_attribute_name.get_alias(), index=agents_index) return results
def run( self, specification, coefficients, agent_set, agents_index=None, chunk_specification=None, data_objects=None, run_config=None, debuglevel=0, ): """ Run a simulation and return a numpy array of length agents_index, giving agent choices (ids of locations). 'specification' is of type EquationSpecification, 'coefficients' is of type Coefficients, 'agent_set' is of type Dataset, 'agent_index' are indices of individuals in the agent_set for which the model runs. If it is None, the whole agent_set is considered. 'chunk_specification' determines number of chunks in which the simulation is processed. Default is to use 300 rows per chunk. 'data_objects' is a dictionary where each key is the name of an data object ('zone', ...) and its value is an object of class Dataset. 'run_config' is of type Resources, it gives additional arguments for the run. 'debuglevel' overwrites the constructor 'debuglevel'. """ if run_config == None: run_config = Resources() self.run_config = run_config.merge_with_defaults(self.run_config) if data_objects is not None: self.dataset_pool.add_datasets_if_not_included(data_objects) ## what is the use of compute location_id string in run? it gets new values anyway # if self.location_id_string is not None: # location_id = agent_set.compute_variables(self.location_id_string, dataset_pool=self.dataset_pool) ## done in choice_model # location_id_name = self.choice_set.get_id_name()[0] # if (location_id_name not in agent_set.get_known_attribute_names()): # agent_set.add_attribute(name=location_id_name, data=resize(array([-1]), agent_set.size())) if self.run_config.get( "agent_units_string", None ): # used when agents take different amount of capacity from the total capacity agent_set.compute_variables([self.run_config["agent_units_string"]], dataset_pool=self.dataset_pool) self.compute_capacity_flag = self.run_config.get("compute_capacity_flag", False) capacity_string = None self.capacity = None if self.compute_capacity_flag: capacity_string = self.run_config.get("capacity_string", None) if capacity_string is None: raise KeyError, "Entry 'capacity_string' has to be specified in 'run_config' if 'compute_capacity_flag' is True" ## if weights is None, use capacity for weights if self.run_config.get("weights_for_simulation_string", None) is None and capacity_string is not None: self.run_config.merge({"weights_for_simulation_string": capacity_string}) return ChoiceModel.run( self, specification, coefficients, agent_set, agents_index=agents_index, chunk_specification=chunk_specification, run_config=self.run_config, debuglevel=debuglevel, )