irlmaks
irlmaks

Reputation: 53

how to get the best solution as a column from genetic algorithm (genalg)

I am using genalg package for Genetic Algorithm. The gene is binary is nature and the result will specify

The length of the gene is 269 and it is difficult to understand which is difficult to comprehend. Is there a way to extract the result from this and have it in a data frame?

cat(summary.rbga(GAmodel))

GA Settings
Type                  = binary chromosome
Population size       = 100
Number of Generations = 100
Elitism               = TRUE
Mutation Chance       = 0

Search Domain
Var 1 = [,]
Var 0 = [,]

GA Results
Best Solution : 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 

I want to convert the Best Solution to a data frame, so that I can actually look up which are the items that I need to pick.

class(GAmodel) = "rbga"

Upvotes: 3

Views: 1895

Answers (2)

Brandon Bass
Brandon Bass

Reputation: 61

For a minimization optimization solved to variable GAModel

bestSolution<-GAmodel$population[which.min(GAmodel$evaluations),]

This pulls the index of the best chromosome from GAmodel$evaluations, based on the fitness function. Using the index, it then returns the full chromosome from the last population which was evaluated.

Upvotes: 6

Blofeld
Blofeld

Reputation: 63

Well there's certainly an issue with the output of genalg, here's how I managed to extract the best solution:

  • summar<-(summary.rbga(my_model))
  • summar<-strsplit(summar,"\n")[[1]]
  • summar<-strsplit(summar[13]," ")[[1]]
  • summar<-as.numeric(summar[-(1:5)])

Upvotes: 1

Related Questions