class NameSampler(object):
    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)

    def normalize(self, names):
        normalized_names = []

        weight_sum = 0.0
        for name, weight in names:
            weight_sum += weight

        for name, weight in names:
            normalized_names.append((name, weight / weight_sum))

        return normalized_names

    def sample(self):
        names = []
        names.append(self.first_name_sampler.sample())
        names.append(self.last_name_sampler.sample())

        return " ".join(names)
class NameSampler(object):
    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)

    def normalize(self, names):
        normalized_names = []

        weight_sum = 0.0
        for name, weight in names:
            weight_sum += weight

        for name, weight in names:
            normalized_names.append((name, weight / weight_sum))

        return normalized_names

    def sample(self):
        names = []
        names.append(self.first_name_sampler.sample())
        names.append(self.last_name_sampler.sample())

        return " ".join(names)
    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 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)
Beispiel #6
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)
class LocationSampler(object):
    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)

    def _dist(self, lat_A, long_A, lat_B, long_B):
        """
        Computes distance between latitude-longitude
        pairs in miles.
        """
        dist = (math.sin(math.radians(lat_A)) *
                math.sin(math.radians(lat_B)) +
                math.cos(math.radians(lat_A)) *
                math.cos(math.radians(lat_B)) *
                math.cos(math.radians(long_A - long_B)))
        dist = (math.degrees(math.acos(dist))) * 69.09
        return dist
        
    def _closest_store(self, zipcode):
        distances = []
        for store in self.stores:
            if store.zipcode == zipcode:
                dist = 0.0
            else:
                latA, longA = self.zipcode_objs[store.zipcode].coords
                latB, longB = self.zipcode_objs[zipcode].coords
                dist = self._dist(latA, longA, latB, longB) 
            distances.append((dist, store))
            
        return min(distances)

    def sample(self):
        return self.sampler.sample()
class LocationSampler(object):
    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)

    def _dist(self, lat_A, long_A, lat_B, long_B):
        """
        Computes distance between latitude-longitude
        pairs in miles.
        """
        dist = (math.sin(math.radians(lat_A)) * math.sin(math.radians(lat_B)) +
                math.cos(math.radians(lat_A)) * math.cos(math.radians(lat_B)) *
                math.cos(math.radians(long_A - long_B)))
        dist = (math.degrees(math.acos(dist))) * 69.09
        return dist

    def _closest_store(self, zipcode):
        distances = []
        for store in self.stores:
            if store.zipcode == zipcode:
                dist = 0.0
            else:
                latA, longA = self.zipcode_objs[store.zipcode].coords
                latB, longB = self.zipcode_objs[zipcode].coords
                dist = self._dist(latA, longA, latB, longB)
            distances.append((dist, store))

        return min(distances)

    def sample(self):
        return self.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)
class ZipcodeSampler(object):
    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 sample(self):
        return self.sampler.sample()
    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)
    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)