forked from GuilhermeCaeiro/trabmh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
114 lines (91 loc) · 4.09 KB
/
utils.py
File metadata and controls
114 lines (91 loc) · 4.09 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import operator
import pdb
import numpy as np
import subprocess
class Struct:
def __init__(self, entries):
self.__dict__.update(entries)
class Utils:
def __init__(self):
pass
@classmethod
def sort_population(self, population):
return sorted(population, key=operator.attrgetter("fitness"), reverse=True)
# population must be sorted
@classmethod
def split_elite_commoners(self, population, elite_size):
return population[:elite_size], population[elite_size:]
@classmethod
def remove_repeated_individuals(self, population):
hashes = []
individuals = []
for individual in population:
if individual.individual_hash not in hashes:
individuals.append(individual)
hashes.append(individual.individual_hash)
return individuals
@classmethod
def get_n_best_individuals_without_repetition(self, population, number_of_individuals):
population = Utils.sort_population(population)
hashes = []
individuals = []
for individual in population:
if individual.individual_hash not in hashes:
individuals.append(individual)
hashes.append(individual.individual_hash)
if len(individuals) == number_of_individuals:
break
return individuals
@classmethod
def get_improvement_candidates(self, population, number_of_individuals):
return self.get_n_best_individuals_without_repetition(population, number_of_individuals)
@classmethod
def get_best_solution(self, population, best_sol):
best_population_fitness = max([i.fitness for i in population])
if best_population_fitness > best_sol:
return best_population_fitness
else:
return best_sol
@classmethod
def convert_binary_to_permutation(self, sequence):
return np.array([np.where(sequence == 1)[0]]).T
@classmethod
def convert_chromosomes_from_binary_to_permutation(self, chromosomes):
return [self.convert_binary_to_permutation(chromosome) for chromosome in chromosomes]
@classmethod
def convert_permutation_to_binary(self, sequence, expected_size):
new_sequence = np.zeros((expected_size, 1))
new_sequence[sequence] = 1
return new_sequence
@classmethod
def convert_chromosomes_from_permutation_to_binary(self, chromosomes, expected_size):
return [self.convert_permutation_to_binary(chromosome, expected_size) for chromosome in chromosomes]
@classmethod
def hash_2_chromosome(self, individual_hash, expected_size):
c = list(bin(int(individual_hash))[2:])
c = [0]*(expected_size-len(c)) + c
# c = '{:>0'+str(expected_size)+'d}'.format(int(format(individual_hash, 'b')))
return np.array(c, dtype=int).reshape(-1,1)
@classmethod
def chromosome_2_hash(self, chromosome):
return int("".join(str(int(i)) for i in chromosome),2)
# method based on the answer of the user "Yuji 'Tomita' Tomita" at
# https://stackoverflow.com/questions/14989858/get-the-current-git-hash-in-a-python-script
@classmethod
def get_git_hash(self):
hash_string = ""
try:
hash_string = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('ascii').strip()
except:
hash_string = "Unable to retrieve the commit hash."
return hash_string
@classmethod
def calculate_gap(self, solution, reference):
gap = (solution - reference) / abs(reference)
return gap
@classmethod
def convert_individuals_binary_chromosomes_to_string(self, individuals):
return ",".join(["".join(str(int(gene)) for gene in individual.binary_chromosome.T[0]) for individual in individuals])
@classmethod
def convert_individuals_hashes_to_string(self, individuals):
return ",".join([str(individual.individual_hash) for individual in individuals])