Skip to content

Commit

Permalink
Better State Control (#8)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ogeagla authored Feb 16, 2024
1 parent a75b922 commit e94f071
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 130 deletions.
48 changes: 25 additions & 23 deletions src/closyr/ops/modify.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
73 changes: 38 additions & 35 deletions src/closyr/symbolic_regression.clj
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
[msg]
(log/info "~~~ Restarting experiment! ~~~")
(update-plot-input-data msg)
true)
:restart)


(defn- check-gui-command-and-maybe-park
Expand All @@ -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} (<!! sim-stop-start-chan)]
(if (= :stop new-state)
:stop
(if (= :restart new-state)
(restart-with-new-inputs msg)
(do
(log/info "~~~ Resuming updates ~~~")
nil))))))))))
(case new-state
:stop :stop
:restart (restart-with-new-inputs msg)
:pause (do
(log/info "~~~ Parking updates due to Stop command ~~~")
(let [{:keys [new-state] :as msg} (<!! sim-stop-start-chan)]
(case new-state
:stop :stop
:restart (restart-with-new-inputs msg)
:start (do
(log/info "~~~ Resuming updates ~~~")
nil))))))))


(defn- ->run-args
Expand Down Expand Up @@ -479,36 +477,38 @@
(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)))


(solver-step
[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)
:final-population population
: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})
Expand All @@ -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)
Expand All @@ -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))


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
149 changes: 77 additions & 72 deletions test/closyr/symbolic_regression_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
(<! (timeout 200))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :start
:input-data-x [0 1 2 3 4]
:input-data-y [1 3 6 18 8]
:input-iters 200
:input-phenos-count 500}))

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

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [1 13 16 8 8]
:input-iters 1500
:input-phenos-count 500}))

(<! (timeout 100))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [1 13 16 8 8]
:input-iters 5
:input-phenos-count 5}))

(<! (timeout 100))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [11 3 6 18 8]
:input-iters 300
:input-phenos-count 400}))

(<! (timeout 100))

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

(<! (timeout 100))

(is (put! symreg/*gui-close-chan* :close-please))
(is (put! symreg/*sim->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 (= (<!! control-process) true))))))))
(when (not (GraphicsEnvironment/isHeadless))
(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
(<! (timeout 200))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :start
:input-data-x [0 1 2 3 4]
:input-data-y [1 3 6 18 8]
:input-iters 200
:input-phenos-count 500}))

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

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [1 13 16 8 8]
:input-iters 1500
:input-phenos-count 500}))

(<! (timeout 100))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [1 13 16 8 8]
:input-iters 5
:input-phenos-count 5}))

(<! (timeout 100))

(is (put! symreg/*sim-stop-start-chan*
{:new-state :restart
:input-data-x [0 1 2 3 4]
:input-data-y [11 3 6 18 8]
:input-iters 300
:input-phenos-count 400}))

(<! (timeout 100))

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

(<! (timeout 100))

(is (put! symreg/*gui-close-chan* :close-please))
(is (put! symreg/*sim->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 (= (<!! control-process) true)))))))))


(deftest derive-log-steps
Expand Down

0 comments on commit e94f071

Please sign in to comment.