
Project clojure-genetic-algo(irthm)
By Cody Shepp (2015)
\url{http://codyshepp.com/articles/3-exploring-genetic-algorithms-with-clojure/}


======================================

(ns clojure-genetic-algo.core-test
  (:require [clojure.test :refer :all]
            [clojure-genetic-algo.core :refer :all]))

(deftest a-test
  (testing "FIXME, I fail."
    (is (= 0 1))))
    
    
    
======================================


(defproject evo "0.1.0-SNAPSHOT"
 :description "FIXME: write description"
 :url "http://example.com/FIXME"
 :license {:name "Eclipse Public License"
           :url "http://www.eclipse.org/legal/epl-v10.html"}
 :dependencies [[org.clojure/clojure "1.6.0"]
                [org.clojure/core.async "0.1.346.0-17112a-alpha"]
                [org.clojure/tools.cli "0.3.1"]
                [quil "2.2.5" :exclusions [org.clojure/clojure]]]
:main evo.core)


======================================



(ns evo.distances)

;; data from http://www.codeproject.com/Articles/259926/Introduction-to-Genetic-Algorithm-Encoding-Camel
;; licensed under the CPOL: http://www.codeproject.com/info/cpol10.aspx
;A distance matrix is defined
(def distances [[ 0   28  57  72  81  85  80  113 89  80  ]
                [ 28  0   28  45  54  57  63  85  63  63  ]
                [ 57  28  0   20  30  28  57  57  40  57  ]
                [ 72  45  20  0   10  20  72  45  20  45  ]
                [ 81  54  30  10  0   22  81  41  10  41  ]
                [ 85  57  28  20  22  0   63  28  28  63  ]
                [ 80  63  57  72  81  63  0   80  89  113 ]
                [ 113 85  57  45  41  28  80  0   40  80  ]
                [ 89  63  40  20  10  28  89  40  0   40  ]
                [ 80  63  57  45  41  63  113 80  40  0   ]])


(defn get-distance [[a b]]
(get-in distances [a b]))

(comment; how to read matrix values with get-in
  (ns evo.distances)
	(def v [[1 2 3]
	       [4 5 6]
	       [7 8 9]]);#'evo.distances/v
	
	;row 0, last column
	(get-in v [0 2]);3
	
	;last row, column 1
	(get-in v [2 1]);8
	
 ;our get-distance function tested
	(get-distance [2 3]);20
)
 
 =============================
 
 (ns evo.draw
  (:require [clojure.core.async :refer [<!!]]
            [quil.core :as q]))
;async = asynchronous programming 
;<!! "take from this channel and block until we get the result"
;http://www.bradcypert.com/clojure-async/
;https://clojure.github.io/core.async/

;quil = ClojureScript library for creating interactive drawings and animations
;http://quil.info/


(def size 2);size of a pixel

(defn setup []
  ;quil.core/smooth. Draws all geometry with smooth (anti-aliased) edges.
  ;https://crossclj.info/fun/quil.core/smooth.html
  (q/smooth)
  (q/frame-rate 1000))

;three numbers for red-green-blue are encoded 
;into a single hex one using base 256.
;Taking an initial value as its first argument, 
;-> threads it through one or more expressions.
;https://clojure.org/guides/threading_macros
(defn rgb->hex [[r g b]];rgb->hex is not a lambda function. It is a name.
  (Long/toHexString (+ b (* g 256) (* r 65536)))); 65536 = 256 * 256


(comment
  (rgb->hex [255 255 255]);ffffff
	(rgb->hex [255 0 0]);ff0000
	(rgb->hex [0 255 0]);ff00
	(rgb->hex [0 0 255]);ff
  (rgb->hex [100 0 0]);640000
  (rgb->hex [0 100 0]);6400
  (rgb->hex [0 0 100]);64
)


;To each fitness value is given a color in relation to its population.
;mn = minimum; mx = max; val = fitness value
(defn rgb [mn mx val]
  (let [ratio (/ (* 2 (- val mn)) (- mx mn))
        r     (int (max 0 (* 255 (- 1 ratio))))
        b     (int (max 0 (* 255 (- ratio 1))))
        g     (- (- 255 b) r)]
    [r g b]))

(comment
	(rgb 0 100 0);[255 0 0] fitness 0 -> red (the fittest)
	(rgb 0 100 50);[0 255 0] fitness 50 -> green (average)
	(rgb 0 100 100);[0 0 255] fitness 255 -> blue (the worst)
  (rgb 0 100 30);[102 153 0] fitness 30 -> redish + greenish
)


;A fitness value is given an rgb color that is encoded in hex.
;the color is chosen according to the population so, max is used.
(defn get-color [value max]
  (let [c (rgb 0 max value)]
    (str (rgb->hex c))))

(comment
	 (get-color 0 100);ff0000
	 (get-color 10 100);cc3300
	 (get-color 20 100);996600
	 (get-color 30 100);669900
	 (get-color 40 100);33cc00
	 (get-color 50 100);ff00
	 (get-color 60 100);cc33
	 (get-color 70 100);9966
	 (get-color 80 100);6699
	 (get-color 90 100);33cc
	 (get-color 100 100);ff
  )
  

;draw a pixel with the color representing the fitness of an individual
(defn draw-square [y x v]
  (q/fill (apply q/color (rgb 350 700 v))); value v; min = 350; max = 700
  (q/no-stroke);no border http://quil.info/api
  (q/rect (* size x) (* size y) size size)); x-origin, y-origin, x-size, y-size


;draw the whole population of a given generation in a row
(defn draw-row [y row]
  (doall (map-indexed #(draw-square y %1 %2) row)))


(comment; map-indexed: returns item (tranformed) + index
        ; https://clojuredocs.org/clojure.core/map-indexed
  (map-indexed vector "foobar")
;#'evo.draw/init
;([0 \f]
; [1 \o]
; [2 \o]
; [3 \b]
; [4 \a]
; [5 \r])
  )

;do the whole drawing
(defn draw-screen [rows]
  (doall (map-indexed #(draw-row %1 %2) rows)));

;a counter with mutable value to be updated. Initial value 0.
(def counter (atom 0))


(defn init [h w ch];height width channel
  (defn draw []
    (let [row (<!! ch); 
          y   @counter]
      (do
        (draw-row y row); draw  row 0-th 
        (reset! counter (inc y))))); y = y+1 ;end of draw
  (q/defsketch evolution;initialize a sketch, a drawing; http://quil.info/api/environment#defsketch
    :title "Evolution"
    :setup setup
    :draw  draw
:size [(* w size) (* h size)]))

===============================

(ns evo.util)

;; from 
;;https://clojuredocs.org/clojure.core/rand-int
(defn unique-random-numbers [n]
  "List with random shuffling of ints between 0 and n"
  (let [a-set (set (take n (repeatedly #(rand-int n))))]; set with random elements; correct size is not guaranteed
    (concat a-set (clojure.set/difference (set (take n (range)))
                                  a-set)))); lacking elements are added.

(comment; rand-int take repeat repeatedly  difference concat
  (rand-int 7);2
  ;#{} is the set ID
  (take 3 '(1 2 3 4 5 6));(1 2 3)
  (repeat 5 (rand-int 100));(99 99 99 99 99)
  (repeatedly 10 #(rand-int 11));(8 9 9 3 6 2 4 8 9 2) coll with repeated elements
  (set '(8 9 9 3 6 2 4 8 9 2));#{4 6 3 2 9 8} repeated elements are filtered
  (set (take 7 (repeatedly #(rand-int 7))));#{0 1 6 2}
  (set (take 7 (repeatedly #(rand-int 7))));#{4 6 2 5}
  (set (take 10 (repeatedly #(rand-int 7))));#{0 1 4 6 3 2 5}
  (set (take 12 (range)));#{0 7 1 4 6 3 2 11 9 5 10 8}
  (clojure.set/difference #{1 2} #{2 3});#{1} 
  (concat [1 2] [3 4]);(1 2 3 4)
  (unique-random-numbers 12);(0 7 1 4 3 2 11 9 10 8 6 5); 12 elements
  (set (take 20 (repeatedly 20 #(rand-int 20))));#{7 1 4 13 6 3 2 19 11 5 14 16 8}; 13 elements
  )


(defn pathify [vec]
  "turns a vector into a vector of n-grams,
   including last element back to the first
   ex: [0 1 2] -> [[0 1] [1 2] [2 0]]"
  (map (fn [i]
           (subvec (into vec vec) i (+ 2 i)))
       (range (count vec)))); range for i

(comment; count range subvec into
   (count [ 1 2 5]);3
   (range 4);(0 1 2 3)
   (subvec [1 2 3 4 5] 0 2);[1 2]
   (subvec [1 2 3 4 5] 2 4);[3 4]
   (into [1 2 3] [1 2 3]);[1 2 3 1 2 3]
   (into [1 2 3] [4 5 6 7]);[1 2 3 4 5 6 7]
   
   (pathify [1 2 3]); ([1 2] [2 3] [3 1])
   (pathify [1 2 3 4 5 6 7]);([1 2] [2 3] [3 4 [4 5] [5 6] [6 7] [7 1])
)




(defn swap [v i1 i2]
  "swaps two elements in a vector"
  (assoc v i2 (v i1) i1 (v i2)))

(comment; assoc, select element
  
  (assoc [1 2 3] 0 10) ; [10 2 3]
  (assoc [1 2 3] 3 10) ;[1 2 3 10]
  (assoc [1 2 3] 5 10);IndexOutOfBoundsException
  ([2 4 3 6 7] 2);3
  (swap [0 1 2 3 4] 1 4);[0 4 2 3 1]  
  )



(defn pair [vec]
  "splits a 1d array into a 2d array of pairs"
  (mapv (fn [i] (subvec vec i (+ 2 i)))
        (map #(* 2 %)
             (range (/ (count vec) 2)))))



(comment
  
  (defn doubling [vec]
        "produces multiples of 2"
        (map #(* 2 %) (range (/ (count vec) 2))))
  (doubling [ 0 2 5 3 2]);(0 2 4)
  (doubling [ 0 2]);(0)
  (doubling [ 0 2 5 3 ]);(0 2)
  (doubling [ 0 2 5 3 2 8]);(0 2 4)
  
  ;;;;;;;;;
  ;Examples from 
  ;http://www.braveclojure.com/core-functions-in-depth/
  (defn titleize
     [topic]
     (str topic " for the Brave and True"))
   
   (map titleize ["Hamsters" "Ragnarok"])
   ; => ("Hamsters for the Brave and True" "Ragnarok for the Brave and True")
   
   (map titleize '("Empathy" "Decorating"))
   ; => ("Empathy for the Brave and True" "Decorating for the Brave and True")
   
   (map titleize #{"Elbows" "Soap Carving"})
   ; => ("Elbows for the Brave and True" "Soap Carving for the Brave and True")
   
   (map #(titleize (second %)) {:uncomfortable-thing "Winking"})
   ; => ("Winking for the Brave and True")
   ;;;;;;;;
  
  (map #(str "Hello " % "!" ) ["Ford" "Arthur" "Tricia"]);("Hello Ford!" "Hello Arthur!" "Hello Tricia!")
  (mapv #(str "Hello " % "!" ) ["Ford" "Arthur" "Tricia"]);["Hello Ford!"  "Hello Arthur!" "Hello Tricia!"]
  
  
  (pair [ 0 1 2 3 4 5]);[[0 1] [2 3] [4 5]]
  (pair [ 0 1 2 3 4]);IndexOutOfBoundsException
  
  )


;; from 
;  https://groups.google.com/forum/#!topic/clojure/vYND1CtKf2M
(deftype OrderPreservingSet [v m s]
  clojure.lang.IObj
    (withMeta [this md] (OrderPreservingSet. v (with-meta m md) s))
    (meta [this] (meta m))
  clojure.lang.IPersistentSet
    (cons [this object]
      (if (contains? m object)
        this
        (OrderPreservingSet.
          (conj v object)
          (assoc m object (count v))
          s)))
    (count [this] (count m))
    (empty [this] (OrderPreservingSet. [] {} (Object.)))
    (equiv [this other] (= (seq this) other))
    (seq [this] (seq (remove #{s} v)))
    (get [this object]
      (v (m object)))
    (contains [this object]
      (contains? m object))
    (disjoin [this object]
      (if-let [idx (m object)]
        (OrderPreservingSet.
          (assoc v idx s)
          (dissoc m object)
          s)
       this))
  clojure.lang.IFn
    (invoke [this object]
      (get this object))
    (invoke [this object not-found]
      (get this object not-found)))

(comment
  
  ;;????
  
  )



(defn order-preserving-set [& things]
  (reduce conj (OrderPreservingSet. [] {} (Object.)) things))

(comment
  (set '(300 900 150 650 300)); #{300 150 650 900}
  (order-preserving-set 300 900 150 650 300);#{300 900 150 650}
)


;; from http://stackoverflow.com/questions/1394991/clojure-remove-item-from-vector-at-a-specified-location
(defn vec-remove
  "remove elem in coll"
  [coll pos]
(vec (concat (subvec coll 0 pos) (subvec coll (inc pos)))))


(comment
  (vec-remove [1 2 3 4 5] 3);[1 2 3 5]
  )

  
  ===========================
  
  (ns evo.genetics
  (:require [evo.util :refer :all]
            [evo.distances :refer [get-distance]]
            [clojure.core.async :refer [>! go chan]]))

(def num-cities 10)     ;; how many cities we have in our problem
(def mutation-chance 5) ;; how likely it is for a mutation to occur
(def fitness-multiplier 100)


;; initialize population
(defn create-population [gen-size genome-size]
  "creates a random population of size gen-size,
   with each genome of size genome-size"
  (map (fn [x] (vec (unique-random-numbers genome-size)))
       (range gen-size))); range of x              

(comment
  
  (unique-random-numbers 5); (1 4 3 0 2)
  (vec (unique-random-numbers 5));[0 1 3 2 4]
  (create-population 4 5)
;([1 4 3 2 0]
; [4 3 2 0 1]
; [4 2 0 1 3]
; [0 1 4 3 2])
)


;; fitness
(defn calculate-fitness [genome]
  "Returns the total distance travelled for a given genome"
  (let [path (pathify genome)
        distances (map get-distance path)]
    (reduce + distances)))

(comment 
  
(pathify [0 1 2 3 4 5 6 7 8 9])
;([0 1] [1 2] [2 3] [3 4]  [4 5]  [5 6]  [6 7]  [7 8] [8 9] [9 0])
 
(map get-distance (pathify [0 1 2 3 4 5 6 7 8 9]))
;(28 28 20 10 22 63 80 40 40 80)

(reduce + [28 28 20 10 22 63 80 40 40 80]);411 = 28 + 28 + ... + 80
(calculate-fitness [0 1 2 3 4 5 6 7 8 9]);411 
  )



;; crossover
;;Recombination of tour
; [0 1 2 3 4 5 6 7 8 9] and tour 
; [7 1 2 3 4 5 6 0 8 9] = 
; [0 1 2 3 4 5 6 0 8 9] is not viable: 0 appears twice and 7 is lacked.
;;But
;[0 	4 	3 	6 	2 	9 	5 	7 	1 	8] and
;[8 	2 	3 	4 	5 	6 	1 	7 	9 	0] can be recombined and produce viable tours as follows:
;[8 	4 	3 	6 	2 	9 	5 	7 	1 	0] and
;[0 	2 	3 	4 	5 	6 	1 	7 	9 	8]. Example by the Author Cody Shepp:
;http://codyshepp.com/articles/3-exploring-genetic-algorithms-with-clojure/


(defn find-cycle [a b seed]
  "finds a single cycle in the parents
   starting at the seed (position) index"
  (loop [coll (mapv vector a b)
         cyl  #{}
         cur  (get-in coll [seed 0])]
    (let [idx (first (keep-indexed #(if (= (first %2) cur) %1) coll))]
      (if (nil? idx)
        (vec cyl)
        (recur (vec-remove coll idx)
               (into cyl (nth coll idx))
               (last (nth coll idx)))))))



(comment;mapv find-cycle
        
  (mapv #(* % 3) (range 10));[0 3 6 9 12 15 18 21 24 27]   f(x) = 3x for x < 10
  
  ;the same in two steps
  (defn triple [x]
           (* 3 x))
   (mapv #(triple %) (range 10));[0 3 6 9 12 15 18 21 24 27]


  
  (mapv vector [0 1 2 3 4] ;the v in mapv means vertical: picks up the columns of a matrix
               [3 4 1 2 0]); [ [0 3] [1 4] [2 1] [3 2] [4 0]]
  
  
  (mapv vector [0 1 2 3 4] 
                [3 4 1 2 0]
                [2 3 1 0 2])
;;[[0 3 2]
 ;[1 4 3]
 ;[2 1 1]
 ;[3 2 0]
 ;[4 0 2]]
 
 
 
 
;;             0  1   2   3   4   5   6   7   8   9               
  (find-cycle [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
              [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]   0); [0 8]
  (find-cycle [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
              [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]   1);[1 4 6 2 9 5] (expected: [1 9 6 4 2 5 1])
  (find-cycle [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
              [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]   2);[3]
  (find-cycle [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
              [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]   3);[1 4 6 2 9 5] (expected [4 2 5 1 9 6 4 ])
  
  
  ;;Bad understood
  )



(defn find-all-cycles [a b]
  "finds all possible cycles in the parents"
  (loop [idx 0
         cyl (order-preserving-set)]
    (let [c (find-cycle a b idx)]
      (if (= idx (count a))
        (vec (seq cyl))
        (recur (inc idx) (into cyl [c]))))))


(comment
  
  (find-all-cycles [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
                   [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]); [[0 8] [1 4 6 2 9 5] [3] [7]]
  
)


(defn make-child [parents cycles]
  "combines parents using cycle crossover"
  (loop [res (vec (repeat 10 nil))
         idx 0]
    (if (= idx (count cycles))
        res
        (recur (reduce (fn [v e] (assoc v (first e) (last e)))
                       res
                       (map (fn [i] [(.indexOf (nth parents (mod idx 2)) i) i])
                            (nth cycles idx)))
               (inc idx)))))



(comment
  (vec (repeat 10 nil));[nil  nil  nil  nil nil nil  nil  nil  nil nil]
 

  (make-child [[8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
               [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]] ;parents
               (find-all-cycles 
                 [8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
                 [0 	2 	3 	4 	5 	6 	1 	7 	9 	8])) ; [8 2 3 4 5 6 1 7 9 0]
  
  )




(defn cycle-crossover [[a b]]
  "returns both permuations of the parents'
   cycle crossover (changed ordering)"
  (let [cycles (find-all-cycles a b)
        c1     (make-child [a b] cycles)
        c2     (make-child [b a] cycles)]
    [c1 c2]))


(comment
  
  (cycle-crossover [[8 	4 	3 	6 	2 	9 	5 	7 	1 	0] 
                    [0 	2 	3 	4 	5 	6 	1 	7 	9 	8]])
;;[[8 2 3 4 5 6 1 7 9 0]
;  [0 4 3 6 2 9 5 7 1 8]]

)


;; mutation
(defn should-mutate? []
  "determines whether or not a given genome
   should be mutated"
  (< (rand-int 100) mutation-chance))


(comment
  
  (< (rand-int 100) 80); true
  (< (rand-int 100) 80); false
  )



(defn mutate [genome]; all mutations are viable
  "swaps two sets of random elements in the given genome"
  (let [a (rand-int (count genome))
        b (rand-int (count genome))
        c (rand-int (count genome))
        d (rand-int (count genome))]
    (swap (swap genome c d) a b)))


(comment
  (mutate [0 1 2 3 4 5 6 7 8 9]); [0 1 2 9 8 5 6 7 4 3]
  (mutate [0 1 2 3 4 5 6 7 8 9]); [9 1 2 0 4 5 6 7 8 3]
)


;; selection
(defn roulette-select [population]
  "selects genomes based on fitness"
  (let [roulette-wheel  (reduce #(conj %1 (+ (* (- 700 %2) fitness-multiplier) (last %1))) [0] (map calculate-fitness population))
        total           (last roulette-wheel)]
    (mapv (fn [x] (nth population (.indexOf roulette-wheel (first (filter #(< (rand-int total) %1) roulette-wheel)))))
          (range (count population)))))



(comment ;# % conj reduce first last rand-int filter

  (let [plus-numbers #(+ 1 %1 %2 %3)]
            (plus-numbers 10 20 30));61
  
  (conj [1 2] 3 4);[1 2 3 4]               
  (conj '(1 2) 3 4);(4 3 1 2)
  
  (reduce conj [1 2 3] [4 5 6]);[1 2 3 4 5 6]
  (reduce #(cons %2 %1) [1 2 3] [4 5 6]);(6 5 4 1 2 3)
  
  
  (first [1 2]); 1
  (first [ [1 2] [3 4] ]); [1 2]


  (last [1 2 3 4 5]);5
  (last {:a 1 :b 2 :c 3 :d 4 :e 5 :f 6 :g 7 :h 8 :i 9});[:a 1] !!!!
 
  (rand-int 30);10
  (filter #(< (rand-int 100) %1) [ 20 70 80 300 3000]);(70 80 300 3000)
  
  ;Not understood
)





(defn select-elites [old new]
  "replaces the worst 10 of the new generation
  with the best 10 of the old generation"
  (let [best  (take 10 (sort-by calculate-fitness old))]
    (into (drop-last 10 new) best)))


(comment drop-ast
  
  (drop-last   [1 2 3 4]);(1 2 3)
  (drop-last 2 [1 2 3 4]);(1 2)
  (into (drop-last 2 [1 2 3 4]) [7 8 9]); (9 8 7 1 2)
)


;; single-generation cycle: select -> crossover -> mutate
(defn next-generation [population]
  ""
  (->> (sort-by calculate-fitness population)
        vec
        ;;roulette-select
        pair
       (map cycle-crossover)
       (reduce into)
       (map (fn [x] (if (should-mutate?)
                        (mutate x); x mutated
                        x))); x untouched
       (select-elites population)))

(comment
  
  (macroexpand '(-> 0 (+ 1) (+ 2) (+ 3)));(+ (+ (+ 0 1) 2) 3)
  (macroexpand '(->> 0 (+ 1) (+ 2) (+ 3)));(+ 3 (+ 2 (+ 1 0)))
  
  )



;; full evolution cycle (many generations)
(defn evolve [max-gen gen-size draw-chan]
  "performs the generation cycle for
   [max-gen] number of generations"
  (loop [population (create-population gen-size num-cities)
         cur-gen    0];initialization
    (if (< cur-gen max-gen); if task is undone
      ;parameters housekeeping
      (let [ngen     (next-generation population)
            gen-num  (inc cur-gen)
            best-fit (calculate-fitness (first (sort-by calculate-fitness ngen)))
            avg-fit  (float (/ (reduce + (map calculate-fitness ngen)) (count ngen)))]
        (do
          (go
           ;;(>! draw-chan (mapv calculate-fitness ngen)))
           (>! draw-chan (mapv calculate-fitness (sort-by calculate-fitness ngen))))
          (recur ngen gen-num)))
      population)))


(comment
  
  (float (/ (reduce + [1 2 3 4 5]) 5));3.0 = average

  )


(defn solve [max-gen gen-size draw-chan]
  "evolves and then extracts the fittest
   genome from the final generation"
  (let [final-population (evolve max-gen gen-size draw-chan)]
    (calculate-fitness (first (sort-by calculate-fitness final-population)))))

#_(solve 1 1 (chan))
#_(>! draw-chan (mapv (fn [x] (Long/parseLong (reduce #(str %1 %2) "" x))) ngen))
#_(>! draw-chan (mapv calculate-fitness (sort-by calculate-fitness ngen)))

(find-all-cycles [0 4 3 6 2 9 5 7 1 8] [8 2 3 4 5 6 1 7 9 0])

=========================

(ns evo.core
  (:require [clojure.tools.cli :refer [parse-opts]]
            [clojure.core.async :refer [chan]]
            [clojure.string :as string]
            [evo.genetics :as g]
            [evo.draw :as d])
  (:gen-class))

(defn main [max-gen gen-size]
  ; A channel is similar to a queue. http://www.bradcypert.com/clojure-async/
  ;it will be used to store rows of a drawing
  (let [ch (chan)]
    (d/init max-gen gen-size ch);evo.draw.init
    (g/solve max-gen gen-size ch))); evo.genetics.solve

;; cli stuff below
(def cli-options; defalut values for parameters
  [["-g" "--gensize SIZE" "Generation Size"
    :default 100
    :parse-fn #(Integer/parseInt %)]
   ["-m" "--maxgen MAX" "Maximum Generations"
    :default 100
    :parse-fn #(Integer/parseInt %)]
   ["-h" "--help"]])

(defn exit [status msg]; exit and print a message
  (println msg)
  (System/exit status))

(defn error-msg [errors]; error message
  (str "The following errors occurred while parsing your command:\n\n"
       (string/join \newline errors)))

(defn -main [& args]
  (let [{:keys [options arguments errors summary]} (parse-opts args cli-options)]
    (cond
       (:help options) (exit 0 summary); help is allowed to be asked for
       ;; todo - validate args
       errors (exit 1 (error-msg errors))); in case of errors
(main (:maxgen options) (:gensize options))));run main with these values



(comment; cond
	(defn pos-neg-or-zero
	     "Determines whether or not n is positive, negative, or zero"
	     [n]
	     (cond; similar to switch
	       (< n 0) "negative"
	       (> n 0) "positive"
	       :else "zero"))
	   (pos-neg-or-zero 5)
	   (pos-neg-or-zero -1)
	   (pos-neg-or-zero 0)
;#'evo.core/pos-neg-or-zero
;positive
;negative
;zero

	;; Generates a random number compares it to user input
	(let [rnd (rand-int 10); equal to r.nextInt(10)
	      guess (Integer/parseInt (read-line))]; read an int number from the keypad when the REPL gets in black
	  (cond
	    (= rnd guess) (println "You got my guess right!")
	    :else (println "Sorry... guess again!")))
);end of comment

