【问题标题】:Calculating with items enclosed in list positions (genetic algorithms fitness)计算包含在列表位置中的项目(遗传算法适应度)
【发布时间】:2018-05-04 21:37:51
【问题描述】:
population = [[[0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1], [1], [0]],
 [[0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1], [3], [1]],
 [[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0], [4], [2]],
 [[1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0], [3], [3]]]

def ProbabilityList2(population):
    fitness = [chromosome[1] for chromosome in population]
    total_weight=sum(fitness)
    relative_fitness= [(chromosome[1]+1)/total_weight for chromosome in population]
    return (relative_fitness)

我正在尝试按照以下逻辑返回一个基于比例适应度值的列表:[[chromosome],[fitness],[counter]]。我想要做的就是为列表中的所有项目(个人)生成一个基于此操作的概率列表,但我收到错误:

TypeError: unsupported operand type(s) for +: 'int' and 'list'

我在使用字典之前解决了这个问题,但是在程序的循环过程中,我得到了重复的条目并且选择函数崩溃了,因为人口中的个体数量和概率(按位置索引)是不均匀的。关于如何以这种格式计算它的任何想法?

【问题讨论】:

    标签: python python-3.x list probability genetic-algorithm


    【解决方案1】:

    chromosome[1] 是一个列表。您可以使用chromosome[1][0] 访问它,或者将其存储在列表之外。 `

    【讨论】:

      【解决方案2】:

      假设fitness 列表是人口适应度列表。因此,要获得适应度的总和,您必须通过循环遍历范围来获得其中的子列表的总和。

      def ProbabilityList2(population):
          fitness = [ chromosome[1] for chromosome in population ]
          total_weight=0
          for i in range(len(fitness)):
              total_weight+=sum(fitness[i])
      

      这将为您提供以下健身列表和总和

      [[1], [3], [4], [3]] # fitness list
      11                   # sum
      

      【讨论】:

        【解决方案3】:

        试试这个功能:

        def probabilityList2(population):    
            fitness = [chromosome[1][0] for chromosome in population]
            total_weight=sum(fitness)
            relative_fitness= [((chromosome[1][0])+1)/total_weight for chromosome in population]
            return relative_fitness
        
        
        probabilityList2(population)
        

        【讨论】:

          猜你喜欢
          • 2011-04-07
          • 1970-01-01
          • 2020-05-12
          • 2023-02-20
          • 2017-12-16
          • 2011-12-21
          • 2020-10-14
          • 2011-10-07
          • 1970-01-01
          相关资源
          最近更新 更多