Skip to content

Commit

Permalink
Code improvements / More schema (#13)
Browse files Browse the repository at this point in the history
* more schema

* schema updates

* bump deps

* code style

* tweak input constraints

* update deptree info

* code style

* test style
  • Loading branch information
ogeagla authored Feb 20, 2024
1 parent 382df97 commit c6f63a1
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 25 deletions.
12 changes: 6 additions & 6 deletions doc/depstree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
[org.clojure/tools.analyzer "1.1.1"]
[org.clojure/tools.reader "1.3.6"]
[org.ow2.asm/asm "9.2"]
[org.clojure/data.csv "1.0.1"]
[org.clojure/tools.cli "1.0.219"]
[org.clojure/data.csv "1.1.0"]
[org.clojure/tools.cli "1.1.230"]
[org.knowm.xchart/xchart "3.8.7"]
[com.madgag/animated-gif-lib "1.4"]
[de.erichseifert.vectorgraphics2d/VectorGraphics2D "0.13"]
Expand All @@ -65,7 +65,7 @@
[org.junit.jupiter/junit-jupiter-engine "5.10.1" :scope "runtime"]
[org.junit.platform/junit-platform-engine "1.10.1" :scope "runtime"]
[org.junit.jupiter/junit-jupiter-params "5.10.1"]
[org.matheclipse/matheclipse-core "3.1.0-20240215.072506-61" :exclusions [[org.slf4j/slf4j-api] [com.fasterxml.jackson.core/jackson-core]]]
[org.matheclipse/matheclipse-core "3.1.0-20240219.223701-64" :exclusions [[org.slf4j/slf4j-api] [com.fasterxml.jackson.core/jackson-core]]]
[com.esotericsoftware/kryo "5.6.0"]
[com.esotericsoftware/minlog "1.3.1"]
[com.esotericsoftware/reflectasm "1.11.9"]
Expand Down Expand Up @@ -97,7 +97,7 @@
[org.jgrapht/jgrapht-core "1.5.3-20240118.050300-19"]
[org.jheaps/jheaps "0.14"]
[org.logicng/logicng-j11 "2.4.3"]
[org.matheclipse/matheclipse-external "3.1.0-20240215.072506-61"]
[org.matheclipse/matheclipse-external "3.1.0-20240219.223701-64"]
[com.fasterxml.jackson.core/jackson-databind "2.16.1"]
[com.fasterxml.jackson.core/jackson-annotations "2.16.1"]
[com.google.guava/guava "33.0.0-jre"]
Expand All @@ -109,10 +109,10 @@
[org.checkerframework/checker-qual "3.41.0"]
[io.pebbletemplates/pebble "3.2.2"]
[org.unbescape/unbescape "1.1.6.RELEASE"]
[org.matheclipse/matheclipse-parser "3.1.0-20240215.072506-61"]
[org.matheclipse/matheclipse-parser "3.1.0-20240219.223701-64"]
[org.organicdesign/Paguro "3.10.3"]
[org.jetbrains/annotations "23.0.0"]
[org.matheclipse/matheclipse-gpl "3.1.0-20240215.072506-61" :exclusions [[org.slf4j/slf4j-api] [com.fasterxml.jackson.core/jackson-core]]]
[org.matheclipse/matheclipse-gpl "3.1.0-20240219.223701-64" :exclusions [[org.slf4j/slf4j-api] [com.fasterxml.jackson.core/jackson-core]]]
[org.apache.logging.log4j/log4j-1.2-api "2.22.1"]
[org.slf4j/jcl-over-slf4j "2.0.12"]
[org.slf4j/slf4j-api "2.0.12"]
Expand Down
4 changes: 2 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
[org.clojure/core.async "1.6.681"]
[metosin/malli "0.14.0"]

[org.clojure/tools.cli "1.0.219"]
[org.clojure/tools.cli "1.1.230"]

[org.clojure/data.csv "1.0.1"]
[org.clojure/data.csv "1.1.0"]

[seesaw/seesaw "1.5.0"]
[flames "0.5.0"]
Expand Down
21 changes: 12 additions & 9 deletions src/closyr/ops/common.clj
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
(str expr) " / " (str variable) " : " (or (.getMessage e) e)))))


