-3
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')
虽然执行此代码块示出了一个错误:在exceuting下面的代码获得以下错误:索引超出范围
x.write(str((V[i])[j]) + '\t')
IndexError: list index out of range
在这里,我已经采取gmax=5
,dim=2
和NP=5
。
列表“V”似乎少于'NP'元素。要么在该循环中更改'i'范围,要么找出为什么'V'具有太少的元素。 – chepner 2014-10-07 14:41:45