{ "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 division, unicode_literals\n\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.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 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": [ "
We can also save this plot using the arguments :code:`save=False`\n and :code:`file_path='file_path'`.