Imported from https://github.com/mataney/APES-optimizer