-
Notifications
You must be signed in to change notification settings - Fork 1
/
species.lua
523 lines (492 loc) · 16.6 KB
/
species.lua
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
local lfs = require 'lfs'
local zlib = require 'zlib'
local filelock = require 'ipc.filelock'
local File_Name_Pattern = '^(%a+)%.specie%.gz$'
local function specieFileNameToName(file_name)
return string.match(file_name, File_Name_Pattern)
end
local function specieNameToFileName(name)
return tostring(name)..'.specie.gz'
end
local ENOENT = 2
local ffi = require 'ffi'
ffi.cdef[[
int kill(unsigned int pid, int sig);
typedef struct {
char *fpos;
void *base;
unsigned short handle;
short flags;
short unget;
unsigned long alloc;
unsigned short buffincrement;
} FILE;
int fileno(FILE* stream);
int ftruncate(int fd, unsigned int length);
int truncate(const char* path, unsigned int length);
unsigned int sleep(unsigned int seconds);
char* strerror(int errnum);
]]
local function ftruncate(file, size)
local fd = ffi.C.fileno(file)
if (not fd) or (fd == -1) then
local errno = ffi.errno
return nil, ffi.string( ffi.C.strerror(errno) ), errno
end
if not size then
local reason, errno
size, reason, errno = file:seek()
if not size then return nil, reason, errno end
end
local result = ffi.C.ftruncate(fd, size)
if (not result) or (result ~= 0) then
local errno = ffi.errno
return nil, ffi.string( ffi.C.strerror(errno) ), errno
end
return size
end -- function ftruncate
local Species = {}
function Species.findSpeciesFiles(dir_path, limit)
dir_path = dir_path or '.'
-- Search filenames for given security name
local files = {}
for file_name in lfs.dir(dir_path) do
local name = specieFileNameToName(file_name)
if name then
if (dir_path ~= '.') then
file_name = dir_path .. '/' .. file_name
end
table.insert(files, file_name)
if limit and (#files >= limit) then break end
end
end
if #files <= 0 then return nil, 'Species files not found!' end
table.sort(files)
return files
end -- function Species.findSpeciesFiles
local Entity_Fields = {
'code', 'exec', 'text', 'rate', 'annual_rate', 'annual_rate_std', 'max_drawdown_percent',
'profit_factor', 'best_duration', 'max_proto_corr', 'signal_pos', 'signal_neg', 'serial'
}
local Specie_Fields = {
'name', 'dataset_last_time', 'last_iteration_time', 'iterations_count',
'epochs_count', 'update_count', 'last_serial',
}
local Info_Fields = { 'name', 'best_score',
'best_code', 'best_exec', 'best_text', 'best_rate', 'annual_rate', 'annual_rate_std',
'max_drawdown_percent', 'profit_factor', 'best_duration', 'max_proto_corr', 'signal_pos', 'signal_neg',
'best_serial', 'last_serial', 'dataset_last_time', 'last_iteration_time', 'iterations_count',
'epochs_count', 'update_count',
}
local function writeField(file, tab, field, indent)
local result, reason, errno
indent = indent or ''
local v = tab[field]
local t = type(v)
if (t == 'string') then
result, reason, errno = file:write(indent, field, '=\'', v, '\',\r\n')
if not result then return nil, reason, errno end
elseif (t == 'number') and (field:sub(-5) == '_time') then
result, reason, errno = file:write(indent, field, '=', tostring(v), ', -- ',
os.date('%Y-%m-%d %H:%M:%S', v), '\r\n')
if not result then return nil, reason, errno end
elseif (t ~= 'nil') then
result, reason, errno = file:write(indent, field, '=', tostring(v), ',\r\n')
if not result then return nil, reason, errno end
end
return true
end
local function specieExport(self, file_name)
file_name = file_name or (self.name..'.export.dat')
local file = assert( io.open(file_name, 'w') )
assert( file:write('return {\r\n') )
for _, entity in ipairs(self) do
assert((type(entity) == 'table') and (type(entity.code) == 'string'),
'speciesExport: code not found in entity!')
assert( file:write(' {\r\n') )
for _, field in ipairs(Entity_Fields) do
assert( writeField(file, entity, field, ' ') )
end
file:write(' },\r\n')
end -- for population
for _, field in ipairs(Specie_Fields) do
assert( writeField(file, self, field, ' ') )
end
assert( file:write('}\r\n') )
file:close()
return true
end -- function specieExport
local function specieSave(self, file, format)
format = format or 'binary'
local result, reason, errno
-- Seek to the beginning of file
result, reason, errno = file:seek('set', 0)
if not result then return nil, reason, errno end
-- Serialize specie (i.e. self)
local inflated
result, inflated = pcall( torch.serialize, self, format )
if not result then return nil, inflated end
-- Get zlib stream to create .gz compatible file
local stream
result, stream = pcall( zlib.deflate, 6, 15+16 )
if not result then return nil, stream end
-- Compress data
local deflated
result, deflated = pcall( stream, inflated, 'finish' )
if not result then return nil, deflated end
inflated = nil
-- Write compressed data to file
result, reason, errno = file:write(deflated)
if not result then return nil, reason, errno end
return ftruncate(file)
end -- function specieSave
function Species.importSpecieFromFile(file_name)
local status, specie = pcall(dofile, file_name)
if not status then return nil, result end
if (type(specie) ~= 'table') then return nil, 'Invalid content in specie file' end
if (type(specie.name) ~= 'string') then
local name = file_name:match('specie%-(%a+)%.dat$')
specie.name = name
end
-- Update metatable
local mt = getmetatable(specie) or {}
local __index = mt.__index
if type(__index) ~= 'table' then __index = {}; mt.__index = __index end
__index.close = function() end
__index.save = function(self)
local file_name = specieNameToFileName(specie.name)
local file, reason, errno = io.open(file_name, 'r+b')
if not file then return nil, reason, errno end
local result
result, reason, errno = specieSave(self, file, 'binary')
file:close()
return result, reason, errno
end
__index.export = specieExport
return setmetatable(specie, mt)
end -- function Species.importSpecieFromFile
function Species.findAndLoadSpecies()
local files, reason = Species.findSpeciesFiles()
if not files then return nil, reason end
local result = {}
for _, file_name in ipairs(files) do
local specie
print('Loading specie from '..tostring(file_name)..'...')
specie, reason = Species.importSpecieFromFile(file_name)
if specie then
table.insert(result, specie)
else
print(' - '..tostring(reason))
end
end -- for files
if #result <= 0 then return nil, 'Failed to load any file!' end
return result
end -- function Species.findAndLoadSpecies
local function indexSort(self)
table.sort(self, function(a, b)
local ar, br = (a.annual_rate or -math.huge), (b.annual_rate or -math.huge)
if (ar*br < 0) and (ar > br) then
return true
elseif (ar*br < 0) and (ar < br) then
return false
elseif (a.best_score or -math.huge ) > (b.best_score or -math.huge) then
return true
elseif (a.best_score or -math.huge ) < (b.best_score or -math.huge) then
return false
else
return (a.epochs_count or 0) > (b.epochs_count or 0)
end
end)
end
local function indexSave(self, file)
local result, reason, errno
-- Seek to the beginning of file
result, reason, errno = file:seek('set', 0)
if not result then return nil, reason, errno end
-- Write index
result, reason, errno = file:write('return {\r\n')
if not result then return nil, reason, errno end
-- Sort species info
indexSort(self)
-- Cycle through species info blocks
for _, info in ipairs(self) do
if (type(info) == 'table')
and (type(info.name) == 'string') then
--and (type(info.best_code) == 'string') then
result, reason, errno = file:write(' {\r\n')
if not result then return nil, reason, errno end
for _, field in ipairs(Info_Fields) do
result, reason, errno = writeField(file, info, field, ' ')
if not result then return nil, reason, errno end
end
result, reason, errno = file:write(' },\r\n')
if not result then return nil, reason, errno end
end
end
result, reason, errno = file:write('}\r\n')
if not result then return nil, reason, errno end
return ftruncate(file)
end -- indexSave
local function indexUpdate(self, name, new_info)
for i, info in ipairs(self) do
if (info.name == name) then
self[i] = new_info
return
end
end
table.insert(self, new_info)
end -- function indexUpdate
function Species.getSpecieInfo(specie)
if (type(specie) ~= 'table')
or (type(specie.name) ~= 'string')
or (type(specie.iterations_count) ~= 'number')
then return nil end
local info = {}
info.name = specie.name
info.last_serial = specie.last_serial
info.dataset_last_time = specie.dataset_last_time
info.last_iteration_time = specie.last_iteration_time
info.iterations_count = specie.iterations_count
info.epochs_count = specie.epochs_count
info.update_count = specie.update_count
local entity = specie[1]
if (type(entity) == 'table') then
info.best_code = entity.code
info.best_exec = entity.exec
info.best_text = entity.text
info.best_score = entity.score
info.best_rate = entity.rate
info.annual_rate = entity.annual_rate
info.annual_rate_std = entity.annual_rate_std
info.max_drawdown_percent = entity.max_drawdown_percent
info.profit_factor = entity.profit_factor
info.best_duration = entity.best_duration
info.max_proto_corr = entity.max_proto_corr
info.signal_pos = entity.signal_pos
info.signal_neg = entity.signal_neg
info.best_serial = entity.serial
end
return info
end -- function Species.getSpecieInfo
function Species.getIndex(dir_path)
dir_path = dir_path or '.'
local index_file_name = dir_path..'/index.dat'
-- Open index file for read and update
local result, reason, errno
local file
file, reason, errno = io.open(index_file_name, 'r+')
if not file then
if (errno ~= ENOENT) then return nil, reason, errno end
-- Create new file if it doesn't exist
file, reason, errno = io.open(index_file_name, 'w')
if not file then return nil, reason, errno end
end
-- Lock file
result, reason, errno = filelock.lock(file, 'w')
if not result then return nil, reason, errno end
-- Prepare close function
local function indexClose()
filelock.unlock(file)
file:close()
file = nil
end
-- Read index file
local index
local text
text, reason, errno = file:read('*a')
if text then
local f
f, reason = loadstring(text)
if f then
-- Safely load index with pcall
result, index = pcall( f )
end
end
-- Check if index was loaded or construct empty table otherwise
if (type(index) ~= 'table') then index = {} end
-- Get names from loaded index
local names_from_index = {}
for i = #index, 1, -1 do
local info = index[i]
if info.name then
names_from_index[info.name] = i
end
end
-- Find all species files
local file_names
file_names, reason = Species.findSpeciesFiles(dir_path)
if (not file_names) then indexClose(); return nil, reason end
-- Get names from file_names
local names_from_files = {}
for _, file_name in ipairs(file_names) do
local name = specieFileNameToName(file_name)
if name then names_from_files[name] = file_name end
end
local f_save_index
-- Check invalid (non-existing) records in index
for i = #index, 1, -1 do
local info = index[i]
if not names_from_files[info.name] then
table.remove(i)
f_save_index = true
end
end
-- Remove non-numeric indexes from index
for k, info in pairs(index) do
if type(k) ~= 'number' then
index[k] = nil
f_save_index = true
end
end
-- Check missing records in index
for name, file_name in pairs(names_from_files) do
if not names_from_index[name] then
local specie = Species.openSpecie(name)
if specie then
local info = Species.getSpecieInfo(specie)
if info then
info.name = name
table.insert(index, info)
f_save_index = true
end
specie:close()
end
end
end
-- Save file if modified index
if f_save_index then
indexSave(index, file)
end
-- Update metatable
local mt = getmetatable(index) or {}
local __index = mt.__index
if type(__index) ~= 'table' then __index = {}; mt.__index = __index end
__index.update = indexUpdate
__index.sort = indexSort
__index.close = indexClose
__index.save = function(self) return indexSave(self, file) end
return setmetatable(index, mt)
end -- function Species.getIndex
function Species.createSpecie(name, format)
format = format or 'binary'
local result, reason, errno
-- Create new file
local file
local file_name = specieNameToFileName(name)
file, reason, errno = io.open(file_name, 'w+b')
if not file then return nil, reason, errno end
-- Lock file
result, reason, errno = lfs.lock(file, 'w')
if not result then file:close(); return nil, reason, errno end
-- Prepare close function
local function specieClose()
lfs.unlock(file)
file:close()
file = nil
end
local specie = { name = name }
-- Update metatable
local mt = getmetatable(specie) or {}
local __index = mt.__index
if type(__index) ~= 'table' then __index = {}; mt.__index = __index end
__index.close = specieClose
__index.save = function(self) return specieSave(self, file, format) end
__index.export = specieExport
return setmetatable(specie, mt)
end -- function Species.createSpecie
function Species.saveSpecie(specie, format)
format = format or 'binary'
assert(type(specie) == 'table' and type(specie.name) == 'string',
'Species.saveSpecie: Invalid specie')
local result, reason, errno
-- Create new file
local file
local file_name = specieNameToFileName(specie.name)
file, reason, errno = io.open(file_name, 'w+b')
if not file then return nil, reason, errno end
-- Lock file
result, reason, errno = lfs.lock(file, 'r')
if not result then file:close(); return nil, reason, errno end
-- Write specie to file
result, reason, errno = specieSave(specie, file, format)
lfs.unlock(file)
file:close()
return result, reason, errno
end -- function Species.saveSpecie
function Species.openSpecie(name, format)
format = format or 'binary'
local result, reason, errno
-- Open existing file for read and update
local file
local file_name = specieNameToFileName(name)
file, reason, errno = io.open(file_name, 'r+b')
if not file then return nil, reason, errno end
-- Lock file
result, reason, errno = lfs.lock(file, 'w')
if not result then file:close(); return nil, reason, errno end
-- Prepare close function
local function specieClose()
lfs.unlock(file)
file:close()
file = nil
end
-- Read specie file
local deflated
deflated, reason, errno = file:read('*a')
if not deflated then specieClose(); return nil, reason, errno end
-- Get zlib stream to decompress .gz file
local stream
result, stream = pcall( zlib.inflate )
if not result then return nil, stream end
-- Decompress data
local inflated
result, inflated = pcall( stream, deflated, 'finish' )
if not result then return nil, inflated end
deflated = nil
-- Deserialize specie
local specie
result, specie = pcall( torch.deserialize, inflated, format )
if (not result) or (type(specie) ~= 'table') then specieClose(); return nil, specie end
-- Update metatable
local mt = getmetatable(specie) or {}
local __index = mt.__index
if type(__index) ~= 'table' then __index = {}; mt.__index = __index end
__index.close = specieClose
__index.save = function(self) return specieSave(self, file, format) end
__index.export = specieExport
return setmetatable(specie, mt)
end -- function Species.openSpecie
--function Species.appendEntityToFile(entity, file_name)
-- assert(type(entity) == 'table' and type(entity.code) == 'string',
-- 'exportPrototype: Incomplete entity specified!')
-- local file, pos, str, idx, res
-- file = io.open(file_name, 'r+')
-- if file then
-- pos = file:seek('end', 0)
-- if pos then
-- pos = file:seek('cur', -10)
-- if pos then
-- str = file:read('*a')
-- assert(str, 'Failed to read last 10 characters of '..tostring(file_name))
-- idx = str:find('}%s*}%s*$')
-- if idx then
-- pos = file:seek('cur', -10 + idx)
-- if pos then
-- res = file:write(',\r\n')
-- end
-- end -- if found two '}'
-- end -- if seeked to the (end-10)
-- end -- if seeked to the end
-- end -- if opened existing file
-- if not file then
-- file = assert( io.open(file_name, 'w') )
-- end
-- if not res then
-- file:seek('set', 0)
-- file:write('return {\r\n')
-- end
-- writeEntityInfo(entity, file)
-- file:write('\r\n}\r\n')
-- file:close()
--end -- function function Species.appendEntityToFile
return Species