| 147 | self.exclude_from_serialization = exclude_from_serialization |
| 148 | |
| 149 | def evaluate( |
| 150 | self, X, *args, return_values_of=None, return_as_dictionary=False, **kwargs |
| 151 | ): |
| 152 | |
| 153 | # if the problem does not require any kwargs they are re-initialized |
| 154 | if not self.requires_kwargs: |
| 155 | kwargs = dict() |
| 156 | |
| 157 | if return_values_of is None: |
| 158 | return_values_of = ["F"] |
| 159 | if self.n_ieq_constr > 0: |
| 160 | return_values_of.append("G") |
| 161 | if self.n_eq_constr > 0: |
| 162 | return_values_of.append("H") |
| 163 | |
| 164 | # make sure the array is at least 2d. store if reshaping was necessary |
| 165 | if isinstance(X, np.ndarray) and X.dtype != object: |
| 166 | X, only_single_value = at_least_2d_array( |
| 167 | X, extend_as="row", return_if_reshaped=True |
| 168 | ) |
| 169 | assert X.shape[1] == self.n_var, ( |
| 170 | f"Input dimension {X.shape[1]} are not equal to n_var {self.n_var}!" |
| 171 | ) |
| 172 | else: |
| 173 | only_single_value = not (isinstance(X, list) or isinstance(X, np.ndarray)) |
| 174 | |
| 175 | # this is where the actual evaluation takes place |
| 176 | _out = self.do(X, return_values_of, *args, **kwargs) |
| 177 | |
| 178 | out = {} |
| 179 | for k, v in _out.items(): |
| 180 | # copy it to a numpy array (it might be one of jax at this point) |
| 181 | v = np.array(v) |
| 182 | |
| 183 | # in case the input had only one dimension, then remove always the first dimension from each output |
| 184 | if only_single_value: |
| 185 | v = v[0] |
| 186 | |
| 187 | # if the NaN values should be replaced |
| 188 | if self.replace_nan_values_by is not None: |
| 189 | v[np.isnan(v)] = self.replace_nan_values_by |
| 190 | |
| 191 | try: |
| 192 | out[k] = v.astype(np.float64) |
| 193 | except: # noqa: E722 |
| 194 | out[k] = v |
| 195 | |
| 196 | if self.callback is not None: |
| 197 | self.callback(X, out) |
| 198 | |
| 199 | # now depending on what should be returned prepare the output |
| 200 | if return_as_dictionary: |
| 201 | return out |
| 202 | |
| 203 | if len(return_values_of) == 1: |
| 204 | return out[return_values_of[0]] |
| 205 | else: |
| 206 | return tuple([out[e] for e in return_values_of]) |