Tutorial 2 (Optional) Genetic Algorithms This is an optional tutorial. Should you decide to answer it please Submit: Your group source code to mooshak http://mooshak.deei.fct.ualg.pt/ up to May 28, 2018. Consider the following code that generates always the same sequence of n double random numbers between 0 and 1, and output them in a specified format. import java.util.*; import java.text.*; public class Mina { public static void main(string[] args) { Random generator = new Random(0); Scanner sc = new Scanner(System.in); int n= sc.nextint(); double d; DecimalFormatSymbols unusualsymbols = new DecimalFormatSymbols(); unusualsymbols.setdecimalseparator('.'); DecimalFormat df = new DecimalFormat("0.00", unusualsymbols); } } for (int i=0; i< n; i++) { d = generator.nextdouble(); System.out.println(df.format(d)); } sc.close(); http://w3.ualg.pt/~jvo/ia IA Lab 4-1
Problem E: Initializing a population You are going to initialize a population of N binary strings of length l. As you know, the genetic algorithm uses pseudo random numbers in its operation, and the output is non-deterministic. In order to eliminate the non-determinism, use the random number generated provided in the code above. Now, let u be random number in [0, 1). If you want to generate a random bit b, you should assume that b = 0 if u < 0.5, and b = 1 otherwise. You should use the first l random numbers to generate the first string, the next random numbers to generate the second string, and so on. The input contains 2 lines. The first line contains a natural number N that specifies the size of the population. The second line contains a natural number l that specifies the string (chromosome) length. The output should be a population of N binary strings of length l, with each string being followed by a newline character. 4 3 Sample 101 110 011 100 http://w3.ualg.pt/~jvo/ia IA Lab 4-2
Problem F: onemax The onemax problem is probably the most widely tested problem in the evolutionary computation field. The objective of the problem is to maximize a function that returns the number of 1s in an input string. The input has a binary string. You can assume that the string length 1000. The output should be the value returned by the onemax function, followed by a newline character. 11010011 Sample 5 http://w3.ualg.pt/~jvo/ia IA Lab 4-3
Problem G: f(x) = x 2 You should implement another fitness function. This one interprets the input string as the binary representation of an integer in base 10, and returns the square of that number. You should consider the leftmost bit to be the most significant one. The input has a binary string of length l. You can assume that l 10. The output should be the value returned by the function, followed by a newline character. 1101 Sample 169 http://w3.ualg.pt/~jvo/ia IA Lab 4-4
Problem H: binary tournament selection You should implement tournament selection. Given a population of N strings with the corresponding fitness values, you are supposed to simulate N tournaments. For each tournament you should pick 2 solutions at random from the population. The winner has the higher fitness. In case of a tie, please assume that the first solution is the winner of the tournament. In order to perform this operation on a population of size N, you are going to use 2 N random numbers. Each random number belongs to the interval [0..1), and you will need to map it into an integer random number in the [a..b] interval. (Think about what is a and b in your case). To do that you can do the following procedure. Let u be a random number in [0..1). The desired integer random number in [a..b] can be calculated as a + round(u b a) The input has 2 lines + N lines. The first line contains a natural N that specifies the size of the population. The second line contains a natural l that specifies the string length l. Each of the next N lines contain the fitness value of each string (a real number). For the purpose if this exercise, you can assume that both N and l are less or equal than 100. The output should be the population of selected strings, i.e., those that won the tournaments. You should send them to the output in the same order as the tournaments take place. That is, the first string sent to the output should be winner of the first tournament, the second string sent to the output should be the winner of the second tournament, and so on. 4 3 8 7 23 5 Sample 101 011 011 011 http://w3.ualg.pt/~jvo/ia IA Lab 4-5
Problem I: Roulette wheel selection Implement roulette wheel selection. Given a population of N bit strings with the corresponding fitness values, you are supposed to spin the roulette N times. The input has N lines. Each line contains a binary string followed by a white space, and by string s fitness value (a real number). For the purpose if this exercise, you can assume that both N less or equal than 100. The output should be the population of selected strings. You should send them to the output sorted by ascending order. 110 1.0 001 3.0 101 4.0 111 2.0 Sample 001 001 101 111 http://w3.ualg.pt/~jvo/ia IA Lab 4-6
Problem J: Stochastic universal sampling (SUS) Implement stochastic universal sampling (SUS). Given a population of N strings with the corresponding fitness values, you are supposed to spin the roulette only once. Therefore, you will only need a single random number. The input has N lines. Each line contains a binary string followed by a white space, followed by string s fitness value (a real number). For the purpose if this exercise, you can assume that the population size and the length of each individual is less or equal than 100. The output should be the population of selected strings. You should send them to the output in the same order as they appear when scanning the roulette from left to right, i.e., from the 1st to the last individual of the population. 110 1.0 001 3.0 101 4.0 111 2.0 Sample 001 001 101 110 http://w3.ualg.pt/~jvo/ia IA Lab 4-7
Problem K: one-point crossover Implement one-point crossover. You should use the random number generator to generate a random number r. You should map this number into one of the possible cross points, and do so from left to right along the string positions. The input has 4 lines. The first line contains parent1, a bit string. The second line contains parent2, another bit string. The output consists of 2 lines: child1 and child2, each followed by a newline character. 11111111 00000000 Sample 11111100 00000011 http://w3.ualg.pt/~jvo/ia IA Lab 4-8
Problem L: uniform crossover Implement uniform crossover. You should use the random number generator and map each random number r into a fair coin toss. For the purpose of this exercise, assume that r < 0.5 corresponds to head otherwise to tail. Whenever the outcome of the coin toss is head you should exchange the corresponding gene values of the parents. The input has two lines. The first line contains parent1, a bit string. The second line contains parent2, another bit string. The output consists of 2 lines: child1 and child2, each followed by a newline character. 11111111 00000000 Sample 10111001 01000110 http://w3.ualg.pt/~jvo/ia IA Lab 4-9
Problem M: bit-flip mutation Implement bit-flip mutation on a binary string. You should use the fixed random number generator and map each random number r into a biased coin toss. Let pm be the mutation probability. If r < pm then the outcome of the coin toss is head, otherwise the outcome is tail. The input has two lines. The first line contains the mutation probability. The second line contains an individual, a binary string. The output consists of the mutated string followed by a newline character. 0.25 11111111 Sample 10111111 http://w3.ualg.pt/~jvo/ia IA Lab 4-10
Problem N: Random permutation Write a computer program that given a number N generates a random permutation of the integers ranging from 0 to N 1. In order to that, you should implement the following algorithm: for( int i=0; i<n; i++) v[i] = i; for(int i=0;i<n-1;i++) { r = obtain a uniformly generated random integer in [i..n-1]; exchange the contents of v[i] with the contents of v[r] } On completion, the array v will contain a random permutation. The body of the second for loop shown above is executed N 1 times. You should map each random number in [0..1) into an appropriate distribution (just like you did in previous assignments). A line containing a natural number N. You can assume N < 100. The output is the random permutation, one element per line, followed by a newline character. 4 Sample 2 1 3 0 http://w3.ualg.pt/~jvo/ia IA Lab 4-11
Problem O: Tournament selection without replacement Implement tournament selection without replacement. You can assume that the population size N is a multiple of the tournament size s. You will need to generate a random permutation of the population s times. In each time, you use the generated permutation (from the beginning to the end) to perform N/s tournaments. The first line contains an integer s that specifies the tournament size. Each of the next N lines contain a binary string followed by a white space, followed by string s fitness value (a real number). You can assume that N is a multiple of s. The output should be the population of selected strings. You should send them to the output in the same order as the tournament winners are found. 2 110 8.0 001 7.5 101 23.0 111 5.0 Sample 101 110 101 001 http://w3.ualg.pt/~jvo/ia IA Lab 4-12
Problem P: One generation on onemax Simulate one generation of a genetic algorithm operating on the onemax problem. After reading the input, you must generate a random population and evaluate the fitness of all its individuals. Then you will do one generation of a genetic algorithm. As you know, a generation consists of applying selection, crossover, mutation, and replacement. For the purpose of this exercise, you must use: tournament selection without replacement with tournament size s onepoint crossover with probability Pc bit-flip mutation with probability Pm replace worse strategy with a replacement fraction of r The first line contains an integer N that specifies the size of the population. The second line contains an integer l that specifies the string length. The third line contains an integer s that specifies the tournament size. The fourth line contains a real number Pm that specifies the mutation probability. The fifth line contains a real number Pc that specifies the crossover probability. The sixth line contains a real number r that specifies the fraction of the worse individuals from the old population that is going to be replaced by newly created individuals. (Important: the actual number of individuals to be replaced should be rounded up to the nearest integer). You can assume that both N and l are less or equal than 100, and that N is a multiple of s. The output consists of 2 lines. In each line you should print the generation number followed by a colon and a white space, followed by the maximum, average, and minimum fitness of the population, all separated by a white space. Each of these numbers must be reported with a precision of 2 decimal points. Each line ends with a newline character. 4 5 2 0.1 0.8 0.5 Sample 0: 4.00 2.75 1.00 1: 4.00 3.50 3.00 http://w3.ualg.pt/~jvo/ia IA Lab 4-13
Problem Q: A genetic algorithm on onemax This is similar to the previous exercise, but this time you will be simulating multiple generations, just like in a complete genetic algorithm (GA). Again you will be simulating your GA on the onemax problem. Another difference from the previous exercise is that this time you will be using a full replacement strategy (it s just a particular case of the replace worse strategy with r = 1.0. You will be using the same selection, crossover, and mutation operators from the previous exercise. The first line contains an integer N that specifies the size of the population. The second line contains an integer l that specifies the string length. The third line contains an integer s that specifies the tournament size. The fourth line contains a real number Pm that specifies the mutation probability. The fifth line contains a real number Pc that specifies the crossover probability. The sixth line contains an integer g that specifies the number of generations (in addition to generation 0 which consists of a randomly generated population) to be performed. You can assume that both N and l are less or equal than 100, and that N is a multiple of s. You can also assume that g is less or equal than 30. The output consists of 1 + g lines. In each line you should print the generation number followed by a colon and a white space, followed by the maximum, average, and minimum fitness of the population, all separated by a white space. Each of these numbers must be reported with a precision of 2 decimal points. Each line ends with a newline character. 4 5 2 0.1 0.7 2 Sample 0: 4.00 2.75 1.00 1: 4.00 3.75 3.00 2: 4.00 3.25 2.00 http://w3.ualg.pt/~jvo/ia IA Lab 4-14