Metaheuristics
I dug up my old metaheuristics code. This is from back when I had a gmail account, 2014. I had stumbled on a textbook of metaheuristics while I was trying to learn Lisp. The algorithms looked straight forward so I implemented some:
- Gradient Descent (Not really a metaheuristic.)
- Hill Climbing
- Steepest Ascent Hill Climbing
- Hill Climbing with Restarts
- Simulated Annealing
- Tabu Search
(defun steepest-ascent-hill-climb (guess copy tweak quality &optional (tweak-number 10) (time-left 1000))
(if (plusp time-left)
(let ((time-at-entry (get-internal-real-time))
(best-tweaked-copy (reduce (alexandria:curry #'return-greater-quality quality)
(mapcar (alexandria:compose tweak copy)
(make-list tweak-number :initial-element guess)))))
(steepest-ascent-hill-climb (return-greater-quality quality best-tweaked-copy guess)
copy
tweak
quality
tweak-number
(- time-left
(- (get-internal-real-time) time-at-entry))))
Unfortunately, the code is bad.
- All the arguments are positional; I should have used keyword arguments.
- The code uses direct recursion; I should have used high order functions or
loop.