Пример #1
0
    def selection(self, fitness_values: List[float]) -> List[int]:

        probabilities = listtools.normListSumTo(fitness_values, 1)
        parent_indices = []
        random.seed()
        parent1_probability = random.random()
        parent2_probability = random.random()

        summ = 0
        for i in range(self.config['population_size']):
            if len(parent_indices) == 2:
                break
            next_sum = summ + probabilities[i]
            if next_sum >= parent1_probability >= summ:
                parent_indices.append(i)
            if next_sum >= parent2_probability >= summ:
                parent_indices.append(i)
            summ = next_sum
        return parent_indices
def selection(fitness_values):
	# normalize the list so we have probabilities to pick parents
	fitness_values = listtools.normListSumTo(fitness_values, 1)
	parents = []
	random.seed()
	parent1_probability = random.random()
	parent2_probability = random.random()
	
	sum = 0
	for i in range(POPULATION_SIZE):
		if len(parents) == 2:
			break
		next_sum = sum + fitness_values[i]
		if parent1_probability <= next_sum and parent1_probability >= sum:
			parents.append(i)
		if parent2_probability <= next_sum and parent2_probability >= sum:
			parents.append(i)
		sum = next_sum

	return parents
    def selection(self, fitness_values):
        """
        Pick two parents according to probability represented by normalized fitness values
        3a[Selection] Select two parent chromosomes from a population according to their fitness (the better fitness, the bigger chance to be selected)
        """
        # normalize the list so we have probabilities to pick parents
        fitness_values = listtools.normListSumTo(fitness_values, 1)
        parentIndices = []
        random.seed()
        parent1_probability = random.random()
        parent2_probability = random.random()

        sum = 0
        for i in range(self.config['population_size']):
            if len(parentIndices) == 2:
                break
            next_sum = sum + fitness_values[i]
            if parent1_probability <= next_sum and parent1_probability >= sum:
                parentIndices.append(i)
            if parent2_probability <= next_sum and parent2_probability >= sum:
                parentIndices.append(i)
            sum = next_sum
        return parentIndices
    def selection(self, fitness_values):
        """
        Pick two parents according to probability represented by normalized fitness values
        3a[Selection] Select two parent chromosomes from a population according to their fitness (the better fitness, the bigger chance to be selected)
        """
        # normalize the list so we have probabilities to pick parents
        fitness_values = listtools.normListSumTo(fitness_values, 1)
        parentIndices = []
        random.seed()
        parent1_probability = random.random()
        parent2_probability = random.random()

        sum = 0
        for i in range(self.config['population_size']):
            if len(parentIndices) == 2:
                break
            next_sum = sum + fitness_values[i]
            if parent1_probability <= next_sum and parent1_probability >= sum:
                parentIndices.append(i)
            if parent2_probability <= next_sum and parent2_probability >= sum:
                parentIndices.append(i)
            sum = next_sum
        return parentIndices
Пример #5
0
 def test_normListSumTo(self):
     lista = [1, 2, 5, 2]
     list2 = listtools.normListSumTo(lista, 1)
     self.assertEqual(sum(list2), 1)
     self.assertListEqual(list2, [0.1, 0.2, 0.5, 0.2])