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 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)
Beispiel #3
0
 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 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 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 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 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