1 | # Author: Simon Blanke |
||
2 | # Email: [email protected] |
||
3 | # License: MIT License |
||
4 | |||
5 | |||
6 | import numpy as np |
||
7 | import pandas as pd |
||
8 | |||
9 | |||
10 | class Results: |
||
11 | def __init__(self, results_list, opt_pros): |
||
12 | self.results_list = results_list |
||
13 | self.opt_pros = opt_pros |
||
14 | |||
15 | self.objFunc2results = {} |
||
16 | self.search_id2results = {} |
||
17 | |||
18 | def _sort_results_objFunc(self, objective_function): |
||
19 | best_score = -np.inf |
||
20 | best_para = None |
||
21 | search_data = None |
||
22 | |||
23 | search_data_list = [] |
||
24 | |||
25 | for results_ in self.results_list: |
||
26 | nth_process = results_["nth_process"] |
||
27 | |||
28 | opt = self.opt_pros[nth_process] |
||
29 | objective_function_ = opt.objective_function |
||
30 | search_space_ = opt.s_space() |
||
31 | params = list(search_space_.keys()) |
||
32 | |||
33 | if objective_function_ != objective_function: |
||
34 | continue |
||
35 | |||
36 | if results_["best_score"] > best_score: |
||
37 | best_score = results_["best_score"] |
||
38 | best_para = results_["best_para"] |
||
39 | |||
40 | search_data = results_["search_data"] |
||
41 | search_data["eval_times"] = results_["eval_times"] |
||
42 | search_data["iter_times"] = results_["iter_times"] |
||
43 | |||
44 | search_data_list.append(search_data) |
||
45 | |||
46 | if len(search_data_list) > 0: |
||
47 | search_data = pd.concat(search_data_list) |
||
48 | |||
49 | self.objFunc2results[objective_function] = { |
||
50 | "best_para": best_para, |
||
51 | "best_score": best_score, |
||
52 | "search_data": search_data, |
||
53 | "params": params, |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
54 | } |
||
55 | |||
56 | def _get_result(self, id_, result_name): |
||
57 | if id_ not in self.objFunc2results: |
||
58 | self._sort_results_objFunc(id_) |
||
59 | |||
60 | search_data = self.objFunc2results[id_][result_name] |
||
61 | |||
62 | return search_data |
||
63 | |||
64 | def best_para(self, id_): |
||
65 | best_para_ = self._get_result(id_, "best_para") |
||
66 | |||
67 | if best_para_ is not None: |
||
68 | return best_para_ |
||
69 | |||
70 | raise ValueError("objective function name not recognized") |
||
71 | |||
72 | def best_score(self, id_): |
||
73 | best_score_ = self._get_result(id_, "best_score") |
||
74 | |||
75 | if best_score_ != -np.inf: |
||
76 | return best_score_ |
||
77 | |||
78 | raise ValueError("objective function name not recognized") |
||
79 | |||
80 | def search_data(self, id_): |
||
81 | search_data = self._get_result(id_, "search_data") |
||
82 | |||
83 | params = self.objFunc2results[id_]["params"] |
||
84 | |||
85 | if search_data is not None: |
||
86 | return search_data |
||
87 | |||
88 | raise ValueError("objective function name not recognized") |
||
89 |