Download presentation
Presentation is loading. Please wait.
Published byBarnard York Modified over 9 years ago
2
Genetic Algorithm Optimization Using the HP Prime Presented by Namir Shammas 1
3
Dedication To Felix Gross for being a wonderful catalyst (pun intended) to visit his city of Berlin and for being a wonderful host. 2
4
Genetic Algorithms—The Basics A class of search heuristics that imitates the process of natural selection. Commonly known as GA. GA belongs to a bigger class of Evolutionary Algorithms (EA). Developed by John Holland in the 1960s at the University of Michigan. Also popularized by his student David E. Goldberg. 3
5
Evolutionary Algorithms— The Wild Bunch! Simulated annealing. Genetic algorithms. Differential algorithms. Ant colony optimization. Particle swarm optimization. And many others. The variants of evolutionary algorithms number near or above one hundred!! 4
6
GA—The Building Blocks Uses genes to represent variables. Uses chromosomes (made up several genes) to represent a data point with a set of variables. Each variable has a value based on a sequence of binary digits. The binary digits are mapped onto the ranges for each real-value variable. 5
7
GA—The Building Blocks Fitness function is basically the function to be optimized (minimized or maximized). The values for the function at different points (i.e. chromosomes) are cost functions. GA uses a population which corresponds to the number of observations (or probes). GA uses a number of generations that represent the number of iterations. 6
8
Why Stick with Binary Digits? Question that may come across your mind! “If you are storing 0s and 1s in a numeric matrix, why can’t we extend the representation of variables using 0 to 9?” Allows us to represent variables with large integers or reduce the number of digits. 7
9
Why Stick with Binary Digits? (Cont.) This approach SEEMS like a good idea, but its not! Main weakness is that using 0 to 9 lowers the variations in the values of the variables. The GA variant struggles with covering a wide range of values and reaching the solution. 8
10
GA—The Steps 1.Initialize the population of chromosomes with random values within ranges for the variables. 2.Select from the populations the fittest (one with best cost functions) chromosome to mate. Usually select the best half of the population. 3.Mate the fittest individual genes. Each two parents produce two children. 4.The children replace the lower half of unfit population of chromosomes. 9
11
GA—The Steps (Cont.) 3.(Ver b) Mate each entire chromosome at a single (or even multiple) crossover point. Reduces the variations in the values of variables. Each two parents produce two children. 4.(Ver b) The children replace only those population members that are less fit. This step adds more complexity: Must sort children according to fitness function. Merge the sorted children and the sorted current generation. 10
12
GA—The Steps (Cont.) 5.Selectively mutate the updated chromosomes. You can shield the elite chromosomes from this process to avoid losing good solutions. 6.Repeat steps 2 to 5 for the specified number of generations. You can include other criteria to stop the iterations if satisfactory results are achieved. 11
13
How to Initialize 12 Specify the number of bits for the genes/variables. Specify the range of values for each variable. Create a matrix of binary digits or Booleans. The number of rows equal the population size. The number of columns equal the number of variables TIMES the number of bits per variable. Set each matrix element to 1 or 0 (True of False) using a RNGs.
14
How to Initialize (Cont.) 13 Chromosome =[111001001,0011011111,…,0000101001] gene 1 gene 2 gene n
15
GA—Get New Generation 14 ChromosomeCost 001011110001107641 111001011001008128 001100100011006523 001011110010007637 110011111110118369 010001011110117903 111011000000017412 010011011100118140
16
Sorting the Function Values 15 Calculate the function value for each chromosome. Sort the function values and the chromosomes. You can use an index array to postpone swapping the chromosome values until the end of the sorting process.
17
GA—Sort by Cost Fx 16 ChromosomeCost 001100100011006523 111011000000017412 001011110010007637 001011110001107641 010001011110117903 111001011001008128 010011011100118140 110011111110118369
18
Selecting the Mates 17 Select pairs of the best chromosomes, starting with the fittest. Roulette selection by ranking of function value OR post-sorting position. Randomly select the fittest chromosomes using a inverse Gaussian distribution. This approach allows you to select randomly among the elite.
19
Roulette Selection 18 Y-axis values are calculated based cumulative sums of x(i)/∑x(i), where the x’s are ranks or function values.
20
Inverse Gaussian Selection 19 Use the absolute values of the resulting x’s to use the right side of the Gaussian curve and obtain the indices to the parents. Since the lowest value of the Gaussian curve is 0, you may need to add 1 (as is the case with PPL) to access the first element.
21
GA—Select for Mating 20 ChromosomeCost 001100100011006523 111011000000017412 001011110010007637 001011110001107641
22
Mating 21 With the parents paired, select a random crossover point to copy the chromosomes from the two parents to the two children.
23
GA—Mating 22 ChromosomeFamilyBinary Digits 3Mom(1)00101111001000 2Dad(1)11101100000001 5Child(1)00101100000001 6Child(2)11101111001000 3Mom(2)00101111001000 4Dad(2)00101111000110 7Child(3)00101111000110 8Child(4)00101111001000
24
Mutation 23 For the non-elite chromosome test the possibility that each binary digit can be mutated. For each digit, compare a uniform random number with the mutation rate. It the random number is less that the mutation rate, flip that digit.
25
GA—Mutation 24 00101100000001 => 0010100000001
26
GA—Iteration Results 25 Pop. After MatingPop After MutationNew Cost 00110010001100 6523 11101100000001 7412 00101111001000001011110100007585 00101111000110000010110001116528 00101100000001001010000000016829 11101111001000111101110100107854 00101111000110001001110010007284 00101111001000001101110010007897
27
How to Enhance GA? 26 There are many ways to enhance the basic GA. Use a variant of the basic GA to handle continuous (i.e. floating point) variables. This change speeds up calculations since we don’t need to map values from binary digits to floating-point values. The algorithm implementation has to be adapted for floating-point variables.
28
How to Enhance GA? (Cont.) 27 Initialize half of the population and assign the second half of the population complimentary values to the first half. This trick ensures that the initial population covers a wide range of values. Allow the mating parents to generate a litter of 3 or more children. Select the best two children. Select the mating crossover point at random.
29
How to Enhance GA? (Cont.) 28 Select the two crossover points at random. This approach gives a better mix of the parent’s chromosomes. Use a random bit mask (same number of bits as a chromosome) as a Boolean filter to select the chromosomes for either child. Allow mating of multiple parents (polygamy, or perhaps hyper-polygamy) to mate, produce a litter of children, and then select the best fit children.
30
How to Enhance GA? (Cont.) 29 Allow mating of multiple parents? Shackwazoo!! Need I say more?
31
How to Enhance GA? (Cont.) 30 Allow mating of multiple parents? Sacre Bleu!! Need I say more?
32
How to Enhance GA? (Cont.) 31 Include search for local optimum using hill-climbing methods. This enhancement is leads to the Memetic Algorithm (MA) based on the memes theory. The hill- climbing step can be placed in one or more stages: Enhance the entire population at the start of each iteration. Enhance the parents before mating.
33
How to Enhance GA? (Cont.) 32 Enhance the new children before mutation. Enhance mutated population. Apply any combination of the above with the exception of the first alternative.
34
How to Enhance GA? (Cont.) 33 Use masking with several of the previous mate selection methods. A mask is a binary/logical filter used to toggle the parent’s chromosome values. Allow GA to restart if early convergence occurs. And other techniques.
35
Test Functions 34
36
Test Functions Sphere function calculates the sum of squares. Optimum at x(i)=0. Sum of absolutes function calculates the sum of absolute values. Optimum at x(i)=0. 35
37
Test Functions Rosenbrock function 36
38
Test Functions Ackley function. Optimum at x(i)=0. 2d case: f(x,y)=-20*exp(-0.2* sqrt(0.5*(x^2+y^2))) – exp(0.5*(cos(2πx) + cos(2πy))+ e + 20 37
39
Test Functions Ackley function 38
40
Test Functions Sphere function. Optimum at x(i)=0. 2d case: f(x,y)=x^2+y^2 39
41
Test Functions Sphere function 40
42
Test Functions Sum of absolute values function. Optimum at x(i)=0. 2d case: f(x,y)=|x|+|y| 41
43
Test Functions Sum of absolutes function 42
44
Difficult Test Functions Tripod function. Optimum is at x(i)=0. Alpine function. Optimum is at x(i)=0. Parabola function. Optimum is at x(i)=0. Griewank function. Optimum is at x(i)=0. Rosenbrock function. Optimum is at x(i)=0. Ackley function. Optimum is at x(i)=0. See Clerc, “Particle Swarm Optimization”, 2005, ISTE USA. 43
45
Other Test Functions Beale’s function. Optimum points are [3,0.5,3,…,0.5]. Goldstein-Price function. Optimum points at x(i)=3. Booth’s function. Optimum points at [1,3,1,…,3] Bukin function. Optimum points at [-10,1,-10,…,1] Matyas function. Optimum points at x(i)=0. See equations for the above functions and more at https://en.wikipedia.org/wiki/Test_functions_for_optimization 44
46
Other Test Functions Better yet, see the following article (included with HHC2015 files) which has 175 test functions: Momin Jamil and Xin-She Yang, A literature survey of benchmark functions for global optimization problems, Int. Journal of Mathematical Modelling and Numerical Optimisation, Vol. 4, No. 2, pp. 150–194 (2013). DOI: 10.1504/IJMMNO.2013.055204 45
47
HP Prime Code HP Prime is the first handheld calculator that can implement GA methods. Use function GA or GACont(). They implement the basic GA method WITH enhancements. GA() and GACont() have exported function MyFx() coded to use the Rosenbrock test function. To use different functions you need to edit MyFx(). 46
48
HP Prime Code GA() has the following parameters: pNumVars – number of variables. pNumBitsPerVar – number of bits per variable. pMaxPop – population size (i.e. number of data points). pMaxGen – number of iterations. pKeep – number of elite members. pMutationRate – mutation rate (between 0 and 1). 47
49
HP Prime Code pRestartAfter – number of no-improvement iterations that cause a restart. pLitterSize – the number of children of a pair of parents. pMatingMode – the mode for mating. pXLow – the array of lower limits for the variables. pXHi – the array of upper limits for the variable. 48
50
HP Prime Code Mating mode index has the following values: 1 – Simple sequential pairing of fit parents. 2 – Rank-based selection. 3 – Normally distributed rank selection. 4 – Roulette method selection. 5 – Mixed mating where one parent is selected sequentially from the fittest group and one at random. 6 to 10 – Use masking with the first above methods 11 – Three-parents mating. 49
51
HP Prime Code 50 ParameterArgument Num Variables4 Num Bits32 Num Chromosomes40 Max Generations1000 Num of Elites5 Mutation rate0.5 Restart After200 Litter size5 Mating Index1 X Low[0.1 0.1 0.1 0.1] X Hi[2 2 2 2]
52
HP Prime Code 51
53
HP Prime Code 52 ParameterArgument Num. of Variables4 Num. Chromosomes40 Max Generations1000 Num. of Elites5 Mutation rate0.5 Restart After200 Litter size5 Mating Index1 X Low[0.1 0.1 0.1 0.1] X Hi[2 2 2 2]
54
HP Prime Code 53
55
References Clever Algorithms: Nature-Inspired Programming Recipes, Jason Brownlee. See URL http://www.cleveralgorithms.com/nature-inspired/index.html 54
56
Thank You!! 55
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.