| Total Complexity | 8 |
| Total Lines | 52 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | # Author: Simon Blanke |
||
| 2 | # Email: [email protected] |
||
| 3 | # License: MIT License |
||
| 4 | |||
| 5 | from ...converter import Converter |
||
| 6 | from ...results_manager import ResultsManager |
||
| 7 | from ...optimizers.base_optimizer import get_n_inits |
||
| 8 | |||
| 9 | |||
| 10 | class BasePopulationOptimizer: |
||
| 11 | def __init__( |
||
| 12 | self, |
||
| 13 | search_space, |
||
| 14 | initialize={"grid": 4, "random": 2, "vertices": 4}, |
||
| 15 | ): |
||
| 16 | super().__init__() |
||
| 17 | self.conv = Converter(search_space) |
||
| 18 | self.results_mang = ResultsManager(self.conv) |
||
| 19 | self.initialize = initialize |
||
| 20 | |||
| 21 | self.eval_times = [] |
||
| 22 | self.iter_times = [] |
||
| 23 | |||
| 24 | def _iterations(self, positioners): |
||
| 25 | nth_iter = 0 |
||
| 26 | for p in positioners: |
||
| 27 | nth_iter = nth_iter + len(p.pos_new_list) |
||
| 28 | |||
| 29 | return nth_iter |
||
| 30 | |||
| 31 | def _create_population(self, Optimizer): |
||
| 32 | if isinstance(self.population, int): |
||
| 33 | population = [] |
||
| 34 | for pop_ in range(self.population): |
||
| 35 | population.append( |
||
| 36 | Optimizer(self.conv.search_space, rand_rest_p=self.rand_rest_p) |
||
| 37 | ) |
||
| 38 | else: |
||
| 39 | population = self.population |
||
| 40 | |||
| 41 | n_inits = get_n_inits(self.initialize) |
||
| 42 | |||
| 43 | if n_inits < len(population): |
||
| 44 | print("\n Warning: Not enough initial positions for population size") |
||
| 45 | print(" Population size is reduced to", n_inits) |
||
| 46 | population = population[:n_inits] |
||
| 47 | |||
| 48 | return population |
||
| 49 | |||
| 50 | def finish_initialization(self): |
||
| 51 | pass |
||
| 52 |