Reading and visualising an optimisation

The optimisation tutorial runs a minimal two-objective search. Here we run a slightly larger three-objective optimisation and focus on the tools for making sense of the result: the several views of the Pareto front, the per-generation history, a convergence trace, and reading the result tables programmatically.

The three objectives — minimise peak surface electric field, minimise peak surface magnetic field, and maximise R/Q — genuinely conflict, so there is a real trade-off surface to explore. See the optimisation guide.

import os
import tempfile

import matplotlib.pyplot as plt

from cavsim2d import Study, EllipticalCavity
from cavsim2d.utils.style import apply_style

apply_style()

bounds are the dimensions the search varies; Req is tuned separately (via tune_config) to keep every candidate at 1300 MHz, so it is not a bound. The bounds keep every candidate a valid cell: A + a stays below the half-length L, and B + b below the room between the iris and the equator. The search is kept small, a handful of candidates over two generations, so it runs in a few minutes.

cavs = Study(os.path.join(tempfile.mkdtemp(), 'opt3'))
mid = [42, 42, 12, 19, 35, 57.7, 103.353]
cavs.add_cavity([EllipticalCavity(1, mid, mid, mid, beampipe='none')], ['TESLA'])

config = {
    'initial_points': 6, 'no_of_generation': 2, 'method': {'LHS': {'seed': 5}},
    'bounds': {'A': [38, 44], 'B': [38, 46], 'a': [10, 13], 'b': [17, 21]},
    'objectives': [['min', 'monopole:Epk/Eacc []'],
                   ['min', 'monopole:Bpk/Eacc [mT/MV/m]'],
                   ['max', 'monopole:R/Q [Ohm]']],
    'tune_config': {'freqs': 1300.0, 'cell_type': {'mid-cell': 'Req'}},
    'mutation_factor': 4, 'crossover_factor': 4, 'elites_for_crossover': 2,
    'chaos_factor': 2,
}
cavs.run_optimisation(config)
opt = cavs.optimisation
print(f'evaluated candidates: {len(opt.history)}')
print(f'Pareto-front size:    {len(opt.pareto)}')
print(f'objectives:           {opt.objective_vars}')
  0%|          | 0/2 [00:00<?, ?it/s]
 50%|█████     | 1/2 [00:28<00:28, 28.09s/it]
100%|██████████| 2/2 [00:57<00:00, 28.64s/it]
100%|██████████| 2/2 [00:57<00:00, 28.56s/it]
evaluated candidates: 16
Pareto-front size:    6
objectives:           ['monopole:Epk/Eacc []', 'monopole:Bpk/Eacc [mT/MV/m]', 'monopole:R/Q [Ohm]']

2. The Pareto front, several ways

With three objectives a single scatter plot cannot show every trade-off at once, so the optimiser offers complementary views (kind=). The scatter matrix shows every pair of objectives:

opt.plot_pareto(kind='scatter', normalise=False);
../../_images/2c66bc6275c6b01d0e34f2b73494a6e2582265ed776d185615370c2462f795e5.png

Parallel coordinates (pcp) draws each candidate as a line across all three objective axes — crossing lines are the sign of a trade-off:

opt.plot_pareto(kind='pcp');
../../_images/2693c68929979119e99fc7f75a5d3df017d8f9ec752d380f579bea9568a03b17.png

A radar plot gives each Pareto candidate its own polygon over the (normalised) objectives — a compact fingerprint of where each design is strong:

opt.plot_pareto(kind='radar');
../../_images/c7356349d5984c1608a4dcce11abb72fcf710c58de3cac1effe90e8026ec398b.png

3. How the search progressed

plot_history colours every evaluated candidate by the generation it was born in, so you can watch the population march toward the front:

opt.plot_history(kind='scatter', color_by_gen=True, normalise=False);
../../_images/fd12444e941d91a00e088eee912a55386bd01b5aff0adb8fa529976d52978e44.png

plot_pareto_history overlays the non-dominated front from each generation:

opt.plot_pareto_history();
../../_images/30f81c37f36d119bfc55571bcf3688d56812afbb66eadfcb9f0895c79795f4f0.png

and plot_convergence tracks a scalar fitness against generation:

opt.plot_convergence();
../../_images/634718f262b7c3dd5c6a683b9e9f44e963aca20618b60303b0148fb9de16c6f4.png

4. Reading the results

Everything is a DataFrame. opt.pareto holds the non-dominated designs — their geometry variables and objective values — ready to sort and pick from.

obj = opt.objective_vars
cols = ['A', 'B', 'a', 'b'] + obj
opt.pareto[cols].round(3)
A B a b monopole:Epk/Eacc [] monopole:Bpk/Eacc [mT/MV/m] monopole:R/Q [Ohm]
0 43.533 41.385 12.261 17.302 2.022 4.089 115.583
1 42.690 38.991 11.803 18.363 2.013 4.103 114.483
2 43.561 44.949 12.351 17.354 2.009 4.125 115.094
3 39.500 44.000 12.750 18.667 1.915 4.316 111.960
4 39.500 40.000 12.250 20.000 1.987 4.268 111.894
5 39.093 43.815 12.874 18.431 1.910 4.334 111.843

For example, the lowest-peak-field design on the front:

best = opt.pareto.sort_values('monopole:Epk/Eacc []').iloc[0]
print(best[cols].round(3).to_string())
A                               39.093
B                               43.815
a                               12.874
b                               18.431
monopole:Epk/Eacc []             1.910
monopole:Bpk/Eacc [mT/MV/m]      4.334
monopole:R/Q [Ohm]             111.843