| 7 | from .evaluator import ModelEvaluator |
| 8 | |
| 9 | class EvolutionarySearch: |
| 10 | def __init__( |
| 11 | self, |
| 12 | search_space: SearchSpace, |
| 13 | evaluator: ModelEvaluator, |
| 14 | population_size: int = 20, |
| 15 | generations: int = 50, |
| 16 | mutation_rate: float = 0.1, |
| 17 | crossover_rate: float = 0.5, |
| 18 | tournament_size: int = 3 |
| 19 | ): |
| 20 | self.search_space = search_space |
| 21 | self.evaluator = evaluator |
| 22 | self.population_size = population_size |
| 23 | self.generations = generations |
| 24 | self.mutation_rate = mutation_rate |
| 25 | self.crossover_rate = crossover_rate |
| 26 | self.tournament_size = tournament_size |
| 27 | |
| 28 | self.population = [] |
| 29 | self.best_architecture = None |
| 30 | self.history = [] |
| 31 | |
| 32 | def initialize_population(self): |
| 33 | print(f"Initializing population of {self.population_size} architectures...") |
| 34 | self.population = [] |
| 35 | |
| 36 | for i in range(self.population_size): |
| 37 | arch = self.search_space.random_architecture() |
| 38 | self.population.append(arch) |
| 39 | |
| 40 | print("Population initialized successfully") |
| 41 | |
| 42 | def evaluate_population(self): |
| 43 | print("Evaluating population...") |
| 44 | |
| 45 | for arch in tqdm(self.population, desc="Evaluating architectures"): |
| 46 | if arch.fitness == 0.0: |
| 47 | fitness, accuracy = self.evaluator.evaluate(arch, self.search_space) |
| 48 | arch.fitness = fitness |
| 49 | arch.accuracy = accuracy |
| 50 | |
| 51 | complexity = self.search_space.estimate_complexity(arch) |
| 52 | arch.params = complexity['params'] |
| 53 | arch.flops = complexity['flops'] |
| 54 | |
| 55 | def tournament_selection(self) -> Architecture: |
| 56 | tournament = random.sample(self.population, self.tournament_size) |
| 57 | return max(tournament, key=lambda x: x.fitness) |
| 58 | |
| 59 | def select_parents(self) -> List[Architecture]: |
| 60 | parent1 = self.tournament_selection() |
| 61 | parent2 = self.tournament_selection() |
| 62 | return [parent1, parent2] |
| 63 | |
| 64 | def create_offspring(self, parents: List[Architecture]) -> Architecture: |
| 65 | if random.random() < self.crossover_rate: |
| 66 | offspring = self.search_space.crossover(parents[0], parents[1]) |