# Unit 3: Programming “Encoding and Decoding”. We can now take a simple message like, Hello and convert it in stages: Hello 73 102 109 109 112 1001001 1100110.

## Presentation on theme: "Unit 3: Programming “Encoding and Decoding”. We can now take a simple message like, Hello and convert it in stages: Hello 73 102 109 109 112 1001001 1100110."— Presentation transcript:

Unit 3: Programming “Encoding and Decoding”

We can now take a simple message like, Hello and convert it in stages: Hello 73 102 109 109 112 1001001 1100110 1101101 1110000 Hello 73 102 109 109 112 But is this message really safe from your classmates? C = [1 5 13 28 61 126 253] Knapsack Sequence 282 193 348 348 19 But is even this message really safe from your classmates?

“Encoding and Decoding” Now let’s let genetic algorithms have a chance at it… 1001001 1100110 1101101 1110000 = “Encoded Message” Someone provide me with an encoded message using your knapsack encoding…don’t give me your secret code!

“Encoding and Decoding” Two encoded messages broken GA’s: 1) “After time adrift among open stars, along tides of light and through shoals of dust, I will return to where I began.”~ Michael Leung 2) “Cody approves of the JEWS encoding system. Stop asking about the name. Happy Hanukkah everyone!”~ Justus Jackson

Perfect Solution

Imperfect Solutions

“Encoding and Decoding” Now that we have managed to both encode and decode a message using a Knapsack sequence, let’s begin work on a GA that can break a code… …even without knowing the knapsack sequence!!!

“Encoding and Decoding” What’s the first step in any GA routine? Need a reminder???

Genetic Algorithms Genetic algorithms usually consist of the following five steps: 1.Create a starting population randomly 2.Test the fitness of each member and assign selection probability 3.Reproduce 4.Test new population for threshold criteria 5.Wash, rinse and repeat…

“Encoding and Decoding” The starting population for our problem should be a set of guesses for what the Knapsack sequence might be, so we know something about the constraints. Any guess must be: Same length as the actual sequence Super increasing Randomly created

“Encoding and Decoding” Write a function called superIncreasing that will create M sets of guesses for the knapsack sequence arranged in an MxN array, where N is the length of the sequence. Input:Description: MThe number of guesses to create NLength of each knapsack sequence Output:Description: PAn MxN array of guesses for the Knapsack sequence Super Increasing – every element is greater than the sum of all previous elements Ex: [1 5 13 28 61 126 253]

“Encoding and Decoding” [ P ] = superIncreasing( M,N ) Hint: Pick a random number between 1-5 Add this number to the superincreasing series Repeat – Sum the series – Add to the sum a random number between 1-5 – Add that new number to the series

“Encoding and Decoding” Write a function called Crossover that will take in P, an mxn array of guesses for the knapsack sequence, and output N a similar array where every two guesses have been randomly allowed to replicate by crossover at a randomly selected point. Input:Description: Parray of guessed knapsack sequences Output:Description: Narray of crossover-replicated sequences 5 10 16 36 71 139 279 3 8 14 28 60 120 238 5 10 16 28 60 120 238 3 8 14 36 71 139 279

“Encoding and Decoding” Write a function called Mutate that will take in P, an mxn array of guesses for the knapsack sequence, and output N, a similar array where a random element in each row may have been “mutated”; for our purposes mutation is adding +/-1 to an element. Input:Description: Parray of guessed knapsack sequences Output:Description: Narray of crossover-replicated sequences 5 10 16 36 71 139 279 38 14 28 60 120 238 1 6 12 23 46 92 182 5 10 16 37 71 139 279 38 14 28 60 120 237 1 6 12 23 46 92 182

“Encoding and Decoding” Let’s get to work on a fitness function that will help us to home in on a solution for the encoded message. 1)Download the function, knapsackFitness.m, from the BPI website. 2)Read through the comments and begin work on coding the steps described. A set of sample input and output data is included so that you can test your function when completed.

“Lockers” At the end of the school year, an obsessive/compulsive worker is asked to clean out N lockers, initially all closed. Each time he cleans, he leaves the door open if it was closed, or closed if it was open. The first time he cleans, he leaves all the doors open. The second time, he closes every second door. The third time he cleans, he closes every third door if it is open, or opens it if was closed. The fourth time…well this continues until he has cleaned the lockers N times. How many lockers are open if N = 4? N=6? N = 1000?

“Lockers” How many lockers are open if N = 4? N=6? N = 1000? Open Closed Open

“Slope” Write a script that will create an empty figure containing only a 8 x 8 grid (i.e. x ranges from 1 to 8, as does y). Using the ginput function, allow the user to plot two points on the grid by clicking anywhere on the grid. Calculate the slope and the distance between the two points.

Download ppt "Unit 3: Programming “Encoding and Decoding”. We can now take a simple message like, Hello and convert it in stages: Hello 73 102 109 109 112 1001001 1100110."

Similar presentations