.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/basic_usage.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_basic_usage.py: Minimize a function =================== This examples shows a basic usage of :func:`stochopy.optimize.minimize`. .. GENERATED FROM PYTHON SOURCE LINES 10-12 Let's import an objective function to optimize. :mod:`stochopy.factory` has several sample benchmark functions to test. We also have to define the feasible space (or boundaries) for each variable to optimize. The length of the boundary array is used internally to define the dimensionality of the problem. In this example, we will optimize 20 variables within [-5.12, 5.12]. .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: default import numpy as np from stochopy.factory import rosenbrock upper = np.full(20, 5.12) bounds = np.column_stack((-upper, upper)) .. GENERATED FROM PYTHON SOURCE LINES 22-24 The main optimization function :func:`stochopy.optimize.minimize` has an API inspired by :mod:`scipy`. In this example, we will use CMA-ES to minimize the Rosenbrock function .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: default from stochopy.optimize import minimize x = minimize(rosenbrock, bounds, method="cmaes", options={"maxiter": 2000, "popsize": 20, "seed": 42}) .. GENERATED FROM PYTHON SOURCE LINES 32-33 :func:`stochopy.optimize.minimize` returns a :class:`stochopy.optimize.OptimizeResult` dictionary-like that contains the optimization result. .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. code-block:: default print(x) .. rst-class:: sphx-glr-script-out .. code-block:: none fun: 9.145428857212641e-09 message: 'best solution value is lower than ftol' nfev: 26940 nit: 1347 status: 1 success: True x: array([1.00000007, 0.99999874, 0.99999912, 0.99999904, 0.99999866, 0.99999691, 0.99999746, 0.99999983, 1.00000106, 1.00000124, 1.00000022, 1.00000054, 1.00000062, 1.00000048, 1.00000085, 1.00000487, 1.00000562, 1.00001043, 1.00002107, 1.0000445 ]) .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.904 seconds) .. _sphx_glr_download_examples_basic_usage.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: basic_usage.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: basic_usage.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_