Optimising with Simulated Annealing
Tasked with optimising a complicated system, I went down the route of heuristic optimisation and ended up creating a simulated annealing algorithm in R.
I don't want to go into too much detail about how the model works, because that has been done well enough elsewhere, but essentially it is a smart way of searching through all the possible solutions to a problem to find the best one, while avoiding getting stuck in local minima. Taking inspiration from nature, it's based on the physical cooling of metals from a high temperature; when this is done very fast (e.g. by dropping the metal into water), the metal forms small grains (crystals), and still has a lot of energy trapped in the crystal structure. If this is done very slowly however, large grains form (more of molecules are aligned), and so the metal reaches a much lower (more optimal) energy state.
While it isn't guaranteed to find the absolute optimum solution, it will get you to a good solution fast, which is often what you really want.

Click permalink below to see the code!
