为什么已经正确定义对象时“ NoneType”对象没有属性“ eval”?

时间:2019-01-12 15:59:59

标签: python

我正在测试写在网站上的代码

http://foreverlearning.altervista.org/genetic-programming-symbolic-regression-pt-3/

该代码的一部分位于网页的底部。运行测试代码mainpova.py时,出现语法错误。

语法错误是

python mainprova4.py
Best solution is ((((3-2)-1)+((x-2)+3))+3) with error 22.0...
Producing gen number 2...
Traceback (most recent call last):
File "mainprova4.py", line 68, in <module>
main()
File "mainprova4.py", line 41, in main
res = member.eval({"x": xs[i], "y": ys[i]})
AttributeError: 'NoneType' object has no attribute 'eval'

mainprova4.py的代码是

import generation as gn
import tree as tr
import generator as gtr
import math

xs = [-1, 1, 0, 3, -2, 0, -1, 3, 2, -2] # Values of x
ys = [1, 1, 0, 2, -2, 5, 3, -1, 5, -4] # Values of y
zs = [3, 3, 1, 12, 3, 6, 5, 9, 10, 1] # Values of z, from z = x^2 + y + 1

def main():
    minHeight = 1
    maxHeight = 5
    minValue = 1
    maxValue = 3
    variables = ["x", "y"]
    operators = ["+", "-", "*"]

    numOfMembers = 150
    maxNumOfGenerations = 500
    currentGen = 1
    crossoverPerc = 0.5
    mutationPerc = 0.3
    randomPerc = 0.1
    copyPerc = 0.1
    shouldPruneForMaxHeight = True



    # Step 1: create first generation
    gen = gn.Generation()
    for i in range(0, numOfMembers):
    gen.addMember(gtr.getTree(minHeight, maxHeight, minValue, maxValue, variables, operators))


    for genNum in range(1, maxNumOfGenerations + 1):
       """ Step 2: evaluate all members """
       for memberNum in range(0, gen.size()):
       member = gen.getMember(memberNum)
       totalError = 0
       for i in range(0, len(xs)):
           res = member.eval({"x": xs[i], "y": ys[i]})
           error = math.fabs(zs[i] - res)
           totalError += error
           gen.setError(memberNum, totalError)

       """ Step 3: sort solutions according to errors """
       gen.sort(descending = False)

       """ Step 4: if best solution has error zero, then stop """
       print("Best solution is " + str(gen.getMember(0)) + " with error " + str(gen.getError(0)) + "...")
       if gen.getError(0) == 0:
       break

       """ If limit reached, then stop process """
       if currentGen == maxNumOfGenerations:
          print("LIMIT REACHED")
      break

       """ Step 5: produce next generation """
       currentGen += 1
       print("Producing gen number " + str(currentGen) + "...")
       gen.next(crossoverPerc, mutationPerc, randomPerc, copyPerc, shouldPruneForMaxHeight, minHeight, maxHeight, minValue, maxValue, variables, operators)

    print("END ~~~~~~~~~~~~~~~~~~~~~~~~")
    print("Best solution found is " + str(gen.getMember(0)) + " with error " + str(gen.getError(0)))

if __name__ == "__main__":
    main()

我在这里想念的是什么?数学模块正确吗?

1 个答案:

答案 0 :(得分:0)

member = gen.getMember(memberNum)返回None。在python中,一切都是对象。并非每个对象都是相同的。例如None对象仅包含以下内容:

>>> dir(None)
['__bool__', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']

您应该调查为什么getMember()函数不返回任何值。也许问题出在您在addMember()上呼叫gen的那一行中?