1# Copyright 2020, 2021 PaGMO development team 2# 3# This file is part of the pygmo library. 4# 5# This Source Code Form is subject to the terms of the Mozilla 6# Public License v. 2.0. If a copy of the MPL was not distributed 7# with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 9from .core import algorithm 10 11 12def _algorithm_extract(self, t): 13 """Extract the user-defined algorithm. 14 15 This method allows to extract a reference to the user-defined algorithm (UDA) stored within this 16 :class:`~pygmo.algorithm` instance. The behaviour of this function depends on the value 17 of *t* (which must be a :class:`type`) and on the type of the internal UDA: 18 19 * if the type of the UDA is *t*, then a reference to the UDA will be returned 20 (this mirrors the behaviour of the corresponding C++ method 21 :cpp:func:`pagmo::algorithm::extract()`), 22 * if *t* is :class:`object` and the UDA is a Python object (as opposed to an 23 :ref:`exposed C++ algorithm <algorithms_cpp>`), then a reference to the 24 UDA will be returned (this allows to extract a Python UDA without knowing its type), 25 * otherwise, :data:`None` will be returned. 26 27 Args: 28 t (:class:`type`): the type of the user-defined algorithm to extract 29 30 Returns: 31 a reference to the internal user-defined algorithm, or :data:`None` if the extraction fails 32 33 Raises: 34 TypeError: if *t* is not a :class:`type` 35 36 Examples: 37 >>> import pygmo as pg 38 >>> a1 = pg.algorithm(pg.compass_search()) 39 >>> a1.extract(pg.compass_search) # doctest: +SKIP 40 <pygmo.core.compass_search at 0x7f8e4792b670> 41 >>> a1.extract(pg.de) is None 42 True 43 >>> class algo: 44 ... def evolve(self, pop): 45 ... return pop 46 >>> a2 = pg.algorithm(algo()) 47 >>> a2.extract(object) # doctest: +SKIP 48 <__main__.algo at 0x7f8e478c04e0> 49 >>> a2.extract(algo) # doctest: +SKIP 50 <__main__.algo at 0x7f8e478c04e0> 51 >>> a2.extract(pg.de) is None 52 True 53 54 """ 55 if not isinstance(t, type): 56 raise TypeError("the 't' parameter must be a type") 57 if hasattr(t, "_pygmo_cpp_algorithm"): 58 return self._cpp_extract(t()) 59 return self._py_extract(t) 60 61 62def _algorithm_is(self, t): 63 """Check the type of the user-defined algorithm. 64 65 This method returns :data:`False` if :func:`extract(t) <pygmo.algorithm.extract>` returns 66 :data:`None`, and :data:`True` otherwise. 67 68 Args: 69 t (:class:`type`): the type that will be compared to the type of the UDA 70 71 Returns: 72 bool: whether the UDA is of type *t* or not 73 74 Raises: 75 unspecified: any exception thrown by :func:`~pygmo.algorithm.extract()` 76 77 """ 78 return not self.extract(t) is None 79 80 81# Do the actual patching. 82setattr(algorithm, "extract", _algorithm_extract) 83setattr(algorithm, "is_", _algorithm_is) 84