{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from __future__ import division\n", "import numpy as np\n", "from matplotlib import pyplot as plt " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# NOTE: make sure that for the following, the\n", "# input x is either a 1-dimensional numpy.array\n", "# or a list \n", "\n", "# sphere function\n", "def sphere(x, alpha=0.5):\n", " return alpha * np.sum(np.array(x)**2)\n", "\n", "# derivative of sphere function\n", "def sphere_der(x, alpha=0.5):\n", " return 2*alpha*np.array(x)\n", "\n", "# ellipsoid with condition number 10^alpha\n", "# make sure to have python 3 or 'division' imported\n", "def elli(x, alpha=1):\n", " s = np.array(x)\n", " n = len(s) # problem dimension\n", " sum = 0\n", " for i in range(n):\n", " sum = sum + s[i]*s[i]*(10**alpha)**(i/(n-1))\n", " sum = sum / 2\n", " return sum\n", "\n", "# derivative of ellipsoid function\n", "def elli_der(x, alpha=1):\n", " s = np.array(x)\n", " n = len(s) # problem dimension\n", " der = np.zeros(n) \n", " for i in range(n):\n", " der[i] = s[i] * (10**alpha)**(i/(n-1))\n", " return der\n", "\n", "# Hessian matrix of ellipsoid function at solution x\n", "def elli_hess(x, alpha=1):\n", " n = len(x)\n", " H = np.eye(n)\n", " for i in range(n):\n", " H[i,i] = (10**alpha)**(i/(n-1))\n", " return H\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plotting some level sets of sphere function:\n", "delta = 0.025\n", "x = np.arange(-2.0, 2.0, delta)\n", "y = np.arange(-2.0, 2.0, delta)\n", "X, Y = np.meshgrid(x, y)\n", "f = np.zeros((X.shape[0], X.shape[1]))\n", "for i in range(X.shape[0]):\n", " for j in range(X.shape[1]):\n", " f[i][j] = sphere([X[i][j], Y[i][j]], alpha=2)\n", " \n", "plt.figure(figsize=(6,6))\n", "plt.contour(X, Y, f, levels=0.2*np.array(range(1, 20)))\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plotting some level sets of ellipsoid function for varying alpha:\n", "delta = 0.025\n", "x = np.arange(-2.0, 2.0, delta)\n", "y = np.arange(-2.0, 2.0, delta)\n", "X, Y = np.meshgrid(x, y)\n", "felli_m1 = np.zeros((X.shape[0], X.shape[1])) # for plot with alpha=-1\n", "felli_0 = np.zeros((X.shape[0], X.shape[1])) # for plot with alpha=0\n", "felli_1 = np.zeros((X.shape[0], X.shape[1])) # for plot with alpha=1\n", "felli_3 = np.zeros((X.shape[0], X.shape[1])) # for plot with alpha=3\n", "for i in range(X.shape[0]):\n", " for j in range(X.shape[1]):\n", " felli_m1[i][j] = elli([X[i][j], Y[i][j]], alpha=-1)\n", " felli_0[i][j] = elli([X[i][j], Y[i][j]], alpha=0) \n", " felli_1[i][j] = elli([X[i][j], Y[i][j]], alpha=1)\n", " felli_3[i][j] = elli([X[i][j], Y[i][j]], alpha=3)\n", " \n", " \n", "plt.figure(figsize=(16,4))\n", "plt.subplot(141) # first plot of a 1x4 grid\n", "plt.contour(X, Y, felli_m1, levels=0.2*np.array(range(1, 20)))\n", "plt.title(\"Ellipsoid with alpha=-1\")\n", "plt.xlabel(\"$x_1$\")\n", "plt.ylabel(\"$x_2$\")\n", "plt.axis('equal')\n", "plt.subplot(142) # second plot of a 1x4 grid\n", "plt.contour(X, Y, felli_0, levels=0.2*np.array(range(1, 20)))\n", "plt.title(\"Ellipsoid with alpha=0\")\n", "plt.xlabel(\"$x_1$\")\n", "plt.ylabel(\"$x_2$\")\n", "plt.axis('equal')\n", "plt.subplot(143) # third plot of a 1x4 grid\n", "plt.contour(X, Y, felli_1, levels=0.2*np.array(range(1, 20)))\n", "plt.title(\"Ellipsoid with alpha=1\")\n", "plt.xlabel(\"$x_1$\")\n", "plt.ylabel(\"$x_2$\")\n", "plt.axis('equal')\n", "plt.subplot(144) # forth plot of a 1x4 grid\n", "plt.contour(X, Y, felli_3, levels=0.2*np.array(range(1, 20)))\n", "plt.title(\"Ellipsoid with alpha=3\")\n", "plt.xlabel(\"$x_1$\")\n", "plt.ylabel(\"$x_2$\")\n", "plt.axis('equal')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }