我正在测试写在网站上的代码
http://foreverlearning.altervista.org/genetic-programming-symbolic-regression-pt-3/
该代码的一部分位于网页的底部。运行测试代码mainpova.py时,出现语法错误。
语法错误是
python mainprova4.py
Best solution is ((3*(1*2))+x) with error 20.0...
Producing gen number 2...
Traceback (most recent call last):
File "mainprova4.py", line 68, in <module>
main()
File "mainprova4.py", line 62, in main
gen.next(crossoverPerc, mutationPerc, randomPerc, copyPerc,shouldPruneForMaxHeight, minHeight, maxHeight, minValue, maxValue, variables, operators)
AttributeError: 'Generation' object has no attribute 'next'
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()
我看不到这是缩进问题。我在这里想念的。 下一个定义的代码是
import random as rnd
import generator as gtr
import treeOperations as trop
class Generation(object):
def __init__(self):
self.membersWithErrors = []
def addMember(self, member):
""" Add a tree to the generation """
self.membersWithErrors.append([member, 0])
def setMember(self, member, index):
""" Updates the member at the specified position """
self.membersWithErrors[index] = member
def setError(self, index, error):
""" Sets the error of the member at the specified position """
self.membersWithErrors[index][1] = error
def getMember(self, index):
""" Returns the member at the specified position """
return self.membersWithErrors[index][0]
def getError(self, index):
""" Returns the error of the member at the specified position """
return self.membersWithErrors[index][1]
def size(self):
""" Returns the number of members curently in the generation """
return len(self.membersWithErrors)
def clear(self):
""" Clears the generation, i.e. removes all the members """
self.membersWithErrors.clear()
def sort(self, descending):
""" Sorts the members of the generation according the their score """
self.membersWithErrors.sort(key = lambda l: l[1], reverse = descending)
def getMembersForReproduction(self, numMembers, pickProb):
""" Returns a certain number of distinct members from the generation.
The first member is selected with probability pickProb. If it's not chosen, the
second member is selected with probability pickProb, and so on. """
selectedMembers = []
while len(selectedMembers) < numMembers:
indexSelected = 0
while rnd.randint(0, 100) > int(pickProb * 100) and indexSelected != len(self.membersWithErrors) - 1:
indexSelected += 1
memberWithErrorSelected = self.membersWithErrors[indexSelected]
if memberWithErrorSelected[0] not in selectedMembers:
selectedMembers.append(memberWithErrorSelected[0])
return selectedMembers
def next(self, crossoverPerc, mutationPerc, randomPerc, copyPerc, shouldPruneForMaxHeight, minHeight, maxHeight, minValue, maxValue, variables, operators):
""" It proceeds to the next generation with the help of genetic operations """
oldMembersWithError = self.membersWithErrors
newMembersWithError = []
maxMembers = len(oldMembersWithError)
numCrossover = int(maxMembers * crossoverPerc)
numMutation = int(maxMembers * mutationPerc)
numRandom = int(maxMembers * randomPerc)
numCopy = maxMembers - numCrossover - numMutation - numRandom
# Crossover
for i in range(0, numCrossover):
members = self.getMembersForReproduction(2, 0.3)
m1 = members[0]
m2 = members[1]
newMember = trop.crossover(m1, m2)
newMembersWithError.append([newMember, 0])
# Crossover
for i in range(0, numCrossover):
members = self.getMembersForReproduction(2, 0.3)
m1 = members[0]
m2 = members[1]
newMember = trop.crossover(m1, m2)
if shouldPruneForMaxHeight and newMember.height() > maxHeight:
newMember = trop.pruneTreeForMaxHeight(newMember, maxHeight, minValue, maxValue, variables)
newMembersWithError.append([newMember, 0])
# Mutation
for i in range(0, numMutation):
m1 = self.getMembersForReproduction(1, 0.3)[0]
newMembersWithError.append([trop.mutation(m1, minValue, maxValue, variables, operators), 0])
# Random
for i in range(0, numRandom):
newMembersWithError.append([gtr.getTree(minHeight, maxHeight, minValue, maxValue, variables, operators), 0])
# Copy
members = self.getMembersForReproduction(numCopy, 0.3)
for m in members:
ewMembersWithError.append([m.clone(), 0])
self.membersWithErrors = newMembersWithError
答案 0 :(得分:1)
您可以在此处看到突出显示的错误:
...hits._source: {
uniqueCoolThings: [
{
"id": 500
},
{
"id": 501
}
]
} ...
这应该是这样的:
import random as rnd
import generator as gtr
import treeOperations as trop
class Generation(object):
def __init__(self):
self.membersWithErrors = []
def addMember(self, member):
""" Add a tree to the generation """
self.membersWithErrors.append([member, 0])
def setMember(self, member, index):
""" Updates the member at the specified position """
self.membersWithErrors[index] = member
def setError(self, index, error):
""" Sets the error of the member at the specified position """
self.membersWithErrors[index][1] = error
def getMember(self, index):
""" Returns the member at the specified position """
return self.membersWithErrors[index][0]
def getError(self, index):
""" Returns the error of the member at the specified position """
return self.membersWithErrors[index][1]
def size(self):
""" Returns the number of members curently in the generation """
return len(self.membersWithErrors)
def clear(self):
""" Clears the generation, i.e. removes all the members """
self.membersWithErrors.clear()
def sort(self, descending):
""" Sorts the members of the generation according the their score """
self.membersWithErrors.sort(key = lambda l: l[1], reverse = descending)
############ INDENTATION PROBLEM ################
def getMembersForReproduction(self, numMembers, pickProb):
""" Returns a certain number of distinct members from the generation.
The first member is selected with probability pickProb. If it's not chosen, the
second member is selected with probability pickProb, and so on. """
selectedMembers = []
while len(selectedMembers) < numMembers:
indexSelected = 0
while rnd.randint(0, 100) > int(pickProb * 100) and indexSelected != len(self.membersWithErrors) - 1:
indexSelected += 1
memberWithErrorSelected = self.membersWithErrors[indexSelected]
if memberWithErrorSelected[0] not in selectedMembers:
selectedMembers.append(memberWithErrorSelected[0])
return selectedMembers
############ HERE IS THE INDENTATION PROBLEM ##########
def next(self, crossoverPerc, mutationPerc, randomPerc, copyPerc, shouldPruneForMaxHeight, minHeight, maxHeight, minValue, maxValue, variables, operators):
""" It proceeds to the next generation with the help of genetic operations """
oldMembersWithError = self.membersWithErrors
newMembersWithError = []
maxMembers = len(oldMembersWithError)
numCrossover = int(maxMembers * crossoverPerc)
numMutation = int(maxMembers * mutationPerc)
numRandom = int(maxMembers * randomPerc)
numCopy = maxMembers - numCrossover - numMutation - numRandom
# Crossover
for i in range(0, numCrossover):
members = self.getMembersForReproduction(2, 0.3)
m1 = members[0]
m2 = members[1]
newMember = trop.crossover(m1, m2)
newMembersWithError.append([newMember, 0])
# Crossover
for i in range(0, numCrossover):
members = self.getMembersForReproduction(2, 0.3)
m1 = members[0]
m2 = members[1]
newMember = trop.crossover(m1, m2)
if shouldPruneForMaxHeight and newMember.height() > maxHeight:
newMember = trop.pruneTreeForMaxHeight(newMember, maxHeight, minValue, maxValue, variables)
newMembersWithError.append([newMember, 0])
# Mutation
for i in range(0, numMutation):
m1 = self.getMembersForReproduction(1, 0.3)[0]
newMembersWithError.append([trop.mutation(m1, minValue, maxValue, variables, operators), 0])
# Random
for i in range(0, numRandom):
newMembersWithError.append([gtr.getTree(minHeight, maxHeight, minValue, maxValue, variables, operators), 0])
# Copy
members = self.getMembersForReproduction(numCopy, 0.3)
for m in members:
ewMembersWithError.append([m.clone(), 0])
self.membersWithErrors = newMembersWithError