Skip to content

Commit cd1b973

Browse files
committed
neuroevolution readme
1 parent 345594f commit cd1b973

15 files changed

+810
-212
lines changed

Diff for: results/neuroevolution/success.png

28.5 KB
Loading

Diff for: src/ex53_ppl.clj

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@
6161
"Make next spore"
6262
([id] (next-spore id nil))
6363
([id pos]
64+
;; 180 17
6465
;; a 180 b -5 !!! -10, 17 (default)
6566
;; a -69 b -1
6667
;; 42 9
6768
;; 79.6 -1.8
68-
(spore id pos 0.67 (m/sq 11.0) (m/radians 180) (m/radians 17) (first pal))))
69+
;; 0 5
70+
;; 180 179
71+
(spore id pos 0.67 (m/sq 11.0) (m/radians 42) (m/radians 9) (first pal))))
6972

7073
(defn draw
7174
[canvas window _ spores]
@@ -86,3 +89,5 @@
8689

8790

8891
(comment c2d/save window "results/ex53/spores.jpg")
92+
93+
(m/unuse-primitive-operators)

Diff for: src/ex63_particle_life.clj

+98-4
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,114 @@
121121
(c2d/set-color canvas (.color ptype))
122122
(c2d/ellipse canvas (.x p) (.y p) 4 4))
123123
#_(when (= frame 200) (c2d/save canvas "results/ex63/plife.jpg"))
124-
[setup (move setup particles 0.02)]))
124+
[setup (move setup particles 0.1)]))
125125

126-
(def setup (random-setup (r/irand 3 8)))
126+
(def setup (random-setup (r/irand 2 5)))
127+
128+
(defn save->setup [m]
129+
(-> m
130+
(update :alpha double-array)
131+
(update :beta double-array)
132+
(update :types (partial mapv map->ParticleType))
133+
(map->Setup)))
127134

