Unconstrained optimisation of discrete functions