Я использую стохастическую универсальную выборку в генетическом алгоритме.Я нашел псевдокод этого.Я выявляю проблемы при его кодировании с использованием Python 3. Ошибка: Истинное значение Серии неоднозначно.Используйте a.empty, a.bool (), a.item (), a.any () или a.all ().Может кто-нибудь мне помочь?
Псевдокод:
SUS(Population, N)
F := total fitness of Population
N := number of offspring to keep
P := distance between the pointers (F/N)
Start := random number between 0 and P
Pointers := [Start + i*P | i in [0..(N-1)]]
return RWS(Population,Pointers)
RWS(Population, Points)
Keep = []
for P in Points
i := 0
while fitness sum of Population[0..i] < P
i++
add Population[i] to Keep
return Keep
Мой пробный код выглядит следующим образом:
def susSelection(popRanked, eliteSize):
df = pd.DataFrame(np.array(popRanked), columns=["Index","Fitness"])
F = df.Fitness.cumsum()
P = F/eliteSize
Start = np.random.uniform(0,P)
Pointers = [Start + i*P for i in range(0,(eliteSize-1))]
return RWS(popRanked, Pointers)
def RWS(pop, points):
Keep = []
for P in points:
i = 0
for j in (0,i):
df = pd.DataFrame(np.array(pop), columns=["Index","Fitness"])
sumFit = df.Fitness.cumsum()
if sumFit< P:
i+=1
else:
Keep.append(pop[i])
return Keep