PK <‡MVT¨Ûp" " plot_mdo_scenario.ipynb{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Create an MDO Scenario\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import annotations\n\nfrom gemseo.api import configure_logger\nfrom gemseo.api import create_design_space\nfrom gemseo.api import create_discipline\nfrom gemseo.api import create_scenario\nfrom gemseo.api import get_available_opt_algorithms\nfrom gemseo.api import get_available_post_processings\nfrom numpy import ones\n\nconfigure_logger()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $(P)$ be a simple optimization problem:\n\n\\begin{align}(P) = \\left\\{\n \\begin{aligned}\n & \\underset{x}{\\text{minimize}}\n & & f(x) = \\sin(x) - \\exp(x) \\\\\n & \\text{subject to}\n & & -2 \\leq x \\leq 2\n \\end{aligned}\n \\right.\\end{align}\n\nIn this subsection, we will see how to use |g| to solve this problem\n$(P)$ by means of an optimization algorithm.\n\n## Define the discipline\nFirstly, by means of the :func:`.create_discipline` API function,\nwe create an :class:`.MDODiscipline` of :class:`.AnalyticDiscipline` type\nfrom a Python function:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "expressions = {\"y\": \"sin(x)-exp(x)\"}\ndiscipline = create_discipline(\"AnalyticDiscipline\", expressions=expressions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can quickly access the most relevant information of any discipline (name, inputs,\nand outputs) with Python's ``print()`` function. Moreover, we can get the default\ninput values of a discipline with the attribute :attr:`.MDODiscipline.default_inputs`\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(discipline)\nprint(f\"Default inputs: {discipline.default_inputs}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can to minimize this :class:`.MDODiscipline` over a design space,\nby means of a quasi-Newton method from the initial point $0.5$.\n\n## Define the design space\nFor that, by means of the :func:`.create_design_space` API function,\nwe define the :class:`.DesignSpace` $[-2, 2]$ with initial value $0.5$\nby using its :meth:`.DesignSpace.add_variable` method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "design_space = create_design_space()\ndesign_space.add_variable(\"x\", l_b=-2.0, u_b=2.0, value=-0.5 * ones(1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the MDO scenario\nThen, by means of the :func:`.create_scenario` API function,\nwe define an :class:`.MDOScenario` from the :class:`.MDODiscipline`\nand the :class:`.DesignSpace` defined above:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scenario = create_scenario(discipline, \"DisciplinaryOpt\", \"y\", design_space)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What about the differentiation method?\nThe :class:`.AnalyticDiscipline` automatically differentiates the\nexpressions to obtain the Jacobian matrices. Therefore, there is no need to\ndefine a differentiation method in this case. Keep in mind that for a\ngeneric discipline with no defined Jacobian function, you can use the\n:meth:`.Scenario.set_differentiation_method` method to define a numerical\napproximation of the gradients.\n\n.. code::\n\n scenario.set_differentiation_method(\"finite_differences\")\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Execute the MDO scenario\nLastly, we solve the :class:`.OptimizationProblem` included in the\n:class:`.MDOScenario` defined above by minimizing the objective function over\nthe :class:`.DesignSpace`. Precisely, we choose the [L-BFGS-B algorithm](https://en.wikipedia.org/wiki/Limited-memory_BFGS) implemented in the\nfunction :code:`scipy.optimize.fmin_l_bfgs_b`.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scenario.execute({\"algo\": \"L-BFGS-B\", \"max_iter\": 100})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The optimum results can be found in the execution log. It is also possible to\nextract them by invoking the :meth:`.Scenario.get_optimum` method. It\nreturns a dictionary containing the optimum results for the\nscenario under consideration:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "opt_results = scenario.get_optimum()\nprint(\n \"The solution of P is (x*,f(x*)) = ({}, {})\".format(\n opt_results.x_opt, opt_results.f_opt\n ),\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. seealso::\n\n You can found the [SciPy](https://www.scipy.org/) implementation of the\n [L-BFGS-B algorithm](https://en.wikipedia.org/wiki/Limited-memory_BFGS)\n algorithm [by clicking here](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.optimize.fmin_l_bfgs_b.html). # noqa\n\n## Available algorithms\nIn order to get the list of available optimization algorithms, use:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algo_list = get_available_opt_algorithms()\nprint(f\"Available algorithms: {algo_list}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Available post-processing\nIn order to get the list of available post-processing algorithms, use:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "post_list = get_available_post_processings()\nprint(f\"Available algorithms: {post_list}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exporting the problem data.\nAfter the execution of the scenario, you may want to export your data to use it\nelsewhere. The :meth:`.Scenario.export_to_dataset` will allow you to export your\nresults to a :class:`.Dataset`, the basic |g| class to store data.\nFrom a dataset, you can even obtain a Pandas dataframe with its method\n:meth:`~.Dataset.export_to_dataframe`:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "dataset = scenario.export_to_dataset(\"a_name_for_my_dataset\")\ndataframe = dataset.export_to_dataframe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also look at the examples:\n\n.. raw:: html\n\n
\n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" } }, "nbformat": 4, "nbformat_minor": 0 }PK <‡MVïÚ plot_doe_scenario.ipynb{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Create a DOE Scenario\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from __future__ import annotations\n\nfrom gemseo.api import configure_logger\nfrom gemseo.api import create_design_space\nfrom gemseo.api import create_discipline\nfrom gemseo.api import create_scenario\nfrom gemseo.api import get_available_doe_algorithms\nfrom gemseo.api import get_available_post_processings\n\nconfigure_logger()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $(P)$ be a simple optimization problem:\n\n\\begin{align}(P) = \\left\\{\n \\begin{aligned}\n & \\underset{x\\in\\mathbb{N}^2}{\\text{minimize}}\n & & f(x) = x_1 + x_2 \\\\\n & \\text{subject to}\n & & -5 \\leq x \\leq 5\n \\end{aligned}\n \\right.\\end{align}\n\nIn this example, we will see how to use |g|\nto solve this problem $(P)$ by means of a Design Of Experiments (DOE)\n\n## Define the discipline\nFirstly, by means of the :meth:`~gemseo.api.create_discipline` API function,\nwe create an :class:`.MDODiscipline` of :class:`.AnalyticDiscipline` type\nfrom a Python function:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "expressions = {\"y\": \"x1+x2\"}\ndiscipline = create_discipline(\"AnalyticDiscipline\", expressions=expressions)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we want to minimize this :class:`.MDODiscipline`\nover a design of experiments (DOE).\n\n## Define the design space\nFor that, by means of the :meth:`~gemseo.api.create_design_space` API function,\nwe define the :class:`.DesignSpace` $[-5, 5]\\times[-5, 5]$\nby using its :meth:`.DesignSpace.add_variable` method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "design_space = create_design_space()\ndesign_space.add_variable(\"x1\", l_b=-5, u_b=5, var_type=\"integer\")\ndesign_space.add_variable(\"x2\", l_b=-5, u_b=5, var_type=\"integer\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Define the DOE scenario\nThen, by means of the :meth:`~gemseo.api.create_scenario` API function,\nwe define a :class:`.DOEScenario` from the :class:`.MDODiscipline`\nand the :class:`.DesignSpace` defined above:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scenario = create_scenario(\n discipline, \"DisciplinaryOpt\", \"y\", design_space, scenario_type=\"DOE\"\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Execute the DOE scenario\nLastly, we solve the :class:`.OptimizationProblem` included in the\n:class:`.DOEScenario` defined above by minimizing the objective function\nover a design of experiments included in the :class:`.DesignSpace`.\nPrecisely, we choose a [full factorial design](https://en.wikipedia.org/wiki/Factorial_experiment) of size $11^2$:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "scenario.execute({\"algo\": \"fullfact\", \"n_samples\": 11**2})" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The optimum results can be found in the execution log. It is also possible to\nextract them by invoking the :meth:`.Scenario.get_optimum` method. It\nreturns a dictionary containing the optimum results for the\nscenario under consideration:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "opt_results = scenario.get_optimum()\nprint(\n \"The solution of P is (x*,f(x*)) = ({}, {})\".format(\n opt_results.x_opt, opt_results.f_opt\n ),\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Available DOE algorithms\nIn order to get the list of available DOE algorithms, use:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "algo_list = get_available_doe_algorithms()\nprint(f\"Available algorithms: {algo_list}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Available post-processing\nIn order to get the list of available post-processing algorithms, use:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "post_list = get_available_post_processings()\nprint(f\"Available algorithms: {post_list}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also look at the examples:\n\n.. raw:: html\n\n \n\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.15" } }, "nbformat": 4, "nbformat_minor": 0 }PK A‡MV.mTÆW&