-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
61 lines (51 loc) · 1.84 KB
/
main.lua
File metadata and controls
61 lines (51 loc) · 1.84 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
local thread = love.thread.newThread("trainer_manager.lua")
local neat = require("neat/neat")
local hyperneat = require("hyperneat")
local status_channel = love.thread.getChannel("status")
local champion_neat_channel = love.thread.getChannel("champion")
function love.load()
thread:start()
end
local last_status = nil
local last_champion = nil
local last_viz = nil
function love.draw()
if last_champion then neat.draw_node_connections(last_champion) end
if last_viz then love.graphics.draw(last_viz, 0, 100) end
if last_status then love.graphics.print(last_status,0, 0) end
end
function love.keypressed(key, scancode)
if scancode == "e" then
end
end
function love.update(dt)
local new_status = status_channel:pop()
if new_status then last_status = new_status end
local new_champion = champion_neat_channel:pop()
if new_champion then last_champion = new_champion end
if new_champion then
local substrate = hyperneat.create_2dsubstrate({
input_res = 64,
output_count = 10,
weight_range = 3.0,
genome = new_champion
})
local cols, rows = hyperneat.get_output_layout(substrate)
-- Create pixel buffer
local imageData = love.image.newImageData(cols * substrate.settings.input_res, rows * substrate.settings.input_res)
for j, o in ipairs(substrate.outputs) do
local col = (j - 1) % cols
local row = math.floor((j - 1) / cols)
for i, input in ipairs(substrate.inputs) do
local w = (substrate.weights[i][j] + 1) * 0.5
imageData:setPixel(
col * substrate.settings.input_res + (input.x + 1) / 2 * (substrate.settings.input_res - 1 ),
row * substrate.settings.input_res + (input.y + 1) / 2 * (substrate.settings.input_res - 1 ),
w, w, w, 1.0
)
end
end
-- Upload to GPU
last_viz = love.graphics.newImage(imageData)
end
end