Ejemplo n.º 1
0
    def search(self, n_iterations=0, report=False, log=False):
        offsprings = []
        while len(offsprings) < len(self.population):

            off1, off2 = p1, p2 = self.selection(self.population,
                                                 self._random_state,
                                                 self.pressure)

            if self._random_state.uniform() < self.p_c:
                self._crossover(p1, p2)

            if self._random_state.uniform() < self.p_m:
                off1 = self._mutation(off1)
                off2 = self._mutation(off2)

            if not (hasattr(off1, 'fitness') and hasattr(off2, 'fitness')):
                self.problem_instance.evaluate(off1)
                self.problem_instance.evaluate(off2)

            #self.select_the_offpring(p1, p2, off1, off2, offsprings)

            offsprings.extend([off1, off2])

        while len(offsprings) > len(self.population):
            offsprings.pop()

        #print(np.std([offspring.fitness for offspring in offsprings]))

        if np.std([offspring.fitness for offspring in offsprings]) < 0.02:
            self.selection = uls.parametrized_tournament_selection(0.2)
        else:
            self.selection = uls.parametrize_roulette_wheel_w_pressure(0.2)

        self.population = np.asarray(self.sort_populations(offsprings))
        self.best_solution = self._get_elite(self.population)

        if report:
            self._verbose_reporter_inner(self.best_solution, n_iterations)

        if self.flag:
            if self.best_solution.fitness - uls.calculate_media_solution(
                    self.population) < 0.02:
                self.selection = uls.parametrize_rank_selection(self.pressure)
            else:
                self.selection = uls.parametrize_roulette_wheel_w_pressure(
                    self.pressure)
Ejemplo n.º 2
0
# restrictions:
# - 5000 f.e./run
# - 50 f.e./generation
# - use at least 5 runs for benchmarks
# ++++++++++++++++++++++++++
n_gen = 100
ps = 50
p_c = .9
p_m = 0.4
radius = .9
pressure = .2
p_migration = 0
ga1 = GeneticAlgorithm(problem_instance=ann_op_i,
                       random_state=random_state,
                       population_size=ps,
                       selection=uls.parametrized_tournament_selection(0.2),
                       crossover=uls.geometric_semantic_crossover,
                       p_c=p_c,
                       mutation=uls.parametrized_ball_mutation(radius),
                       p_m=p_m,
                       pressure=pressure)

