-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.py
90 lines (64 loc) · 9.64 KB
/
util.py
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
from constants import *
import numpy as np
import matplotlib.pyplot as plt
def in_margins(pos):
out_of_board = [(coord > -MARGIN and coord < (BOARD_SIZE + MARGIN)) for coord in pos]
inner_board = [(coord > -INNER_MARGIN and coord < (BOARD_SIZE + INNER_MARGIN)) for coord in pos]
return all(out_of_board) and not all(inner_board)
def list_from_pairs(pairs):
return [item for pair in pairs for item in pair]
def pairs_from_list(lst):
return [(lst[2*i], lst[2*i + 1]) for i in range(len(lst) // 2)]
def gen_board_location(b_size):
return tuple(np.random.randint(0, b_size, size=2))
def gen_location():
while True:
x, y = tuple(np.random.randint(BOARD_SIZE + 2*MARGIN, size=2) - MARGIN)
"""
#y = np.random.randint(BOARD_SIZE + 2*MARGIN, size=1)[0] - MARGIN
if np.random.randint(2, size=1)[0]:
y += BOARD_SIZE + INNER_MARGIN + MARGIN
if np.random.randint(2, size=1)[0]:
x, y = y, x
"""
if in_margins((x, y)):
return x, y
def lst_1D_to_2D(l, n):
return [l[i:i + n] for i in range(0, len(l), n)]
def lst_2D_2_1D(sqr):
"""
This function takes 2D array and transforms it to 1D array
:param sqr: the 2D array that should be changed
:return: the 1D transformed array
"""
if type(sqr[0]) == int:
return sqr
l = []
for i in range(len(sqr)):
for j in range(len(sqr[i])):
l.append(sqr[i][j])
return l
def check_in_margins():
for i in range(-23, 53):
s = ""
for j in range(-23, 53):
s += str(in_margins((i, j)))[0]
print(s)
def plot_fitness():
lst_1 = [0.33499999999999996, 0.37, 0.37, 0.37, 0.37, 0.37, 0.37, 0.37, 0.37, 0.37, 0.37, 0.51, 0.51, 0.51, 0.51, 0.51, 0.5449999999999999, 0.5449999999999999, 0.5449999999999999, 0.5449999999999999, 1.055, 1.055, 1.165, 1.165, 1.165, 1.165, 1.165, 1.165, 1.165, 1.165, 1.165, 1.26, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.29, 1.625, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.665, 1.6749999999999998, 1.6749999999999998, 1.6749999999999998, 1.6749999999999998, 1.6749999999999998, 1.6749999999999998, 1.875, 1.875, 1.875, 1.875, 1.875, 1.915, 1.915, 1.915, 1.915, 1.915, 1.915, 1.915, 1.915, 2.02, 2.02, 2.02, 2.125, 2.13, 2.13, 2.13, 2.13, 2.13, 2.13, 2.13, 2.13, 2.13, 2.27, 2.27, 2.27, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.295, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.41, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.45, 2.485, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.4899999999999998, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.495, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.595, 2.595, 2.595, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9]
lst_2 = [1.27, 1.27, 1.27, 1.27, 1.605, 1.85, 1.85, 1.85, 1.85, 1.85, 1.85, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 1.9700000000000002, 2.07, 2.07, 2.07, 2.07, 2.07, 2.11, 2.11, 2.11, 2.11, 2.11, 2.11, 2.11, 2.155, 2.155, 2.155, 2.155, 2.155, 2.155, 2.16, 2.225, 2.225, 2.225, 2.225, 2.225, 2.3649999999999998, 2.3649999999999998, 2.3649999999999998, 2.3649999999999998, 2.3649999999999998, 2.3649999999999998, 2.3649999999999998, 2.47, 2.47, 2.47, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.555, 2.595, 2.595, 2.595, 2.595, 2.595, 2.595, 2.595, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9, 2.9]
lst_3 = [0.9363867684478372, 0.9363867684478372, 1.338676844783715, 1.3811704834605598, 1.4180661577608142, 1.453435114503817, 1.4643765903307888, 1.4824427480916031, 1.5244274809160305, 1.556234096692112, 1.5844783715012722, 1.6381679389312978, 1.6381679389312978, 1.644529262086514, 1.7376590330788804, 1.7376590330788804, 1.7944020356234096, 2.088295165394402, 2.1748091603053434, 2.1748091603053434, 2.1748091603053434, 2.2966921119592874, 2.2966921119592874, 2.329007633587786, 2.329262086513995, 2.374300254452926, 2.374300254452926, 2.374300254452926, 2.374300254452926, 2.397201017811705, 2.425445292620865, 2.425445292620865, 2.440712468193384, 2.4804071246819337, 2.4804071246819337, 2.4804071246819337, 2.4804071246819337, 2.9]
lst_4 = [1.3208677685950414, 1.45, 1.45, 1.4787190082644628, 1.4787190082644628, 1.493595041322314, 1.493595041322314, 1.4985537190082645, 1.4985537190082645, 1.503512396694215, 1.5382231404958677, 1.5382231404958677, 1.5382231404958677, 1.56301652892562, 1.5764462809917357, 1.5764462809917357, 1.5764462809917357, 1.5768595041322315, 1.5768595041322315, 1.5768595041322315, 1.5820247933884297, 1.5820247933884297, 1.5820247933884297, 1.5820247933884297, 1.5880165289256198, 1.6012396694214877, 1.6012396694214877, 1.6012396694214877, 1.6012396694214877, 1.6035123966942149, 1.6035123966942149, 1.6035123966942149, 1.6047520661157024, 1.6047520661157024, 1.6047520661157024, 1.6047520661157024, 1.6047520661157024, 1.606818181818182, 1.606818181818182, 1.606818181818182, 1.606818181818182, 1.606818181818182, 1.606818181818182, 1.6086776859504133, 1.6086776859504133, 1.6152892561983472, 1.6171487603305785, 1.6171487603305785, 1.6171487603305785, 1.6299586776859505, 1.6301652892561984, 1.6415289256198347, 1.6415289256198347, 1.6415289256198347, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281, 1.64400826446281]
lst = [0.3095419847328244, 0.3095419847328244, 0.3189567430025445, 0.3583969465648855, 0.3583969465648855,
0.37697201017811705, 0.3846055979643766, 0.4632315521628499, 0.4632315521628499, 0.4707379134860051,
0.5170483460559796, 0.5170483460559796, 0.5170483460559796, 0.6030534351145038, 0.6470737913486005,
0.7020356234096692, 0.7020356234096692, 0.703944020356234, 0.703944020356234, 0.703944020356234,
0.7159033078880407, 0.7159033078880407, 0.7159033078880407, 0.7159033078880407, 0.733587786259542,
0.733587786259542, 0.733587786259542, 0.733587786259542, 0.733587786259542, 0.733587786259542,
0.733587786259542, 0.733587786259542, 0.733587786259542, 0.733587786259542, 1.0, 1.0]
plt.plot(range(len(lst)), lst)
plt.xlabel("Generation", fontsize=14, fontdict=FONTDICT)
plt.ylabel("Fitness", fontsize=14, fontdict=FONTDICT)
plt.title("Fitness as a function of generation", fontsize=18, fontdict=FONTDICT)
plt.show()
# plot_fitness()