From e94f07157bdc97d2fbb90e1128b8454432da9257 Mon Sep 17 00:00:00 2001 From: Octavian Geagla Date: Thu, 15 Feb 2024 18:08:10 -0700 Subject: [PATCH] Better State Control (#8) * better code style in control flow * better code style in control flow * better code style in control flow * update mods sampler * runner args * don't run graphical tests when jvm is headless * disable PR CI tests --- src/closyr/ops/modify.clj | 48 ++++---- src/closyr/symbolic_regression.clj | 73 +++++------ test/closyr/symbolic_regression_test.clj | 149 ++++++++++++----------- 3 files changed, 140 insertions(+), 130 deletions(-) diff --git a/src/closyr/ops/modify.clj b/src/closyr/ops/modify.clj index 9bf373e..8e17acc 100644 --- a/src/closyr/ops/modify.clj +++ b/src/closyr/ops/modify.clj @@ -234,27 +234,29 @@ "A coll whose random element is the number of modifications to apply in succession" (->> [] - (concat (repeat 60 1)) - (concat (repeat 40 2)) - (concat (repeat 25 3)) - (concat (repeat 20 4)) - (concat (repeat 19 5)) - (concat (repeat 18 6)) - (concat (repeat 17 7)) - (concat (repeat 16 8)) - (concat (repeat 15 9)) - (concat (repeat 14 10)) - (concat (repeat 13 11)) - (concat (repeat 12 12)) - (concat (repeat 11 13)) - (concat (repeat 10 14)) - (concat (repeat 9 15)) - (concat (repeat 8 16)) - (concat (repeat 7 17)) - (concat (repeat 6 18)) - (concat (repeat 5 19)) - (concat (repeat 4 20)) - (concat (repeat 3 21)) - (concat (repeat 2 22)) - (concat (repeat 1 23)) + (concat (repeat 40 1)) + (concat (repeat 30 2)) + (concat (repeat 20 3)) + (concat (repeat 18 4)) + (concat (repeat 16 5)) + (concat (repeat 15 6)) + (concat (repeat 14 7)) + (concat (repeat 13 8)) + (concat (repeat 12 9)) + (concat (repeat 11 10)) + (concat (repeat 10 11)) + (concat (repeat 9 12)) + (concat (repeat 8 13)) + (concat (repeat 7 14)) + (concat (repeat 6 15)) + (concat (repeat 5 16)) + (concat (repeat 4 17)) + (concat (repeat 3 18)) + (concat (repeat 2 19)) + (concat (repeat 1 20)) + ;; (concat (repeat 3 21)) + ;; (concat (repeat 2 22)) + ;; (concat (repeat 1 23)) + ;; (concat (repeat 1 24)) + ;; (concat (repeat 1 25)) vec)) diff --git a/src/closyr/symbolic_regression.clj b/src/closyr/symbolic_regression.clj index 8941756..54f7c41 100644 --- a/src/closyr/symbolic_regression.clj +++ b/src/closyr/symbolic_regression.clj @@ -329,7 +329,7 @@ [msg] (log/info "~~~ Restarting experiment! ~~~") (update-plot-input-data msg) - true) + :restart) (defn- check-gui-command-and-maybe-park @@ -339,20 +339,18 @@ :as run-args}] (let [[{:keys [new-state] :as msg} ch] (alts!! [sim-stop-start-chan] :default :continue :priority true)] (when (and msg (not= msg :continue)) - (if (= :stop new-state) - :stop - (if (= :restart new-state) - (restart-with-new-inputs msg) - (do - (log/info "~~~ Parking updates due to Stop command ~~~") - (let [{:keys [new-state] :as msg} (run-args @@ -479,7 +477,7 @@ (log/info "Running with logging every n steps: " (:log-steps run-config)) (assoc this :ga-result init-pop - :iters iters + :iters-to-go iters :start-ms start))) @@ -487,7 +485,7 @@ [this] (let [{:keys [iters log-steps]} run-config population (:ga-result this) - iters-to-go (:iters this)] + iters-to-go (:iters-to-go this)] (binding [ops/*log-steps* log-steps] (if (zero? iters-to-go) (assoc this :status :done :result {:iters-done (- iters iters-to-go) @@ -495,20 +493,22 @@ :next-step :wait}) (let [{scores :pop-scores :as ga-result} (ga/evolve population)] (ops/report-iteration iters-to-go iters ga-result run-args run-config) - (assoc this :ga-result ga-result :iters (next-iters iters-to-go scores))))))) + (assoc this :ga-result ga-result :iters-to-go (next-iters iters-to-go scores))))))) (next-state [this] (let [{:keys [iters initial-phenos initial-muts use-gui?]} run-config - iters-to-go (:iters this) - population (:ga-result this) - should-return (and use-gui? (check-gui-command-and-maybe-park run-args))] - (if (and use-gui? should-return) - (if (= :stop should-return) + iters-to-go (:iters-to-go this) + population (:ga-result this) + should-return-state (and use-gui? (check-gui-command-and-maybe-park run-args))] + (if (and use-gui? should-return-state) + (case should-return-state + :stop {:iters-done (- iters iters-to-go) :final-population population :next-step :stop} + :restart {:iters-done (- iters iters-to-go) :final-population population :next-step :restart}) @@ -517,7 +517,7 @@ (run-iteration [this] - (let [{iter-status :status iters-to-go :iters ga-result :ga-result + (let [{iter-status :status iters-to-go :iters-to-go ga-result :ga-result done-result :result :as res} (solver-step this)] (if (= :done iter-status) @@ -530,7 +530,7 @@ (end [this {:keys [next-step] :as return-value}] - (print-end-time (:start-ms this) (- (:iters run-config) (:iters this)) next-step) + (print-end-time (:start-ms this) (- (:iters run-config) (:iters-to-go this)) next-step) return-value)) @@ -676,16 +676,18 @@ (defn- run-app-with-gui - [] - (run-solver - {:initial-phenos (ops-init/initial-phenotypes 50) - :initial-muts (ops-init/initial-mutations) - :iters 100 - :use-gui? true - :max-leafs ops/default-max-leafs - :use-flamechart false - :input-xs-exprs example-input-xs-exprs - :input-ys-exprs example-input-ys-exprs})) + ([] + (run-app-with-gui {:use-flamechart false})) + ([{:keys [use-flamechart]}] + (run-solver + {:initial-phenos (ops-init/initial-phenotypes 50) + :initial-muts (ops-init/initial-mutations) + :iters 100 + :use-gui? true + :max-leafs ops/default-max-leafs + :use-flamechart use-flamechart + :input-xs-exprs example-input-xs-exprs + :input-ys-exprs example-input-ys-exprs}))) (def ^:private ^:dynamic *is-testing* false) @@ -723,4 +725,5 @@ (comment (macroexpand-1 `(log/info "Hello"))) (comment (log/info "Hello")) (comment (run-app-without-gui)) +(comment (run-app-with-gui {:use-flamechart true})) (comment (run-app-with-gui)) diff --git a/test/closyr/symbolic_regression_test.clj b/test/closyr/symbolic_regression_test.clj index 2518987..02a1ecb 100644 --- a/test/closyr/symbolic_regression_test.clj +++ b/test/closyr/symbolic_regression_test.clj @@ -5,7 +5,10 @@ [closyr.ops :as ops] [closyr.ops.common :as ops-common] [closyr.ops.initialize :as ops-init] - [closyr.symbolic-regression :as symreg])) + [closyr.symbolic-regression :as symreg]) + (:import + (java.awt + GraphicsEnvironment))) (alter-var-root #'symreg/*is-testing* (constantly true)) @@ -202,77 +205,79 @@ (deftest can-run-experiment-gui:start-restart-stop - (binding [ops/*print-top-n* 1] - (testing "gui can start and restart experiments; NOTE: do not run this while in headless mode, eg on CI" - (with-redefs-fn {#'symreg/config->log-steps (fn [_ _] 500)} - (fn [] - (let [control-process (go - (gui-chan* :next)) - true)] - - (symreg/run-solver - {:initial-phenos (ops-init/initial-phenotypes 20) - :initial-muts (ops-init/initial-mutations) - :input-xs-exprs symreg/example-input-xs-exprs - :input-ys-exprs symreg/example-input-ys-exprs - :iters 20 - :use-gui? true}) - - - (is (= (log-steps (fn [_ _] 500)} + (fn [] + (let [control-process + (go + (gui-chan* :next)) + true)] + + (symreg/run-solver + {:initial-phenos (ops-init/initial-phenotypes 20) + :initial-muts (ops-init/initial-mutations) + :input-xs-exprs symreg/example-input-xs-exprs + :input-ys-exprs symreg/example-input-ys-exprs + :iters 20 + :use-gui? true}) + + + (is (= (