  We present an optimisation method for the assignment of photometric galaxies
into a chosen set of redshift bins. This is achieved by combining simulated
annealing, an optimisation algorithm inspired by solid-state physics, with an
unsupervised machine learning method, a self-organising map (SOM) of the
observed colours of galaxies. Starting with a sample of galaxies that is
divided into redshift bins based on a photometric redshift point estimate, the
simulated annealing algorithm repeatedly reassigns SOM-selected subsamples of
galaxies, which are close in colour, to alternative redshift bins. We optimise
the clustering cross-correlation signal between photometric galaxies and a
reference sample of galaxies with well-calibrated redshifts. Depending on the
effect on the clustering signal, the reassignment is either accepted or
rejected. By dynamically increasing the resolution of the SOM, the algorithm
eventually converges to a solution that minimises the number of mismatched
galaxies in each tomographic redshift bin and thus improves the compactness of
their corresponding redshift distribution. This method is demonstrated on the
synthetic LSST cosmoDC2 catalogue. We find a significant decrease in the
fraction of catastrophic outliers in the redshift distribution in all
tomographic bins, most notably in the highest redshift bin with a decrease in
the outlier fraction from 57 per cent to 16 per cent.
