Merge pull request #65 from thelmuth/fix/float-and-better-logging

Added float_div, and made logging print each value on one line instead of pretty-printed across many lines.
This commit is contained in:
Lee Spector 2023-11-07 21:37:50 -05:00 committed by GitHub
commit 98d7126dee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 7 deletions

View File

@ -125,8 +125,9 @@ Calling `(-main)` will run the default genetic programming problem.
## Description ## Description
Propel is an implementation of the Push programming Propeller is an implementation of the Push programming
language and the PushGP genetic programming system in Clojure. language and the PushGP genetic programming system in Clojure, based
on Tom Helmuth's little PushGP implementation [propel](https://github.com/thelmuth/propel).
For more information on Push and PushGP see For more information on Push and PushGP see
[http://pushlanguage.org](http://pushlanguage.org). [http://pushlanguage.org](http://pushlanguage.org).

View File

@ -1,7 +1,6 @@
(ns propeller.gp (ns propeller.gp
"Main genetic programming loop." "Main genetic programming loop."
(:require [clojure.string] (:require [clojure.string]
[clojure.pprint]
[propeller.genome :as genome] [propeller.genome :as genome]
[propeller.simplification :as simplification] [propeller.simplification :as simplification]
[propeller.variation :as variation] [propeller.variation :as variation]
@ -22,20 +21,21 @@
"Reports information each generation." "Reports information each generation."
[evaluations pop generation argmap training-data] [evaluations pop generation argmap training-data]
(let [best (first pop)] (let [best (first pop)]
(clojure.pprint/pprint (utils/pretty-map-println
{:generation generation {:generation generation
:best-plushy (:plushy best) :best-plushy (:plushy best)
:best-program (genome/plushy->push (:plushy best) argmap) :best-program (genome/plushy->push (:plushy best) argmap)
:best-total-error (:total-error best) :best-total-error (:total-error best)
:evaluations evaluations :evaluations evaluations
:ds-indices (map #(:index %) training-data) :ds-indices (if (:downsample? argmap)
(map #(:index %) training-data)
nil)
:best-errors (:errors best) :best-errors (:errors best)
:best-behaviors (:behaviors best) :best-behaviors (:behaviors best)
:genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop))) :genotypic-diversity (float (/ (count (distinct (map :plushy pop))) (count pop)))
:behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop))) :behavioral-diversity (float (/ (count (distinct (map :behaviors pop))) (count pop)))
:average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop))) :average-genome-length (float (/ (reduce + (map count (map :plushy pop))) (count pop)))
:average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))}) :average-total-error (float (/ (reduce + (map :total-error pop)) (count pop)))})))
(println)))
(defn cleanup (defn cleanup
[] []

View File

@ -183,6 +183,14 @@ Otherwise, acts as a NOOP"
;; FLOAT Instructions only ;; FLOAT Instructions only
;; ============================================================================= ;; =============================================================================
;; Divides the top two items on the float stack
;; If denominator is 0, returns 1.0
(def-instruction
:float_div
^{:stacks #{:float}}
(fn [state]
(make-instruction state #(float (if (zero? %2) 1 (/ %1 %2))) [:float :float] :float)))
;; Pushes the cosine of the top FLOAT ;; Pushes the cosine of the top FLOAT
(def-instruction (def-instruction
:float_cos :float_cos

View File

@ -151,3 +151,14 @@
(if (coll? thing-or-collection) (if (coll? thing-or-collection)
(rand-nth thing-or-collection) (rand-nth thing-or-collection)
thing-or-collection)) thing-or-collection))
(defn pretty-map-println
"Takes a map and prints it, with each key/value pair on its own line."
[mp]
(print "{")
(let [mp-seq (seq mp)
[first-key first-val] (first mp-seq)]
(println (pr-str first-key first-val))
(doseq [[k v] (rest mp-seq)]
(println (str " " (pr-str k v)))))
(println "}"))