Watch one crossover, then check the answer exactly
1 means include that asset. We require exactly two assets, a cardinality constraint. The stipulated one-period scores are 4, 3, 2 and 1 points. These are teaching scores, not estimated returns or portfolio weights; there is no risk, correlation or transaction-cost model here.For , let the fitness be
The last term penalises the wrong number of selected assets. The coefficient 100 is stipulated for this tiny example: it makes every infeasible chromosome worse than every feasible one. It is not a generally suitable penalty for other problems.
1010 and 0101. Their fitness scores are and . Cut each string after the second bit and exchange the tails:| Candidate | Before recombination | After recombination | Fitness after |
|---|---|---|---|
| First | 10 followed by 10 | 10 followed by 01: 1001 | 5 |
| Second | 01 followed by 01 | 01 followed by 10: 0110 | 5 |
1100, with score 7. In fact, this additive toy needs no genetic algorithm: simply choose the two largest scores. Its purpose is to make the operators auditable. More complicated interactions and constraints can remove that simple solution, but complexity should be established from the actual objective and feasible set.The constraint must survive the operators
1001, flip its second bit. The mutation produces 1101: three assets, raw score 8, but fitness . More selected assets raised the raw score while violating the problem.1001 to 1100 while keeping exactly two assets. This illustrates a possible move, not a claim about how frequently a search will find it. A repair rule also changes which solutions the search tends to visit, so it belongs in the algorithm specification.Ordinary one-point crossover can itself violate cardinality for other parent pairs. Feasible parents alone do not make an operator feasible.
What makes it a search algorithm
One generation combines several decisions:
- Evaluate the population under the declared fitness and constraint treatment.
- Select parents with a bias toward better fitness. Tournament selection, for example, compares sampled candidates; fitness-proportional selection instead samples using nonnegative selection weights.
- Apply the chosen crossover and mutation operators, then evaluate the resulting candidates.
- Choose survivors and repeat until an evaluation budget or another declared stopping rule is reached.
Selection that repeatedly reproduces similar candidates can leave too little diversity to explore alternatives. Mutation offers additional variation, but its presence does not guarantee that a finite run escapes a poor region. Report the encoding, operators, population size, stopping budget and random seeds; compare runs against an exact benchmark where available or against simpler searches under a comparable evaluation budget.
For a trading-rule fitness based on historical performance, search is also repeated model selection. Keep backtest overfitting separate from numerical optimisation: improving the search score can mean fitting historical noise more effectively. More generations do not replace evaluation on data kept outside the search.
Sources
- Cornell Computational Optimization Open Textbook: Genetic algorithm, Algorithm Discussion—representation, evaluation, genetic operators and stopping rules.
- José M. Vidal: Genetic Algorithms, sections 2–2.3—binary representations, crossover, mutation and alternative selection rules.
- Marek Obitko: Genetic Algorithm, Basic Description and Some Comments, hosted by the University of Bologna—generation/replacement cycle and elitism.