-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.lua
More file actions
101 lines (87 loc) · 2.05 KB
/
misc.lua
File metadata and controls
101 lines (87 loc) · 2.05 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
function InitializePool()
print("Initialize pool...")
Pool = CreatePool()
for i = 1, Population do
AddToSpecies(BasicGenome())
end
InitializeRun()
end
function InitializeRun()
savestate.load(RestartState);
Rightmost = 0
Pool.currentFrame = 0
Timeout = TimeoutConstant
ClearJoypad()
local species = Pool.species[Pool.currentSpecies]
local genome = species.genomes[Pool.currentGenome]
GenerateNetwork(genome)
EvaluateCurrent()
end
function ClearJoypad()
Controller = {}
for b = 1, #ButtonNames do
Controller["P1 " .. ButtonNames[b]] = false
end
joypad.set(Controller)
end
function DeepCopy(obj)
if type(obj) ~= 'table' then return obj end
local res = setmetatable({}, getmetatable(obj))
for k, v in pairs(obj) do res[DeepCopy(k)] = DeepCopy(v) end
return res
end
function Split(val, delimiter)
local res = {}
local i = 1
for tmp in val:gmatch(delimiter) do
res[i] = tmp
i = i + 1
end
return res
end
function SavePool(filename)
filename = filename or forms.gettext(SaveFile)
local pool = DeepCopy(Pool)
for i = 1, #pool.species do
for k = 1, #pool.species[i].genomes do
pool.species[i].genomes[k].network = {}
end
end
Jwrap:writeToFile(pool, filename)
end
function LoadPool(filename)
print("Load file pool...")
filename = filename or forms.gettext(LoadFile)
Pool = Jwrap:readFromFile(filename)
if Pool ~= nil then
while FitnessAlreadyMeasured() do
NextGenome()
end
InitializeRun()
Pool.currentFrame = Pool.currentFrame + 1
end
end
function PlayTop()
local maxfitness = 0
local maxs, maxg
for s, species in pairs(Pool.species) do
for g, genome in pairs(species.genomes) do
if genome.fitness > maxfitness then
maxfitness = genome.fitness
maxs = s
maxg = g
end
end
end
Pool.currentSpecies = maxs
Pool.currentGenome = maxg
Pool.maxFitness = maxfitness
forms.settext(MaxFitnessLabel, "Max Fitness: " .. math.floor(Pool.maxFitness))
print("<>------- Running top genome -------<> : ")
InitializeRun()
Pool.currentFrame = Pool.currentFrame + 1
return
end
function OnExit()
forms.destroy(form)
end