在根据另一个列表对一个列表进行排序时出现ValueError

时间:2019-04-03 17:54:08

标签: python-3.x genetic-algorithm

专家,我正在尝试编写“遗传算法”的代码。我最初考虑的人口为100,并试图优化球面功能。尺寸为10,我尝试使用1000代。出于同样的原因,我应用了锦标赛选择,统一交叉和随机变异。但是,当我尝试根据适应度值对总体进行排序,然后再次计算相应的适应度值时,出现了错误。 错误为:“ ValueError:具有多个元素的数组的真值不明确。请使用a.any()或a.all()”。

此外,我收到此警告:“ C:\ Users \ Saanika \ Anaconda3 \ lib \ site-packages \ ipykernel__main __。py:5:DeprecationWarning:空数组的真值不明确。返回False,但在将来会导致错误。请使用array.size > 0检查数组是否为空。”

我曾尝试搜索各种其他技术,以根据另一个列表对一个列表进行排序,但是到处都找到了相同的方法。对于警告,我尝试更新numpy,但这没有帮助。

随机变异

        def random_mutation(population, population_size):
            for x in range(0, population_size):
                value = np.random.rand(0, 1)
                if(value <= 0.001):
                        y = np.random.randint(0, 9)
                        population[x][y] = np.random.rand(-5.12, 5.12)
            return population

        # Uniform Crossover
        def uniform_crossover(population, population_size):
            population3 = []
            threshold = 0.4
            count = 0
            for i in range(0, 99):
                p1 = population[i]
                j = i
                while(i == j):
                    j = np.random.randint(0, 99)
                p2 = population[j]
                r = np.random.uniform(low = 0, high = 1, size = None)
                if(r <= 0.8):
                    count = count + 1
                    c = []
                    for y in range (0, dimension):
                        r = np.random.uniform(low = 0, high = 1, size = None)
                        if(r > threshold):
                            c.append((p1[y] + p2[y]) / 2)
                        else:
                            c.append(p1[y])
                    population3.append(c)
                population3.append(p1)
                population3.append(p2)
            return population3

        # Performing tournament selection
        def tournament_selection(population, population_size):
            population2 = []
            for i in range(population_size):
                p1 = population[i]
                j = i
                while(i == j):
                    j = np.random.randint(0, 99)
                p2 = population[j]
                if(fitness[i] < fitness[j]):
                    p = p1
                else:
                    p = p2
                population2.append(p)
            return population2

        # Calculating fitness values and creating a fitness list
        def fitness_cal(population, population_size):
            fitness = []
            for x in range(population_size):
                value = 0
                for y in range(dimension):
                    value = value + population[x][y] ** 2
                fitness.append(value)
            return fitness

        import random
        import numpy as np
        population_size = 100
        dimension = 10
        population = []
        for x in range(population_size):
            new_population = np.random.uniform(low = -5.12, high = 5.12, size = 10)
            population.append(new_population)

        for generations in range(1000):
            fitness = []
            fitness = fitness_cal(population, population_size)
            population2 = []
            population2 = tournament_selection(population, population_size)
            fitness2 = []
            fitness2 = fitness_cal(population2, population_size)
            population3 = []
            population3 = uniform_crossover(population2, population_size)
            population_size3 = len(population3)
            fitness3 = []
            fitness3 = fitness_cal(population3, population_size3)
            zipped_pairs = zip(fitness3, population3)
            population3 = [population3 for _, population3 in sorted(zipped_pairs)]
            population3 = population3[0 : 100]
            fitness3 = []
            fitness3 = fitness_cal(population3, population_size)
            population3 = random_mutation(population3, population_size)
            population = []
            population = population3


Screenshot of the error and warning.

0 个答案:

没有答案