Keep Your Laptop Battery Healthy

Laptop batteries are known to cost upwards of $100.00, depending on your brand. In order to avoid battery burnout, only charge your laptop when needed. Learn how to keep your laptop battery healthy!

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Adding Mutation

Genetic Algorithms in Elixir — by Sean Moriarity (17 / 101)

👈 Running Your Solution | TOC | What You Learned 👉

Despite initializing your population to a seemingly random distribution, eventually the parents got too genetically similar to make any improvements during crossover. This illustrates the importance of including the mutation step in your algorithm and how vital exploration is to informed search techniques.

After the crossover function in the algorithm, add the following:

Now, the structure of the algorithm looks like this:

Mutation is similar to the other functions in that it accepts a population as a parameter. You only want to mutate a small percentage of your population as well — this is to preserve the progress that’s already been made. Below your crossover definition, add the following:

This function iterates over the entire population and randomly shuffles a chromosome with a probability of 5%. The :rand.uniform() < 0.05 condition is a pattern that emerges a lot throughout this book. It’s one way of simulating a random event in Elixir.

Enum.shuffle/1 takes in an enumerable and randomizes the elements in the enumerable. Think of it like shuffling a deck of cards. Doing this actually preserves the fitness of the chromosome; however, it also prevents the parents from becoming too similar before they crossover. You’ll learn more about this technique and others in Chapter 7, Preventing Premature Convergence.

With the mutation function implemented, you’re ready to try running your algorithm again, like this:

Congratulations, you’ve just written your first genetic algorithm.

👈 Running Your Solution | TOC | What You Learned 👉

Add a comment

Related posts:

Hal yang Membuat Produk Mudah Digunakan

Angkat tanganmu kalau kamu pernah mendengar kalimat tersebut. Atau mungkin kamu pernah mendengar variasi lain seperti Setiap mendengar kalimat tersebut, hati saya selalu berdebar, karena untuk…

TENTANG MEMULAI

Bukan soal mimpi yang berhasil atau tidak, melainkan bagaimana kita berani melangkah untuk mendapatkan pelajaran dan hikmah serta memulai perjalanan menuju mimpi yang besar. Jadikanlah ketakutan…

Using Mix to Write Genetic Algorithms

Explore the powers of genetic algorithms through practical examples, all in a language you already know.