def testWeightedIndexRespectsDistribution(self): # TODO: This is a change detector; find a better strategy than RNG seeding. numpy.random.seed(0) distribution = numpy.array([0.3, 0.0, 0.3, 0.3, 0.1]) expected = [2, 2, 2, 2, 2, 3, 3, 3, 3, 4] self.assertItemsEqual( expected, helpers.weighted_index(distribution, len(expected)))
def testWeightedIndexDefaultNumberResults(self): distribution = numpy.array([0.0, 1.0, 0.0, 0.0]) # 1 until the end of time. expected = numpy.array([1, 1, 1, 1]) numpy.testing.assert_equal(expected, helpers.weighted_index(distribution))
def get_signal(self, meaning_index): """Returns a signal by sampling from the P matrix. Arguments: meaning_index: Index of the meaning for which to produce a signal. Returns: The index of a signal determined by the weights in the active matrix. Raises: IndexError if meaning_index >= number of signals or meaningy_index < 0. """ return helpers.weighted_index(self._active_matrix[meaning_index], 1)[0]
def get_meaning(self, signal_index): """Returns a meaning by sampling from the Q matrix. Arguments: signal_index: Index of the signal for which to produce a meaning. Returns: The index of a meaning determined by the weights in the passive matrix. Raises: IndexError if signal_index >= number of meanings or signal_index < 0. """ return helpers.weighted_index(self._passive_matrix[signal_index], 1)[0]
def reproduce(state): payoff_vector = numpy.vectorize( lambda individual: fitness.total_payoff(individual, state. individuals)) payoffs = payoff_vector(state.individuals) normalized = helpers.safe_divide(payoffs, numpy.sum(payoffs)) new_indices = helpers.weighted_index(normalized) # TODO: Create a vfunc to create a freq dict from new_indices, then another # to cache association matrices, obviating duplicate calculation. Then # again, maybe each child SHOULD get a separate A matrix? parents = state.individuals[new_indices] return numpy.array([ model.Interlocutor.from_association_matrix(reveal(revelator, k)) for revelator in parents ])