Optimization of Two Variable Function
The function to be optimized is given by:
[pic]
The maximum value of this two variable function is desired, however Matlab’s gatool finds the minimum of fitness functions and so as in the previous example the function must be altered as follows:
[pic]
Now we must enter this function, as before, into a Matlab function file.
Start Matlab and change the working directory to your Knowledge Based Systems folder. (i.e. U:\Current Class\KBS\) Create an m-file by either typing “edit fitness2” at the command prompt, or clicking the new file icon on the toolbar. The Matlab Genetic Algorithm accepts multiple variable functions, however these variables must be contained in an array. Therefore every “x” in the above equation is replaced with “x(1)” and every “y” with “x(2)”. Once the m-file editor is open, enter the following code:
function y = fitness2(x) y = -((1-x(1))^2*exp(-x(1)^2-(x(2)+1)^2) - (x(1) - x(1)^3 - x(2)^3)*exp(-x(1)^2-x(2)^2)); end
Save the file. (Note: The file must be saved under the same name as the function name. i.e. fitness2 as shown above)
In the Matlab command window type “gatool”. This will open the genetic algorithm tool as shown in Figure 1.
[pic]
Enter the name of your fitness function in the Fitness function text box preceded with an @ symbol as shown in Figure 1. Enter 2 for the number of variables and select Best fitness as the plot option.
Run the solver with all the default settings and observe results similar to Figure 2.
[pic]
The optimum value of x can be seen after the simulation in the gatool window in the Final point field. Record this number
Next modify the number of generations in the “Stopping Criteria” drop box. Change the number of generations to 25 and re-run the solution. Your results should resemble those of Figure 3.
[pic]
It should be noted that the Matlab Genetic