gemseo.utils.testing.opt_lib_test_base module#

Common tools for testing opt libraries.

class OptLibraryTestBase[source]#

Bases: object

Main testing class.

static create_test(problem, algo_name, settings)[source]#

Create a function to run and test an optimization algorithm.

Parameters:
  • problem (OptimizationProblem) -- The optimization problem.

  • algo_name (str) -- The name of the optimization algorithm.

  • settings (dict[str, Any]) -- The settings of the optimization algorithm.

Returns:

The error message if the optimizer cannot find the solution, otherwise None.

static generate_error_test(opt_lib_name, algo_name, **settings)[source]#

Solve the Power 2 problem with an optimization library.

This optimization problem has constraints.

This problem raises an error when calling the objective.

Parameters:
  • opt_lib_name -- The name of the optimization library.

  • algo_name -- The name of the optimization algorithm.

  • **settings -- The settings of the optimization algorithm.

Returns:

An optimization library after the resolution of the Rosenbrock problem.

static generate_one_test(algo_name, **settings)[source]#

Solve the Power 2 problem with an optimization library.

This optimization problem has equality constraints.

Parameters:
  • algo_name (str) -- The name of the optimization algorithm.

  • **settings (Any) -- The settings of the optimization algorithm.

Returns:

An optimization library after the resolution of the Power 2 problem.

Return type:

tuple[BaseOptimizationLibrary, OptimizationProblem]

static generate_one_test_unconstrained(opt_lib_name, algo_name, **settings)[source]#

Solve the Rosenbrock problem with an optimization library.

This optimization problem has no constraints.

Parameters:
  • opt_lib_name -- The name of the optimization library.

  • algo_name -- The name of the optimization algorithm.

  • **settings -- The settings of the optimization algorithm.

Returns:

An optimization library after the resolution of the Rosenbrock problem.

generate_test(opt_lib_name, get_settings=None, get_problem_options=None)[source]#

Generates the tests for an opt library Filters algorithms adapted to the benchmark problems.

Parameters:
  • opt_lib_name -- The name of the optimization library.

  • get_settings -- A function to get the settings of the algorithm.

  • get_problem_options -- A function to get the options of the problem.

Returns:

The test methods to be attached to an unitest class.

static get_pb_instance(pb_name, pb_options=None)[source]#

Return an optimization problem.

Parameters:
  • pb_name (str) -- The name of the optimization problem.

  • pb_options (dict[str, Any] | None) -- The options to be passed to the optimization problem.

Raises:

ValueError -- When the problem is not available.

Return type:

Rosenbrock | Power2 | Rastrigin

static norm(x)[source]#

Compute the Euclidean norm of a vector.

Parameters:

x (NDArray[float]) -- The vector.

Returns:

The Euclidean norm of the vector.

Return type:

float

static relative_norm(x, x_ref)[source]#

Compute a relative Euclidean norm between to vectors.

Parameters:
  • x (NDArray[float]) -- The vector.

  • x_ref (NDArray[float]) -- The reference vector.

Returns:

The relative Euclidean norm between two vectors.

Return type:

float

static run_and_test_problem(problem, algo_name, **settings)[source]#

Run and test an optimization algorithm.

Parameters:
  • problem (OptimizationProblem) -- The optimization problem.

  • algo_name (str) -- The name of the optimization algorithm.

  • **settings (Any) -- The settings of the optimization algorithm.

Returns:

The error message if the optimizer cannot find the solution, otherwise None.

Return type:

str | None