nmoo.denoisers.resample_average
Averaging: a naive method to reduce 0-mean noise.
1""" 2Averaging: a naive method to reduce 0-mean noise. 3""" 4__docformat__ = "google" 5 6from typing import List 7 8from pymoo.core.problem import Problem 9import numpy as np 10 11from nmoo.wrapped_problem import WrappedProblem 12 13 14class ResampleAverage(WrappedProblem): 15 """ 16 A resampler tries to denoise a noisy problem by evaluating a solution 17 multiple times and averaging the outputs. 18 """ 19 20 _n_evaluations: int 21 22 def __init__( 23 self, 24 problem: Problem, 25 n_evaluations: int = 5, 26 *, 27 name: str = "resample_avg", 28 **kwargs, 29 ): 30 """ 31 Constructor. 32 33 Args: 34 problem (pymoo `Problem`): Noisy pymoo problem (or 35 `nmoo.wrapped_problem.WrappedProblem`). 36 n_evaluations (int): Number of times to evaluate the problem on 37 each solution. Defaults to 5. 38 name (str): An optional name for this problem. This will be used 39 when creating history dump files. Defaults to `resample_avg`. 40 """ 41 super().__init__(problem, name=name, **kwargs) 42 43 if n_evaluations <= 0: 44 raise ValueError("The number of evaluations should be at least 1.") 45 self._n_evaluations = n_evaluations 46 47 def _evaluate(self, x, out, *args, **kwargs): 48 outs: List[dict] = [] 49 for _ in range(self._n_evaluations): 50 outs.append({}) 51 self._problem._evaluate(x, outs[-1], *args, **kwargs) 52 for k in outs[0]: # By assumption, self._n_evaluations > 0 53 # TODO: What if the type is not consistent across evaluations? 54 if isinstance(outs[0][k], (int, float, np.ndarray)): 55 out[k] = np.average([o[k] for o in outs], axis=0) 56 else: 57 # TODO: Deal with different non numeric outputs 58 out[k] = outs[-1][k] 59 self.add_to_history_x_out(x, out)
15class ResampleAverage(WrappedProblem): 16 """ 17 A resampler tries to denoise a noisy problem by evaluating a solution 18 multiple times and averaging the outputs. 19 """ 20 21 _n_evaluations: int 22 23 def __init__( 24 self, 25 problem: Problem, 26 n_evaluations: int = 5, 27 *, 28 name: str = "resample_avg", 29 **kwargs, 30 ): 31 """ 32 Constructor. 33 34 Args: 35 problem (pymoo `Problem`): Noisy pymoo problem (or 36 `nmoo.wrapped_problem.WrappedProblem`). 37 n_evaluations (int): Number of times to evaluate the problem on 38 each solution. Defaults to 5. 39 name (str): An optional name for this problem. This will be used 40 when creating history dump files. Defaults to `resample_avg`. 41 """ 42 super().__init__(problem, name=name, **kwargs) 43 44 if n_evaluations <= 0: 45 raise ValueError("The number of evaluations should be at least 1.") 46 self._n_evaluations = n_evaluations 47 48 def _evaluate(self, x, out, *args, **kwargs): 49 outs: List[dict] = [] 50 for _ in range(self._n_evaluations): 51 outs.append({}) 52 self._problem._evaluate(x, outs[-1], *args, **kwargs) 53 for k in outs[0]: # By assumption, self._n_evaluations > 0 54 # TODO: What if the type is not consistent across evaluations? 55 if isinstance(outs[0][k], (int, float, np.ndarray)): 56 out[k] = np.average([o[k] for o in outs], axis=0) 57 else: 58 # TODO: Deal with different non numeric outputs 59 out[k] = outs[-1][k] 60 self.add_to_history_x_out(x, out)
A resampler tries to denoise a noisy problem by evaluating a solution multiple times and averaging the outputs.
ResampleAverage( problem: pymoo.core.problem.Problem, n_evaluations: int = 5, *, name: str = 'resample_avg', **kwargs)
23 def __init__( 24 self, 25 problem: Problem, 26 n_evaluations: int = 5, 27 *, 28 name: str = "resample_avg", 29 **kwargs, 30 ): 31 """ 32 Constructor. 33 34 Args: 35 problem (pymoo `Problem`): Noisy pymoo problem (or 36 `nmoo.wrapped_problem.WrappedProblem`). 37 n_evaluations (int): Number of times to evaluate the problem on 38 each solution. Defaults to 5. 39 name (str): An optional name for this problem. This will be used 40 when creating history dump files. Defaults to `resample_avg`. 41 """ 42 super().__init__(problem, name=name, **kwargs) 43 44 if n_evaluations <= 0: 45 raise ValueError("The number of evaluations should be at least 1.") 46 self._n_evaluations = n_evaluations
Constructor.
Arguments:
- problem (pymoo
Problem
): Noisy pymoo problem (ornmoo.wrapped_problem.WrappedProblem
). - n_evaluations (int): Number of times to evaluate the problem on each solution. Defaults to 5.
- name (str): An optional name for this problem. This will be used
when creating history dump files. Defaults to
resample_avg
.
Inherited Members
- nmoo.wrapped_problem.WrappedProblem
- add_to_history
- add_to_history_x_out
- all_layers
- depth
- dump_all_histories
- dump_history
- ground_problem
- innermost_wrapper
- reseed
- start_new_run
- pymoo.core.problem.Problem
- evaluate
- do
- nadir_point
- ideal_point
- pareto_front
- pareto_set
- has_bounds
- has_constraints
- bounds
- name
- calc_constraint_violation