avatarThe Pragmatic Programmers

Summary

This article discusses initializing a population for a genetic algorithm in Elixir.

Abstract

The article focuses on the One-Max problem, which requires the maximum sum of bitstrings of length N. The population consists of N-length bitstrings, with the number of chromosomes being irrelevant to the problem. A larger population means a larger area of the search space, while a smaller population may lead to premature convergence. The article suggests a population size of 100 for this example problem. The population is defined as a nested list comprehension, with Enum.random/1 used to select a random value from an enumerable. The 0..1 syntax is used to produce variation in the population of 1000-length bitstrings.

Opinions

  • The population size should strike a balance between the size of the search space and the time it takes to produce a viable solution.
  • Starting with a random distribution of bitstrings introduces solutions with sufficiently different characteristics and helps avoid premature convergence.
  • The article suggests using a population size of 100 for this example problem.
  • The article does not provide a definitive answer on the optimal population size for genetic algorithms in general.

Initializing the Population

Genetic Algorithms in Elixir — by Sean Moriarity (12 / 101)

👈 Introducing the One-Max Problem | TOC | Understanding the Flow of Genetic Algorithms 👉

You need to start by initializing a population. Remember, the problem wants the maximum sum of bitstrings of length N. So your population will consist of some number of N-length bitstrings. For this example, N is 1000, so you need a population of 1000-length bitstrings.

The number of chromosomes in your population is irrelevant. A larger population means you’re currently looking at a larger area of the search space. Typically, the more chromosomes you have, the longer it takes to perform transformations on the entire population. Conversely, the fewer chromosomes you have, the longer it takes to produce a viable solution to your problem and the more susceptible your population is to premature convergence. For this example problem, a population size of 100 strikes a nice balance between the two.

Understanding Population Size

INFORMATION

In a traditional search method like depth-first search or breadth-first search, you examine one solution at a time and determine where to go next. Even in other informed search algorithms like A* or uniform-cost search, you examine one solution at a time. Genetic algorithms examine many solutions at once, ruling out large areas of the search space after every generation. The population size dictates the size of the area of the search space you’re looking at.

At the top of the one_max.exs file, define a population consisting of 100, 1000-length bitstrings, like this:

​ population = for _ <- 1..100, ​do​: for _ <- 1..1000, ​do​: Enum.random(0..1)

This is a nested list comprehension. You create a list of size 100 consisting of lists of size 1000.

Enum.random/1 takes an enumerable and selects a random value from the enumerable. Enumerables are data structures that implement the Enumerable protocol, and that can take advantage of Elixir’s Enum library. This book makes extensive use of Enum and the Enumerable protocol. You can read more about both in the Elixir documentation.[4]

The 0..1 syntax is a generator that creates a Range from 0 to 1. You don’t need to understand what a Range is — you just need to understand that when fed into Enum.random/1, this function will select a 0 or a 1 at random. This is done to produce variation in the population of 1000-length bitstrings. Starting with a random distribution of bitstrings introduces solutions with sufficiently different characteristics. This helps to avoid premature convergence.

👈 Introducing the One-Max Problem | TOC | Understanding the Flow of Genetic Algorithms 👉

Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

Smgaelixir
Recommended from ReadMedium