{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Genetic Algorithm \n", "\n", "In this notebook, we implement a new selection operator (tournament selection) and a new cost function (for the Goodwin oscillator). At the end, there is also code to plot the parameters as they change over the generations." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import os.path\n", "import scipy.linalg\n", "import scipy.integrate\n", "import scipy.signal\n", "import matplotlib.cm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Support function for GA\n", "Copy-paste your copy of generateIndividual, generateChild, and GA below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Update code to include linear ranking selection\n", "\n", "Copy-paste your GASettings definition and add to it a field named linearRankingRatio. Give it a default value of 4." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copy-paste your select_pool below. Add to it support for linear ranking selection (call it \"linearRanking\").\n", "\n", "In linear rankng selection, we compute a discrete probability distribution with the probability of each individual in the previous generation being chosen for the breeding pool. The probabilities are constructed so that the probability of choosing the lowest-cost individual is settings.linearRankingRatio times the probability of choosing the highest cost individual. For each individual we want to add to the pool we use this probability distribution by choosing a number from a uniform distribution between 0 and 1 and then determining which index in the cumulative distribution is the smallest such that the cumulative distribution is larger than the random number.\n", "\n", "The algorithm is this. If $n$ is the number of individuals in the previous generation and $r$ is the linearRankingRatio, then we construct the probability distribution according to\n", "\n", "