PK p`Rn plot_simple_opt_2.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Analytical test case # 2\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we consider a simple optimization problem to illustrate\nalgorithms interfaces and optimization libraries integration.\n\n## Imports\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom future import standard_library\nfrom numpy import cos, exp, ones, sin\n\nfrom gemseo.algos.design_space import DesignSpace\nfrom gemseo.algos.doe.doe_factory import DOEFactory\nfrom gemseo.algos.opt.opt_factory import OptimizersFactory\nfrom gemseo.algos.opt_problem import OptimizationProblem\nfrom gemseo.api import configure_logger, execute_post\nfrom gemseo.core.function import MDOFunction\n\nconfigure_logger()\n\nstandard_library.install_aliases()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the objective function\nWe define the objective function $f(x)=sin(x)-exp(x)$\nusing a :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` objects.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f_1 = MDOFunction(sin, name=\"f_1\", jac=cos, expr=\"sin(x)\")\nf_2 = MDOFunction(exp, name=\"f_2\", jac=exp, expr=\"exp(x)\")\nobjective = f_1 - f_2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. seealso::\n\n The following operators are implemented: addition, subtraction and multiplication.\n The minus operator is also defined.\n\n## Define the design space\nThen, we define the :class:`.DesignSpace` with |g|.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = DesignSpace()\ndesign_space.add_variable(\"x\", 1, l_b=-2.0, u_b=2.0, value=-0.5 * ones(1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the optimization problem\nThen, we define the :class:`.OptimizationProblem` with |g|.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem = OptimizationProblem(design_space)\nproblem.objective = objective"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve the optimization problem using an optimization algorithm\nFinally, we solve the optimization problems with |g| interface.\n\n### Solve the problem\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"opt = OptimizersFactory().execute(problem, \"L-BFGS-B\", normalize_design_space=True)\n\nprint(\"Optimum = \", opt)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that you can get all the optimization algorithms names:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_list = OptimizersFactory().algorithms\nprint(\"Available algorithms \", algo_list)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Save the optimization results\nWe can serialize the results for further exploitation.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem.export_hdf(\"my_optim.hdf5\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Post-process the results\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"execute_post(problem, \"OptHistoryView\", show=True, save=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Note
We can also save this plot using the arguments :code:`save=False`\n and :code:`file_path='file_path'`.
\n\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve the optimization problem using a DOE algorithm\nWe can also see this optimization problem as a trade-off\nand solve it by means of a design of experiments (DOE).\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"opt = DOEFactory().execute(problem, \"lhs\", n_samples=10, normalize_design_space=True)\nprint(\"Optimum = \", opt)"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK o`RD%
%
plot_simple_opt_1.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Analytical test case # 1\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we consider a simple optimization problem to illustrate\nalgorithms interfaces and :class:`.MDOFunction`.\n\n## Imports\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom future import standard_library\nfrom numpy import cos, exp, ones, sin\nfrom scipy import optimize\n\nfrom gemseo.api import configure_logger\nfrom gemseo.core.function import MDOFunction\n\nconfigure_logger()\n\nstandard_library.install_aliases()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the objective function\nWe define the objective function $f(x)=sin(x)-exp(x)$\nusing a :class:`.MDOFunction` defined by the sum of :class:`.MDOFunction` s.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"f_1 = MDOFunction(sin, name=\"f_1\", jac=cos, expr=\"sin(x)\")\nf_2 = MDOFunction(exp, name=\"f_2\", jac=exp, expr=\"exp(x)\")\nobjective = f_1 - f_2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
".. seealso::\n\n The following operators are implemented: $+$, $-$\n and $*$. The minus operator is also defined.\n\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"print(\"Objective function = \", objective)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Minimize the objective function\nWe want to minimize this objective function over $[-2,2]$,\nstarting from 1.\nWe use scipy.optimize for illustration.\n\nNote
:class:`.MDOFunction` objects are callable like a Python function.
\n\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"x_0 = -ones(1)\nopt = optimize.fmin_l_bfgs_b(objective, x_0, fprime=objective.jac, bounds=[(-0.2, 2.0)])\n\nprint(\"Optimum = \", opt)"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK p`R%. plot_simple_opt_3.ipynb{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Analytical test case # 3\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we consider a simple optimization problem to illustrate\nalgorithms interfaces and DOE libraries integration.\nInteger variables are used\n\n## Imports\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from __future__ import absolute_import, division, print_function, unicode_literals\n\nfrom future import standard_library\nfrom numpy import sum as np_sum\n\nfrom gemseo.algos.design_space import DesignSpace\nfrom gemseo.algos.doe.doe_factory import DOEFactory\nfrom gemseo.algos.opt_problem import OptimizationProblem\nfrom gemseo.api import configure_logger, execute_post\nfrom gemseo.core.function import MDOFunction\n\nstandard_library.install_aliases()\nLOGGER = configure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the objective function\nWe define the objective function $f(x)=\\sum_{i=1}^dx_i$\nusing a :class:`.MDOFunction`.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"objective = MDOFunction(np_sum, name=\"f\", expr=\"sum(x)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the design space\nThen, we define the :class:`.DesignSpace` with |g|.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"design_space = DesignSpace()\ndesign_space.add_variable(\"x\", 2, l_b=-5, u_b=5, var_type=\"integer\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the optimization problem\nThen, we define the :class:`.OptimizationProblem` with |g|.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"problem = OptimizationProblem(design_space)\nproblem.objective = objective"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solve the optimization problem using a DOE algorithm\nWe can see this optimization problem as a trade-off\nand solve it by means of a design of experiments (DOE),\ne.g. full factorial design\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"DOEFactory().execute(problem, \"fullfact\", n_samples=11 ** 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Post-process the results\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"execute_post(\n problem, \"ScatterPlotMatrix\", variables_list=[\"x\", \"f\"], save=False, show=True\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that you can get all the optimization algorithms names:\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"algo_list = DOEFactory().algorithms\nprint(\"Available algorithms \", algo_list)"
]
}
],
"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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK p`Rn plot_simple_opt_2.ipynbPK o`RD%
%
F plot_simple_opt_1.ipynbPK p`R%. % plot_simple_opt_3.ipynbPK 7