CS6665 10 Optimtool GA

August 29, 2017 | Author: Linh Lém Lỉnh | Category: Function (Mathematics), Bit, Vector Space, Genetic Algorithm, Mathematical Optimization
Share Embed Donate


Short Description

Download CS6665 10 Optimtool GA...

Description

Using optimtool/ga in Matlab

1

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

2

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: 5

4

3

2

f ( x) = x − 12.1x − 40.59 x − 17.015 x − 71.95 x + 35.88

• Notice that the minimum ~-165 occurs at x=~5.75

3

4

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

6

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 7

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)

8

9

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). 10

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.

11

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. 12

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

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. 14

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

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 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. 32

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.

33

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.

34

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.

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 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 38

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

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
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF