-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.lua
More file actions
201 lines (174 loc) · 7.57 KB
/
thread.lua
File metadata and controls
201 lines (174 loc) · 7.57 KB
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
require "love.image"
require "love.math"
local hyperneat = require "hyperneat"
local neat = require "neat/neat"
local status_channel = love.thread.getChannel("status")
local champion_neat_channel = love.thread.getChannel("champion")
status_channel:push('initializing...')
local population = {}
for i = 1, 100 do
local hyperneat_settings = {
input_res = 28,
output_count = 10
}
if false then
hyperneat_settings.genome = {
fitness = 0,
settings = { innovation_counter = 841 },
nodes = {
{ id = 1, type = "input", activation = neat.get_default_activation_by_name("identity") },
{ id = 2, type = "input", activation = neat.get_default_activation_by_name("identity") },
{ id = 3, type = "input", activation = neat.get_default_activation_by_name("identity") },
{ id = 4, type = "input", activation = neat.get_default_activation_by_name("identity") },
{ id = 5, type = "input", activation = neat.get_default_activation_by_name("identity") },
{ id = 6, type = "output", activation = neat.get_default_activation_by_name("tanh") },
{ id = 7, type = "hidden", activation = neat.get_default_activation_by_name("tanh") },
{ id = 8, type = "hidden", activation = neat.get_default_activation_by_name("cos") },
{ id = 9, type = "hidden", activation = neat.get_default_activation_by_name("sigmoid") },
},
connections = {
{ in_node = 1, out_node = 6, weight = 0.6012, innovation = 341, enabled = false },
{ in_node = 2, out_node = 6, weight = 0.1527, innovation = 342, enabled = false },
{ in_node = 3, out_node = 6, weight = 0.6256, innovation = 343, enabled = true },
{ in_node = 4, out_node = 6, weight = -1.7850, innovation = 344, enabled = false },
{ in_node = 5, out_node = 6, weight = -0.8807, innovation = 345, enabled = false },
{ in_node = 5, out_node = 7, weight = -0.3454, innovation = 672, enabled = false },
{ in_node = 7, out_node = 6, weight = -0.1346, innovation = 673, enabled = true },
{ in_node = 5, out_node = 8, weight = -0.4274, innovation = 699, enabled = true },
{ in_node = 8, out_node = 7, weight = -0.5572, innovation = 700, enabled = true },
{ in_node = 7, out_node = 8, weight = 0.1041, innovation = 839, enabled = true },
{ in_node = 1, out_node = 9, weight = -0.5523, innovation = 840, enabled = true },
{ in_node = 9, out_node = 6, weight = -1.0307, innovation = 841, enabled = true },
}
}
end
table.insert(population, hyperneat.create_2dsubstrate(hyperneat_settings))
end
local function gather_png_files(path, file_list)
local items = love.filesystem.getDirectoryItems(path)
local current_dir = path:match("([^/]+)$") or path
for _, item in ipairs(items) do
local full_path = path .. "/" .. item
local info = love.filesystem.getInfo(full_path)
if info.type == "directory" then
gather_png_files(full_path, file_list)
elseif info.type == "file" and item:match("%.png$") then
table.insert(file_list, {
path = full_path,
directory = current_dir
})
end
end
end
local all_files = {}
gather_png_files("dataset", all_files)
status_channel:push('dataset has been read')
--math.randomseed(os.time())
--for i = #all_files, 2, -1 do
-- local j = math.random(i)
-- all_files[i], all_files[j] = all_files[j], all_files[i]
--end
local function process_datasets(path)
local items = love.filesystem.getDirectoryItems(path)
local current_dir = path:match("([^/]+)$")
print(current_dir)
for _, item in ipairs(items) do
local full_path = path .. "/" .. item
local info = love.filesystem.getInfo(full_path)
if info.type == "directory" then
process_datasets(full_path)
elseif info.type == "file" and item:match("%.png$") then
-- current_dir holds the immediate parent directory name
local full_data = love.filesystem.newFileData(full_path)
local image_data = love.image.newImageData(full_data)
local w, h = image_data:getDimensions()
local inputs = {}
for y = 0, h - 1 do
for x = 0, w - 1 do
local r, g, b, a = image_data:getPixel(x, y)
table.insert(inputs, r * 2 - 1)
end
end
for _, substrate in ipairs(population) do
local results = hyperneat.evaluate(substrate, inputs)
local max = math.max(results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8], results[9], results[10])
if max > 0.70 then
if current_dir == "0" and results[1] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "1" and results[2] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "2" and results[3] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "3" and results[4] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "4" and results[5] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "5" and results[6] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "6" and results[7] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "7" and results[8] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "8" and results[9] == max then
substrate.fitness = substrate.fitness + 1
elseif current_dir == "9" and results[10] == max then
substrate.fitness = substrate.fitness + 1
end
end
end
-- image_data now contains the pixel data
-- Access pixels via image_data:getPixel(x, y)
image_data = nil
full_data = nil
end
end
end
local function train(i)
print(all_files[i].directory)
local full_data = love.filesystem.newFileData(all_files[i].path)
local image_data = love.image.newImageData(full_data)
local w, h = image_data:getDimensions()
local inputs = {}
for y = 0, h - 1 do
for x = 0, w - 1 do
local r, g, b, a = image_data:getPixel(x, y)
table.insert(inputs, r * 2 - 1)
end
end
for _, substrate in ipairs(population) do
local results = hyperneat.evaluate(substrate, inputs)
local max = math.max(results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8], results[9], results[10])
local min = math.min(results[1], results[2], results[3], results[4], results[5], results[6], results[7], results[8], results[9], results[10])
local sorted_results = {}
for _, v in ipairs(results) do
table.insert(sorted_results, v)
end
table.sort(sorted_results, function (a, b) return a > b end)
local dir_number = tonumber(all_files[i].directory)
if sorted_results[1] > 0.5 and sorted_results[2] < 0.4 and max == results[dir_number + 1] then
substrate.fitness = substrate.fitness + 1
else
substrate.fitness = substrate.fitness - 1
end
substrate.genome.fitness = substrate.fitness
end
image_data = nil
full_data = nil
end
local max_files = 10
while true do
for i = 1, math.min(max_files, #all_files) do
if max_files >= #all_files then
train(i)
else
train(math.random(1, #all_files))
end
end
neat.print_genome(population[1].genome)
if population[1].genome.fitness < 1 then
max_files = max_files - 1
end
champion_neat_channel:push(neat.purify_genome(population[1].genome))
population = hyperneat.evolve_population(population)
max_files = max_files + 1
end