Repository navigation
Expand file tree
/
Copy pathgpUtils.lua
More file actions
164 lines (153 loc) · 3.96 KB
/
Copy pathgpUtils.lua
File metadata and controls
164 lines (153 loc) · 3.96 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
----------------------------------------------------------------------
--
-- Deep Genetic Programming: Reifying an AI researcher.
--
-- Main script file
-- * Parsing options
-- * Loading datas
-- * Initializing
-- * Launching
--
----------------------------------------------------------------------
require 'nngraph'
require 'graph'
require 'nn'
--require 'cunn'
-- Sleep function
function sleep(n)
os.execute("sleep " .. tonumber(n))
end
-- Performs a recursive copy of a table
function deepcopy(object)
local lookup_table = {}
-- Main function
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
-- Pretty print for a table (with indent)
function print_table(theTable, indent)
local iString = ""
for index = 1, indent do
iString = iString .. "-"
end
-- walk all the topmost values in the table
for k,v in pairs(theTable) do
print(iString ,k ,v)
if type(v) == "table" then
print_table(v, indent + 1)
end
end
end
-- Print a complete network
function print_network(theProgram)
print("Network:");
for counter = 1, table.getn(theProgram) do
print(theProgram[counter].name);
if counter < table.getn(theProgram) then
print(' | ');
print(' v ');
end
end
print("Program Stack Top")
end
function writeFile(filename)
local file = io.open(filename, "w")
file:write(pool.generation .. "\n")
file:write(pool.maxFitness .. "\n")
file:write(#pool.species .. "\n")
for n,species in pairs(pool.species) do
file:write(species.topFitness .. "\n")
file:write(species.staleness .. "\n")
file:write(#species.genomes .. "\n")
for m,genome in pairs(species.genomes) do
file:write(genome.fitness .. "\n")
file:write(genome.maxneuron .. "\n")
for mutation,rate in pairs(genome.mutationRates) do
file:write(mutation .. "\n")
file:write(rate .. "\n")
end
file:write("done\n")
file:write(#genome.genes .. "\n")
for l,gene in pairs(genome.genes) do
file:write(gene.into .. " ")
file:write(gene.out .. " ")
file:write(gene.weight .. " ")
file:write(gene.innovation .. " ")
if(gene.enabled) then
file:write("1\n")
else
file:write("0\n")
end
end
end
end
file:close()
end
function savePool()
local filename = forms.gettext(saveLoadFile)
writeFile(filename)
end
-- Print a genome
function printGenome(genome)
-- First an old-school print
local cells = {}
local i = 1
local cell = {}
print('Modules');
for n,neuron in pairs(genome.neurons) do
print('Id:'..n..' - '..neuron.module.name);
end
print('Connections');
for _,gene in pairs(genome.genes) do
if gene.enabled then
print(' '..gene.out..' -> '..gene.into);
end
end
for mutation,rate in pairs(genome.mutationRates) do
print(' m:'..mutation..' = '..rate);
end
end
-- Display a genome
function displayGenome(genome, idx)
-- First an old-school print
local cells = {}
local i = 1
local cell = {}
print('Modules');
for n,neuron in pairs(genome.neurons) do
print('Id:'..n..' - '..neuron.module.name);
end
print('Connections');
for _,gene in pairs(genome.genes) do
if gene.enabled then
print(' '..gene.out..' -> '..gene.into);
else
print(' '..gene.out..' /> '..gene.into);
end
end
for mutation,rate in pairs(genome.mutationRates) do
print(' m:'..mutation..' = '..rate);
end
-- Now a fancy print
local g = genome.model;
g:float();
indata = torch.rand(ninputs):float();
tmpInData = {};
for i = 1,#genome.neurons[1].output do
table.insert(tmpInData, indata);
end
g:forward(indata);
--graph.dot(g.fg, 'Forward Graph', '/tmp/fg_'..idx);
end