PK \VWQ plot_simple_opt_1.ipynb{
"cells": [
{
"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 annotations\n\nfrom numpy import cos\nfrom numpy import exp\nfrom numpy import ones\nfrom numpy import sin\nfrom scipy import optimize\n\nfrom gemseo import configure_logger\nfrom gemseo.core.mdofunctions.mdo_function import MDOFunction\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the objective function\nWe define the objective function $f(x)=\\sin(x)-\\exp(x)$\nusing an :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": [
"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\n
Note
: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)])\nopt"
]
}
],
"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
}PK ]VW/x plot_simple_opt_2.ipynb{
"cells": [
{
"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 annotations\n\nfrom numpy import cos\nfrom numpy import exp\nfrom numpy import ones\nfrom numpy import sin\n\nfrom gemseo import configure_logger\nfrom gemseo import execute_post\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.core.mdofunctions.mdo_function import MDOFunction\n\nconfigure_logger()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the objective function\nWe define the objective function $f(x)=\\sin(x)-\\exp(x)$\nusing an :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\", 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)\nopt"
]
},
{
"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": [
"OptimizersFactory().algorithms"
]
},
{
"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.to_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 ``save=False``\n and ``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)\nopt"
]
}
],
"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
}PK ]VWu` plot_simple_opt_3.ipynb{
"cells": [
{
"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 annotations\n\nfrom numpy import sum as np_sum\n\nfrom gemseo import configure_logger\nfrom gemseo import execute_post\nfrom gemseo.algos.design_space import DesignSpace\nfrom gemseo.algos.doe.doe_factory import DOEFactory\nfrom gemseo.algos.opt_problem import OptimizationProblem\nfrom gemseo.core.mdofunctions.mdo_function import MDOFunction\n\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 an :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,\n \"ScatterPlotMatrix\",\n variable_names=[\"x\", \"f\"],\n save=False,\n 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": [
"DOEFactory().algorithms"
]
}
],
"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
}PK \VWQ plot_simple_opt_1.ipynbPK ]VW/x " plot_simple_opt_2.ipynbPK ]VWu` # plot_simple_opt_3.ipynbPK 3