Created
April 17, 2013 18:00
-
-
Save codyrioux/5406405 to your computer and use it in GitHub Desktop.
A genetic algorithm function in clojure.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns genetic-algorithm.core | |
(:use (clojure set)) | |
(:gen-class)) | |
(defn ga | |
"Main function for running the genetic algorithm. | |
This will perform the fitness -> selection -> crossover -> mutation cycle | |
until we have reached the maximum number of generations or our termination | |
condition specified by terminate? is satisfied. | |
Arguments: | |
fitness : A function that can take an individual and return a fitness score for that individual. | |
selection : A fn that takes a population (with fitness metadata) and performs selection returning a new population. | |
crossover : A fn that takes a population and performs crossover, returning a new population. | |
mutation : A fn which takes a population and mutation rate as a parameter and returns a mutated population. | |
terminate? : A function that takes the population (with fitness metadata) and determines if the ga should terminate. | |
intial-population : A collection representing the initial population. | |
tmax : The maximum number of generations for which to run the algorithm. | |
mr : The mutation rate. P(mutation) | |
Returns: A tuple [generation, population] | |
The population at the time of termination has fitness metadata. " | |
[fitness selection crossover mutation terminate? initial-population tmax mr] | |
(loop [t 1, pt initial-population] | |
(let [pt (map #(with-meta %1 {:fitness (fitness %1)}) pt)] | |
(println (str "Generation " t " [" (count pt) "]")) | |
(cond | |
(or (> t tmax) (terminate? pt)) [t pt] | |
:else (recur | |
(+ t 1) | |
(->> | |
pt | |
(selection) | |
(crossover) | |
(map #( mutation mr %1)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment