Optimisation

An optimisation searches a range of cavity shapes for the best ones. You give the dimensions to vary and their ranges, and the figures of merit to make large or small. Figures of merit usually pull against each other (a lower peak field, for example, costs R/Q), so there is no single best shape. The search returns the Pareto front instead: the shapes that no other shape beats in every objective at once. It uses a genetic algorithm, which breeds new candidates from the best so far, and tunes every candidate to the target frequency before it scores it.

Shape optimisation teaches the basics; this page is the full reference.

Running it

An optimisation starts from a cavity in a study, the template. Each candidate is the template with the dimensions in bounds changed; the dimensions you leave out keep the template’s values.

study = Study('my_optimisation')
study.add_cavity(EllipticalCavity(1, mid_cell, beampipe='none'), names='template')

study.run_optimisation({
    'bounds': {'A': [40, 44], 'B': [40, 46], 'a': [10, 13], 'b': [17, 21]},
    'objectives': [['min', 'monopole:Epk/Eacc []'],
                   ['max', 'monopole:R/Q [Ohm]']],
    'tune_config': {'freqs': 1300, 'cell_type': {'mid-cell': 'Req'}},
    'initial_points': 10,
    'no_of_generation': 3,
})

Any cavity can be the template, a spline or an RF gun as well as an elliptical cell, and bounds takes its own dimension names: 'p2_z' for a spline control point, for example, or 'L3' for a gun (in metres).

Settings

study.optimisation.config_sample returns every setting with its default; Optimisation — study.run_optimisation describes them.

bounds

(dict) The range, [lower, upper], of each dimension to vary, in the template’s units (mm, except for an RF gun). Equal bounds hold a dimension fixed.

objectives

(list) One [direction, name] per objective. The direction is 'min' or 'max'. The name is a figure of merit with its polarisation in front, as 'monopole:Epk/Eacc []' or 'dipole:R/Q [Ohm]'; see Objective names. Two wakefield objectives are also available: ['min', 'ZL', [1, 2, 5]] and ['min', 'ZT', [1, 2, 3, 5]] minimise the peak longitudinal or transverse impedance in each frequency interval, in GHz, between the listed values.

tune_config

(dict) How each candidate is tuned before it is scored; see Tuning. A figure of merit that must have an exact value, such as R/Q = 157 Ohm, belongs in its qoi_targets, not in objectives: an objective is only traded off against the others, never met exactly.

initial_points

(int, default 5) How many candidates to score before the first generation.

method

(dict, default {'LHS': {'seed': 5}} ) How the first candidates are spread over the bounds. Latin hypercube sampling (LHS) covers the range evenly; the seed makes the run repeatable.

no_of_generation

(int, default 100) The largest number of generations. The search stops earlier once the front stops improving; see When the search stops.

crossover_factor, mutation_factor, chaos_factor

(int, default 5 each) How many new candidates each generation makes by crossing two good candidates, by changing one at random, and from scratch.

elites_for_crossover

(int, default 2) How many of the best candidates breed by crossover.

Two-cell candidates

A two-cell cavity tunes to a family of designs rather than one (see Tuning), and every member of the family is scored on its own, so one candidate adds several rows to the history. tune_config['equal_cell_freq'] = True scores only the design with every cell at the target frequency, and tune_config['n_family'] changes how many members are tried.

When the search stops

After each generation the search compares the new front with the last one. When the front has improved by less than eps_tol (default 1e-4, as a fraction of each objective’s range) for eps_consecutive generations in a row (default 3), the search stops. archive_size (default 100) caps how many of the best designs are kept from one generation to the next.

Reading the results

The results are saved in the study’s optimisation folder and read as pandas DataFrames:

opt = study.optimisation
opt.pareto           # the designs on the Pareto front
opt.history          # every candidate scored, with its generation
opt.pareto_history   # the front after each generation
opt.objective_vars   # the names of the objective columns

Each row holds a candidate’s dimensions and its objectives, so the front can be sorted directly, for example opt.pareto.sort_values('monopole:Epk/Eacc []').

The plots:

opt.plot_pareto()                    # the front, each objective against each other
opt.plot_pareto(kind='pcp')          # parallel coordinates: one line per design
opt.plot_pareto(kind='radar')        # one polygon per design
opt.plot_pareto(kind='heatmap')      # one row per design
opt.plot_history(color_by_gen=True)  # every candidate, coloured by generation
opt.plot_pareto_history()            # the front after each generation
opt.plot_convergence()               # how much each generation improved the front

normalise=True (the default) scales each objective to the range 0 to 1, so objectives of different sizes share one axis.

How-to guides

Spline cavities optimises a spline cavity, and Optimising a cavity inside a beam line a cavity with its neighbours in a beam line. An optimisation that ranks candidates by their spread under manufacturing tolerances is in Uncertainty quantification.