{ "metadata": { "name": "", "signature": "sha256:4d6348f7698218d06b7810f787c71ce8055e98c24b697cf65e919d8b6a0d6d46" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "pysolve tutorial" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_pysolve_ is a Python library written to aid in solving systems of (nonlinear) equations." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This tutorial will walk through and explain in greater detail how to use pysolve." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To install\n", "\n", "```\n", "pip install pysolve\n", "```\n", "\n", "Note that there are external dependencies. _pysolve_ is dependent on the _sympy_ and _numpy_ packages." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Specifying the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The sample simulates the SIM model from \"Monetary Economics\" by Godley and Lavoie (2006)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pysolve.model import Model\n", "\n", "def create_sim_model():\n", " model = Model()\n", "\n", " model.set_var_default(0)\n", " model.var('Cd', desc='Consumption goods demand by households')\n", " model.var('Cs', desc='Consumption goods supply')\n", " model.var('Gs', desc='Government goods, supply')\n", " model.var('Hh', desc='Cash money held by households')\n", " model.var('Hs', desc='Cash money supplied by the government')\n", " model.var('Nd', desc='Demand for labor')\n", " model.var('Ns', desc='Supply of labor')\n", " model.var('Td', desc='Taxes, demand')\n", " model.var('Ts', desc='Taxes, supply')\n", " model.var('Y', desc='Income = GDP')\n", " model.var('YD', desc='Disposable income of households')\n", "\n", " model.param('Gd', desc='Government goods, demand')\n", " model.param('W', desc='Wage rate')\n", " model.param('alpha1', desc='Propensity to consume out of income')\n", " model.param('alpha2', desc='Propensity to consume out of wealth')\n", " model.param('theta', desc='Tax rate')\n", "\n", " model.add('Cs = Cd') # 3.1\n", " model.add('Gs = Gd') # 3.2\n", " model.add('Ts = Td') # 3.3\n", " model.add('Ns = Nd') # 3.4\n", " model.add('YD = (W*Ns) - Ts') # 3.5\n", " model.add('Td = theta * W * Ns') # 3.6, theta < 1.0\n", " model.add('Cd = alpha1*YD + alpha2*Hh(-1)') # 3.7, 0 < alpha2 < alpha1 < 1\n", " model.add('Hs - Hs(-1) = Gd - Td') # 3.8\n", " model.add('Hh - Hh(-1) = YD - Cd') # 3.9\n", " model.add('Y = Cs + Gs') # 3.10\n", " model.add('Nd = Y/W') # 3.11\n", " \n", " return model\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are four main parts when creating a model.\n", "* Creation of the model object\n", "* Specifying the variables\n", "* Specifying the parameters\n", "* Specifying the equations" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Sample line 4** creates the model object. Currently, there are no parameters to the model object.\n", "```python\n", " model = Model()\n", "```" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Model attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* variables\n", "* parameters\n", "* equations\n", "* solutions" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **variable**, within _pysolve_, is a value that the solver is allowed to change when solving a system of equations. It also means that an equation defining the variable is required before attempting to solve the system.\n", "\n", "The solver will attempt to \"solve\" the equation by iterating, through whatever method is selected, until the value of all variables are within a specified tolerance.\n", "\n", "Variable specification is done in **Sample lines 6-17**.\n", "\n", "```python\n", " model.set_var_default(0)\n", " model.var('Cd', desc='Consumption goods demand by households')\n", " model.var('Cs', desc='Consumption goods supply')\n", " model.var('Gs', desc='Government goods, supply')\n", " model.var('Hh', desc='Cash money held by households')\n", " model.var('Hs', desc='Cash money supplied by the government')\n", " model.var('Nd', desc='Demand for labor')\n", " model.var('Ns', desc='Supply of labor')\n", " model.var('Td', desc='Taxes, demand')\n", " model.var('Ts', desc='Taxes, supply')\n", " model.var('Y', desc='Income = GDP')\n", " model.var('YD', desc='Disposable income of households')\n", "```\n", "\n", "The first parameter (and only required parameter) is the name of the variable. This is case-sensitive and must be unique (across all variables and parameters) within the model. In general, avoid using common mathematical names, such as I, E, pi, and nan, as these are used by sympy.\n", "\n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Accessing the variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable is returned from the call to _model.var()_ and this can be used to modify the variable. In addition, the variables can be retrieved from the model also. The following snippets of code will return the same variable for _x_:\n", "```python\n", " x_var = model.var('x')\n", "```\n", "is equivalent to\n", "```python\n", " model.var('x')\n", " x_var = model.variables['x']\n", "```" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "variable Attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* name\n", "* desc\n", "* default\n", "* value\n", "* model\n", "* equation" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Defaults" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is **highly recommended**, though not required, that defaults be given to the variables. This is done on **Sample line 6**.\n", "\n", "```python\n", " model.set_var_default(0)\n", "```\n", "\n", "The default value is used to give the variable an initial value when the variable is created. If variables are created **before** the call to _set_var_default()_, they will not have an initial value. If the variable is used before a value is assigned, this may cause hard-to-debug errors when running the simulation.\n", "\n", "Default values are also used in cases where values for a previous iteration are requested (but we may not have iterated far enough).\n", "\n", "Defaults may also be given to individual variables using the _default_ named parameter. This value will take precedence over the _set_var_default_ value.\n", "\n", "```python\n", " model.var('YD', desc='Disposable income', default=1.2)\n", "```\n", "\n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Creating multiple variables at once" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The creation of multiple variables at the same is also supported. However the ability to specify descriptions and defaults is not allowed. Although those values can be set separately.\n", "\n", "```python\n", " model.vars('x', 'y', 'z')\n", " \n", " # Note that setting the default will not set the value for x, as that is\n", " # done only during creation of the variable.\n", " model.variables['x'].default = 1.1\n", " model.variables['x'].description = 'variable for x'\n", " model.variables['x'].value = 2\n", "```\n" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A **parameter**, within _pysolve_, is a value that the solver is not allowed to change when solving a system of equations. That is, it is considered a constant by the solver. An error will be generated if an equation is used to define the value of a parameter. Note that I use the term \"parameter\" for true system parameters and exogenous variables.\n", "\n", "Note that the solver will not change the value of a parameter. But the value of a parameter may\n", "be changed in between iterations by the user.\n", "\n", "Parameter specification is done in **Sample lines 19-23**.\n", "\n", "```python\n", " model.param('Gd', desc='Government goods, demand')\n", " model.param('W', desc='Wage rate')\n", " model.param('alpha1', desc='Propensity to consume out of income')\n", " model.param('alpha2', desc='Propensity to consume out of wealth')\n", " model.param('theta', desc='Tax rate')\n", "```\n", "\n", "The first parameter (and only required parameter) is the name of the parameter. This is case-sensitive and must be unique (across all variables and parameters) within the model. In general, avoid using common mathematical names, such as I, E, pi, and nan, as these are used by sympy.\n", "\n", "\n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Accessing the parameters" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The parameter is returned from the call to _model.param()_ and this can be used to modify the parameter. In addition, the parameters can be retrieved from the model also. The following snippets of code will return the same parameter for _a_:\n", "```python\n", " a_param = model.var('a')\n", "```\n", "is equivalent to\n", "```python\n", " a_param = model.variables['a']\n", "```" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "parameter Attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* name\n", "* desc\n", "* default\n", "* value\n", "* model" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Defaults" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is **highly recommended**, though not required, that defaults be given to the parameters. For example,\n", "\n", "```python\n", " model.set_param_default(0)\n", "```\n", "\n", "The default value is used to give the parameter an initial value when the variable is created. If parameters are created **before** the call to _set_param_default()_, they will not have an initial value. If the parameter is used before a value is assigned, this may cause hard-to-debug errors when running the simulation.\n", "\n", "Default values are also used in cases where values for a previous iteration are requested (but we may not have iterated far enough).\n", "\n", "Defaults may also be given to individual parameters using the _default_ named parameter. This value will take precedence over the _set_param_default_ value.\n", "\n", "```python\n", " model.param('YD', desc='Disposable income', default=1.2)\n", "```\n" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Equations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The equations are at the heart of the model. However there are some specifics that \n", "must be understood when writing the equations. The equations may be non-linear, there is\n", "no linearity requirement. Because of this, the equations may or may not converge to a solution.\n", "\n", "Hers are the equations from **Sample lines 25-35**\n", "\n", "```python\n", " model.add('Cs = Cd') # 3.1\n", " model.add('Gs = Gd') # 3.2\n", " model.add('Ts = Td') # 3.3\n", " model.add('Ns = Nd') # 3.4\n", " model.add('YD = (W*Ns) - Ts') # 3.5\n", " model.add('Td = theta * W * Ns') # 3.6, theta < 1.0\n", " model.add('Cd = alpha1*YD + alpha2*Hh(-1)') # 3.7, 0 < alpha2 < alpha1 < 1\n", " model.add('Hs - Hs(-1) = Gd - Td') # 3.8\n", " model.add('Hh - Hh(-1) = YD - Cd') # 3.9\n", " model.add('Y = Cs + Gs') # 3.10\n", " model.add('Nd = Y/W') # 3.11\n", "\n", "```\n", "\n", "The model equations follow the python way of writing equations. Thus '*' is used for multiplication for example." ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Requirements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Each variable can only be solved for once**\n", "\n", " There cannot be two equations solving for a variable. For example, the following is not allowed\n", "```python\n", " x = y + z + ...\n", " x = log(y) + 2*z + ...\n", "```\n", "* **Each variable must have an equation defining it**\n", "\n", " There must be an equation that \"defines\" the variable. This is mostly a requirement for the Gauss-Seidel algorithm. Thus, for a variable _x_, there must be an equation of the form, _x = ..._\n", " \n", " \n", "* **The left-hand side of the equation must contain only one variable**\n", "\n", " The variable on the left-hand side of the equation is the variable being defined. Thus there can only be one variable on the left-hand side.\n", " \n", " This also means that there can be constants and parameters on the left-hand side. For example:\n", " \n", "```python\n", " # Good\n", " 10*x = .....\n", " x - x(-1) = ....\n", " \n", " # Bad\n", " x*y = ....\n", " log(x) = ...\n", "```\n", "\n", "* **The variable on the left-hand side must be of linear form**\n", "\n", " There cannot be an exponential power or inside of a function parameter.\n", "\n", "```python\n", " # Bad\n", " log(x) = ...\n", " x**2 = ...\n", "```\n", " \n", "\n", "* **Only variables can be solved for (no parameters)**\n", "\n", " Only variables can be solved for. Parameters cannot be defined.\n", " \n", " \n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Accessing previous iterations's values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the sample, there are some lines which look like this:\n", "```python\n", " model.add('Hh - Hh(-1) = YD - Cd') # 3.9\n", "```\n", "This equation uses the value of Hh from a previous iteration (or solution from a previous iteration). This is done with the \"Hh(-1)\". Using \"-1\" will refer to the previous iteration,\n", "\"-2\" will refer to the iteration before that, and so on.\n", "\n", "Values from a previous iteration are treated as parameters by the system.\n", "\n", "This will work for both parameters and variables." ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Functions available for use within an equation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **The delta ('d') function**\n", "\n", " This is a special function. The purpose of this is to reduce the amount of work in order to display the difference between the current value and the immediate past value. \n", "\n", " Thus\n", "```python\n", " d(x)\n", "```\n", " is equivalent to using\n", "```python\n", " x - x(-1)\n", "```\n", "\n", " However, \"d(x)\" cannot be used on the left-hand side of an equation.\n", "\n", "\n", "* **if_true**\n", "\n", " This function will return the value 1.0 if the argument is true, 0.0 otherwise.\n", "```python\n", " x = 5\n", " if_true(x >= 4)\n", "```\n", " The call to if_true() here will return 1.0.\n", "\n", "\n", "* **Abs**\n", "\n", " Absolute value\n", "\n", "\n", "* **exp**\n", "\n", " The natural exponential function.\n", "\n", "\n", "* **log**\n", "\n", " The natural logarithim.\n", "\n", "\n", "* **Max**\n", "\n", " Returns the maximum of two values.\n", "\n", "\n", "* **Min**\n", "\n", " Returns the mininum of two values.\n", "\n", "\n", "* **sqrt**\n", "\n", " Returns the square root of a value.\n", " \n", "\n", "* **sign**\n", "\n", " Returns the sign of the argument.\n", " \n" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Model.evaluate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a special function that is available. This takes a string argument. It will evaluate the string and then return the value.\n", "\n", "```python\n", " # assuming that x = 1 and y = 2\n", " # this will return the value 11\n", " val = model.evaluate('3*x + 4*y')\n", "```" ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Equation attributes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* equation\n", "* desc\n", "* model\n", "* expr\n", "* func\n", "* variable" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Solving the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To solve the model, we will run various iterative algorithms until the values converge. In addition, the Model object will store solutions from previous runs." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Sample" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pysolve.utils import is_close\n", "\n", "steady_state = create_sim_model()\n", "steady_state.set_values({'alpha1': 0.6,\n", " 'alpha2': 0.4,\n", " 'theta': 0.2,\n", " 'Gd': 20,\n", " 'W': 1})\n", "\n", "for _ in xrange(100):\n", " steady_state.solve(iterations=100, threshold=1e-4)\n", "\n", " prev_soln = steady_state.solutions[-2]\n", " soln = steady_state.solutions[-1]\n", " if is_close(prev_soln, soln, rtol=1e-4):\n", " break\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Setting initial values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the _set_values()_ function to change the values for a group of parameters for variables. Arguments to this function can either be a python dictionary() or a python list(). There are two use cases here.\n", "\n", "The specification of the value involves the name of the variable and the value. The value may either be a numeric value (integer or float) or may be a string value. In the case of a string value, the string will be evaluated with the current context of values.\n", "\n", "The python dictionary is the more natural, however, python dictionaries are unordered, so the actual order in which the variables are applied is not apparent.\n", "\n", "If a list ot tuples is passed in, the values will be evaluated in the order of the list, thus one can use the value of previous entries and use calculated values.\n", "\n", "Example using a dictionary\n", "```python\n", " model.set_values({'x': 1.1,\n", " 'y': 2.2})\n", "```\n", "\n", "Example using a list of tuples\n", "```python\n", " model.set_values([('x', 1.1),\n", " ('y', 'x*2')]\n", "```\n", "\n", "Using a formula for _y_ in the dictionary example is not guaranteed to work since the order of the dictionary is not guaranteed." ] }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Invoking solve()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```python\n", " model.solve(iterations=10, threshold=0.001, method='gauss-seidel', debuglist=None)\n", "```\n", "\n", "The method is pretty straightforward.\n", "\n", "* **iterations**\n", "\n", " This is the maximum number of iterations allowed until we reach convergence. If all of the variables fail to converge, then a SolutionNotFound() exception is raised.\n", "\n", "\n", "* **threshold**\n", "\n", " This is the relative tolerance that is used to test for convergence.\n", "\n", "\n", "* **method**\n", "\n", " This is the method used to find a solution. There are currently three options: 'gauss-seidel',\n", " 'newton-raphson', and 'broyden'. For 'gauss-seidel', the equations are evaluated in the order\n", " in which they are added.\n", " \n", " \n", "* **debuglist**\n", "\n", " If a list is passed in, then the debuglist will contain a list of the values that the solver\n", " is using, one entry per iteration.\n" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Using the model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the model refers to being able to access the solutions." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Sample" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is some code that prints out the values from the zeroth iteration (the initial values), the first to third iterations, and the final iteration.\n", "\n", "Note that the values are not quite whole. So at the end, I take the final solution, round off the values to 1 decimal place and then print it out. Note that _print_solutions()_ expects a list, which is why I convert _nice_solution_ into a list before invoking the function." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pysolve.utils import is_aclose, round_solution\n", "\n", "def print_solutions(solns, vars, indexes):\n", " print \"----------------\"\n", " s = \"{0:12} :\".format('subiter')\n", " for i in indexes:\n", " s += \" {0: >10} \".format(i)\n", " print s\n", " for x in vars:\n", " s = \"{0:12} :\".format(x[0])\n", " for i in indexes:\n", " s += \" {0: >10.6f}\".format(solns[i][x[1]])\n", " if i != 0:\n", " if not is_aclose([solns[i][x[1]],], [solns[i-1][x[1]],], rtol=1e-4):\n", " s += '*'\n", " else:\n", " s += ' '\n", " else:\n", " s += ' '\n", " print s\n", "\n", "ax = sorted([(str(k), k) for k,v in steady_state.solutions[0].items()], key=lambda x: x[0])\n", "print_solutions(steady_state.solutions, ax, [0, 1, 2, 3, -1])\n", "\n", "nice_solution = round_solution(steady_state.solutions[-1], decimals=1)\n", "print_solutions([nice_solution,], ax, [0])\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "----------------\n", "subiter : 0 1 2 3 -1 \n", "Cd : 0.000000 18.462208* 27.930344* 35.941876* 79.966725 \n", "Cs : 0.000000 18.460760* 27.928167* 35.939386* 79.966662 \n", "Gd : 20.000000 20.000000 20.000000 20.000000 20.000000 \n", "Gs : 0.000000 20.000000* 20.000000 20.000000 20.000000 \n", "Hh : 0.000000 12.308139* 22.722942* 31.535565* 79.965855 \n", "Hs : 0.000000 12.307511* 22.721372* 31.532915* 80.053316 \n", "Nd : 0.000000 38.460760* 47.928167* 55.939386* 99.966662 \n", "Ns : 0.000000 38.462444* 47.930699* 55.942282* 99.961105 \n", "Td : 0.000000 7.692489* 9.586140* 11.188456* 19.992221 \n", "Ts : 0.000000 7.692097* 9.585551* 11.187783* 19.992638 \n", "W : 1.000000 1.000000 1.000000 1.000000 1.000000 \n", "Y : 0.000000 38.460760* 47.928167* 55.939386* 99.966662 \n", "YD : 0.000000 30.770347* 38.345148* 44.754499* 79.968467 \n", "_Hh__1 : 0.000000 0.000000 12.308139* 22.722942* 79.964113 \n", "_Hs__1 : 0.000000 0.000000 12.307511* 22.721372* 80.045537 \n", "alpha1 : 0.600000 0.600000 0.600000 0.600000 0.600000 \n", "alpha2 : 0.400000 0.400000 0.400000 0.400000 0.400000 \n", "theta : 0.200000 0.200000 0.200000 0.200000 0.200000 \n", "----------------\n", "subiter : 0 \n", "Cd : 80.000000 \n", "Cs : 80.000000 \n", "Gd : 20.000000 \n", "Gs : 20.000000 \n", "Hh : 80.000000 \n", "Hs : 80.100000 \n", "Nd : 100.000000 \n", "Ns : 100.000000 \n", "Td : 20.000000 \n", "Ts : 20.000000 \n", "W : 1.000000 \n", "Y : 100.000000 \n", "YD : 80.000000 \n", "_Hh__1 : 80.000000 \n", "_Hs__1 : 80.000000 \n", "alpha1 : 0.600000 \n", "alpha2 : 0.400000 \n", "theta : 0.200000 \n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "The solutions data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An individual solution is just a python dictionary, mapping the variable/parameter name to a value." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from pprint import pprint\n", "\n", "pprint(steady_state.solutions[-1])" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "{'Cd': 79.96672536628363,\n", " 'Cs': 79.96666214153672,\n", " 'Gd': 20.0,\n", " 'Gs': 20.0,\n", " 'Hh': 79.96585457376605,\n", " 'Hs': 80.05331608214169,\n", " 'Nd': 99.96666214153672,\n", " 'Ns': 99.96110498893788,\n", " 'Td': 19.992220997787577,\n", " 'Ts': 19.992638037619106,\n", " 'W': 1.0,\n", " 'Y': 99.96666214153672,\n", " 'YD': 79.96846695131877,\n", " '_Hh__1': 79.9641129887309,\n", " '_Hs__1': 80.04553707992926,\n", " 'alpha1': 0.6,\n", " 'alpha2': 0.4,\n", " 'theta': 0.2}\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 6, "metadata": {}, "source": [ "Accessing previous solutions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The data in model.solutions is a list of solutions, where each individual solution is a dict() of parameter and variable values.\n", "\n", "Thus, the normal python list access and slicing features are available to access the information.\n", "\n", "So to access the last solution in the list\n", "```python\n", " model.solutions[-1]\n", "```\n", "To gather solutions from 5 to 10 (python lists are 0-based)\n", "```python\n", " model.solutions[5:10]\n", "```\n", "To get all solutions starting from 5 on\n", "```python\n", " model.solutions[5:]\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }