-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathworker_closed.jl
More file actions
91 lines (81 loc) · 1.98 KB
/
worker_closed.jl
File metadata and controls
91 lines (81 loc) · 1.98 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
using Cxx
using Libdl
using LinearAlgebra
using StatsBase
using Colors
using Random
using BSON: @save, @load
include("pancakerobot.jl")
const ID = parse(Int, ARGS[1])
const ARG_ENV = parse(Int, ARGS[2])
const GENERATIONS = 200
const RATE = [90/10, MAX_PRESS/10, 9/10]
const DIR = "genomes_closed_$(ARG_ENV)"
RNG = Random.MersenneTwister(ID*10000)
function wrap(array, low, high)
arr = deepcopy(array)
for i in 1:size(arr)[1]
for j in 1:size(arr)[2]
if arr[i, j] < low
arr[i, j] = (high + arr[i, j]) + 1
#arr[i, j] = low
end
if arr[i, j] > high
arr[i, j] = (arr[i, j] - high) - 1
#arr[i, j] = high
end
end
end
return Int64.(arr)
end
function new_genome()
global RNG
genome = nothing
if ARG_ENV == 0
genome = [0, MAX_PRESS, rand(RNG, 0:8, 10, 2)]
elseif ARG_ENV == 15
genome = [90, 0, rand(RNG, 0:8, 10, 2)]
end
return genome
end
function mutate_genome(genome)
global RNG
newg = deepcopy(genome)
gene = round.(RATE[3] .* randn(RNG, 10, 2) .+ genome[3])
gene = wrap(gene, 0, 8)
newg[3] = gene
return newg
end
results = nothing
gen = nothing
worker = nothing
fit = nothing
if isfile("$DIR/worker_$(lpad(ID, 2, "0"))_results.bson")
global results, gen, worker, fit, RNG
@load "$DIR/worker_$(lpad(ID, 2, "0"))_results.bson" results
gen, worker, fit, RNG = results[end]
else
global results, gen, worker, fit, RNG
gen = 0
worker = new_genome()
fit, _ = fitness(worker, ARG_ENV)
results = [(gen, worker, fit, deepcopy(RNG))]
@save "$DIR/worker_$(lpad(ID, 2, "0"))_results.bson" results
f = open("$DIR/worker_$(lpad(ID, 2, "0"))_best.txt", "w")
write(f, "$fit")
close(f)
end
for g in gen+1:GENERATIONS
global results, worker, fit
spawn = mutate_genome(worker)
spawn_fit, _ = fitness(spawn, ARG_ENV)
if spawn_fit > fit
worker = spawn
fit = spawn_fit
end
push!(results, (g, worker, fit, deepcopy(RNG)))
@save "$DIR/worker_$(lpad(ID, 2, "0"))_results.bson" results
f = open("$DIR/worker_$(lpad(ID, 2, "0"))_best.txt", "w")
write(f, "$fit")
close(f)
end