128135
(def window (c2d/show-window {:canvas (c2d/black-canvas csize csize :highest)
129136
:draw-fn draw
130137
:background :black
131-
:draw-state (do
138+
:draw-state (let [setup setup #_(save->setup (setups :s1))]
132139
(pp/pprint setup)
133140
[setup (random-particles setup 1200)])}))
134141

135142

143+
(def pairs-love-hate {:n 2,
144+
:alpha
145+
[0.8531190685431542, 0.4124191049307851, 0.6363336352718891, 0.0],
146+
:beta
147+
[0.17515989405988655, 0.46898628375997914, 1.0E-5, 0.245296441325918],
148+
:types
149+
[{:id 0,
150+
:rmax 52.39644371175454,
151+
:color [31.0 119.0 180.0 255.0],
152+
:friction 0.9735391337659955,
153+
:directional? false}
154+
{:id 1,
155+
:rmax 76.58245632190017,
156+
:color [255.0 127.0 14.0 255.0],
157+
:friction 0.8565072274554544,
158+
:directional? false}],
159+
:fmult 11.78333128371779})
136160

137-
(def setups {:s4 {:n 3,
161+
(def setups {:s5 {:n 7,
162+
:alpha
163+
[0.14861021110200467, 0.6692996723215351, 0.8164479197480894,
164+
0.2971907681082335, 0.3390239085309035, -0.7785871214228197,
165+
0.807017246117286, 0.9897060316942456, 0.22462200467136376,
166+
0.24556166541932525, 0.9585743441404408, 0.46410543836135454,
167+
0.582672695098386, 0.4553549448206484, -0.9197920663869434,
168+
0.7244029506599556, 0.809370932139867, 0.8852742660527777,
169+
0.6153018098109097, 0.0, 0.8316971648547771, -0.6639246865662674,
170+
0.5979391646072336, 0.6024358095947859, 0.0, 0.5092272162340464,
171+
0.7028458867058891, 0.0, 0.380693263489916, 0.47368547609290784, 0.0,
172+
-0.01146080240482461, 0.11103110451012577, -0.9841353913583804,
173+
0.22910442818466126, 0.21225199666703465, 0.0, 0.3816397307749264,
174+
0.7107575951883653, -0.774523033517672, 0.0, 0.2784820533021294,
175+
0.5996965240685289, 0.5250427837238822, 0.2640012136092506,
176+
0.574189388135993, 0.5634533172450109, 0.49515357274531,
177+
-0.23345515434391828],
178+
:beta
179+
[0.39652995834906474, 0.18350480619310794, 0.34400004339297835,
180+
1.0E-5, 0.17535457154020762, 0.24633939044495803, 1.0E-5,
181+
0.0029488960022599027, 0.3854141065610507, 0.3037948040259639,
182+
0.1315968496394307, 0.09738530814951005, 1.0E-5, 0.3232729351116344,
183+
1.0E-5, 0.22153527794443625, 0.3268308089470868, 0.09744546120909733,
184+
1.0E-5, 0.06246113757148177, 0.05631946789826031, 0.3366222338984227,
185+
0.03675528120398472, 0.441394942824003, 0.0563859289193688,
186+
0.36463601158570325, 0.029368028675658624, 0.08811533231738519,
187+
0.017925239286269264, 0.2122421441106669, 0.2503850525716419,
188+
0.3272795892148501, 0.3991277529353564, 0.1418344093235479,
189+
0.011702419598665973, 0.3549620830153355, 0.17802794018769988,
190+
0.29575004512407876, 0.043728710826954426, 0.4477330979749426,
191+
0.08525557470032102, 0.20495483818859214, 0.28051143823078967,
192+
0.4399178328957438, 0.2692395895013477, 0.3540625752279375, 1.0E-5,
193+
0.1663827780283793, 0.2801211666493402],
194+
:types
195+
[{:id 0,
196+
:rmax 13.290700740078464,
197+
:color [31.0 119.0 180.0 255.0],
198+
:friction 0.9527625125622025,
199+
:directional? false}
200+
{:id 1,
201+
:rmax 80.6737289396209,
202+
:color [255.0 127.0 14.0 255.0],
203+
:friction 0.9334759162001242,
204+
:directional? false}
205+
{:id 2,
206+
:rmax 24.254597157847872,
207+
:color [44.0 160.0 44.0 255.0],
208+
:friction 0.7240566396062796,
209+
:directional? true}
210+
{:id 3,
211+
:rmax 71.76840597967437,
212+
:color [214.0 39.0 40.0 255.0],
213+
:friction 0.7899062609246527,
214+
:directional? false}
215+
{:id 4,
216+
:rmax 75.90713756033553,
217+
:color [148.0 103.0 189.0 255.0],
218+
:friction 0.8092729978675278,
219+
:directional? false}
220+
{:id 5,
221+
:rmax 66.98691395167273,
222+
:color [140.0 86.0 75.0 255.0],
223+
:friction 0.9857219741051314,
224+
:directional? false}
225+
{:id 6,
226+
:rmax 58.981056481884046,
227+
:color [227.0 119.0 194.0 255.0],
228+
:friction 0.7945210982191895,
229+
:directional? false}],
230+
:fmult 12.216106395481184}
231+
:s4 {:n 3,
138232
:alpha
139233
[0.6684104736324896, 0.964616865419524, 0.0, 0.4797524863542435,
140234
0.420585350696979, 0.0, 0.2919907666469499, 0.764963999761529,

Diff for: src/ex65_reaction_diffusion.clj

+10-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
:Da 1.0 :Db m/THIRD
4444
:f 0.055 :k 0.062
4545
:dt 1.0
46-
:show-map? true}))
46+
:show-map? true
47+
:color-a? true}))
4748

4849
(defn diffuse! [^double d ^doubles source ^doubles target]
4950
(doseq [^long x rsize ^long y rsize
@@ -80,6 +81,10 @@
8081
(m/- abb (m/* (m/+ f k) b))))) 0.0 1.0)))
8182
data)
8283

84+
(defn color-a [^double a ^double b] (v/vec3 (m/- 1.0 a) b (m/sqrt (m/* a b))))
85+
(defn color-b [^double a ^double b] (v/vec3 (m/- 1.0 b) a (m/sqrt (m/* b a))))
86+
87+
8388
(defn draw
8489
[canvas window _ {:keys [^doubles A ^doubles B show-map? f k] :as curr}]
8590
(when (c2d/mouse-pressed? window)
@@ -96,14 +101,16 @@
96101
:k (norm-k mx)
97102
:show-map? false))
98103
:m (assoc curr :show-map? true)
104+
:z (update curr :color-a? not)
99105
curr)
100-
curr)]
106+
curr)
107+
color-fn (if (:color-a? curr) color-a color-b)]
101108

102109
(c2d/set-background canvas (c/color 10 10 20) 100)
103110
(doseq [^long x rsize ^long y rsize
104111
:let [a (Array/get2d A size x y)
105112
b (Array/get2d B size x y)]]
106-
(c2d/set-color canvas (v/mult (v/vec3 (m/- 1.0 a) b (m/sqrt (m/* a b))) 255.0))
113+
(c2d/set-color canvas (v/mult (color-fn a b) 255.0))
107114
(c2d/rect canvas (m/* x scale) (m/* y scale) scale scale))
108115
(let [f (m/approx (if show-map? (norm-f my) f) 6)
109116
k (m/approx (if show-map? (norm-k mx) k) 6)]

Diff for: src/ex68_langton_ant.clj

+69-26
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,77 @@
11
(ns ex68-langton-ant
22
(:require [clojure2d.pixels :as p]
3-
[clojure2d.core :as c2d]))
3+
[clojure2d.core :as c2d]
4+
[clojure2d.color :as c]
5+
[fastmath.vector :as v]
6+
[clojure2d.extra.utils :as utils]
7+
[fastmath.core :as m]))
48

5-
(defn add [[x1 y1] [x2 y2] w h] [(mod (+ x1 x2) w) (mod (+ y1 y2) h)])
9+
(defn modadd
10+
[[^long x1 ^long y1] [^long x2 ^long y2] ^long w ^long h]
11+
[(m/mod (m/+ x1 x2) w)
12+
(m/mod (m/+ y1 y2) h)])
613

7-
(defn move [buff [[x y :as pos] [dx dy]]]
14+
(defn move [buff [[x y :as pos] [^long dx ^long dy] rules]]
815
(let [w (c2d/width buff)
916
h (c2d/height buff)
10-
col? (pos? (p/get-value buff 0 x y))
11-
ndir (if col? [dy (- dx)] [(- dy) dx])]
12-
(p/set-color! buff x y (if col? :black :white))
13-
[(add pos ndir w h) ndir]))
14-
15-
(def speed 100)
16-
17-
(defn draw [canvas _ frame [buff [[x y] _ :as pos-dir]]]
18-
(-> canvas
19-
(c2d/set-background 20 20 20)
20-
(c2d/image (c2d/resize buff 800 800))
21-
(c2d/set-color :blue 200)
22-
(c2d/rect 10 10 100 35)
23-
(c2d/set-color :white)
24-
(c2d/set-font-attributes 20)
25-
(c2d/text (str (* speed frame)) 20 35)
26-
(c2d/set-color :red)
27-
(c2d/set-stroke 2)
28-
(c2d/ellipse (* 2 x) (* 2 y) 10 10 true ))
29-
[buff (nth (iterate (partial move buff) pos-dir) speed)])
30-
31-
(def window (c2d/show-window {:canvas (c2d/canvas 800 800)
17+
c1 (c/set-alpha (p/get-color buff x y) 255.0)
18+
[c2 r] (rules c1)
19+
ndir (if (= \L r) [dy (m/- dx)] [(m/- dy) dx])]
20+
(p/set-color! buff x y c2)
21+
[(modadd pos ndir w h) ndir rules]))
22+
23+
(defn draw [canvas window _ {:keys [^long speed ^long steps ^long scale buff pos-dir] :as data}]
24+
(if (c2d/key-pressed? window)
25+
data
26+
(let [[pos dir] pos-dir
27+
hsc (m// scale 2)
28+
r (* hsc 5)
29+
pos2 (v/shift (v/mult pos scale) hsc)
30+
dir2 (v/add pos2 (v/mult dir scale))]
31+
(-> canvas
32+
(c2d/set-background 20 20 20)
33+
(c2d/image buff)
34+
(c2d/set-color :blue 200)
35+
(c2d/rect 10 10 150 35)
36+
(c2d/set-color :white)
37+
(c2d/set-font-attributes 20)
38+
(c2d/text (str steps) 20 35)
39+
(c2d/set-color :red)
40+
(c2d/set-stroke 2)
41+
(c2d/ellipse (pos2 0) (pos2 1) r r true)
42+
(c2d/line (pos2 0) (pos2 1) (dir2 0) (dir2 1)))
43+
(assoc data
44+
:steps (+ speed steps)
45+
:pos-dir (nth (iterate (partial move buff) pos-dir) speed)))))
46+
47+
(def pal (concat [(c/color :black)] (c/palette :category20)))
48+
49+
(defn rule->data [rule]
50+
(->> (take (count rule) pal)
51+
(cycle)
52+
(partition 2 1)
53+
(take (count rule))
54+
(map (fn [r [c1 c2]] [c1 [c2 r]]) rule)
55+
(into {})))
56+
57+
(def r1 "RRLLLRLLLRRR")
58+
(def r2 "LLRRRLRLRLLR")
59+
(def r3 "LRRRRRLLR")
60+
(def r4 "LRRL")
61+
(def r5 "RRLRR")
62+
(def r6 "RRRL")
63+
(def r7 "RRRRL")
64+
65+
(defn init [{:keys [^long scale rule] :as data}]
66+
(let [ps (m// 800 scale)]
67+
(assoc data
68+
:steps 0
69+
:buff (p/pixels ps ps)
70+
:pos-dir [[(m/inc (m// ps 2)) (m/inc (m// ps 2))] [0 1] (rule->data rule)])))
71+
72+
(def window (c2d/show-window {:canvas (c2d/canvas 800 800 :low)
3273
:background :black
3374
:draw-fn draw
34-
:draw-state [(p/pixels 400 400) [[100 100] [0 1]]]}))
75+
:draw-state (init {:scale 2 :speed 10000 :rule "RLRRLRLL"})}))
76+
77+
(comment (utils/show-palette pal))

Diff for: src/ex71_ince_beams.clj

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
;; ince beams example - idea by @CTW (Thomas Clark)
2+
;; https://en.wikipedia.org/wiki/Gaussian_beam#Ince-Gaussian_modes
3+
;; https://www.researchgate.net/publication/8567409_Ince-Gaussian_modes_of_the_paraxial_wave_equation_and_stable_resonators
4+
;; Clojure Conj 2024: https://youtu.be/_D5d6Ls6pBw?t=1951
5+
(ns ex71-ince-beams
6+
(:require [clojure2d.core :as c2d]
7+
[clojure2d.pixels :as p]
8+
[fastmath.core :as m]
9+
[fastmath.vector :as v]
10+
[fastmath.polynomials :as poly]
11+
[clojure2d.extra.utils :as u]
12+
[clojure2d.color :as c]
13+
[fastmath.complex :as cplx]
14+
[fastmath.random :as r])
15+
(:import [fastmath.vector Vec2]))
16+
17+
(set! *warn-on-reflection* true)
18+
(set! *unchecked-math* :warn-on-boxed)
19+
20+
(defn rayleigh-range
21+
^double [^double waist ^double wavelength]
22+
(m// (m/* m/PI (m/sq waist)) wavelength))
23+
24+
(defn width
25+
^double [^double z ^double zR ^double waist]
26+
(m/* waist (m/sqrt (m/inc (m/sq (m// z zR))))))
27+
28+
(defn csq
29+
^double [^double ellipticity ^double waist]
30+
(m/inc (m/* 0.5 (m/sq waist) ellipticity)))
31+
32+
(defn IG0
33+
"An Ince-Gaussian beam function for z=0."
34+
[p m ellipticity waist wavelength]
35+
(let [ellipticity (double ellipticity)
36+
waist (double waist)
37+
zR (rayleigh-range waist wavelength)
38+
w (width 0.0 zR waist)
39+
ince (poly/ince-C p m ellipticity)
40+
ince-radial (poly/ince-C-radial p m ellipticity)
41+
k (m// m/TWO_PI (double wavelength))
42+
rscale (m// (m/* w (m/sqrt (m/* 0.5 ellipticity))))]
43+
44+
(fn ^double [^double x ^double y]
45+
(let [^Vec2 xi-eta (cplx/acosh (cplx/scale (cplx/complex x y) rscale))
46+
r**2 (m/+ (m/sq x) (m/sq y))]
47+
(m/* (m// waist w)
48+
(double (ince-radial (xi-eta 0)))
49+
(double (ince (xi-eta 1)))
50+
(m/exp (m/* -1.0 k
51+
(m// r**2 (m/* 2.0 zR)))))))))
52+
53+
(defn IG-profile
54+
([IG-fn]
55+
(IG-profile IG-fn [0 0] [200 200]))
56+
([IG-fn [^double x0 ^double y0] [^long width ^long height]]
57+
(let [hw (m// width 2.0)
58+
hh (m// height 2.0)]
59+
(for [^long x (range width)
60+
^long y (range height)]
61+
(m/sq (IG-fn (m/+ x0 (m/- x hw))
62+
(m/+ y0 (m/- y hh))))))))
63+
64+
(defn normalize
65+
"Peak normalize a tensor."
66+
;; TODO: extend to deal with non-zero floored tensors.
67+
[d]
68+
(let [peak (double (reduce m/max d))]
69+
(map (fn [^double v] (m// v peak)) d)))
70+
71+
(defn profile [p m ellipticity waist]
72+
(-> (IG0 p m ellipticity waist 0.78)
73+
(IG-profile [0.0 0.0] [100 100])
74+
(normalize)))
75+
76+
(defn show-profile
77+
[p m ellipticity waist]
78+
(let [p (profile p m ellipticity waist)
79+
g (c/gradient [:black (c/gray 200) :white])]
80+
(c2d/with-canvas [c (c2d/canvas 800 800)]
81+
(c2d/set-background c :black)
82+
(doseq [[^long i ^double v] (map-indexed vector p)
83+
:let [x (m/* 8 (m/quot i 100))
84+
y (m/* 8 (m/rem i 100))]]
85+
(c2d/set-color c (g v))
86+
(c2d/rect c x y 8 8))
87+
(u/show-image c))))
88+
89+
(show-profile 11 5 7 11)
90+
91+
(defn render-profile
92+
[p m ellipticity waist wavelength]
93+
(let [buffer (p/gradient-renderer 800 800 :sinc 1)
94+
r (range -100 100 0.05)
95+
n (m/make-norm -100 100 0 800)
96+
p (IG0 p m ellipticity waist wavelength)]
97+
(doseq [^double x r ^double y r
98+
:let [x (m/+ x (r/grand 0.05))
99+
y (m/+ y (r/grand 0.05))
100+
xx (n x) yy (n y)
101+
v (m/sq (double (p x y)))]]
102+
(p/add-pixel! buffer xx yy v))
103+
(u/show-image (p/to-pixels buffer {:logarithmic? true}))))
104+
105+
(render-profile 7 5 3 15 0.78)
106+

0 commit comments

Comments
 (0)