def __init__(self, name: str, position: list, surround=None): self._position = position self.name = name self.color = [rand_unif(0, 1), rand_unif(0, 1), rand_unif(0, 1)] self.field_of_vision = surround # sense surroundings self._reserves = 0 self.nn = NeuralNet(8, 4, 2) self.genome = []
def sample_from_mixture(b): """ Sample from a mixture of Unif(0, - log(b)) and - log(b) + Exp(1). """ log_b = log(b) mass_to_left = exp(-b) * (-log_b) mass_to_right = 0.36787944117144233 # = exp(-1) prob_to_left = mass_to_left / (mass_to_left + mass_to_right) sampled_from_left = prob_to_left > rand_unif() if sampled_from_left: rv = (-log_b) * rand_unif() else: rv = -log_b + rand_exp(1.) return rv
def generate_loot(amount: int): loot_results = {'coins': None, 'gemstones': None, 'items': None} loot_totals = copy(loot_results) loot_types = list(loot_results.keys()) shuffle(loot_types) allocated_value = 0 for loot in loot_types: remaining_value = amount - allocated_value if loot == loot_types[-1]: loot_type_value = remaining_value else: loot_type_value = round(rand_unif(0, remaining_value)) if loot_type_value < amount / 10: loot_type_value = 0 elif remaining_value - loot_type_value < amount / 10: loot_type_value = remaining_value loot_totals[loot] = loot_type_value allocated_value = allocated_value + loot_type_value if loot == 'coins': loot_results[loot] = assorted_coinage(loot_type_value) elif loot == 'gemstones': loot_results[loot] = make_verbose_items( generate_gemstones(loot_type_value), gems_data) elif loot == 'items': loot_results[loot] = make_verbose_items( generate_items(loot_type_value), items_data) else: loot_results[loot] = loot_type_value print(loot_totals) return loot_results
def sample_from_transformed_precision(b): """ Sample from a density proportional to exp(- b * exp(x)) by rejection sampler. """ accepted = False while not accepted: rv = sample_from_bounding_dist(b) target_logp = -b * exp(rv) bound_logp = compute_bounding_logp(rv, b) accept_prob = exp(target_logp - bound_logp) accepted = accept_prob > rand_unif() return rv
def realize(self): """ returns a realization of the discrete random variable. each vals[i] is returned at a corresponding frequency in probs[i]. """ # generate uniform rv u = rand_unif() # find first value in cuts[i] that is strictly larger than u, and then # return vals[i] i = 0 while i < self.nvals: if u < self.cuts[i]: break i = i + 1 # if there is a mistake return None return self.vals[i]