我正在测试写在网站上的代码
http://foreverlearning.altervista.org/genetic-programming-symbolic-regression-pt-3/
该代码的一部分位于网页的底部。运行测试代码mainpova.py时,出现语法错误。
语法错误是
python mainprova4.py
Traceback (most recent call last):
File "mainprova4.py", line 1, in <module>
import generation as gn
File "/home/adam/DocumentsNew2/MathCode/SymbolicRegression/WebpageCode /generation.py", line 105, in <module>
for i in range(0, numCrossover):
NameError: name 'numCrossover' is not defined
此代码是
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])
# 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:
newMembersWithError.append([m.clone(), 0])
self.membersWithErrors = newMembersWithError
# No side effects
def pruneTreeForMaxHeight(tree, maxHeight, minValue, maxValue, variables):
""" Returns a new tree that is like the specified tree
but pruned so that its height is maxHeight """
def pruneTreeAux(tree, maxHeight, counter, minValue, maxValue, variables):
if tree.height() == 1:
return tree.clone()
if counter == maxHeight:
return gtr.getLeaf(minValue, maxValue, variables)
pruned1 = pruneTreeAux(tree.op1, maxHeight, counter + 1, minValue, maxValue, variables)
pruned2 = pruneTreeAux(tree.op2, maxHeight, counter + 1, minValue, maxValue, variables)
return tr.BinaryOperatorInternalNode(tree.operator, pruned1, pruned2)
return pruneTreeAux(tree, maxHeight, 1, minValue, maxValue, variables)
# 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])
已经定义了numCrossover。我在这里想念什么?
答案 0 :(得分:2)
第117行带有注释的for循环#交叉超出了next()内的定义范围
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])
# 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:
newMembersWithError.append([m.clone(), 0])
self.membersWithErrors = newMembersWithError
# No side effects
def pruneTreeForMaxHeight(tree, maxHeight, minValue, maxValue, variables):
""" Returns a new tree that is like the specified tree
but pruned so that its height is maxHeight """
def pruneTreeAux(tree, maxHeight, counter, minValue, maxValue, variables):
if tree.height() == 1:
return tree.clone()
if counter == maxHeight:
return gtr.getLeaf(minValue, maxValue, variables)
pruned1 = pruneTreeAux(tree.op1, maxHeight, counter + 1, minValue, maxValue, variables)
pruned2 = pruneTreeAux(tree.op2, maxHeight, counter + 1, minValue, maxValue, variables)
return tr.BinaryOperatorInternalNode(tree.operator, pruned1, pruned2)
return pruneTreeAux(tree, maxHeight, 1, minValue, maxValue, variables)
def pruneTreeAux(tree, maxHeight, counter, minValue, maxValue, variables):
if tree.height() == 1:
return tree.clone()
if counter == maxHeight:
return gtr.getLeaf(minValue, maxValue, variables)
pruned1 = pruneTreeAux(tree.op1, maxHeight, counter + 1, minValue, maxValue, variables)
pruned2 = pruneTreeAux(tree.op2, maxHeight, counter + 1, minValue, maxValue, variables)
return tr.BinaryOperatorInternalNode(tree.operator, pruned1, pruned2)
return pruneTreeAux(tree, maxHeight, 1, minValue, maxValue, variables)
# 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])