001  (ns nature.fitness-functions
002    "Helper functions for common fitness function needs"
003    (:require [nature.population-presets :as pp]))
004  
005  (defn binary-sequence-to-int
006    "Convert `binary-seq`, a collection of binary values, to the positive integer it represents"
007    [binary-seq]
008    (Integer/parseInt (apply str "" binary-seq) 2))
009  
010  (defn gray-binary-sequence-to-int
011    "Convert `binary-seq`, a collection of binary values, to the positive integer it represents
012     when considered by its Grey Coding: https://en.wikipedia.org/wiki/Gray_code"
013    [binary-seq]
014    (let [std-binary (binary-sequence-to-int binary-seq)]
015      (loop [mask (bit-shift-right std-binary 1)
016             val  std-binary]
017        (if (= 0 mask)
018          val
019          (recur (bit-shift-right mask 1) (bit-xor val mask))))))
020  
021  (defn int-to-decimal-mesh
022    "Splits the continuous range from `lower-bound` to `upper-bound` into discrete `blocks`,
023     than returns the decimal value of the `val-to-convert`th block"
024    [val-to-convert lower-bound upper-bound blocks]
025    (let [block-size (/ (- upper-bound lower-bound) blocks)
026          offset     (* val-to-convert block-size)]
027      (+ lower-bound offset)))