0

I am trying to add a section to a Clojure code. After line no. 99 of the below code:

https://github.com/lspector/Clojush/blob/master/src/clojush/pushgp/breed.clj

I want to add these codes:

   (if (= num-parents 2)     
       (let [initial-other-parents (vec (repeatedly      
                                   (+ num-parents 4)     ; selecting parents more than required by 4
                                           (fn []
                                             (loop [re-selections 0
                                                    other (select population argmap)]
                                               (if (and (= other first-parent)
                                                        (< re-selections
                                                           (:self-mate-avoidance-limit argmap)))
                                                 (recur (inc re-selections)
                                                        (select population argmap))
                                                 other)))))
        
         all-parents (concat (if (nil? first-parent)     ;gathering all created parents 
                                   nil
                                   (vector first-parent))
                                 initial-other-parents)    
        
        (defn eclid-dist [u v]                            ;defining a function to calculate distances
          (->> (mapv - u v)
               (mapv #(Math/pow % 2))
               (reduce +)
               Math/sqrt))
        
        (defn find-largest-dist-pair [vec-map]             ;defining a function to return two vectors (parents) with the largest distance
          (apply max-key second
                 (for [[[k0 v0] & r] (iterate rest vec-map)
                       :while r
                       [k1 v1] r]
                   [[k0 k1] (eclid-dist v0 v1)])))
        
         final-parents (find-largest-dist-pair (:error all-parents)) ;selecting two parents with the largest distance
        
        op-fn (:fn (get genetic-operators operator))                     ; extracting the operator
        
        child (apply op-fn (concat final-parents                         ; creating child
                                   (vector (assoc argmap
                                                  :population population))))]
        
        )

For running the added part line by line, I extracted values before the changes using adding the below code after line 84 of the main code:

 (spit "initial-setting.edn" {:operator-list operator-list
                            :first-parent first-parent
                            :population population
                            :location location
                            :rand-gen rand-gen
                            :argmap argmap})

My question is:

  • what is the best way to assign extracted values by "spit" to variables and execute the added section line by line for debugging? I am using Calva as IDE and tried to put #break and use lein to run the code and debug it but it did not work. Here is my pervious post on that: How to set a breakpoint in a Clojure program using Calva?
ZGI
  • 45
  • 5
  • 1
    Why are there defns inside a let? Does above code even compile and are you actually asking how to fix it? Best to what metric? Why not add more key/value pairs to the map? Also asking more than one question is frown upon on SO - for sure your IDE or editor has a tutorial on how to debug clojure code and I think it would be best to remove this part. – cfrick Dec 25 '22 at 14:43
  • Hi and thank you for your comment. I just wanted to put all the added codes in a section. I tested defns separately and it works, I just need to match it with the main code. I already tried to put a breakpoint and run the debugger but it was not successful. That is why I decided to debug it manually by extracting values one step before changes, assigning them to variables, and running line by line. I added some more explanations and the link to my previous post on that to this post. – ZGI Dec 25 '22 at 23:19
  • 2
    At least move the defns out of the let (or define the functions inside the let, if you don't need them elsewhere, but the proper way `(let [eclid-dist (fn [u v] ...))` – cfrick Dec 27 '22 at 11:13
  • flowstorm is the best Clojure debugging tool I know of. – triplej Jan 02 '23 at 19:07

0 Answers0