Event \(i\) with \(h_i\) hours has expected payoff \(f_i(h_i)\). A pair of events that both get hours adds synergy points \(S_{ij}\), largest for Land Nav with Tactics. One quarter's score adds random noise to each part:
\(O\) is the overhead: uniform from 5 to 15 points when four events run, and 0 with three. The expected score is \(\sum_i f_i(h_i) + \sum_{i \(a\) is the most points an event can add, \(b\) how fast it approaches them, \(\sigma\) the standard deviation of one quarter's result, and \(c\) the points lost at zero hours. The synergy weights \(s_{ij}\) are in part 1 of The Script. There are 15 sets of three or four events, and each set has a continuum of hour splits. The score of any plan is only seen through noisy quarters, and the synergies are not visible in the payoff curves. A genetic algorithm uses only noisy scores: it maintains a population of plans, makes the plans that score well more likely to become parents, and crosses them over and mutates them. 1. Encode each plan as a chromosome. 2. Draw a random population and score each chromosome on simulated quarters. 3. Copy the elites, the fittest chromosomes, into the next generation. 4. Select parents. 5. Cross over each pair of parents. 6. Mutate each child. 7. Score the new generation, elites included, on new quarters, and repeat from step 3 until the best score levels off. The Bias-Variance Tradeoff app covers noise and estimation error.The five events
Event \(a\) \(b\) \(\sigma\) \(c\) Marksmanship 28 0.06 3 3 Land Nav 25 0.04 4 2 Communications 22 0.10 2 1.5 Tactics 30 0.04 5 4 First Aid 20 0.15 2 2 Why a genetic algorithm
The recipe
In R
simulate_quarter(hours) draws one quarter and expected_score(hours) gives its mean. Part 1 of The Script defines both.Related
If every chosen weight is 0, each chosen event gets \(B/n\), with \(n\) the number of chosen events. Only ratios matter, so weights (10, 20, 30) and (20, 40, 60) on the same events decode to the same hours.
Which events to run is a yes or no choice, and how to split the hours is a continuous one. Keeping them in separate genes lets crossover and mutation change either kind on its own. A child that ends up choosing fewer than three or more than four events is repaired after crossover and after mutation.
The genetic algorithm (GA) scores a chromosome by the mean of \(n\) simulated quarters, \(F = \frac1n \sum_k y_k\). This fitness has the expected fitness shown here as its expected value, and its standard deviation is \(s/\sqrt n\), where \(s\) is one quarter's standard deviation. A real problem has no formula for the expected fitness; it is computed here from the model only to show how close the GA gets.
random_chromosome(), decode(chromosome, budget), and expected_score(hours). Part 2 of The Script uses all three.
Rank the \(n\) chromosomes from 1 (least fit) to \(n\) (fittest). A tournament draws \(k\) of them without replacement, and rank \(j\) wins exactly when it is drawn and the other \(k - 1\) drawn have lower ranks:
Here choose(a, b) is the number of ways to pick b of a, as R's choose() computes it. Ranks 1 to \(k - 1\) can never win. The chance depends only on rank, so under tournament selection, as under rank selection, the size of the gaps between fitness values makes no difference.
Roulette weights each chromosome by its fitness above the minimum, plus a small shift so the least fit has a chance above zero. This tab adds 0.1, and the genetic algorithm (GA) adds \(10^{-6}\) in its runs. With raw fitness instead, a population whose scores are all near 80 would give every chromosome nearly the same chance.
Elites copy into the next generation unchanged, but every chromosome, elites included, is scored again on new simulated quarters. A generation's best score can therefore fall even with elites; the overall best is the highest score seen in any generation.
tournament(fitness, k), roulette(fitness), and rank_select(fitness). Part 3 of The Script draws parents with each.
One-point crossover draws a cut \(c\) from 1 to 9 and gives the child parent 1's genes 1 to \(c\) and parent 2's genes \(c + 1\) to 10. Two-point crossover draws two different cuts \(a < b\) and gives the child parent 2's genes \(a + 1\) to \(b\) and parent 1's genes elsewhere. Uniform crossover takes each gene from parent 2 with probability 0.5 and from parent 1 otherwise.
If the child chooses more than four events, repair switches off random chosen events until four remain. If it chooses fewer than three, repair switches on random unchosen events until three are chosen. Weight genes are never changed by repair.
Each child comes from its own pair of selected parents, so a generation with \(n - e\) children selects \(2(n - e)\) parents. Crossover happens with probability 0.8 (the crossover rate); otherwise the child starts as a copy of parent 1. Every child is then mutated.
crossover(p1, p2, method) and repair(chromosome). Part 4 of The Script builds and repairs a child.
Each selection gene flips with probability \(p\). Unpaired, each weight gene with probability \(p\) becomes
R's round() sends a value ending in exactly .5 to the even neighbor. Repair then restores three or four chosen events.
The weight genes are visited in order. A gene not yet used mutates with probability \(p\): a partner is drawn at random from the other unused weight genes, one draw \(z\) is added to the gene and subtracted from the partner, and both are marked used. The five weights have the same total afterward unless a weight is clipped at 0 or 100.
Decoding divides by the chosen weights' total. An unpaired change to a chosen event's weight changes that total, so every chosen event's hours shift. A paired transfer between two chosen events does not change that total, so only those two events' hours change.
A genetic algorithm run mutates every child with paired transfers and \(\sigma = 15\), at the mutation rate (0.2 by default).
mutate(chromosome, rate, sigma, paired). Part 5 of The Script mutates a chromosome.
The guided steps run 30 chromosomes for 100 generations from seed 1, with tournament selection (\(k = 3\)), one-point crossover at rate 0.8, paired mutation at rate 0.2 with \(\sigma = 15\), 2 elites, and 30 quarters per evaluation. Each generation selects \(2(n - e)\) parents for its \(n - e\) children.
Draw 50 pairs of chromosomes at random (with replacement) and average the share of their ten genes that differ. A population of identical chromosomes has diversity 0.
Each fitness is a mean of noisy quarters, so some chromosomes score above their expected fitness by luck, and the largest score in a run is the luckiest. For \(N\) independent normal scores with mean \(\mu\) and standard deviation \(\sigma_F\), the expected largest is close to, and below,
The ceiling puts the optimum's expected fitness in for \(\mu\), the optimum's one-quarter standard deviation in for \(s\), and \(N = 200\) times the population size, the fitness values of a 200-generation run, so it is about the highest score noise could give the optimum in a run that long. The guided run has 100 generations and \(101 \times 30 = 3{,}030\) fitness values, so noise reaches the ceiling less often there. The overall best is a fitness the run reported, while the expected fitness of its allocation is that allocation's true quality.
The overall best is the highest fitness of any generation so far, so it never falls. The generation best can fall, because every chromosome is scored again each generation.
run_ga(budget = 150, generations = 100) after set.seed(1) reproduces this tab's run in part 6 of The Script, and part 7 finds the true optimum with optim().
The Bias-Variance Tradeoff app covers noise and estimation error.
Every setting uses tournament selection with \(k = 3\), one-point crossover at rate 0.8, and paired mutation with \(\sigma = 15\). Each run starts from set.seed() at the seed shown, so two settings that differ in one value still differ in every random draw after the first place they diverge.
A run of \(G\) generations scores \(n\) chromosomes in each of \(G + 1\) generations (the first is the random start), each on \(n_q\) quarters:
One run per setting is one random draw. To compare settings, run each from several seeds and compare the averages, or the share of seeds on which one setting does better. The free-play task averages seeds 1 to 10.
The model is known here, so each allocation's expected fitness is computed exactly. On a real problem, score the genetic algorithm's final allocation again on many new quarters: a fresh estimate does not include the luck that made it the best.
Part 8 of The Script runs the quarters-per-evaluation comparison; change one argument of run_ga() for the others.
| Call | What it does |
|---|---|
ga(type = "real-valued", fitness, lower, upper) | Maximizes fitness over genes between lower and upper. Also type = "binary" and "permutation". |
popSize, maxiter, run | Population size, the number of generations, and an early stop after run generations without a better fitness. |
pcrossover, pmutation, elitism | The crossover rate, the mutation rate, and the number of elites. |
selection = gareal_tourSelection | Tournament selection with \(k = 3\). gareal_lrSelection selects by rank and gareal_rwSelection by roulette. |
seed = 1 | Calls set.seed(1) before the run. |
summary(fit), plot(fit), fit@solution | The settings and the best fitness; the best, mean, and median fitness by generation; the best genes. |