-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.jl
121 lines (102 loc) · 2.16 KB
/
simulation.jl
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
module SocIntSim
import ProgressMeter
const PM = ProgressMeter
import NKLandscapes
const NK = NKLandscapes
if length(ARGS) == 0
error("No simulation config specified.")
end
simname = ARGS[1]
include("$(simname).jl")
function writeheader(stream)
write(stream, join([
"# N=$(N)",
"# K=$(K)",
"# P=$(P)",
"# G=$(G)",
"# S=$(S)",
"# E=$(E)",
"# C=$(C)",
"# M=$(M)",
"# T=$(T)",
"# W_soc=$(W_soc)",
"# W_int=$(W_int)"
], "\n"), "\n")
line = join([
"trial",
"simulationType",
"K",
"generation",
"meanFitness",
"medianFitness",
"maxFitness"
], ",")
write(stream, line, "\n")
end
function writerow(stream, trial, simtype, kval, generation, fits)
line = join([
trial,
simtype,
kval,
generation,
mean(fits),
median(fits),
maximum(fits)
], ",")
write(stream, line, "\n")
end
function runtrial(trial, stream, progress, kval)
l = NK.NKLandscape(N, kval)
p = rand(NK.Population, l, P)
# Social
sp = NK.Population(p)
for i = 1:G
fits = NK.popfits(sp)
writerow(stream, trial, "social", kval, i, fits)
NK.bwmutate!(sp, W_soc)
NK.elitesel!(sp, E)
PM.next!(progress)
end
# Intelligence
ip = NK.Population(p)
for i = 1:G
fits = NK.popfits(ip)
writerow(stream, trial, "intelligence", kval, i, fits)
for i = 1:NK.popsize(ip)
g = ip.genotypes[i]
choice = g
for _ = 1:C
option = NK.bwmutate(g, W_int)
if NK.fitness(option) > NK.fitness(choice)
choice = option
end
end
ip.genotypes[i] = choice
end
if S == :M
NK.moransel!(ip, M)
elseif S == :P
NK.propsel!(ip)
elseif S == :T
NK.tournsel!(ip, 2) # Use tournament size 2 for now
elseif S == :N
continue
else
error("Illegal selection type: ", S)
end
PM.next!(progress)
end
end
# Run the simulation
stream = open("$(simname).csv", "w")
kvals = collect(K)
progress = PM.Progress(T * G * 2 * length(kvals), 1, "Running...", 40)
writeheader(stream)
for kval = kvals
for trial = 1:T
runtrial(trial, stream, progress, kval)
flush(stream)
end
end
close(stream)
end