我想知道编译器写的原因:
分配前引用的局部变量'randomindexmother1'
我一直在寻找,我不需要将randomindexfather1视为全局变量。因此,我认为这是elif陈述的主要内容。
def generation(listPaths):
# This function will give us the new generation of kids
# we will introduce the survived paths
# This function will take as input the list sorted paths and sorted fitnesses of each path
# listP will be a list produced in a while statmentself.
rand1 = 0
rand2 = 0
lunusedindex = []
newpaths = []
i = 0
fpoint = listPaths[0][0][0]
lpoint = listPaths[0][0][-1]
mother = []
father = []
kid1 = []
srtlist = []
# this is going to be the random number between the fitness of the maximum path fitness and the minimum of
# the half population
leng = len(listPaths)
lunusedindex = range(leng)
while i < leng / 2:
# here we select an aleatory father and an aleatory mother
rand1 = random.choice(lunusedindex)
rand2 = random.choice(lunusedindex)
if rand1 == rand2:
while rand1 == rand2:
rand1 = random.choice(lunusedindex)
# here we delete the mother and the father selected to not repeat a father and mother
del lunusedindex[rand1]
if rand1 < rand2:
del lunusedindex[rand2 - 1]
else:
del lunusedindex[rand2]
k = 0
while k < 2:
mother = copy.deepcopy(listPaths[rand1][0])
father = copy.deepcopy(listPaths[rand2][0])
del mother[0]
del mother[-1]
del father[0]
del father[-1]
indexfather = range(len(father))
indexmother = range(len(mother))
kid1.append(fpoint)
j = 0
ref = 0
rem = 0
while j < len(father) or len(mother):
# with this statment we ensuring that less points are going to be repeated and if mother or father
# become empty, this means that all the points are used but some of them can be repeated
if len(indexfather) <= 2:
randomindexfather1 = random.randint(indexfather[0], indexfather[1 - ref])
del indexfather[0:randomindexfather1 + 1]
ref += 1
if len(indexfather) == 0:
randomindexfather2 = randomindexfather1 + 1
else:
randomindexfather2 = random.randint(indexfather[0], indexfather[1 - ref])
elif len(indexmother) <= 2:
randomindexmother1 = random.randint(indexmother[0], indexmother[1 - rem])
del indexmother[0:randomindexmother1 + 1]
rem += 1
if len(indexmother) == 0:
randomindexmother2 = randomindexmother1 + 1
else:
randomindexmother2 = random.randint(indexmother[0], indexmother[1 - rem])
elif len(indexfather) > 2:
randomindexfather1 = random.randint(indexfather[0], (len(indexfather) - 2))
del indexfather[0:randomindexfather1 + 1]
randomindexfather2 = random.randint(indexfather[0], (len(indexfather) - 1))
elif len(indexmother) > 2:
randomindexmother1 = random.randint(indexmother[0], (len(indexmother) - 2))
del indexmother[0:randomindexmother1 + 1]
randomindexmother2 = random.randint(indexmother[0], (len(indexmother) - 1))
kid1.extend(father[randomindexfather1:randomindexfather2])
kid1.extend(mother[randomindexmother1:randomindexmother2])
del father[randomindexfather1:randomindexfather2]
del mother[randomindexmother1:randomindexmother2]
j += 1
# with this code we take parts of the parents until they will be empty,the problem is that points are going to be repeated
kid1.append(lpoint)
for x in kid1:
inde = random.uniform(0, len(kid1) - 1)
if kid1[inde] in kid1:
del kid1[inde]
distances = []
totdist = 0
z = 1
while z < len(kid1) - 1:
# menys 1 ja que tenim len(kid1)-1 intervals
distances.append(distpoint(kid1[z - 1], kid1[z]))
totdist = sum(distances)
newpaths.append([kid1, totdist])
k += 1
i += 1
srtlist = sorted(newpaths, key=operator.itemgetter(1))
return srtlist
x = [[[[3, 4], [2, 4], [5, 8], [2, 4], [4, 5]], 5], [[[3, 4], [6, 8], [4, 6], [6, 4], [4, 5]], 4]]
Y = generation(x)
print Y
答案 0 :(得分:3)
您的执行路径并未设置randomindexmother1
使用它。假设len(indexfather) <= 2
然后您不执行其他子句并点击kid1.extend(mother[randomindexmother1:randomindexmother2])
而不设置变量。看起来像程序逻辑中的错误。