{ "cells": [ { "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 import configure_logger\nfrom gemseo import create_design_space\nfrom gemseo import create_discipline\nfrom gemseo import create_scenario\nfrom gemseo import get_available_doe_algorithms\nfrom gemseo 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 :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\": \"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 :func:`.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 :func:`.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\naccess them with :attr:`.Scenario.optimization_result`:\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "optimization_result = scenario.optimization_result\nf\"The solution of P is (x*, f(x*)) = ({optimization_result.x_opt}, {optimization_result.f_opt})\"" ] }, { "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": [ "get_available_doe_algorithms()" ] }, { "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": [ "get_available_post_processings()" ] }, { "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.18" } }, "nbformat": 4, "nbformat_minor": 0 }