mathematics//optimization//evolutionary algorithm//fitness function

A fitness function is the rule that assigns a score to each candidate solution in a search, so that an evolutionary algorithm or any black-box optimizer can rank candidates and keep the better ones. It only has to say which candidate is better; it never has to say in which direction to change one, and that is what separates it from a training loss.


A fitness function is the rule that assigns a score to each candidate solution in a search, so that an evolutionary algorithm or any black-box optimizer can rank candidates and keep the better ones. It only has to say which candidate is better; it never has to say in which direction to change one, and that is what separates it from a training loss.

For a prompt search over a benchmark of NNN examples, the plain fitness is the average score of the outputs:

F(p)=1N∑i=1Nscore⁡(p,xi)F(p)=\frac{1}{N}\sum_{i=1}^{N}\operatorname{score}(p, x_i)F(p)=N1​i=1∑N​score(p,xi​)

Here ppp is the candidate prompt and xix_ixi​ each example. When cost and speed matter too, the fitness can subtract them, F(p)=quality(p)−λ cost(p)−μ latency(p)F(p)=\text{quality}(p)-\lambda,\text{cost}(p)-\mu,\text{latency}(p)F(p)=quality(p)−λcost(p)−μlatency(p), with λ\lambdaλ and μ\muμ saying how much a cent or a second is worth against a point of accuracy.

A loss function is minimized by following its gradient, so it has to be differentiable and smooth enough to give a useful slope: cross-entropy, squared error. A fitness function may be anything computable: a count of passed tests, the time a simulated drone stays airborne, an F1 score, a human vote. In return the optimizer that uses it must try candidates and compare, which costs many more evaluations.

They are close relatives. A fitness can be the negative of a loss, and in reinforcement learning a reward plays a similar role and is turned into a training objective through policy gradients. The difference is functional: what the optimizer does with the number.

Both are a cost function with a sign and a use, and both inherit its main danger: the search optimizes exactly what is written. A fitness measured on twenty examples rewards prompts that fit those twenty; a fitness that counts passed tests rewards code that games the tests (reward hacking in another field).

Design the score before the search.

Most failed optimizations had a fine optimizer and a fitness that measured something nearby, so the first work is a held-out set, a metric tied to the real goal, and a check that the winner still wins outside the examples it was chosen on.