Genetic Algorithms in Elixir: N-Queens Ch4, pp89

Implementing the fitness function.

however they can still be in the same row

The genotype function generates an Elixir list of the columns that each queen is in (Enum.shuffle(0…7). A list 8 elements long consisting of all the digits 0-7 in some random order.
Each element of the list represents the row for the queens column. As each element index must be unique (0, 1, 2, …,7) otherwise it would be a very strange list, each row can only contain one column.

Therefore there is no need to check that the rows only contain one queen.

1 Like