1
Using optimtool/ga in Matlab
2
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
3
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:
• Notice that the minimum ~-165 occurs at x=~5.75
88.3595.71015.1759.401.12)( 2345 xxxxxxf 88.3595.71015.1759.401.12)( 2345 xxxxxxf
4
5
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.
6
7
Optimtool – Double Vector or Binary Chromosome
• A double vector chromosome is simply a row vector of n double values. These values can be thought of as genes. Thus 4 genes/chromosome means a double vector of 4 elements.
• A binary chromosome is a k bit vector of binary values– Note that for this chromosome, genes are not actually
delineated. In other words if one needed a chromosome with 4 genes of 2, 8, 9, and 7 bits each, then one would specify a 26 bit chromosome
8
Optimtool –Double Vector Chromosome
• In order to use Optimtool as a genetic algorithm solver, one must select “ga – Genetic Algorithm” in the solver box. (Next slide)
9
10
Optimtool –Double Vector Chromosome
• When using double vectors, you don’t have as much latitude compared to binary in how chromosomes change. Because a chromosome is made up of individual double precision values, crossover only occurs on the boundaries of the individual doubles. Mutation thus takes on more significance in terms of moving in the search space. This is especially true if your chromosome consists of a single double (gene).
11
Optimtool –Double Vector Chromosome
• Since in this problem, we have only one gene, we will only have a single parameter and thus fill in number of variables window with a 1
• Note:– With Double Vectors, a chromosome is a row
vector whose length is the number of genes in a chromosome.
12
Optimtool –Double Vector Chromosome
• Problem Segment (portion of the window labeled “Problem”– In this segment, one specifies a m-file that is to be used as a
fitness function and the number of variables. You must create the m-file in your Matlab workspace or working directory and reference it in optimtool as
@m-filename– In the definition of the m-file, for this example there will be
one passed parameter, namely a row vector of a single double representing the chromosome.
– The m-file must return a scalar. The Matlab GA’s goal is to find the minimum of the fitness function.
13
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.
14
Optimtool –Double Vector Chromosome
• On slide 3 we defined the fitness function. The following is the m-file (with no documentation) for that fitness function.
function [ funval ] = polyVal( X )%funval= X^5-12.1*X^4+40.59*X^3-17.015*X^2…
-71.95*X+35.88;end• For the preceding m-file fitness function, you would type
@polyVal in the fitness function window.
15
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.
16
Optimtool –Double Vector Chromosome
• Constraints– There are several options for setting constraints. In
essence what they allow you to do is restrict the range of values that each of the genes can have.
– For a three gene chromosome [x1 x2 x3] consider the following constraints:• -5.5 <= x1 <= 7.6
• 10 <= x2 <= 25
• -15.0 <= x3 <= -7.6
17
Optimtool –Double Vector Chromosome
• In this case, it would be simplest to use the lower and upper bounds boxes. In doing this, you have two choices.– Define two vectors in your work space and refer to them
in the windows, or define the vectors in the window.– Vectors in the Matlab work space>> low=[-5.5;10;-15];>> upp=[7.6 ;25; -7.6]And then in the upper and lower windows type low and
upp respectively
18
Optimtool –Double Vector Chromosome
• In this example problem, it would be simplest to use the lower and upper bounds boxes.– To specify the bounds in the windows type[-5.5;10;-15] in the window labeled Lower: and [7.6;
25; -7.6] in the window labeled Upper:
19
Optimtool –Double Vector Chromosome
• For our problem, we have only one gene/chromosome. Also, looking at the figure of slide 4 you can see that the range is (approximately) -1.5 to 6.75. Thus,– you can simply type [-1.5] and [6.75] in the upper
and lower windows.
20
Optimtool –Double Vector Chromosome - OPTIONS
• Even though the next portion of the optimtool window is the Run solver part, you must first set the options in the right hand side of the optimtool window.
• Population– Population type: this should be Double Vector– Population size: you can leave it at the default of
20 chromosomes or specify a larger population size in its specify: window.
21
Optimtool –Double Vector Chromosome - OPTIONS
• Creation function– Here you can use constraint dependent or feasible
population. Either will operate the same – (see the reference)
• 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.
22
Optimtool –Double Vector Chromosome - OPTIONS
• Initial range: - IMPORTANT– If you use the default, then your initial range for the
randomly chosen vectors and each gene will be [0;1]. In this case you should specify the initial range as the same as the lower and upper bounds, i.e. [-1.5;6.75]
• 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.
23
Optimtool –Double Vector Chromosome - OPTIONS
Once parameters, etc. have been set, select start in the Run solver window.
• In this example, for the plot window I chose a plot interval of 10 and best fitness and best individual.
• The next two slides shows a run.
24
25
26
Double Vector Example
• Although hard to read, in the window of the preceding slide there are three outputs to note:– Current iteration 51 (Since we have stopped, this the number
of iterations of the GA required to arrive at this value)– In the window we see
optimization runningobjective function value: -165.9….Optimization terminated, average ….
– In the bottom box labeled final point is the value of the chromosome that gave the best (minimum) fitness of 165.9…
27
Double Vector Example
• It should be noted that there is a bug in the Matlab Ga/Optimtool. Specifically, if you use the default population size, the routine always stops at 51 generations. The fact that it says it stopped because of no change in the fitness is not necessarily correct.
• In order to not always stop at 51 iterations you must specify stall generations as other than 50 and a population size other than the default of 100
28
29
Optimtool – Binary Chromosome
• In general, a binary chromosome allows some additional flexibility in searching the space. For example, crossover is no longer constrained to occur on gene boundaries. It can occur within genes.
• Binary chromosomes also require a few different initial settings and a significantly different fitness function.
30
Binary Chromosome – Fitness Function
• As noted on slide 7, the number of bits in a chromosome is the sum of the number of bits in each gene, but how many bits are needed in each gene?
• The number of bits needed in each gene is dependent on the range of values for that gene, and resolution desired.
31
Binary Strings in Matlab/Optimtool
• When you specify a binary vector, you specify the number of bits as the total number of bits in all of the variables.– As far as optimtool is concerned, a chromosome is
just a string of bits. It is up to the writer of the fitness function to separate these bits into individual genes and then evaluate the genes, i.e convert them into decimal values.
32
Binary Chromosome Vector Example
• Let’s say that we have three genes, a resolution for each gene of 0.1, and each gene has a range of values of:– Gene 1: -1 to 1 => 2/0.1 = 20 values or 5 bits– Gene 2: 10 to 20 => 10/0.1 = 100 values or 7 bits– Gene 3: -100 to 10 => 110/0.1 = 1100 values or 11
bits– Thus, total gene size is 23 bits.
33
Binary Chromosome Vector Example
• We will assume that the fitness is simply the sum of the values of the individual genes.– What this means is that our optimum will be a
gene of all 0’s.• In the m-file that calculates the fitness, we will
need to convert the 23 bit string into 3 equivalent decimal values.
34
Optimtool/Matlab Binary Strings
• In Optimtool, if you specify, under population type, a binary string(vector), and under the number of variables 23, then each string is a random string of 23 bits, e.g. it is of the form– String=[1 0 1 0 … 0] % for a total of 23 bits The
next two slides show how to extract a portion of such a string to get a particular gene’s decimal equivalent value.
35
Optimtool/Matlab Binary Strings
– The actual fitness function would call ConvertPortionToBinary for each of the genes in the chromosome.
– Finally, the fitness function would simply, for the fitness of the given chromosome, return the sum of these individual gene values.• Obviously, for most problems, the fitness function itself
would need to perform a more complicated function.
36
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% neededportion = 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 removeds = strrep(int2str(portion),' ','');% And finally, this parts creates in output the actual decimal equivalentj=1;L=length(s);while(j<=L) output=output * 2 + bin2dec(s(j)); j=j+1;endend
37
Binary Chromosome – Bits in Gene for Single Gene Example
• In this problem, there is only one gene, and its values range from -1.5 to 6.75.
• Let’s say we want a resolution of at least 0.01– That means there must be at least 8250 values in
the range -1.5 to 6.756.75 –(-1.5) = 8.25For a resolution of at least 0.01 which is 1/100, we
need at least 8.25(100) = 825 values.
38
Binary Chromosome – Bits in Gene
• What the preceding tells us is that we need a binary string that has at least 825 different values. Thus we need a binary string of at least 10 bits since 210 = 1024 > 825.– Remember that the actual decimal equivalent values of
this string will be 0 to 1023• That means our conversion from a binary string to
a float will be:dec_val=8.25(DecimalValueofString/1023)-
1.5
39
Binary Gene Fitness Function
• The fitness function for this example must– Convert the binary string to its floating point
equivalent (this will be x in the function on slide 3)– Substitute this value into the polynomial on slide 3
and evaluate the polynomial– Then return this value as the fitness
40
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.
41
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.5fitness= 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;endend
42
Fitness Function
• Note that the previous fitness function called the function ConvertBinary.
• The file in which CalculateFitness is stored is called CalculateFitness
43
Example Continuing
• Now, as before, the first step is to select ga Genetic Algorithm in the Solver box
44
45
Optimtool – Binary Chromosome
• Next you must assign a fitness function and specify the number of variables.– In this case the name of the function is
CalculateFitness (slide 41) and the number of variables is the number of bits in a chromosome which was calculated on slide 38 as 10 bits
– Note: don’t forget the @ in front of the fitness function name
46
47
Optimtool – Binary Chromosome
• Because this is a binary chromosome, you don’t need to specify any constraints.
• In the options segments specify:– Population type: Bit string– Population size: 50 (as an example)– Creation function: uniform– Generations: 500 (as an example)– Stall generations: 500 (as an example)– Other options: default
48
49
Optimtool – Binary Chromosome
• Now you are ready to run the ga– Click on the Start button in the run solver segment
• 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.
50
51
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
52
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
88.3595.7101.1759.401.12)( 2345 xxxxxxf
53
Homework 4
– In Homework 4 you have multiple variables, i.e. A-F and your function is not parameterized
– Also, to calculate the fitness, you need the H4Data table
54
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
55
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)