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
Example #2
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