def _create_random_individual(self): random_scores = np.random.rand(len(self._sentences)) scored_sentences = zip(self._sentences, random_scores) sorted_sentences = sorted(scored_sentences, key=lambda tup: tup[1], reverse=True) return greedy_optimizer(sorted_sentences, self._max_length)
def _reproduction(self, population_winners, population_size, reproduction_rate="auto"): if reproduction_rate == "auto": reproduction_rate = self._reproduction_rate parents = [] number_families = int(reproduction_rate * population_size) for i in xrange(number_families): parents.append(random.sample(population_winners, 2)) children = [] for father, mother in parents: genetic_pool = [s for s in self._sentences if s in father] genetic_pool.extend([s for s in self._sentences if s in mother]) random_scores = np.random.rand(len(genetic_pool)) scored_sentences = zip(self._sentences, random_scores) sorted_sentences = sorted(scored_sentences, key=lambda tup: tup[1], reverse=True) child = greedy_optimizer(sorted_sentences, self._max_length) children.append(child) return children