-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitness-continuous-auto
205 lines (159 loc) · 6.21 KB
/
fitness-continuous-auto
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
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
from matplotlib.lines import Line2D
from matplotlib.patches import Circle
import pickle
from os import makedirs, path
import os
import sys
from automatic_plot_helper import detect_all_isings
from automatic_plot_helper import load_settings
#stuff for detect_isings
from os import listdir
from os.path import isfile, join
'''
loadfiles = ['beta_experiment/beta-0-1/sim-20180512-105719',
'beta_experiment/beta-1/sim-20180511-163319',
'beta_experiment/beta-10/sim-20180512-105824']
'''
loadfile = sys.argv[1]
loadfiles = [loadfile]
settings = load_settings(loadfile)
#loadfiles = ['sim-20191114-000009_server']
energy_model = settings['energy_model']
numAgents = settings['pop_size']
#iter_list = np.arange(0, 2000, 1)
iter_list = detect_all_isings(loadfile)
autoLoad = True
saveFigBool = True
fixGen2000 = False
# IC = [0, 0, 1, 1, 1, 1, 2, 2]
new_order = [2, 0, 1]
labels = [r'$\beta_i = 0.1$', r'$\beta_i = 1$', r'$\_i = 10$']
cmap = plt.get_cmap('seismic')
norm = colors.Normalize(vmin=0, vmax=len(loadfiles)) # age/color mapping
# norm = [[194, 48, 32, 255],
# [146, 49, 182, 255],
# [44, 112, 147, 255]
# ]
# norm = np.divide(norm, 255)
a = 0.15 # alpha
def upper_tri_masking(A):
m = A.shape[0]
r = np.arange(m)
mask = r[:, None] < r
return A[mask]
def fitness(loadfile, iter_list, ising_list, numAgents, autoLoad, saveFigBool):
folder = 'save/' + loadfile
folder2 = folder + '/figs/fitness/'
fname2 = folder2 + 'fitness-' + \
str(iter_list[0]) + '-' + str(iter_list[1] - iter_list[0]) + '-' + str(iter_list[-1]) + \
'.npz'
if path.isfile(fname2) and autoLoad:
txt = 'Loading: ' + fname2
print(txt)
data = np.load(fname2)
FOOD = data['FOOD']
elif not ising_list is None:
FOOD = np.zeros((len(iter_list), numAgents))
for ii, isings in ising_list:
food = []
for i, I in isings:
else:
FOOD = np.zeros((len(iter_list), numAgents))
for ii, iter in enumerate(iter_list):
filename = 'save/' + loadfile + '/isings/gen[' + str(iter) + ']-isings.pickle'
startstr = 'Loading simulation:' + filename
print(startstr)
try:
isings = pickle.load(open(filename, 'rb'))
except Exception:
print("Error while loading %s. Skipped file" % filename)
#Leads to the previous datapoint being drawn twice!!
food = []
for i, I in enumerate(isings):
if energy_model:
food.append(I.energy)
else:
food.append(I.fitness)
# food = np.divide(food, 6)
FOOD[ii, :] = food
if not path.exists(folder2):
makedirs(folder2)
np.savez(fname2, FOOD=FOOD)
return FOOD
FOODS = []
for loadfile in loadfiles:
f = fitness(loadfile, iter_list, numAgents, autoLoad, saveFigBool)
# FIX THE DOUBLE COUNTING PROBLEM
if f.shape[0] > 2000 and fixGen2000:
print('Fixing Double Counting at Gen 2000')
f[2000, :] = f[2000, :] - f[1999, :]
FOODS.append(f)
# FIX THE DOUBLE COUNTING OF THE FITNESS
plt.rc('text', usetex=True)
font = {'family': 'serif', 'size': 28, 'serif': ['computer modern roman']}
plt.rc('font', **font)
plt.rc('legend', **{'fontsize': 20})
fig, ax = plt.subplots(1, 1, figsize=(19, 10))
fig.text(0.51, 0.035, r'$Generation$', ha='center', fontsize=20)
# fig.text(0.07, 0.5, r'$Avg. Food Consumed$', va='center', rotation='vertical', fontsize=20)
fig.text(0.07, 0.5, r'$Food Consumed$', va='center', rotation='vertical', fontsize=20)
title = 'Food consumed per organism'
fig.suptitle(title)
for i, FOOD in enumerate(FOODS):
# for i in range(0, numAgents):
# ax.scatter(iter_list, FOOD[:, i], color=[0, 0, 0], alpha=0.2, s=30)
c = cmap(norm(new_order[i]))
# c = norm[i]
# c = norm[IC[i]]
muF = np.mean(FOOD, axis=1)
ax.plot(iter_list, muF, color=c, label=labels[new_order[i]])
# for numOrg in range(FOOD.shape[1]):
# ax.scatter(iter_list, FOOD[:, numOrg],
# alpha=0.01, s=8, color=c, label=labels[new_order[i]])
# maxF = np.max(FOOD, axis=1)
# minF = np.min(FOOD, axis=1)
# ax.fill_between(iter_list, maxF, minF,
# color=np.divide(c, 2), alpha=a)
sigmaF = FOOD.std(axis=1)
ax.fill_between(iter_list, muF + sigmaF, muF - sigmaF,
color=c, alpha=a
)
custom_legend = [Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(1)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(0)), markersize=15),
Line2D([0], [0], marker='o', color='w',
markerfacecolor=cmap(norm(2)), markersize=15),]
# custom_legend = [Circle((0, 0), 0.001,
# facecolor=cmap(norm(1))),
# Circle((0, 0), 1,
# facecolor=cmap(norm(0))),
# Circle((0, 0), 1,
# facecolor=cmap(norm(2)))]
ax.legend(custom_legend, [r'$\beta = 10$', r'$\beta = 1$', r'$\beta = 0.1$'], loc='upper left')
# plt.legend(loc=2)
# yticks = np.arange(0, 150, 20)
# ax.set_yticks(yticks)
# xticks = [0.1, 0.5, 1, 2, 4, 10, 50, 100, 200, 500, 1000, 2000]
# ax.set_xscale("log", nonposx='clip')
# ax.set_xticks(xticks)
# ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
folder = 'save/' + loadfile
savefolder = folder + '/figs/fitness_combined/'
savefilename = savefolder + 'fitness_gen_' + str(iter_list[0]) + '-' + str(iter_list[-1]) + '.png'
if not path.exists(savefolder):
makedirs(savefolder)
if saveFigBool:
plt.savefig(savefilename, bbox_inches='tight', dpi=150)
# plt.close()
savemsg = 'Saving ' + savefilename
print(savemsg)
# if saveFigBool:
# savefolder = folder + '/figs/fitness/'
# savefilename = savefolder + 'fitness_gen_' + str(iter_list[0]) + '-' + str(iter_list[-1]) + '.png'
# plt.savefig(bbox_inches = 'tight', dpi = 300)
plt.show()