gemseo.algos.base_algo_factory module#

Abstract factory to create libraries of algorithms.

class BaseAlgoFactory(use_cache=False)[source]#

Bases: object

A base class for creating factories for objects of kind AlgoLib.

This factory can create objects from a base class or any of its subclasses that can be imported from the given module sources. The base class and the module sources shall be defined as class attributes of the factory class, for instance:

class AFactory(BaseAlgoFactory):
    _CLASS = ABaseClass
    _PACKAGE_NAMES = (
        "first.module.fully.qualified.name",
        "second.module.fully.qualified.name",
    )

A factory instance can use a cache for the objects it creates, this cache is only used by one factory instance and is not shared with another instance. The cache is activated by passing use_cache = True to the constructor. When the cache is activated, a factory will return an object already created when possible and will create a new object otherwise.

Parameters:

use_cache (bool) --

Whether to cache the created objects.

By default it is set to False.

clear_lib_cache()[source]#

Clear the library cache.

Return type:

None

create(algo_name)[source]#

Create an algorithm library from an algorithm name.

Parameters:

algo_name (str) -- The name of an algorithm.

Returns:

The algorithm library.

Raises:

ImportError -- If the algorithm is not available.

Return type:

BaseAlgorithmLibrary

execute(problem, settings_model=None, **settings)[source]#

Execute a problem with an algorithm.

Parameters:
  • problem (BaseProblem) -- The problem to execute.

  • settings_model (BaseAlgorithmSettings | None) -- The algorithm settings as a Pydantic model. If None, use **settings.

  • **settings (Any) -- The algorithm settings. These arguments are ignored when settings_model is not None.

Returns:

The result.

Return type:

OptimizationResult | ODEResult

get_class(name)[source]#

Return a class from its name.

Parameters:

name (str) -- The name of the class.

Returns:

The class.

Raises:

ImportError -- If the class is not available.

Return type:

type

get_library_name(name)[source]#

Return the name of the library related to the name of a class.

Parameters:

name (str) -- The name of the class.

Returns:

The name of the library.

Return type:

str

is_available(name)[source]#

Check the availability of a library name or algorithm name.

Parameters:

name (str) -- The name of the library name or algorithm name.

Returns:

Whether the library or algorithm is available.

Return type:

bool

property algo_names_to_libraries: dict[str, str]#

The mapping from the algorithm names to the libraries.

property algorithms: list[str]#

The available algorithms names.

property libraries: list[str]#

List the available library names in the present configuration.

Returns:

The names of the available libraries.