Passed
Push — master ( 588022...8a2a5a )
by Simon
01:36
created

scripts._generator   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 33
dl 0
loc 64
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A create_class_file_content() 0 2 1
1
import os
2
from pathlib import Path
3
4
# List of algorithm names and corresponding class names
5
algo_info = [
6
    ("downhill_simplex", "DownhillSimplexOptimizer"),
7
    ("simulated_annealing", "SimulatedAnnealingOptimizer"),
8
    ("direct_algorithm", "DirectAlgorithm"),
9
    ("lipschitz_optimization", "LipschitzOptimizer"),
10
    ("pattern_search", "PatternSearch"),
11
    ("random_restart_hill_climbing", "RandomRestartHillClimbingOptimizer"),
12
    ("random_search", "RandomSearchOptimizer"),
13
    ("powells_method", "PowellsMethod"),
14
    ("differential_evolution", "DifferentialEvolutionOptimizer"),
15
    ("evolution_strategy", "EvolutionStrategyOptimizer"),
16
    ("genetic_algorithm", "GeneticAlgorithmOptimizer"),
17
    ("parallel_tempering", "ParallelTemperingOptimizer"),
18
    ("particle_swarm_optimization", "ParticleSwarmOptimizer"),
19
    ("spiral_optimization", "SpiralOptimization"),
20
    ("bayesian_optimization", "BayesianOptimizer"),
21
    ("forest_optimizer", "ForestOptimizer"),
22
    ("tree_structured_parzen_estimators", "TreeStructuredParzenEstimators"),
23
]
24
25
BASE_DIR = Path("generated_opt_algos")
26
27
28
# Template for the Python class file
29
def create_class_file_content(class_name: str) -> str:
30
    return f'''from hyperactive.opt._adapters._gfo import _BaseGFOadapter
31
32
33
class {class_name}(_BaseGFOadapter):
34
35
    def _get_gfo_class(self):
36
        """Get the GFO class to use.
37
38
        Returns
39
        -------
40
        class
41
            The GFO class to use. One of the concrete GFO classes
42
        """
43
        from gradient_free_optimizers import {class_name}
44
45
        return {class_name}
46
'''
47
48
49
# Main generation loop
50
for name, class_name in algo_info:
51
    algo_folder = BASE_DIR / name
52
    algo_folder.mkdir(parents=True, exist_ok=True)
53
54
    init_file = algo_folder / "__init__.py"
55
    class_file = algo_folder / f"_{name}.py"
56
57
    # Create __init__.py (empty)
58
    init_file.touch(exist_ok=True)
59
60
    # Write the optimizer class file
61
    class_file.write_text(create_class_file_content(class_name))
62
63
print(f"Generated {len(algo_info)} folders in {BASE_DIR.resolve()}")
64