Payoff curves

Simulated quarters

◆ The Blueprint
The model

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:

$$f_i(h) = a_i\big(1 - e^{-b_i h}\big) - c_i\,e^{-b_i h}, \qquad S_{ij} = s_{ij}\,\frac{h_i}{h_i + 20}\,\frac{h_j}{h_j + 20}$$
$$y = \sum_i \big(f_i(h_i) + \varepsilon_i\big) + \sum_{i

\(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

The five events
Event\(a\)\(b\)\(\sigma\)\(c\)
Marksmanship280.0633
Land Nav250.0442
Communications220.1021.5
Tactics300.0454
First Aid200.1522

\(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.

Why a genetic algorithm

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.

The recipe

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.

In R

simulate_quarter(hours) draws one quarter and expected_score(hours) gives its mean. Part 1 of The Script defines both.

Related

The Bias-Variance Tradeoff app covers noise and estimation error.

Chromosome

Hours by event

◆ The Blueprint
Decoding
$$h_i = \begin{cases} B\,\dfrac{w_i}{\sum_{j:\,s_j = 1} w_j} & s_i = 1 \\[4pt] 0 & s_i = 0 \end{cases}$$

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.

Why two kinds of genes

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.

Fitness and expected fitness

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.

In R

random_chromosome(), decode(chromosome, budget), and expected_score(hours). Part 2 of The Script uses all three.

Population fitness

Chance of selection

◆ The Blueprint
The tournament's chances

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:

$$P(\text{rank } j \text{ wins}) = \frac{\text{choose}(j - 1,\ k - 1)}{\text{choose}(n,\ k)}$$

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 and its shift

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 under noise

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.

In R

tournament(fitness, k), roulette(fitness), and rank_select(fitness). Part 3 of The Script draws parents with each.

Parents and child

Hours by event

◆ The Blueprint
The three methods

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.

Repair

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.

In a genetic algorithm run

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.

In R

crossover(p1, p2, method) and repair(chromosome). Part 4 of The Script builds and repairs a child.

Before and after

Weight changes

◆ The Blueprint
Gene by gene

Each selection gene flips with probability \(p\). Unpaired, each weight gene with probability \(p\) becomes

$$w' = \operatorname{round}\big(\min(\max(w + z,\ 0),\ 100)\big), \qquad z \sim N(0, \sigma^2)$$

R's round() sends a value ending in exactly .5 to the even neighbor. Repair then restores three or four chosen events.

Paired transfers

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.

Why paired helps

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.

In a genetic algorithm run

A genetic algorithm run mutates every child with paired transfers and \(\sigma = 15\), at the mutation rate (0.2 by default).

In R

mutate(chromosome, rate, sigma, paired). Part 5 of The Script mutates a chromosome.

Fitness by generation

Diversity by generation

Best allocation and the true optimum

◆ The Blueprint
The guided run's settings

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.

Diversity

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.

Why the best score overstates

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,

$$\mu + \sigma_F\sqrt{2 \ln N}, \qquad \sigma_F = \frac{s}{\sqrt{n}}$$

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.

Overall best and generation best

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.

In R

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().

Related

The Bias-Variance Tradeoff app covers noise and estimation error.

Overall best by generation

Results
◆ The Blueprint
The runs

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.

The cost of a run

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:

$$\text{quarters} = (G + 1) \times n \times n_q$$
Fair comparisons

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.

Expected fitness in practice

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.

In R

Part 8 of The Script runs the quarters-per-evaluation comparison; change one argument of run_ga() for the others.

The GA package, function by function
CallWhat it does
ga(type = "real-valued", fitness, lower, upper)Maximizes fitness over genes between lower and upper. Also type = "binary" and "permutation".
popSize, maxiter, runPopulation size, the number of generations, and an early stop after run generations without a better fitness.
pcrossover, pmutation, elitismThe crossover rate, the mutation rate, and the number of elites.
selection = gareal_tourSelectionTournament selection with \(k = 3\). gareal_lrSelection selects by rank and gareal_rwSelection by roulette.
seed = 1Calls set.seed(1) before the run.
summary(fit), plot(fit), fit@solutionThe settings and the best fitness; the best, mean, and median fitness by generation; the best genes.
ga_in_R.R