Source code for gemseo.problems.dataset.rosenbrock

# Copyright 2021 IRT Saint Exupéry,
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 3 as published by the Free Software Foundation.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# Contributors:
#    INITIAL AUTHORS - initial API and implementation and/or initial
#                           documentation
#        :author: Matthias De Lozzo
"""Rosenbrock dataset.

This :class:`.Dataset` contains 100 evaluations
of the well-known Rosenbrock function:

.. math::


This function is known for its global minimum at point (1,1),
its banana valley and the difficulty to reach its minimum.

This :class:`.Dataset` is based on a full-factorial
design of experiments.

`More information about the Rosenbrock function

from __future__ import annotations

from numpy import hstack
from numpy import linspace
from numpy import meshgrid

from gemseo.datasets.dataset import Dataset
from gemseo.datasets.io_dataset import IODataset
from gemseo.datasets.optimization_dataset import OptimizationDataset

[docs] def create_rosenbrock_dataset( n_samples: int = 100, opt_naming: bool = True, categorize: bool = True ) -> Dataset: """Rosenbrock dataset parametrization. Args: n_samples: The number of samples. opt_naming: Whether to use an optimization naming. categorize: Whether to distinguish between the different groups of variables. Returns: The Rosenbrock dataset. """ # Create function. root_n_samples = int(n_samples**0.5) x_i = linspace(-2.0, 2.0, root_n_samples) x_i, y_i = meshgrid(x_i, x_i) x_i = x_i.reshape((-1, 1)) y_i = y_i.reshape((-1, 1)) z_i = 100 * (y_i - x_i**2) ** 2 + (1 - x_i) ** 2 data = hstack((x_i, y_i, z_i)) # Create groups. if categorize: if opt_naming: groups = { "x": OptimizationDataset.DESIGN_GROUP, "rosen": OptimizationDataset.OBJECTIVE_GROUP, } cls = OptimizationDataset else: groups = {"x": IODataset.INPUT_GROUP, "rosen": IODataset.OUTPUT_GROUP} cls = IODataset else: groups = None cls = Dataset() dataset = cls.from_array(data, ["x", "rosen"], {"x": 2, "rosen": 1}, groups) = "Rosenbrock" dataset.misc["root_n_samples"] = root_n_samples return dataset