def choose_category(self, trans_time=None, num_purchases=None):
        category_weights = self.customer_state.item_category_weights(
            trans_time)

        if num_purchases != 0:
            category_weights.append(("stop", 0.1))

        weight_sum = 0.0
        for category, weight in category_weights:
            weight_sum += weight

        category_probabilities = []
        for category, weight in category_weights:
            category_probabilities.append((category, weight / weight_sum))

        sampler = RouletteWheelSampler(category_probabilities)

        return sampler.sample()
    def __init__(self, stores=None, zipcode_objs=None, avg_distance=None):
        lambd = 1.0 / avg_distance
        self.stores = stores
        self.zipcode_objs = zipcode_objs

        zipcode_weights = dict()
        weight_sum = 0.0
        for zipcode in zipcode_objs.iterkeys():
            dist, nearest_store = self._closest_store(zipcode)
            weight = lambd * np.exp(-lambd * dist)
            weight_sum += weight
            zipcode_weights[zipcode] = weight

        zipcode_probs = []
        for zipcode in zipcode_objs.iterkeys():
            zipcode_probs.append(
                (zipcode, zipcode_weights[zipcode] / weight_sum))

        self.sampler = RouletteWheelSampler(zipcode_probs)
Exemplo n.º 3
0
    def __init__(self, zipcode_objs, income_scaling_factor=None):

        pop_probs = dict()
        income_probs = dict()

        pop_sum = 0.0
        max_income = 0.0
        min_income = 100000.0
        for obj in zipcode_objs.itervalues():
            pop_sum += obj.population
            max_income = max(max_income, obj.median_household_income)
            min_income = min(min_income, obj.median_household_income)

        income_k = np.log(income_scaling_factor) / (max_income - min_income)

        income_normalization_factor = 0.0
        income_weights = dict()
        for obj in zipcode_objs.itervalues():
            w = np.exp(income_k * (obj.median_household_income - min_income))
            income_normalization_factor += w
            income_weights[obj.zipcode] = w

        income_probs = dict()
        for obj in zipcode_objs.itervalues():
            income_probs[obj.zipcode] = income_weights[
                obj.zipcode] / income_normalization_factor

        prob_probs = dict()
        for obj in zipcode_objs.itervalues():
            pop_probs[obj.zipcode] = obj.population / pop_sum

        normalization_factor = 0.0
        for z in income_probs.iterkeys():
            normalization_factor += income_probs[z] * pop_probs[z]

        zipcode_probs = []
        for z in income_probs.iterkeys():
            zipcode_probs.append(
                (z, income_probs[z] * pop_probs[z] / normalization_factor))

        self.sampler = RouletteWheelSampler(zipcode_probs)
    def __init__(self, first_names, last_names):
        normalized_first_names = self.normalize(first_names)
        normalized_last_names = self.normalize(last_names)

        self.first_name_sampler = RouletteWheelSampler(normalized_first_names)
        self.last_name_sampler = RouletteWheelSampler(normalized_last_names)