Я пытаюсь реализовать выбор турнира в генетическом алгоритме для выбора переменной в задачах регрессии, и я пробовал это в R:
tournament_selection<-function(pop_size,population_chromo,k){
#we pick individuals out of the population to serve as parents of new offspring
#pick some number of possible parents, and then choose the best one as the winner.
#How many possible parents should be allowed to compete is the value of k
best<-rep(0,pop_size)
for(i in 1:pop_size){
ind<-sample(1:50,2,replace=FALSE) #k=2
#which of the 2 has smaller bic?
fit1<-evaluation(population_chromo[ind[1]])
fit2<-evaluation(population_chromo[ind[2]])
if(fit1<fit2){
best[i]<-ind[1]
}else{
best[i]<-ind[2]
}
}
return(best)
}
оценка - это функция, которая вычисляет балл BICкоторый состоит из 50 человек в виде последовательности из 0 и 1
Результат, который я получаю:> tour_selection (pop_size, population_chromo, 2) [1] 39 39 39 39 39 39 39 39 39 39 39 39 39 39 3939 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 [32] 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39
что не соответствует ожиданиям,По сути, я хотел бы вернуть массив с индексами, соответствующими выбранным мной паентам. Есть идеи, что не так с кодом?