{ "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 division, unicode_literals\n\nfrom numpy import cos, exp, ones, sin\nfrom scipy import optimize\n\nfrom gemseo.api 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 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\n
:class:`.MDOFunction` objects are callable like a Python function.