The confinement of heat in the core of a magnetic fusion reactor is optimised using a multidimensional optimisation algorithm. For the first time in such a study, the loss of heat due to turbulence is modelled at every stage using first-principles nonlinear simulations which accurately capture the turbulent cascade and large-scale zonal flows. The simulations utilise a novel approach, with gyrofluid treatment of the small-scale drift waves and gyrokinetic treatment of the large-scale zonal flows. A simple near-circular equilibrium with standard parameters is chosen as the initial condition. The figure of merit, fusion power per unit volume, is calculated, and then two control parameters, the elongation and triangularity of the outer flux surface, are varied, with the algorithm seeking to optimise the chosen figure of merit. A two-fold increase in the plasma power per unit volume is achieved by moving to higher elongation and strongly negative triangularity. † Email address for correspondence: highcock@chalmers.se † This is based on the observation that the global confinement of heat improves with size (EJ Doyle et al. 2007), which is believed to stem from the corresponding increase in the height of the spontaneously-formed transport barrier which forms at the edge of the plasma in typical conditions (see e.g. Maggi et al. 2007).