⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaoptionsdemo.html

📁 遗传算法工具包
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd">   <head>      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">         <!--This HTML is auto-generated from an M-file.To make changes, update the M-file and republish this document.      -->      <title>Genetic Algorithm options</title>      <meta name="generator" content="MATLAB 7.0.0.4032 (R14) Prerelease">      <meta name="date" content="2004-03-29">      <meta name="m-file" content="gaoptionsdemo">      <meta name="title" content="Genetic Algorithm options">      <meta name="description" content="This is a demonstration of how to create and manage options for the genetic algorithm function GA using GAOPTIMSET in the Genetic Algorithm and Direct Search Toolbox."><style>body {  background-color: white;}h1 {  color: #990000;   font-size: x-large;}h2 {  color: #990000;  font-size: medium;}p.footer {  text-align: right;  font-size: xx-small;  font-weight: lighter;  font-style: italic;  color: gray;}pre.codeinput {  margin-left: 30px;}span.keyword {color: #0000FF}span.comment {color: #228B22}span.string {color: #A020F0}span.untermstring {color: #B20000}span.syscmd {color: #B28C00}pre.showbuttons {  margin-left: 30px;  border: solid black 2px;  padding: 4px;  background: #EBEFF3;}pre.codeoutput {  color: gray;  font-style: italic;}pre.error {  color: red;}    </style></head>   <body>      <h1>Genetic Algorithm options</h1>      <p>This is a demonstration of how to create and manage options for the genetic algorithm function GA using GAOPTIMSET in the         Genetic Algorithm and Direct Search Toolbox.      </p>      <h2>Contents</h2>      <ul>         <li><a href="#1">Setting up a problem for GA</a></li>         <li><a href="#6">How the Genetic Algorithm works</a></li>         <li><a href="#7">Adding visualization</a></li>         <li><a href="#9">Specifying population options</a></li>         <li><a href="#16">Reproducing your results</a></li>         <li><a href="#24">Modifying the stopping criteria</a></li>         <li><a href="#26">Choosing GA operators</a></li>      </ul>      <h2>Setting up a problem for GA<a name="1"></a></h2>      <p>GA searches for an unconstrained minimum of a function using the genetic algorithm. For this demo we will use GA to minimize         the fitness function SHUFCN. SHUFCN is a real valued function of two variables.      </p>      <p>We can use the function PLOTOBJECTIVE in the toolbox to plot the function SHUFCN over the range = [-2 2;-2 2].</p><pre class="codeinput">plotobjective(@shufcn,[-2 2; -2 2]);</pre><img vspace="5" hspace="5" src="gaoptionsdemo_01.png"><p>To use the GA solver, we need to provide at least two input arguments, a fitness function and the number of variables in the         problem. The first two output arguments returned by GA are x, the best point found, and Fval, the function value at the best         point. A third output argument, exitFlag tells you the reason why GA stopped. GA can also return a fourth argument, Output,         which contains information about the performance of the solver.      </p><pre class="codeinput">FitnessFunction = @shufcn;numberOfVariables = 2;</pre><p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);fprintf(<span class="string">'The number of generations was : %d\n'</span>, Output.generations);fprintf(<span class="string">'The number of function evaluations was : %d\n'</span>, Output.funccount);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The number of generations was : 100The number of function evaluations was : 2000The best function value found was : -186.651</pre><p>Note that when you run this demo, your result may be different from the results shown; This will be explained in a section         later in this demo.      </p>      <h2>How the Genetic Algorithm works<a name="6"></a></h2>      <p>The Genetic Algorithm (GA) works on a population using a set of operators that are applied to the population. A population         is a set of points in the design space. The initial population is generated randomly by default. The next generation of the         population is computed using the fitness of the individuals in the current generation.      </p>      <h2>Adding visualization<a name="7"></a></h2>      <p>GA can accept one or more plot functions through an OPTIONS argument. This feature is useful for visualizing the performance         of the solver at run time. Plot functions can be selected using GAOPTIMSET. The help for GAOPTIMSET contains a list of plot         functions to choose from.      </p>      <p>Here we use GAOPTIMSET to create an options structure to select two plot functions. The first plot function is GAPLOTBESTF,         which plots the best and mean score of the population at every generation. The second plot function is GAPLOTSTOPPING, which         plots the percentage of stopping criteria satisfied.      </p><pre class="codeinput">opts = gaoptimset(<span class="string">'PlotFcns'</span>,{@gaplotbestf,@gaplotstopping});</pre><p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);</pre><pre class="codeoutput">Optimization terminated: stop requested.</pre><img vspace="5" hspace="5" src="gaoptionsdemo_02.png"><h2>Specifying population options<a name="9"></a></h2>      <p>The default initial population is created using a uniform random number generator. Default values for the population size         and the range of the initial population are used to create the initial population.      </p>      <p><b>Specify a population size</b></p>      <p>The default population size used by GA is 20. This may not be sufficient for problems with a large number of variables; a         smaller population size may be sufficient for smaller problems. Since we only have two variables, we specify a population         size of 10. We will pass our options structure 'opts', created above, to GAOPTIMSET to modify the value of the parameter 'PopulationSize'         to be 10.      </p><pre class="codeinput">opts = gaoptimset(opts,<span class="string">'PopulationSize'</span>,10);</pre><p><b>Specify initial population range</b></p>      <p>The initial population is generated using a uniform random number generator in a default range of [0;1]. This creates an initial         population where all the points are in the range 0 to 1. For example, a population of size 3 in a problem with two variables         could look like:      </p><pre class="codeinput">Population = rand(3,2)</pre><pre class="codeoutput">Population =    0.9637    0.7889    0.4736    0.3356    0.5621    0.1337</pre><p>The initial range can be set by changing the 'PopInitRange' option using GAOPTIMSET. The range must be a matrix with two rows.         If the range has only one column, i.e., it is 2-by-1, then the range of every variable is the given range. For example, if         we set the range to [-1; 1], then the initial range for both our variables is -1 to 1. To specify a different initial range         for each variable, the range must be specified as a matrix with two rows and 'numberOfVariables' columns. For example if we         set the range to [-1 0;   1 2], then the first variable will be in the range -1 to 1, and the second variable will be in the         range 0 to 2 (so each column corresponds to a variable).      </p>      <p>The initial range can be specifed using GAOPTIMSET. We will pass our options structure 'opts'  created above to GAOPTIMSET         to modify the value of the parameter 'PopInitRange'.      </p><pre class="codeinput">opts = gaoptimset(opts,<span class="string">'PopInitRange'</span>,[-1 0;1 2]);</pre><p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);fprintf(<span class="string">'The number of generations was : %d\n'</span>, Output.generations);fprintf(<span class="string">'The number of function evaluations was : %d\n'</span>, Output.funccount);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: stall generations limit exceeded.The number of generations was : 60The number of function evaluations was : 600The best function value found was : -185.551</pre><img vspace="5" hspace="5" src="gaoptionsdemo_03.png"><h2>Reproducing your results<a name="16"></a></h2>      <p>By default, GA starts with a random initial population which is created using MATLAB random number generators. The next generation         is produced using GA operators that also use these same random number generators. Every time a random number is generated,         the state of the random number generators change. This means that even if you do not change any options, when you run again         you may get different results.      </p>      <p>Here we run the solver twice to show this phenomenon.</p>      <p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The best function value found was : -186.724</pre><p>Run GA again.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The best function value found was : -186.714</pre><p>In the previous two runs GA might give different results. The results are different because the states of the random number         generators have changed from one run to another.      </p>      <p>We can reproduce our results if we reset the states of the random number generators by using information returned by GA. GA         returns the states of the random number generators at the time you call GA in the Output argument. This information can be         used to reset the states. Here we reset the states using this output information so the results of the next two runs are the         same.      </p>      <p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The best function value found was : -186.73</pre><p>Reset the states.</p><pre class="codeinput">rand(<span class="string">'state'</span>,Output.randstate);randn(<span class="string">'state'</span>,Output.randnstate);</pre><p>Run GA again.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The best function value found was : -186.73</pre><h2>Modifying the stopping criteria<a name="24"></a></h2>      <p>GA uses four different criteria to determine when to stop the solver. GA stops when the maximum number of generations is reached;         by default this number is 100. GA also detects if there is no change in the best fitness value for some time given in seconds         (stall time limit), or for some number of generations (stall generation limit). Another criteria is the maximum time limit         in seconds. Here we modify the stopping criteria to increase the maximum number of generations to 150 and the stall generations         limit to 100.      </p><pre class="codeinput">opts = gaoptimset(opts,<span class="string">'Generations'</span>,150,<span class="string">'StallGenLimit'</span>, 100);</pre><p>Run the GA solver again.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);fprintf(<span class="string">'The number of generations was : %d\n'</span>, Output.generations);fprintf(<span class="string">'The number of function evaluations was : %d\n'</span>, Output.funccount);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The number of generations was : 150The number of function evaluations was : 1500The best function value found was : -186.675</pre><img vspace="5" hspace="5" src="gaoptionsdemo_04.png"><h2>Choosing GA operators<a name="26"></a></h2>      <p>GA starts with a random set of points in the population and uses operators to produce the next generation of the population.         The different operators are scaling, selection, crossover, and mutation. The toolbox provides several functions to choose         from for each operator. Here we choose FITSCALINGPROP for 'FitnessScalingFcn' and SELECTIONTOURNAMENT for 'SelectionFcn'.      </p><pre class="codeinput">opts = gaoptimset(<span class="string">'SelectionFcn'</span>,@selectiontournament, <span class="keyword">...</span>    <span class="string">'FitnessScalingFcn'</span>,@fitscalingprop);</pre><p>Run the GA solver.</p><pre class="codeinput">[x,Fval,exitFlag,Output] = ga(FitnessFunction,numberOfVariables,opts);fprintf(<span class="string">'The number of generations was : %d\n'</span>, Output.generations);fprintf(<span class="string">'The number of function evaluations was : %d\n'</span>, Output.funccount);fprintf(<span class="string">'The best function value found was : %g\n'</span>, Fval);</pre><pre class="codeoutput">Optimization terminated: maximum number of generations exceeded.The number of generations was : 100The number of function evaluations was : 2000The best function value found was : -186.678

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -