【发布时间】:2023-03-23 21:25:01
【问题描述】:
def evolve():
global pop,fvals
for g in xrange(0,gmax):
for i in xrange(0,NP):
while 1:
r1=random.randint(0,NP-1)
if r1!=i:
break
while 1:
r2=random.randint(0,NP-1)
if r2!=r1 and r2!=i:
break
while 1:
r3=random.randint(0,NP-1)
if r3!=r2 and r3!=r1 and r3!=i:
break
U=[]
V=[]
for j in xrange(0,dim):
U.insert(j,(pop[r3])[j] + F*((pop[r1])[j]-(pop[r2])[j]))
jrand = floor(int(rand1()*dim))
for j in xrange(0,dim):
if rand1()<=cr or j==jrand:
U.insert(j,(pop[r3])[j] + F*((pop[r1])[j]-(pop[r2])[j]))
else:
U.insert(j,(pop[i])[j])
V.insert(i,U)
fvals2.insert(i,fun(U))
x=open("x.out","w")
for i in xrange(NP):
for j in xrange(dim):
print i
print j
x.write(str((V[i])[j]) + '\t')
x.write(str(fvals2[i]))
x.write('\n')
执行此代码块时显示错误:
x.write(str((V[i])[j]) + '\t')
IndexError: list index out of range
这里我取了gmax=5、dim=2和NP=5。
【问题讨论】:
-
列表
V似乎少于NP元素。要么更改i在该循环中的范围,要么找出为什么V的元素太少。
标签: python file syntax-error