Artificial Intelligence · Genetic Algorithms
Encoding and Fitness Functions
Overview
A genetic algorithm searches by maintaining a population of encoded candidate solutions and repeatedly selecting, recombining and mutating them.
Encoding is the mapping from a problem solution to a chromosome; fitness function measures how good a chromosome is for selection.
These two ideas decide whether the search is valid, efficient and able to distinguish good from bad solutions. You need basic binary arithmetic, probability, and the standard genetic algorithm cycle before studying this lesson.
Key concepts
- A chromosome is the encoded representation of a candidate solution. A gene is one position in the chromosome, and an allele is the value at that gene. The position is the locus.
- Genotype is the encoded chromosome; phenotype is the actual solution after decoding. Encoding maps phenotype to genotype; decoding maps genotype back to phenotype.
- A standard binary encoding uses a bit string. An -bit string represents an unsigned integer from to . Continuous variables are handled by linear mapping.
- A real-valued or value encoding uses real numbers, characters or arbitrary objects as genes. It is natural for continuous optimisation but requires suitable crossover and mutation operators.
- A permutation encoding represents an ordered sequence in which each element appears once, such as a route or job order. Ordinary single-point crossover can produce invalid permutations; use order-based or cycle crossover.
- A tree encoding represents tree-shaped solutions such as expression trees. Crossover exchanges subtrees.
- Combinations is not a standard chromosome encoding in the usual textbook list. Subset selection is normally encoded by a binary mask or by a permutation/value scheme, not by a representation called “combinations”.
- Gray coding is a binary-like encoding in which consecutive integers differ in exactly one bit. It reduces Hamming cliffs of standard binary coding. Example: 7 in standard binary is 0111 and 8 is 1000, differing in four bits.
- A fitness function assigns a non-negative real value to a chromosome; higher fitness is better. For roulette-wheel selection, all fitness values must be positive, otherwise selection probabilities are invalid.
- The objective function gives the problem value to maximise or minimise. The fitness function may be the objective itself for maximisation, or a transformed objective for minimisation.
- Fitness-proportionate selection, also called roulette-wheel selection, selects an individual with probability proportional to its fitness.
- Fitness scaling changes raw fitness before selection. Linear scaling, sigma truncation, rank selection and tournament selection are used when raw fitness causes too much or too little selection pressure.
- A genetic algorithm contains evolution as the overall process, and specifically uses selection, reproduction through crossover, and mutation. All four ideas are part of the standard genetic algorithm framework; evolution is the broad metaphor, while selection, reproduction and mutation are its operators.
How it works
- Identify the phenotype: what a candidate solution looks like, and what constraints it must satisfy.
- Choose an encoding: binary for simple discrete/integer problems, value/real for continuous parameters, permutation for orderings, tree for structured programs.
- Define decoding: convert the chromosome into the phenotype. For binary integers, convert bits to decimal, then linearly map to the required interval if necessary.
- Define the objective function and convert it into a fitness function. For maximisation, use the objective if positive; for minimisation, use a decreasing transformation such as or .
- Initialise a population of encoded chromosomes.
- Evaluate every chromosome with the fitness function.
- Select parents using fitness-proportionate, tournament or rank selection.
- Apply crossover to produce offspring.
- Apply mutation to maintain diversity and avoid premature convergence.
- Repair or reject invalid chromosomes, especially in permutation or constrained encodings.
- Repeat evaluation and selection until a stopping condition is met.
Drawing the diagram…
Formulas
- Binary decoding of an -bit unsigned chromosome: where is the bit at position counted from the right, with as the least significant bit.
- Linear mapping from an -bit unsigned integer to the real interval :
- Fitness-proportionate selection probability of chromosome : where is population size and is fitness. The expected number of copies is .
- Valid minimisation fitness transformation for non-negative cost : or, with a known upper bound :
- Binary-to-Gray code: Gray-to-binary uses and then .
Worked examples
Example 1: binary decoding and mapping.
A 4-bit chromosome is , mapped from .
Binary value: .
Answer: .
Example 2: minimisation fitness and roulette selection.
Costs are . Use , so fitness values are:
Total fitness . Probabilities:
If a random number is generated, it falls in the second interval, so the cost-6 individual is selected. This shows why the minimisation transformation must make smaller costs correspond to larger fitness.
Example 3: two-step decoding and fitness.
A 6-bit chromosome is decoded as:
Map to :
Cost .
Fitness:
Answer: .
Example 4: statement-type question.
Statement I: In Gray coding, consecutive integers differ in exactly one bit.
Statement II: Standard binary coding can show a Hamming cliff, for example 7 = 0111 and 8 = 1000 differ in four bits.
Statement III: In permutation encoding, ordinary single-point crossover always produces valid permutations.
I is true; II is true; III is false because ordinary crossover can duplicate or delete elements. Answer: I and II only.
Example 5: roulette-wheel probability.
Fitness values are , total .
Probability of the fitness-15 individual:
For a population of 4, its expected number of copies is .
Comparison
| Encoding | Form | Best for | Main trap |
|---|---|---|---|
| Binary encoding | Bit string | Discrete, integer, subset masks | Hamming cliffs; invalid mapping if range not handled |
| Value or real-valued encoding | Real numbers, characters, objects | Continuous parameters | Needs suitable crossover and mutation |
| Permutation encoding | Order of unique elements | Routes, schedules, job sequences | Ordinary crossover/mutation can break validity |
| Tree encoding | Tree or expression structure | Programs, parse trees | Crossover must swap valid subtrees |
| Concept | Meaning | Exam reminder |
|---|---|---|
| Objective function | Problem value to maximise or minimise | May be negative or minimised |
| Fitness function | Non-negative selection measure | Roulette requires positive fitness |
| Maximisation | Higher objective is better | Fitness can equal objective if positive |
| Minimisation | Lower cost is better | Use reciprocal or upper-bound subtraction |
| Strength | This is the selection probability in fitness-proportionate selection |
| Selection method | Basis | Property |
|---|---|---|
| Roulette-wheel | Fitness proportion | Sensitive to scaling and negative values |
| Rank selection | Rank order | Avoids dominance by very high fitness |
| Tournament selection | Best among random subset | Simple and robust; not proportional to raw fitness |
Revision summary
- Encoding changes the search space; a bad encoding makes valid solutions hard to generate.
- Binary, value/real, permutation and tree are standard encodings; “combinations” is not a standard encoding name.
- For -bit unsigned binary, range is to .
- Gray code: adjacent integers differ by one bit; standard binary can have Hamming cliffs.
- Fitness must be non-negative for roulette-wheel selection; positive is safer.
- Minimisation: smaller cost must become larger fitness.
- Roulette probability is fitness divided by total fitness.
- Expected copies .
- Permutation encoding needs order-based crossover or repair.
- Fitness scaling is used when raw fitness causes premature convergence or weak selection pressure.
- Selection, reproduction/crossover and mutation are explicit genetic algorithm operators; evolution is the overall framework.
- Check validity of offspring before evaluating fitness.
Previous-year exam insights
- Direct concept questions ask which item is “not a solution representation” or “not an encoding”. Learn the standard list and do not accept invented names.
- Numeric questions combine binary conversion, linear mapping and fitness calculation.
Always decode the bit string first, then apply the stated formula. - Roulette-wheel questions ask for probability or expected copies.
Check whether the answer is requested as a fraction or a percentage. - Statement-based questions often test permutation crossover validity, Gray coding, and whether value encoding can use real numbers.
- Match-the-list questions pair encoding with suitable problem: real optimisation with value encoding, route with permutation encoding, subset with binary mask.
- Assertion-reason questions test fitness scaling: as a population converges, raw fitness values become close, so scaling may restore selection pressure.
- Code/numeric traps include negative fitness from , division by zero, and random values interpreted before normalising by total fitness.
- For minimisation, check monotonic direction first: lower cost must produce higher fitness.
Practice at PARIKSHA
You are ready for the PARIKSHA tests of this sub-topic when you can:
- name and define binary, value/real, permutation and tree encodings with one suitable use each.
- decode an -bit unsigned chromosome and map it linearly to a given interval.
- explain why “combinations” is not a standard chromosome encoding.
- convert a minimisation cost into a valid positive fitness using at least two transformations.
- compute roulette-wheel probabilities and expected copies from a list of fitness values.
- explain why non-positive fitness breaks fitness-proportionate selection.
- distinguish binary coding from Gray coding and give a Hamming-cliff example.
- state why ordinary single-point crossover can fail in permutation encoding and name a valid alternative.
- identify when fitness scaling is needed and what problem it solves.
- solve statement-type and match-the-list questions on encoding suitability and selection.
Attempt the sub-topic test first, then move to the subject test.