-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve_not.jl
220 lines (202 loc) · 5.65 KB
/
evolve_not.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using ALIFE2023
using ALIFE2023.Inverter
using ModelingToolkit
using Plots
using Graphs
using StatsBase
using StatsPlots
using GraphRecipes
using CSV
using Tables
using Distributions
using LaTeXStrings
using GeneticLogicGraph
function build_inverter_experiment(M, N)
N = length(Inverter.components(N))
n = 2 * ((N - 2)^2 + N - 1)
MR = 4 / n
return Algorithm(
random_population(M, N, n, 1, 1),
fill(-Inf, M),
mutation_operator(N, MR, 1, 1),
decoder(N, 1, 1),
Inverter.selector(N),
)
end
function evolve_inverter(alg, G)
scores = Matrix{Float64}(undef, G, size(alg, 1))
for g in 1:G
evolve!(alg, 4)
@show g, alg.scores
scores[g, :] .= alg.scores
@named model = Circuit(alg.decode(alg.population[1,:]), Inverter.components())
problem = problem_from_model(model)
d = Inverter.complement(problem, model, 1024)
CSV.write("not-$g-complement.csv", Tables.table(d), writeheader=false)
end
return scores
end
@time bscore = Inverter.benchmark_score(2048)
@show bscore
alg = build_inverter_experiment(1, 7)
function cgp_convergence_iterations(N)
i = 0
alg = build_inverter_experiment(1, N)
while true
i = i + evolve!(alg, 4)
g = alg.decode(alg.population[1, :])
if g.ne > 0
score, swap = alg.select(g, Inverter.benchmark_graph(N))
swap && return i
end
end
end
function random_graph_convergence_iterations(N)
i = 0
N = length(Inverter.components(N))
alg = build_inverter_experiment(1, N)
n = 2 * ((N - 2)^2 + N - 1)
while true
i = i + 1
g = random_genome(N, n, 1, 1)
if alg.decode(g).ne > 0
score, swap = alg.select(alg.decode(g), Inverter.benchmark_graph(N))
swap && return i
end
end
end
function average_random_graph_convergence(n, N)
is = zeros(Int, n)
Threads.@threads for i in eachindex(is)
is[i] = random_graph_convergence_iterations(N)
end
return is
end
function record_averages(n, Ns, fn)
iterations = Matrix{Int}(undef, n, length(Ns))
for i in eachindex(Ns)
@show Ns[i]
Threads.@threads for j in 1:n
iterations[j, i] = random_graph_convergence_iterations(Ns[i])
end
end
CSV.write("iterations-$(fn).csv", Tables.table(iterations), writeheader=false)
end
function load_averages(fn)
CSV.File(fn, header=false) |> Tables.matrix
end
function record_cgp_averages(n, Ns, fn)
iterations = Matrix{Int}(undef, n, length(Ns))
for i in eachindex(Ns)
@show Ns[i]
Threads.@threads for j in 1:n
iterations[j, i] = cgp_convergence_iterations(Ns[i])
end
end
CSV.write("cgp-iterations-$(fn).csv", Tables.table(iterations), writeheader=false)
end
function timecourse_of_benchmark()
inverter = Inverter.benchmark_model()
problem = Inverter.benchmark_problem()
lprob = change_input_levels(problem, [(@nonamespace inverter.LacI).λ], [10 * log(2) / 90])
hprob = change_input_levels(problem, [(@nonamespace inverter.LacI).λ], [100 * log(2) / 90])
Plots.theme(:dao)
plt = plot()
plot!(plt, solve(lprob, SSAStepper(), saveat=1), idxs=[8], label="Low Input")
plot!(plt, solve(hprob, SSAStepper(), saveat=1), idxs=[8], label="High Input")
plot!(
plt,
xlabel="Time (hours)",
ylabel="Abundance of output",
legend=:best,
size=(350, 275),
tickfontsize=8,
legendfontsize=8,
guidefontsize=8,
xticks=([720, 1440, 2160, 2880], ["12", "24", "36", "48"])
)
return plt
end
function complement_of_benchmark()
inverter = Inverter.benchmark_model()
problem = Inverter.benchmark_problem()
comp = Inverter.benchmark_distribution(1024)
Plots.theme(:dao)
plt = plot()
histogram!(plt, comp)
plot!(
plt,
xlabel="High - Low output",
ylabel="Frequency",
legend=false,
size=(350, 275),
tickfontsize=8,
legendfontsize=8,
guidefontsize=8,
)
return plt
end
function convergence_to_benchmark()
ifn = "/home/lewis/sauces/julia/GeneticLogicGraph/src/iterations.csv"
cfn = "/home/lewis/sauces/julia/GeneticLogicGraph/src/cgp-iterations.csv"
A = load_averages(ifn)
B = load_averages(cfn)
Plots.theme(:dao)
x = repeat([3, 4, 5, 6, 7, 8], inner=64)
plt = plot()
Aμs = mean(A', dims=2)
Bμs = mean(B', dims=2)
Aσs = std(A', dims=2)
Bσs = std(B', dims=2)
Aϵs = Aσs / √64
Bϵs = Bσs / √64
@show Aμs, Aσs, Aϵs
@show Bμs, Bσs, Bϵs
groupedboxplot!(
plt,
vcat(x, x),
vcat(A[:], B[:]),
group=vcat(zeros(Int, length(x)), ones(Int, length(x))),
markersize=3,
markershape=:x,
markerstrokewidth=0,
outliers=false,
labels=["Random search" "Algorithm search"]
)
scatter!(
plt,
x .- rand(Normal(0.25, 0.05), length(x)),
A[:],
color=:black,
markersize=1.5,
markeralpha=0.5,
label=false
)
scatter!(
plt,
x .+ rand(Normal(0.25, 0.05), length(x)),
B[:],
color=:black,
markersize=1.5,
markeralpha=0.5,
label=false
)
plot!(
plt,
minorgrid=false,
minorticks=false,
yscale=:log10,
legend=:best,
size=(350, 275),
tickfontsize=8,
legendfontsize=8,
guidefontsize=8,
xlabel=L"Graph size $|V|$",
ylabel="Function evaluations",
)
return plt
end
function runner(N, fn)
record_averages(8, N, fn)
record_cgp_averages(8, N, fn)
end