Repository: scipy/scipy
File: scipy/optimize/_differentialevolution.py (Python)

Code before:
                    warnings.warn("differential evolution didn't find a "
                                  "solution satisfying the constraints, "
                                  "attempting to polish from the least "
                                  "infeasible solution",
                                  UserWarning, stacklevel=2)

            pf = self.polish
            _f = self.original_func
            if not callable(pf):
                pf = partial(minimize, method=polish_method)
                def _f(x):
                    return list(self._mapwrapper(self.func, np.atleast_2d(x)))[0]

                if self.disp:
                    print(f"Polishing solution with '{polish_method}'")

            result = pf(
                _f,
                np.copy(DE_result.x),
                bounds=Bounds(lb=self.limits[0], ub=self.limits[1]),
                constraints=self.constraints
            )

            if not isinstance(result, OptimizeResult):
                raise ValueError(
                    "The result from a user defined polishing function "
                     "should return an OptimizeResult."
                )

            self._nfev += result.get("nfev", 0)
            DE_result.nfev = self._nfev

            # Polishing solution is only accepted if there is an improvement in
            # cost function, the polishing was successful and the solution lies
            # within the bounds.
            if (result.fun < DE_result.fun and
                    result.success and
                    np.all(result.x <= self.limits[1]) and
                    np.all(self.limits[0] <= result.x)):
                DE_result.fun = result.fun
                DE_result.x = result.x
                DE_result.jac = result.get("jac", None)
                # to keep internal state consistent
                self.population_energies[0] = result.fun
                self.population[0] = self._unscale_parameters(result.x)

        if self._wrapped_constraints:
            DE_result.constr = [c.violation(DE_result.x) for
                                c in self._wrapped_constraints]
            DE_result.constr_violation = np.max(
                np.concatenate(DE_result.constr))

Review comment (performance):
```suggestion
                    "The result from a user defined polishing function "
                    "should return an OptimizeResult."
```

Code after:
                                  "solution satisfying the constraints, "
                                  "attempting to polish from the least "
                                  "infeasible solution",
                                  UserWarning, stacklevel=2)

            pf = self.polish
            _f = self.original_func
            if not callable(pf):
                pf = partial(minimize, method=polish_method)
                def _f(x):
                    return list(self._mapwrapper(self.func, np.atleast_2d(x)))[0]

                if self.disp:
                    print(f"Polishing solution with '{polish_method}'")

            result = pf(
                _f,
                np.copy(DE_result.x),
                bounds=Bounds(lb=self.limits[0], ub=self.limits[1]),
                constraints=self.constraints
            )

            if not isinstance(result, OptimizeResult):
                raise ValueError(
                    "The result from a user defined polishing function "
                     "should return an OptimizeResult."
                )

            self._nfev += result.get("nfev", 0)
            DE_result.nfev = self._nfev

            # Polishing solution is only accepted if there is an improvement in
            # cost function, the polishing was successful and the solution lies
            # within the bounds.
            if (result.fun < DE_result.fun and
                    result.success and
                    np.all(result.x <= self.limits[1]) and
                    np.all(self.limits[0] <= result.x)):
                DE_result.fun = result.fun
                DE_result.x = result.x
                DE_result.jac = result.get("jac", None)
                # to keep internal state consistent
                self.population_energies[0] = result.fun
                self.population[0] = self._unscale_parameters(result.x)

        if self._wrapped_constraints:
            DE_result.constr = [c.violation(DE_result.x) for
                                c in self._wrapped_constraints]
            DE_result.constr_violation = np.max(
                np.concatenate(DE_result.constr))
            DE_result.maxcv = DE_result.constr_violation