Designing a Fitness Function
The fitness function is not a detail of the implementation. It is the problem statement — and getting it wrong is the most common way a genetic algorithm fails.
All It Can See
The search cannot see your problem. It sees the single number fitness returns for each candidate, and selection multiplies whatever earns a high one. That is the whole mechanism.
Reward the Outcome
Evolving a sensor's duty cycle, it is tempting to reward "keeps the radio off" — you know the radio drains the battery. You will get exactly that: radio off, battery fine, no data delivered.
Placing Sensors
- 40 candidate positions over 200 grid cells; each has a coverage set and a cost
- A candidate is a 40-bit string — bit i is 1 if position i is used
- First fitness: cells covered. The winner selects almost everything
- Nothing in that fitness mentions money, so the search never cared
Weighted Sums
Collapse the objectives into one number with weights chosen in advance — after rescaling each objective, or your units decide the weights for you.
The Pareto Front
- A dominates B when it is never worse and somewhere better
- Solutions nothing dominates form the non-dominated set
- Return a spread of compromises, then let a human choose
- A weighted sum can never reach concave parts of the trade-off surface
NSGA-II in Outline
- Non-dominated sorting — partition the population into fronts; lower rank wins
- Crowding distance — the tie-break inside a front; prefer sparse regions so the front stays spread
- Elitism by combination — pool parents and offspring, sort, truncate
Handling Constraints
- Penalty — subtract a term that grows with the violation; infeasible genes survive
- Rejection — worst possible fitness. Useless when feasible solutions are rare
- Repair — make it feasible, then score it
- Feasibility-preserving operators — an invalid child cannot be built
- Report feasibility separately from fitness
Premature Convergence
Crossing two near-copies returns the same individual, so mutation becomes the only novelty — a slow random walk. From outside it looks like a finished run.
- Best minus mean fitness collapsing early
- Count of distinct genotypes falling steeply
- Per-gene allele frequencies saturating at 0 or 1
Keeping the Population Varied
- Lower selection pressure — smaller tournaments, or rank-based selection
- Raise mutation, adaptively when a diversity measure drops
- Fitness sharing — a crowd is penalised for being a crowd (Goldberg, 1989)
- Crowding replacement — a child replaces who it most resembles
- Restarts and islands — keep an elite archive; let sub-populations diverge
Budget in Evaluations
100 individuals over 200 generations is 20,000 evaluations. At two seconds each that is over eleven hours on one core — arithmetic to do before the run, not after.
Key Takeaways
- Fitness is the problem statement — suspect it first
- Reward the outcome, never the behaviour you assume produces it
- Weighted sums need rescaling; NSGA-II returns a front instead
- Constraints: penalty, rejection, repair, or operators that cannot break them
- Watch diversity, and counter its loss before the run flatlines
- Cache, subsample, parallelise — evaluations are what cost you