Worms learn how to optimise foraging by switching their resp