-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
70 lines (46 loc) · 1.63 KB
/
utils.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
import os
import json
import numpy as np
from scipy.stats import truncnorm
def queue_to_array(queue):
result = []
while not queue.empty():
result.append(queue.get())
return np.array(result)
def get_data(filename=None, file_index=-1):
if not filename:
files = os.listdir('results/')
files.sort()
filename = 'results/' + files[file_index]
return json.load(open(filename))
def get_individual(filename=None, file_index=-1, run=-1, individual=-1):
data = get_data(filename, file_index)
individuals = data['run'][run]['individuals']
individuals.sort(key=lambda i: i['fitness'])
ind = individuals[individual]
print(f'Fitness: {ind["fitness"]:.2f}')
return ind['genotype'], ind['fitness']
def get_info(filename=None, file_index=-1):
data = get_data(filename, file_index)
r = {
'settings': data['settings'],
'fitness': data['best_solution']['fitness']}
return r
def get_generation_genotypes(filename=None, file_index=-1, generation=-1):
"""use the last generation by default"""
data = get_data(filename, file_index)
return [i['genotype'] for i in data['run'][generation]['individuals']]
def angle_between(v1, v2):
""" Returns the angle in radians between vectors 'v1' and 'v2'::
>>> angle_between((1, 0, 0), (0, 1, 0))
1.5707963267948966
>>> angle_between((1, 0, 0), (1, 0, 0))
0.0
>>> angle_between((1, 0, 0), (-1, 0, 0))
3.141592653589793
"""
def unit_vector(vector):
""" Returns the unit vector of the vector. """
return vector / np.linalg.norm(vector)
v1_u = unit_vector(v1) v2_u = unit_vector(v2)
return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0))