Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Representation and Fitness

Real-Valued Encoding for Genetic Algorithms

Encode continuous parameters directly as floating-point genes, handle bounds and mixed gene types, and optimize classic test functions with a real-coded GA in Python.

When the thing you are optimizing is a list of real numbers (the weights of a model, the dimensions of a beam, the gains of a controller) turning each number into 30 bits is awkward. The chromosome gets long, precision is limited by the bit count, and a flipped high bit throws a value across the whole range. Real-valued (floating-point) encoding skips all that: the chromosome is the vector of numbers.

A real-coded GA keeps the same loop as before but needs new operators. Mutation adds a small random number to a gene instead of flipping it, and crossover blends numbers instead of swapping bits. This lesson shows the representation, the bound handling and mutation. Real crossover has its own lesson.

Genes are numbers

For a problem with d variables, a chromosome is a list of d floats with a lower and upper bound for each. The GA never decodes anything: fitness is computed directly from the list.

Python
chromosome = [1.2, -0.7, 3.14]
bounds = [(-5, 5), (-5, 5), (0, 10)]

Benefits:

  • No precision limit apart from floating-point itself.
  • Short chromosomes: d genes, not d * L bits.
  • Natural neighbours: a small mutation makes a small change.
  • Mixed ranges: each gene has its own bounds, so a gene can be a tiny coefficient while another is a large dimension.

Creating the population

Initialize each gene uniformly at random inside its bounds. The test function below is the Rastrigin function, a classic benchmark with a grid of local minima and a global minimum of 0 at the origin. It is a minimization problem, so we flip the sign to get a fitness that is bigger when better.

Python Random population on the Rastrigin function

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Mutating real genes

The standard real mutation is Gaussian mutation: add a number drawn from a bell curve with mean 0 and standard deviation sigma to a gene. Small sigma means fine adjustments; large sigma means exploration. Because values can leave the allowed range, clip them back (or reflect them at the bound).

A second option is uniform mutation (replace the gene with a fresh random value in its range) which gives rare long jumps. Most implementations use Gaussian mutation with a gene-wise probability, and sometimes occasionally uniform mutation to escape traps.

Python A real-coded GA with Gaussian mutation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The step size sigma matters a great deal. With a tiny sigma the population cannot leave the local pit it starts in, so costs stay at whole steps of about 1, 2, 3 or 4 (each wrong pit adds roughly 1). A step size comparable to the spacing of the pits (here sigma = 1.0) lets mutation hop between pits and finds costs near 0. If sigma is far too large, say 5 on this range, mutation becomes random search again. Later, in parameter control and adaptation, you will see how to change sigma automatically.

Integers, mixed genes and categories

Real encoding extends naturally:

  • Integer genes: store ints and mutate by adding a small integer step (or sample a fresh one). Rounding a real gene in the decoder also works.
  • Categorical genes (for example "relu", "tanh", "sigmoid"): store an index or the label and mutate by picking another category.
  • Mixed chromosomes: a list where each position has its own type and range, with a per-gene mutation function.
Python A mixed chromosome with per-gene rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This pattern, a spec list plus per-gene operators, is the basis of hyperparameter search with GAs, which you meet again in Applications and Tuning.

Pitfalls

  • Scale differences. If one gene ranges over [0, 0.001] and another over [0, 1000], a single global sigma is wrong for one of them. Scale sigma to each gene's range or normalize all genes to [0, 1] and map them in the decoder.
  • Boundary solutions. Clipping piles individuals at the edge. If the optimum is at a bound, that is fine, otherwise reflect or resample.
  • Premature collapse. Real genes can converge to nearly identical values quickly. Watch diversity (see population initialization and diversity).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of real-valued encoding for continuous problems?

    Genes are the numbers themselves, so there is no bit-length precision limit and the chromosome has one gene per variable.
  2. 2. What does Gaussian mutation do to a gene?

    It adds noise with mean zero and standard deviation sigma, producing small changes most of the time.
  3. 3. Why must you clip or reflect mutated real genes?

    Adding noise can push a value outside its valid range, which could make the decoded solution invalid.
  4. 4. One gene ranges over [0, 0.001] and another over [0, 1000]. What is the sound fix for mutation step size?

    A single step size is huge for the small gene and negligible for the large one. Scaling per gene keeps changes meaningful.

Summary

  • Real-valued encoding stores continuous parameters directly as floats with per-gene bounds.
  • Initialize uniformly within bounds; mutate with clipped Gaussian noise.
  • sigma controls exploration: too small stalls, too large overshoots.
  • Mixed chromosomes use per-gene types (float, int, category) with matching operators.
  • Normalize or scale genes so one step size makes sense for all of them.

Next: Permutation Encoding