Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. Programming Challenge - Let's build some AI!
  2.  
  3. Hi everyone! In this challenge, we will build simple genetic algorithm.
  4.  
  5. The goal is to create genetic algorithm that will learn and output predefined text ("Hello World!").
  6.  
  7. The goal can be achieved with any language and you'll need just simple loops, collection and knowledge how to create and use objects, even beginners can try to complete this challenge.
  8.  
  9. How?
  10. I'll try to explain it as best as I can. Genetic algorithms are approximation algorithms - they often do not find the best solution, but they can find very good solutions, fast. It's used when traditional algorithms are either way too slow, or they even don't exist. It's used to, for example, design antennas, or wind turbines. We will use it to write "Hello World".
  11.  
  12. First of all, we define our Entity. It is solution to given problem, it can be list of integers that describe antenna shape, decision tree, or string ("Hello World"). Each entity contains the solution (string solution) and fitness function. Fitness function says, how good our entity is. Our fitness function will return, how similar is entity solution text to "Hello World" string.
  13.  
  14. But how will the program work? First of all, we will create list of entities List<Entity>. We will make, for example, 1000 entities (randomly generated). Their Entity.solution will be randomized string of length 11 (because "Hello World" is 11 characters long).
  15.  
  16. Once we have these entities, we will repeat following steps, until the best entity has fitness == 1.0, or 100% similarity to target string.
  17.  
  18. First of all, we compute fitness function of all entities. Then, we will create empty list of entities of length 1000. Now, we will 1000-times pick two entities (probably weighted based on their fitness) and combine their strings. We will use the string to create new entity and we will add the new entity to the new list of entities.
  19.  
  20. Now, we delete old entities and replace them with entities we just made.
  21.  
  22. The last step is mutation - because what if no entity has the "W" character? We will never get our "Hello World". So we will go through every entity and change 5% (or whatever number you want) of characters in their solution to random characters.
  23.  
  24. We let it run for a while - and it is done!
  25.  
  26. So to sum up what we did:
  27.  
  28. entities <- 1000 random entities
  29. while entities.best.fitness < 1:
  30. for every entity: compute fitness
  31. newEntities <- empty list
  32. 1000-times:
  33. choose two entities from "entities", based on their fitness
  34. combine solutions of these entities and make newEntity
  35. newEntities.add(newEntity)
  36. for every entity: mutate // Randomly change parts of their strings
  37.  
  38. print(entities.best.solution) // Hello World!
  39. Now go and create the best, fastest, and most pointless, genetic algorithm we've ever seen!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement