Henry
Henry

Reputation: 1042

Fitness function

Whilst searching on Google about Genetic Algorithms, I came across OneMax Problem, my search showed that this is one of the very first problem that the Genetic Algorithm was applied to. However, I am not exactly sure what is OneMax problem. Can anyone explain.

Any help is appreciated

Upvotes: 1

Views: 1752

Answers (1)

Alexander Weber
Alexander Weber

Reputation: 799

The goal of One-Max problem is to create a binary string of length n where every single gene contains a 1. The fitness function is very simple, you just iterate through your binary string counting all ones. This is what the sum represents in the formula you provided with your post. It is just the number of ones in the binary string. You could also represent the fitness as a percentage, by dividing the number of ones by n * 0.01. A higher fitness would have a higher percentage. Eventually you will get a string of n ones with a fitness of 100% at some generation.

double fitness(List<int> chromosome) {
  int ones = chromosome.stream().filter(g -> g == 1).count();
  return ones / chromosome.size() * 0.01;
}

Upvotes: 2

Related Questions