Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sequence generation option by giving a generator method #65

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject nature "1.0.0"
(defproject org.clojars.btezergil/nature "1.0.0"
:description "A simple genetic algorithms library for Clojure(Script)"
:url "https://github.com/nnichols/nature"
:license {:name "Eclipse Public License"
Expand Down
44 changes: 31 additions & 13 deletions src/nature/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
[nature.population-operators :as po]
[nature.monitors :as monitors]))

(defn evolution-loop
"Evolution loop that is used commonly by sequence generator function and random generation from allele set."
[initial-population generations population-size binary-operators unary-operators options]
(let [solutions (max 1 (:solutions options))
carry-over (max 1 (:carry-over options))
monitors (:monitors options)]
(loop [population initial-population
current-generation 0]
(when monitors (monitors/apply-monitors monitors population current-generation))
(if (>= current-generation generations)
(take solutions (sort-by :fitness-score #(> %1 %2) population))
(recur (po/advance-generation population population-size binary-operators unary-operators {:carry-over carry-over}) (inc current-generation))))))

(defn evolve
"Create and evolve a population under the specified conditions until a termination criteria is reached
`allele-set` is a collection of legal genome values
Expand All @@ -13,23 +26,28 @@
`binary-operators` is a collection of partial functions accepting and returning 1 or more individuals
`unary-operators` is a collection of partial functions accepting and returning exactly 1 individual
`options` an optional map of pre-specified keywords to values that further tune the behavior of nature.
Current examples follow:
`:carry-over` an integer representing the top n individuals to be carried over between each generation. Default is 1
`:solutions` an integer representing the top n individuals to return after evolution completes. Default is 1
`:monitors` a sequence of functions, assumed to be side-effectful, to be executed against `population` and `current-genration` for run-time stats. Default is nil"
Current examples follow:
`:carry-over` an integer representing the top n individuals to be carried over between each generation. Default is 1
`:solutions` an integer representing the top n individuals to return after evolution completes. Default is 1
`:monitors` a sequence of functions, assumed to be side-effectful, to be executed against `population` and `current-genration` for run-time stats. Default is nil"
([allele-set genome-length population-size generations fitness-function binary-operators unary-operators]
(evolve allele-set genome-length population-size generations fitness-function binary-operators unary-operators {:solutions 1, :carry-over 1}))

([allele-set genome-length population-size generations fitness-function binary-operators unary-operators options] ;; TODO - Curry the genetic operators one more level, so the fitness-function can be pressed in
{:pre [(and (every? coll? [allele-set binary-operators unary-operators])
(every? int? [genome-length population-size generations])
(fn? fitness-function))]}
(let [solutions (max 1 (:solutions options))
carry-over (max 1 (:carry-over options))
monitors (:monitors options)]
(loop [population (io/build-population population-size allele-set genome-length fitness-function)
current-generation 0]
(when monitors (monitors/apply-monitors monitors population current-generation))
(if (>= current-generation generations)
(take solutions (sort-by :fitness-score #(> %1 %2) population))
(recur (po/advance-generation population population-size binary-operators unary-operators {:carry-over carry-over}) (inc current-generation)))))))
(evolution-loop (io/build-population population-size allele-set genome-length fitness-function) population-size generations binary-operators unary-operators options)))

(defn evolve-with-sequence-generator
"Same with evolve method, but takes a sequence generator function instead of an allele set and genome length.
This method uses the sequence generator function to generate sequences for the initial population."
([generator-function population-size generations fitness-function binary-operators unary-operators]
(evolve-with-sequence-generator generator-function population-size generations fitness-function binary-operators unary-operators {:solutions 1, :carry-over 1}))

([generator-function population-size generations fitness-function binary-operators unary-operators options]
{:pre [(and (every? coll? [binary-operators unary-operators])
(every? int? [population-size generations])
(every? fn? [generator-function fitness-function]))]}
(evolution-loop (io/build-population population-size generator-function fitness-function) population-size generations binary-operators unary-operators options)))

10 changes: 7 additions & 3 deletions src/nature/initialization_operators.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
:fitness-score (fitness-function genetic-sequence))))

(defn build-population
"Build `population-size` individuals by invoking `build-individual` on random, conforming genetic sequences."
[population-size alleles sequence-length fitness-function]
(repeatedly population-size #(build-individual (generate-sequence alleles sequence-length) fitness-function)))
"Build `population-size` individuals by invoking `build-individual` on random, conforming genetic sequences.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but I'd really appreciate a test with a trivial sequence-generator-function if you wouldn't mind

If given a sequence generator function, uses this function to generate valid sequences instead."
([population-size sequence-generator-function fitness-function]
(repeatedly population-size #(build-individual (sequence-generator-function) fitness-function)))

([population-size alleles sequence-length fitness-function]
(repeatedly population-size #(build-individual (generate-sequence alleles sequence-length) fitness-function))))