CS6665 10 Optimtool GA
CS6665 10 Optimtool GA
CS6665 10 Optimtool GA
Optimtool
Optimtool is an optimization tool with several different applications, one of which is genetic algorithms. These slides are to show how to use optimtool with two kinds of chromosomes:
Binary chromosomes Double vector chromosomes
Function to Optimize
The goal of this optimization example is to find the minimum for the following function. Its plot is on the next slide:
f ( x ) ! x 5 12.1x 4 40.59 x 3 17.015 x 2 71.95 x 35.88
Optimtool
Optimtool is invoked by typing optimtool at the Matlab prompt. Once started, you will see the window on the next page: Don t be intimidated by the complexity and the number of parameters of this window. Much of what we will use is the default. Note also that on the right side of the window there are references for the various options. Selecting one of these options will expand the window to give further explanation of the option.
5
11
Question
Since Optimtool only works to find the minimum could you use it if the fitness function was for a maximum?
Yes, simply use 1/fitness If fitness can go to zero, then you might want to use 1/(1+fitness) Of course if fitness can be -1, then that solution won t work, and another must be used.
13
Fitness Function
Note that in optimtool the fitness function is defined without parameters while in the Matlab definition, the parameter X is shown.
This means that in your Matlab workspace there must be only one function with that name. If you have a chromosome with say 10 genes, then polyVal will still be defined as an m-file with one parameter. The difference is that in the function you must recognize that what is passed is really a vector (X) with 10 elements.
15
18
19
Initial population
Leave this blank so that optimtool creates the initial population
Initial scores leave this blank so that optimtool will calculate the fitness for the initial population.
21
Scaling: default or experiment Selection: Default or experiment For all of the rest, you can use the default, expect you should also experiment. Note: The generations under stopping criteria should generally be set higher than 100.
22
23
24
25
28
30
31
In the m-file that calculates the fitness, we will need to convert the 23 bit string into 3 equivalent decimal values.
33
34
35
function [ output ] = ConvertPortionToBinary( input_string, position, number_of_bits ) %Takes a binary row vector (input_string) as input and % converts it to its decimal equivalent. % position is where in input to begin the conversion % number_of_bits is the number of bits starting at position to convert % The decimal equivalent is output in the parameter output output=0; % We begin by extracting from the total string the individual bits that are % needed portion = input_string(position:position+number_of_bits-1); % Since the string portion is of the form [1 0 1 1 0 ...], we need to % compact it so the spaces between binary digits is removed s = strrep(int2str(portion),' ',''); % And finally, this parts creates in output the actual decimal equivalent j=1; L=length(s); while(j<=L) output=output * 2 + bin2dec(s(j)); j=j+1; end end
36
37
That means our conversion from a binary string to a float will be: dec_val=8.25(DecimalValueofString/1023)-1.5
38
39
Binary Chromosome
What if we want to maximize a fitness function f(x)?
Although Optimtool only finds a minimum, it can still be used.
Instead of returning f(x), return 1/f(x) Remember that if f(x) can be 0, you must not allow this to happen, e.g. 1/(1+f(x))
The next slide shows the fitness function for the single gene example.
40
function [ fitness ] = CalculateFitness(Bstring) % Takes a binary row vector (Bstring) as input % This string is 10 bits and hence 0 to 1023 in decimal % Converts the vector to a decimal number in the % range -1.5 to 6.75 6.75-(-1.5) = 8.25 [DecimalValue]=ConvertBinary(Bstring) DecimalValue=8.25*(DecimalValue/1023) - 1.5 fitness= DecimalValue^5-12.1*DecimalValue^4+40.59*... DecimalValue^3-17.015*DecimalValue^2-71.95*DecimalValue+35.88; end function [ output ] = ConvertBinary( input ) %Takes a binary row vector as input %Converts it to its decimal equivalent. output=0; s = strrep(int2str(input),' ',''); j=1; L=length(s); while(j<=L) output=output * 2 + bin2dec(s(j)); j=j+1; end end
41
Fitness Function
Note that the previous fitness function called the function ConvertBinary. The file in which CalculateFitness is stored is called CalculateFitness
42
Example Continuing
Now, as before, the first step is to select ga Genetic Algorithm in the Solver box
43
44
45
46
48
The result is shown as before on the next slide. Notice that at the bottom of the slide the final (optimum) point is given in decimal.
49
50
Result
From the previous slide, the actual (binary) chromosome value was displayed as: 1101111100. Thus the decimal value of x for the best fitness is: 11011111002 = 89210 8.25(892/1023)-1.5 = 5.69
51
Homework 4
All of what has been shown for the Binary chromosome works for homework 4 with one variation. Note that in this homework the fitness calculation will need to be different.
In the preceding example, the equation for the fitness function was already parameterized and you were looking for a single value of x
f ( x ) ! x 5 12.1x 4 40.59 x 3 17.01x 2 71.95 x 35.88
52
Homework 4
In Homework 4 you have multiple variables, i.e. AF and your function is not parameterized
53
Homework 4
There are multiple ways of addressing this problem:
Embed H4Data as a declared array in your fitness function If H4Data is already a matrix in your Matlab work space, you can sort of pass H4Data directly to your fitness function
Rather than declaring a fitness function in the fitness function window as: @CalculateFitness
54
Homework 4
Rather than declaring a fitness function in the fitness function window as:
@CalculateFitness
Declare it as @(X)CalculateFitness(X,H4Data) The assumption is that H4Data already exists in the Matlab workspace You also now need to alter the CalculateFitness declaration to be CalculateFitness(X,H4Data)
55