1
|
|
|
from typing import Union |
2
|
|
|
from ..optimizers._distribution import run_search |
3
|
|
|
from ..optimizers._results import Results |
4
|
|
|
from ..optimizers._print_results import PrintResults |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CompositeOptimizer: |
8
|
|
|
optimizers: list |
9
|
|
|
|
10
|
|
|
def __init__(self, *optimizers): |
11
|
|
|
self.optimizers = list(optimizers) |
12
|
|
|
|
13
|
|
|
def __add__(self, optimizer_instance): |
14
|
|
|
self.optimizers.append(optimizer_instance) |
15
|
|
|
return self |
16
|
|
|
|
17
|
|
|
def run( |
18
|
|
|
self, |
19
|
|
|
max_time=None, |
20
|
|
|
distribution: str = "multiprocessing", |
21
|
|
|
n_processes: Union[str, int] = "auto", |
22
|
|
|
verbosity: list = ["progress_bar", "print_results", "print_times"], |
23
|
|
|
): |
24
|
|
|
if not verbosity: |
25
|
|
|
verbosity = [] |
26
|
|
|
|
27
|
|
|
self.collected_searches = [] |
28
|
|
|
for optimizer in self.optimizers: |
29
|
|
|
self.collected_searches += optimizer.searches |
30
|
|
|
|
31
|
|
|
for nth_process, search in enumerate(self.collected_searches): |
32
|
|
|
search.pass_args(max_time, nth_process, verbosity) |
33
|
|
|
|
34
|
|
|
self.results_list = run_search( |
35
|
|
|
self.collected_searches, distribution, n_processes |
36
|
|
|
) |
37
|
|
|
|
38
|
|
|
self.results_ = Results(self.results_list, self.collected_searches) |
39
|
|
|
|
40
|
|
|
self._print_info(verbosity) |
41
|
|
|
|
42
|
|
|
def _print_info(self, verbosity): |
43
|
|
|
print_res = PrintResults(self.collected_searches, verbosity) |
44
|
|
|
|
45
|
|
|
if verbosity: |
46
|
|
|
for _ in range(len(self.collected_searches)): |
47
|
|
|
print("") |
48
|
|
|
|
49
|
|
|
for results in self.results_list: |
50
|
|
|
nth_process = results["nth_process"] |
51
|
|
|
print_res.print_process(results, nth_process) |
52
|
|
|
|