Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Representation and Fitness

Designing Fitness Functions

Learn how to design fitness functions that guide a genetic algorithm: minimization versus maximization, scaling, noise, shaping partial credit, and avoiding loopholes.

The fitness function is the only thing in a genetic algorithm that knows what you actually want. Selection, crossover and mutation are blind machinery, so whatever the fitness function rewards, the GA will find, including ways of scoring well that you never intended. Designing it well is often the difference between a GA that quietly solves your problem and one that produces confident nonsense.

This lesson covers the practical rules: turning costs into fitness, giving the search a gradient to climb, handling noise and expense, and spotting loopholes. Later lessons add fitness scaling inside selection and penalties for constraints.

What a good fitness function does

A good fitness function is:

  • Correct: higher fitness means a genuinely better solution for the real goal.
  • Informative: it tells apart nearly good and very bad solutions, not just "solved" or "not solved".
  • Cheap enough: you will call it thousands to millions of times.
  • Deterministic or well-behaved: the same chromosome should get (about) the same score.
  • Total: it returns a number for every chromosome, even invalid ones.

Minimizing and maximizing

Problems are stated as "minimize cost" or "maximize profit". Roulette-style selection needs non-negative numbers where bigger is better, so convert:

Goal Conversion Notes
Minimize cost c >= 0 fitness = 1 / (1 + c) Always in (0, 1]; 1 means cost 0
Minimize cost, any sign fitness = -c Fine for tournament selection; shift for roulette
Maximize profit p fitness = p Shift by a constant if p can be negative
Minimize error with a known bound C_max fitness = C_max - c Linear and keeps the gaps between solutions

Tournament and rank selection only compare, so they work with any monotone fitness, including negative values. Roulette wheel does not. Pick the conversion your selection method needs.

Python Converting cost to fitness

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 choice changes how strongly selection prefers better individuals. The reciprocal compresses the high-cost region (cost 9 versus cost 99 only 10 times), whereas the linear conversion makes cost 9 91 times more attractive.

Give the search something to climb

The most common design mistake is an all-or-nothing fitness: 1 if the solution is perfect, 0 otherwise. For a random population almost everyone scores 0, selection has nothing to prefer, and evolution becomes random search. You need a fitness that gives partial credit.

Compare two fitness functions for guessing a 10-character secret code:

Python Needle in a haystack versus partial credit

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.

With the all-or-nothing score nothing guides the search, so all five runs fail; with partial credit every run finds the secret, in under 80 generations. Partial credit is a form of fitness shaping: you encode your knowledge of what progress looks like.

Ask: "if I handed a human two solutions, one almost right and one hopeless, would my function score them differently?" If not, the GA cannot tell them apart either.

Noise and cost

Noisy fitness. If scoring uses randomness (a simulation with random events, a game against random opponents), the same chromosome can score differently each time. Remedies: average several evaluations, use fixed random seeds per generation, or compare with larger tournaments to reduce the effect of one lucky score.

Expensive fitness. If one evaluation takes seconds or minutes, you can:

  • cache scores of repeated chromosomes (functools.lru_cache or a dictionary keyed by the genes),
  • evaluate in parallel,
  • use a cheap surrogate model (a regression model trained on past evaluations) to pre-screen candidates,
  • stop evaluations early when a candidate is clearly poor.
Python Caching repeated evaluations

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.

Loopholes and reward hacking

An evolutionary search is an excellent loophole finder. Famous stories include simulated creatures that grew very tall and fell over to "travel far", and scheduling systems that scored perfectly by leaving the hardest tasks unassigned. If your fitness measures a proxy for what you want, the GA optimizes the proxy.

Protect yourself by:

  1. Testing the fitness function on solutions you construct by hand, good and bad.
  2. Looking at the actual best individuals, not just the numbers.
  3. Adding terms for hidden requirements (every task must be assigned, the robot must stay upright).
  4. Evaluating the final solution with an independent measure.

Combining several goals

Real problems often have several aims (cost, quality, time). The simplest approach is a weighted sum:

fitness = w1 * quality - w2 * cost - w3 * time

It is easy, but the weights hide trade-offs and are hard to choose. Chapter 5 shows how to keep the objectives separate and find a whole set of best compromises instead.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is an all-or-nothing fitness function (1 if perfect, else 0) a poor choice?

    When nearly everyone scores the same, selection cannot prefer better candidates, and the search degrades to random guessing.
  2. 2. Which fitness conversion lets roulette selection handle a cost c that is never negative?

    1 / (1 + c) is positive and larger for lower cost, which is what roulette selection needs.
  3. 3. How can you reduce the cost of an expensive fitness function late in a run?

    Converged populations contain many copies of the same chromosome, so caching avoids repeated expensive evaluations.
  4. 4. A GA finds a solution with a great score that clearly misses the real goal. What is the most likely cause?

    A GA exploits whatever the fitness function actually measures, so a flawed or incomplete proxy produces unintended solutions.

Summary

  • The fitness function defines the goal; the GA will optimize whatever it measures, loopholes included.
  • Convert costs to fitness with 1/(1+c), -c or C_max - c depending on the selection method.
  • Provide partial credit so the search has a gradient to follow.
  • Handle noise by averaging, and expensive evaluations by caching, parallelism and surrogates.
  • Inspect real best solutions and test the function by hand; combine objectives carefully.

Next: Population Initialization and Diversity