Conditions | 3 |
Total Lines | 75 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # Author: Simon Blanke |
||
104 | def add_search( |
||
105 | self, |
||
106 | objective_function: callable, |
||
107 | search_space: Dict[str, list], |
||
108 | n_iter: int, |
||
109 | search_id=None, |
||
110 | optimizer: Union[str, Type[RandomSearchOptimizer]] = "default", |
||
111 | n_jobs: int = 1, |
||
112 | initialize: Dict[str, int] = {"grid": 4, "random": 2, "vertices": 4}, |
||
113 | constraints: List[callable] = None, |
||
114 | pass_through: Dict = None, |
||
115 | callbacks: Dict[str, callable] = None, |
||
116 | catch: Dict = None, |
||
117 | max_score: float = None, |
||
118 | early_stopping: Dict = None, |
||
119 | random_state: int = None, |
||
120 | memory: Union[str, bool] = "share", |
||
121 | memory_warm_start: pd.DataFrame = None, |
||
122 | ): |
||
123 | """ |
||
124 | Add a new optimization search process with specified parameters. |
||
125 | |||
126 | Parameters: |
||
127 | - objective_function: The objective function to optimize. |
||
128 | - search_space: Dictionary defining the search space for optimization. |
||
129 | - n_iter: Number of iterations for the optimization process. |
||
130 | - search_id: Identifier for the search process (default: None). |
||
131 | - optimizer: The optimizer to use for the search process (default: "default"). |
||
132 | - n_jobs: Number of parallel jobs to run (default: 1). |
||
133 | - initialize: Dictionary specifying initialization parameters (default: {"grid": 4, "random": 2, "vertices": 4}). |
||
134 | - constraints: List of constraint functions (default: None). |
||
135 | - pass_through: Dictionary of additional parameters to pass through (default: None). |
||
136 | - callbacks: Dictionary of callback functions (default: None). |
||
137 | - catch: Dictionary of exceptions to catch during optimization (default: None). |
||
138 | - max_score: Maximum score to achieve (default: None). |
||
139 | - early_stopping: Dictionary specifying early stopping criteria (default: None). |
||
140 | - random_state: Seed for random number generation (default: None). |
||
141 | - memory: Option to share memory between processes (default: "share"). |
||
142 | - memory_warm_start: DataFrame containing warm start memory (default: None). |
||
143 | """ |
||
144 | |||
145 | self.check_list(search_space) |
||
146 | |||
147 | constraints = constraints or [] |
||
148 | pass_through = pass_through or {} |
||
149 | callbacks = callbacks or {} |
||
150 | catch = catch or {} |
||
151 | early_stopping = early_stopping or {} |
||
152 | |||
153 | optimizer = self._default_opt(optimizer) |
||
154 | search_id = self._default_search_id(search_id, objective_function) |
||
155 | s_space = SearchSpace(search_space) |
||
156 | |||
157 | optimizer.setup_search( |
||
158 | objective_function=objective_function, |
||
159 | s_space=s_space, |
||
160 | n_iter=n_iter, |
||
161 | initialize=initialize, |
||
162 | constraints=constraints, |
||
163 | pass_through=pass_through, |
||
164 | callbacks=callbacks, |
||
165 | catch=catch, |
||
166 | max_score=max_score, |
||
167 | early_stopping=early_stopping, |
||
168 | random_state=random_state, |
||
169 | memory=memory, |
||
170 | memory_warm_start=memory_warm_start, |
||
171 | verbosity=self.verbosity, |
||
172 | ) |
||
173 | |||
174 | n_jobs = mp.cpu_count() if n_jobs == -1 else n_jobs |
||
175 | |||
176 | for _ in range(n_jobs): |
||
177 | nth_process = len(self.opt_pros) |
||
178 | self.opt_pros[nth_process] = optimizer |
||
179 | |||
270 |