| 35 | self.n_eval = 0 |
| 36 | |
| 37 | def eval( |
| 38 | self, |
| 39 | problem: Problem, |
| 40 | pop: Population, |
| 41 | skip_already_evaluated: bool | None = None, |
| 42 | evaluate_values_of: list[Any] | None = None, |
| 43 | count_evals: bool = True, |
| 44 | **kwargs, |
| 45 | ): |
| 46 | |
| 47 | # load the default settings from the evaluator object if not already provided |
| 48 | evaluate_values_of = ( |
| 49 | self.evaluate_values_of |
| 50 | if evaluate_values_of is None |
| 51 | else evaluate_values_of |
| 52 | ) |
| 53 | skip_already_evaluated = ( |
| 54 | self.skip_already_evaluated |
| 55 | if skip_already_evaluated is None |
| 56 | else skip_already_evaluated |
| 57 | ) |
| 58 | |
| 59 | # check the type of the input |
| 60 | is_individual = isinstance(pop, Individual) |
| 61 | |
| 62 | # make sure the object is a population |
| 63 | if is_individual: |
| 64 | pop = Population().create(pop) |
| 65 | |
| 66 | # filter the index to have individual where not all attributes have been evaluated |
| 67 | if skip_already_evaluated: |
| 68 | I = np.array( # noqa: E741 |
| 69 | [ |
| 70 | i |
| 71 | for i, ind in enumerate(pop) |
| 72 | if not all([e in ind.evaluated for e in evaluate_values_of]) |
| 73 | ] |
| 74 | ) |
| 75 | |
| 76 | # if skipping is deactivated simply make the index being all individuals |
| 77 | else: |
| 78 | I = np.arange(len(pop)) # noqa: E741 |
| 79 | |
| 80 | # evaluate the solutions (if there are any) |
| 81 | if len(I) > 0: |
| 82 | # do the actual evaluation - call the sub-function to set the corresponding values to the population |
| 83 | self._eval(problem, pop[I], evaluate_values_of, **kwargs) |
| 84 | |
| 85 | # update the function evaluation counter |
| 86 | if count_evals: |
| 87 | self.n_eval += len(I) |
| 88 | |
| 89 | # allow to have a callback registered |
| 90 | if self.callback: |
| 91 | self.callback(pop) |
| 92 | |
| 93 | if is_individual: |
| 94 | return pop[0] |