ga2 = GeneticAlgorithm(
    problem_instance=ann_op_i,
    random_state=random_state,
    population_size=ps,
    selection=uls.parametrize_roulette_wheel_w_pressure(0.2),
    crossover=uls.geometric_semantic_crossover,
    p_c=p_c,
    mutation=uls.parametrized_ball_mutation(radius),
    p_m=p_m,
Ejemplo n.º 3
0
    #++++++++++++++++++++++++++
    # THE PROBLEM INSTANCE
    # - optimization of ANN's weights is a COP
    #++++++++++++++++++++++++++
    ann_op_i = ANNOP(search_space=(-2, 2, n_weights), fitness_function=ann_i.stimulate,
                     minimization=False, validation_threshold=validation_threshold)

    #++++++++++++++++++++++++++
    # THE SEARCH
    # restrictions:
    # - 5000 offsprings/run max*
    # - 50 offsprings/generation max*
    # - use at least 5 runs for your benchmarks
    # * including reproduction
    #++++++++++++++++++++++++++
    ga1 = GeneticAlgorithm2(ann_op_i, random_state, ps, uls.parametrized_tournament_selection(pressure),
                          uls.one_point_crossover, p_c, uls.parametrized_ball_mutation(radius), 0.2)
    ga2 = GeneticAlgorithm(ann_op_i, random_state, ps, uls.parametrized_tournament_selection(pressure),
                           uls.two_point_crossover, p_c, uls.parametrized_ball_mutation(radius), 0.1)
    sa1 = SimulatedAnnealing(ann_op_i, random_state, ps, uls.parametrized_ball_mutation(radius), control, update_rate)
    search_algorithms = [ga1, ga2]

    # initialize search algorithms
    [algorithm.initialize() for algorithm in search_algorithms]

    # execute search
    [algorithm.search(n_iterations=n_gen, report=False) for algorithm in search_algorithms]

#++++++++++++++++++++++++++
# TEST
# - test algorithms on unseen data
Ejemplo n.º 4
0
    def search(self, n_iterations, report=False, log=False):
        elite = self.best_solution
        offsprings = []
        while len(offsprings) < len(self.population):
            off1, off2 = p1, p2 = [
                self.selection(self.population, self.problem_instance.minimization, self._random_state,
                               self.presure)
                for _ in
                range(2)]

            if self._random_state.uniform() < self.p_c:
                off1, off2 = self._crossover(p1, p2)

            if self._random_state.uniform() < self.p_m:
                off1 = self._mutation(off1)
                off2 = self._mutation(off2)

            if not (hasattr(off1, 'fitness') and hasattr(off2, 'fitness')):
                self.problem_instance.evaluate(off1)
                self.problem_instance.evaluate(off2)
            offsprings.extend([off1, off2])

        while len(offsprings) > len(self.population):
            offsprings.pop()

        elite_offspring = self._get_elite(offsprings)
        elite = self._get_best(elite, elite_offspring)

        # Elitism
        selectTheBestOffsprings = pd.DataFrame(np.asanyarray([[offspring, offspring.fitness] for offspring
                                                              in offsprings]))
        selectTheBestPopulation = pd.DataFrame(np.asanyarray([[individual, individual.fitness] for individual
                                                              in self.population]))

        selectTheBestOffsprings.rename(index=str, columns={0: "Offspring", 1: "Fitness"}, inplace=True)
        selectTheBestPopulation.rename(index=str, columns={0: "Individual", 1: "Fitness"}, inplace=True)

        selectTheBestOffsprings.sort_values(ascending=False, inplace=True, by="Fitness")
        selectTheBestPopulation.sort_values(ascending=False, inplace=True, by="Fitness")

        selectTheBestOffspringsList = selectTheBestOffsprings['Offspring'].tolist()
        selectTheBestPopulationList = selectTheBestPopulation['Individual'].tolist()

        newPopulation = selectTheBestOffspringsList[:24] + selectTheBestPopulationList[:1]

        self.population = newPopulation
        if elite.fitness == self.best_solution.fitness:
            self.repetition = self.repetition+1
        else:
            self.best_solution = elite
            self.repetition = 0

        if self.repetition > 3:
            if self.selection == uls.parametrize_roulette_wheel(self.presure):
                self.selection = uls.parametrized_tournament_selection(self.presure)
                self.repetition = 0
            else:
                self.selection = uls.parametrize_roulette_wheel(self.presure)
                self.repetition = 0

        if self.repetition >8:
            self.p_m = 0.9

        if self.p_m == 0.9 and  self.repetition<4:
            self.p_m = 0.3
#++++++++++++++++++++++++++
# THE OPTIMIZATION
# restrictions:
# - 5000 f.e./run
# - 50 f.e./generation
# - use at least 5 runs for benchmarks
#++++++++++++++++++++++++++
n_gen = 100
ps = 10
p_c = .8
p_m = .8
radius = .002
pressure = .5

ga1 = GeneticAlgorithm(ann_op_i, random_state, ps,
                       uls.parametrized_tournament_selection(pressure),
                       uls.cicle_crossover, p_c,
                       uls.parametrized_gaussian_member_mutation(radius), p_m,
                       pressure)

ga2 = GeneticAlgorithm(ann_op_i, random_state, ps,
                       uls.parametrized_tournament_selection(pressure),
                       uls.cicle_crossover, p_c,
                       uls.parametrized_gaussian_member_mutation(radius), p_m,
                       pressure)

ga3 = GeneticAlgorithm(ann_op_i, random_state, ps,
                       uls.parametrized_tournament_selection(pressure),
                       uls.cicle_crossover, p_c,
                       uls.parametrized_gaussian_member_mutation(radius), p_m,
                       pressure)
Ejemplo n.º 6
0
import os
import datetime
import logging
import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import utils as uls
from problems.ANNOP import ANNOP
from ANN.ANN import ANN, softmax, sigmoid
from algorithms.genetic_algorithm_Elitism import GeneticAlgorithm
# setup logger
file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "LogFiles/" + (str(datetime.datetime.now().date()) + "-" + str(datetime.datetime.now().hour) + \
            "_" + str(datetime.datetime.now().minute) + "_log.csv"))
logging.basicConfig(filename=file_path,
                    level=logging.DEBUG,
                    format='%(name)s,%(message)s')
#++++++++++++++++++++++++++
# THE DATA
# restrictions:
# - MNIST digits (8*8)
# - 33% for testing
# - flattened input images
#++++++++++++++++++++++++++
# import data
digits = datasets.load_digits()
flat_images = np.array([image.flatten() for image in digits.images])
print(flat_images.shape)
print(digits.target_names)