1 | import random |
---|
2 | |
---|
3 | import numpy as np |
---|
4 | |
---|
5 | from ..base.remove_diagonal import remove_diagonal |
---|
6 | |
---|
7 | |
---|
8 | class PopulationStructures: |
---|
9 | def __init__(self, initial_individual, archive_size=0, popsize=100) -> None: |
---|
10 | self.population_size = popsize |
---|
11 | self.population = [initial_individual.copy() |
---|
12 | for _ in range(self.population_size)] |
---|
13 | self.archive = [] |
---|
14 | self.archive_size = archive_size |
---|
15 | |
---|
16 | def change_individual(self, dissim): |
---|
17 | no_diagonal = remove_diagonal(dissim.copy()) |
---|
18 | minval = np.min(no_diagonal) |
---|
19 | to_remove_pair = list(set(np.where(no_diagonal == minval)[0])) |
---|
20 | last_index = len(dissim)-1 |
---|
21 | if last_index in to_remove_pair: |
---|
22 | return last_index |
---|
23 | else: |
---|
24 | return random.choice(to_remove_pair) |
---|
25 | |
---|
26 | def update_archive(self, dissim_matrix, population_archive): |
---|
27 | if self.archive_size < 1: |
---|
28 | return |
---|
29 | current_archive_ind = [i for i in range( |
---|
30 | self.population_size, len(dissim_matrix))] |
---|
31 | for i in range(self.population_size): |
---|
32 | current_archive_ind = current_archive_ind + [i] |
---|
33 | temp_dissim_matrix = dissim_matrix[current_archive_ind, |
---|
34 | ][:, current_archive_ind] |
---|
35 | if len(current_archive_ind) > self.archive_size: |
---|
36 | to_remove = self.change_individual(temp_dissim_matrix) |
---|
37 | current_archive_ind.pop(to_remove) |
---|
38 | |
---|
39 | self.archive = np.array(population_archive)[ |
---|
40 | current_archive_ind].tolist() |
---|