(defn- inversely-proportional-to-leaf-size
(defn- probability-inversely-proportional-to-leaf-size
[leaf-count scalar]
(let [leaf-scalar (min 1.0
(max 0.005
Expand All @@ -159,19 +159,22 @@
(defn should-modify-leaf
"Based on probability, check if should modify leaf"
[leaf-count {^IAST expr :expr ^ISymbol x-sym :sym :as pheno}]
(inversely-proportional-to-leaf-size leaf-count 1.5))
(probability-inversely-proportional-to-leaf-size
leaf-count 1.5))


(defn should-modify-branch
"Based on probability, check if should modify branch"
[leaf-count {^IAST expr :expr ^ISymbol x-sym :sym :as pheno}]
(inversely-proportional-to-leaf-size leaf-count 0.5))
(probability-inversely-proportional-to-leaf-size
leaf-count 0.5))


(defn should-modify-ast-head
"Based on probability, check if should modify AST head"
[leaf-count {^IAST expr :expr ^ISymbol x-sym :sym :as pheno}]
(inversely-proportional-to-leaf-size leaf-count 0.25))
(probability-inversely-proportional-to-leaf-size
leaf-count 0.25))


(def do-not-simplify-fns*
Expand All @@ -186,20 +189,20 @@

(defn- check-simplify-timing
[^IAST expr done?*]
(let [report-done?* (atom false)]
(let [report-slow-operation?* (atom false)]
(go-loop [c 0]
(when-not @done?*
;; wait sequence in ms looks like: 100, 316, 1000, ...
(<! (timeout (int (Math/pow (max 2.0 (/ *long-simplify-thresh-ms* 200))
(+ 1.5 (/ c 4))))))
(when (> c 6)
(reset! report-done?* true)
(reset! report-slow-operation?* true)
(swap! do-not-simplify-fns* assoc (str expr) 1)
(log/warn "Warning: simplify taking a long time: " c
" " (.leafCount expr) " : " (str expr)
" total slow fns: " (count @do-not-simplify-fns*)))
(recur (inc c))))
(when @report-done?*
(when @report-slow-operation?*
(log/warn "Warning: simplify took a long time: "
" : " (str expr)
" total slow fns: " (count @do-not-simplify-fns*)))))
Expand Down Expand Up @@ -243,8 +246,8 @@
(defn ^IAST maybe-simplify
"Maybe simplify pheno expr"
[{^IAST expr :expr ^ISymbol x-sym :sym ^ExprEvaluator util :util p-id :id simple? :simple? :as pheno}]
(if (and (<= (.leafCount expr) *simplify-max-leafs*)
(not simple?)
(if (and (not simple?)
(<= (.leafCount expr) *simplify-max-leafs*)
(< (rand) *simplify-probability-sampler*))
(let [start (Date.)
done?* (atom false)
Expand Down
8 changes: 4 additions & 4 deletions src/closyr/symbolic_regression.clj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
(chan))


(def ^:private sim-input-args*
(def sim-input-args*
"Data from GUI to use in the iterations"
(atom nil))

Expand Down Expand Up @@ -158,7 +158,7 @@
true))))


(defn- repaint-gui
(defn- update-gui-with-solver-data
[chart-iter
{:keys [^XYChart best-fn-chart
^XYChart scores-chart
Expand Down Expand Up @@ -256,7 +256,7 @@
(when-let [sim-msg (<! sim->gui-chan)]
(try

(repaint-gui chart-iter ui-elements conf sim-msg)
(update-gui-with-solver-data chart-iter ui-elements conf sim-msg)

(catch Exception e
(log/error "Err in redrawing GUI: " (.getMessage e))
Expand Down Expand Up @@ -354,7 +354,7 @@

(defn ->run-args
"Generate one-time computed args for solver"
{:malli/schema [:=> [:cat map?] #'specs/SolverRunArgs]}
{:malli/schema [:=> [:cat #'specs/SolverInputArgs] #'specs/SolverRunArgs]}
[{input-xs-exprs :input-xs-exprs
input-xs-vec :input-xs-vec
input-ys-vec :input-ys-vec
Expand Down
19 changes: 18 additions & 1 deletion src/closyr/util/spec.clj
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@


(def ^:private PopulationCount
[:int {:min 1 :max 10000}])
[:int {:min 1 :max 100000}])


(def ^:private GAPhenotype
Expand Down Expand Up @@ -265,6 +265,23 @@
[:max-leafs [:maybe #'MaxLeafs]]])


(def ^:private SolverInputArgs
[:map
{:closed true}
[:use-flamechart {:optional true} [:maybe :boolean]]
[:use-gui? {:optional true} [:maybe :boolean]]
[:initial-muts {:optional true} [:sequential #'GAMutation]]
[:initial-phenos {:optional true} [:maybe #'GAPopulationPhenotypes]]
[:input-xs-exprs {:optional true} [:vector #'SymbolicExpr]]
[:input-ys-exprs {:optional true} [:vector #'SymbolicExpr]]
[:input-xs-vec #'NumberVector]
[:input-ys-vec #'NumberVector]
[:input-iters {:optional true} #'Iterations]
[:iters {:optional true} #'Iterations]
[:input-phenos-count {:optional true} #'PopulationCount]
[:max-leafs {:optional true} [:maybe #'MaxLeafs]]])


(def ^:private SolverGUIMessage
[:map
{:closed true}
Expand Down
31 changes: 28 additions & 3 deletions test/closyr/symbolic_regression_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
:initial-muts (ops-init/initial-mutations)
:iters 5
:use-gui? false
:use-flamechart true
:use-flamechart false
:input-xs-exprs (->> (range 50)
(map (fn [i] (* Math/PI (/ i 15.0))))
ops-common/doubles->exprs)
Expand All @@ -167,7 +167,14 @@
100))

(is (= iters-done
5))))))
5))

(is (= (set (keys @symreg/sim-input-args*))
#{:input-xs-exprs
:input-xs-vec
:input-ys-vec}))

(reset! symreg/sim-input-args* {})))))

(testing "with provided data using record"
(with-redefs-fn {#'symreg/config->log-steps (fn [_ _] 10)}
Expand All @@ -192,7 +199,14 @@
100))

(is (= iters-done
5))))))))
5))

(is (= (set (keys @symreg/sim-input-args*))
#{:input-xs-exprs
:input-xs-vec
:input-ys-vec}))

(reset! symreg/sim-input-args* {})))))))


#_(deftest can-run-experiment-gui:start-stop
Expand Down Expand Up @@ -242,6 +256,9 @@
:input-iters 200
:input-phenos-count 500}))

(is (= (set (keys @symreg/sim-input-args*))
#{:input-xs-vec :input-ys-vec}))

(<! (timeout 100))
(is (put! symreg/*sim-stop-start-chan*
{:new-state :pause}))
Expand Down Expand Up @@ -280,6 +297,14 @@

(<! (timeout 100))

(is (= (set (keys @symreg/sim-input-args*))
#{:input-iters
:input-phenos-count
:input-xs-exprs
:input-xs-vec
:input-ys-vec
:max-leafs}))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :stop}))

Expand Down
33 changes: 33 additions & 0 deletions test/closyr/util_spec_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@
ISymbol)))


(deftest inputs-from-gui-spec
(testing "GUI input data"
(is (= (me/humanize
(m/explain #'specs/SolverGUIMessage
{:max-leafs 20,
:input-iters 10000,
:input-phenos-count 50000,
:new-state :start,
:input-data-x
[0.0
1.6331994645247656
3.2931726907630523
4.953145917001338
6.613119143239625
8.273092369477911
9.933065595716197
11.593038821954485
13.25301204819277
14.912985274431056],
:input-data-y
[2.3704663212435237
3.2253886010362693
3.7694300518134716
3.9248704663212437
1.1269430051813476
-5.090673575129534
1.204663212435233
2.215025906735751
1.5155440414507773
6.955958549222798]}))
nil))))


(deftest generates-custom-types
(testing "valid Expr"
(is (instance?
Expand Down

0 comments on commit c6f63a1

Please sign in to comment.