cs6665 10 optimtool ga

55
Using optimtool/ga in Matlab 1

Upload: illyes

Post on 27-Nov-2014

432 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS6665 10 Optimtool GA

1

Using optimtool/ga in Matlab

Page 2: CS6665 10 Optimtool GA

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

Page 3: CS6665 10 Optimtool GA

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

Page 4: CS6665 10 Optimtool GA

4

Page 5: CS6665 10 Optimtool GA

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.

Page 6: CS6665 10 Optimtool GA

6

Page 7: CS6665 10 Optimtool GA

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

Page 8: CS6665 10 Optimtool GA

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)

Page 9: CS6665 10 Optimtool GA

9

Page 10: CS6665 10 Optimtool GA

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

Page 11: CS6665 10 Optimtool GA

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.

Page 12: CS6665 10 Optimtool GA

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.

Page 13: CS6665 10 Optimtool GA

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.

Page 14: CS6665 10 Optimtool GA

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.

Page 15: CS6665 10 Optimtool GA

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.

Page 16: CS6665 10 Optimtool GA

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

Page 17: CS6665 10 Optimtool GA

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

Page 18: CS6665 10 Optimtool GA

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:

Page 19: CS6665 10 Optimtool GA

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.

Page 20: CS6665 10 Optimtool GA

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.

Page 21: CS6665 10 Optimtool GA

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.

Page 22: CS6665 10 Optimtool GA

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.

Page 23: CS6665 10 Optimtool GA

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.

Page 24: CS6665 10 Optimtool GA

24

Page 25: CS6665 10 Optimtool GA

25

Page 26: CS6665 10 Optimtool GA

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…

Page 27: CS6665 10 Optimtool GA

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

Page 28: CS6665 10 Optimtool GA

28

Page 29: CS6665 10 Optimtool GA

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.

Page 30: CS6665 10 Optimtool GA

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.

Page 31: CS6665 10 Optimtool GA

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.

Page 32: CS6665 10 Optimtool GA

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.

Page 33: CS6665 10 Optimtool GA

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.

Page 34: CS6665 10 Optimtool GA

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.

Page 35: CS6665 10 Optimtool GA

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.

Page 36: CS6665 10 Optimtool GA

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

Page 37: CS6665 10 Optimtool GA

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.

Page 38: CS6665 10 Optimtool GA

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

Page 39: CS6665 10 Optimtool GA

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

Page 40: CS6665 10 Optimtool GA

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.

Page 41: CS6665 10 Optimtool GA

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

Page 42: CS6665 10 Optimtool GA

42

Fitness Function

• Note that the previous fitness function called the function ConvertBinary.

• The file in which CalculateFitness is stored is called CalculateFitness

Page 43: CS6665 10 Optimtool GA

43

Example Continuing

• Now, as before, the first step is to select ga Genetic Algorithm in the Solver box

Page 44: CS6665 10 Optimtool GA

44

Page 45: CS6665 10 Optimtool GA

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

Page 46: CS6665 10 Optimtool GA

46

Page 47: CS6665 10 Optimtool GA

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

Page 48: CS6665 10 Optimtool GA

48

Page 49: CS6665 10 Optimtool GA

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.

Page 50: CS6665 10 Optimtool GA

50

Page 51: CS6665 10 Optimtool GA

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

Page 52: CS6665 10 Optimtool GA

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

Page 53: CS6665 10 Optimtool GA

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

Page 54: CS6665 10 Optimtool GA

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

Page 55: CS6665 10 Optimtool GA

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)