Esempio n. 1
0
    def init_population(self, X, y):
        n_x, n_y = X.shape[0], y.shape[0]
        population = []
        for i in range(self.pop_size):
            c = Chromosome(n_x, n_y)
            f = c.compute_fitness(X, y, self.activation)
            c.set_fitness(f)

            population.append(c)

            if f > self.best_fitness:
                self.best_fitness = f
                self.best_chromosome = copy.deepcopy(c)

        self.population = population
Esempio n. 2
0
FUNCTION_ADF = FUNCTION + ADF

# read data
nd, nv, data = read_data(
    "dataset/F0_1_training_data.txt")  # "./dataset/F49_2_training_data.txt"
cuda = torch.device("cuda")
data = torch.tensor(data)

population = []

for _ in range(NP):
    # random initialization
    c = Chromosome(var_num=nv)

    # evaluate initial population
    c.compute_fitness(data)

    population.append(c)

for epo in range(1000000):
    # population.sort(key=lambda x: x.fitness)
    # best_individual = population[0]
    best_individual = sorted(population, key=lambda x: x.fitness)[0]

    if epo % 10 == 0:
        print("epoch:", epo, " fitness:", best_individual.fitness)
        print(best_individual.gene)

        if best_individual.fitness < 1e-5:
            break