Example #1
0
    def setUp(self):
        """Test Problem: Assume you can only carry 3 items to sell in your bag.
        Pick a combination of items that maximises your profit. The profit for
        each item is listed.
        """
        self.seed_data = [('apple', 15), ('banana', 10), ('carrot', 12),
                          ('pear', 5), ('mango', 8)]

        member_1 = pyeasyga.Chromosome([0, 1, 0, 1, 1])
        member_2 = pyeasyga.Chromosome([1, 1, 0, 0, 1])
        member_3 = pyeasyga.Chromosome([1, 1, 1, 1, 0])
        member_4 = pyeasyga.Chromosome([0, 1, 1, 1, 1])

        self.population = []
        self.population.append(member_1)
        self.population.append(member_2)
        self.population.append(member_3)
        self.population.append(member_4)

        self.ga = pyeasyga.GeneticAlgorithm(self.seed_data)
        self.ga.population_size = 10
        self.ga.generations = 10

        self.ga.fitness_function = lambda member, data: sum([
            profit for (selected, (fruit, profit)) in zip(member, data)
            if selected and member.count(1) == 3
        ])

        self.ga.selection_function = self.ga.tournament_selection
Example #2
0
    def create_initial_population(self):

        initial_population = []
        if len(self.seed_data) > 2:
            initial_population.append(pyeasyga.Chromosome(self.seed_data[2]))

        while len(initial_population) < self.population_size:
            genes = self.create_individual(self.seed_data)
            individual = pyeasyga.Chromosome(genes)
            initial_population.append(individual)
        self.current_generation = initial_population
Example #3
0
    def test_chromosome_initialisation_2(self):
        chromosome = pyeasyga.Chromosome(['d', 'e', 'f'])
        chromosome.fitness = 20

        assert chromosome.genes == ['d', 'e', 'f']
        assert chromosome.fitness == 20
        assert str(chromosome) == "(20, ['d', 'e', 'f'])"
Example #4
0
def create_individual(prob):
    individual = []
    for p in prob:
        if random() < p:
            individual.append(1)
        else:
            individual.append(0)
    return pyeasyga.Chromosome(individual)
Example #5
0
def rn_create_individual(prob):
    individual = []
    for (mean, stdev) in prob:
        # value = random.uniform(mean - stdev, mean + stdev)
        # individual.append(value)
        individual.append(np.random.normal(mean, stdev))
    # Return a new individual
    return pyeasyga.Chromosome(individual)
Example #6
0
    def test_create_next_generation(self):
        pop_size = self.ga.population_size
        self.ga.create_first_generation()
        self.ga.create_next_generation()
        current_gen = self.ga.current_generation

        assert len(current_gen) == pop_size
        assert isinstance(current_gen[0], type(pyeasyga.Chromosome([1])))
Example #7
0
    def test_create_initial_population(self):
        pop_size = self.ga.population_size
        self.ga.create_initial_population()
        initial_pop = self.ga.current_generation

        assert len(initial_pop) == pop_size
        assert isinstance(initial_pop[0], type(pyeasyga.Chromosome([1])))
        assert sum([member.fitness for member in initial_pop]) == 0
Example #8
0
    def test_run(self):
        self.ga.run()
        current_gen = self.ga.current_generation
        last_generation = self.ga.last_generation()

        assert len(current_gen) == self.ga.population_size
        assert isinstance(current_gen[0], type(pyeasyga.Chromosome([1])))
        assert isinstance(next(last_generation), type((1, [1, 1, 1, 1, 1])))
        assert len(next(last_generation)) == 2
        assert len(next(last_generation)[1]) == 5
Example #9
0
def create_individual(prob):
    individual = []
    for p in prob:
        mean, stdev = p
        value = random.uniform(mean - stdev, mean + stdev)
        individual.append(value)
    mean = numpy.mean(individual)
    stdev = numpy.std(individual, ddof=1)
    individual = numpy.random.normal(mean, stdev, len(individual))
    # Return a new individual
    return pyeasyga.Chromosome(individual)
Example #10
0
    def test_create_new_population(self):
        """ Write more functional test """
        pop_size = self.ga.population_size
        self.ga.create_initial_population()
        self.ga.calculate_population_fitness()
        self.ga.rank_population()
        self.ga.create_new_population()
        current_gen = self.ga.current_generation

        assert len(current_gen) == pop_size
        assert isinstance(current_gen[0], type(pyeasyga.Chromosome([1])))
Example #11
0
    def create_new_population(self):

        new_population = []
        for i in range(
                int(CONSTANT.POPULATION_BEST_PORTION * self.population_size)):
            elite = copy.deepcopy(self.current_generation[i])
            new_population.append(elite)

        selection = self.selection_function

        while len(new_population) < int(self.population_size *
                                        (CONSTANT.POPULATION_BEST_PORTION +
                                         CONSTANT.POPULATION_REMAIN_PORTION)):
            parent_1 = copy.deepcopy(selection(self.current_generation))
            parent_2 = copy.deepcopy(selection(self.current_generation))

            child_1, child_2 = parent_1, parent_2
            child_1.fitness, child_2.fitness = 0, 0

            can_crossover = random.random() < self.crossover_probability
            can_mutate = random.random() < self.mutation_probability

            if can_crossover:
                child_1.genes, child_2.genes = self.crossover_function(
                    parent_1.genes, parent_2.genes)

            if can_mutate:
                self.mutate_function(child_1.genes)
                self.mutate_function(child_2.genes)

            new_population.append(child_1)
            if len(new_population) < self.population_size:
                new_population.append(child_2)

        remain_num = self.population_size - len(new_population)
        for i in range(remain_num):
            new_population.append(
                pyeasyga.Chromosome(self.create_individual(0)))

        self.current_generation = new_population
Example #12
0
    def test_chromosome_initialisation_1(self):
        chromosome = pyeasyga.Chromosome(['a', 'b', 'c'])

        assert chromosome.genes == ['a', 'b', 'c']
        assert chromosome.fitness == 0
        assert str(chromosome) == "(0, ['a', 'b', 'c'])"