Solution to Exercise 1: Using Both Algorithms

Solve the assignments of this chapter.

We need to solve:

maxx,yx2y2+6x+4y\max_{x,y} -x^2 - y^2 + 6x + 4y

We already know how to solve this problem with other algorithms, but let’s practice what we’ve learned. Using the gradient and the Hessian, for example, we can solve this problem analytically. The maximum of this function is (3,2)(3, 2).

This time, a solution will be a vector (x,y)(x, y). For the genetic algorithm implementation, we use the following operations:

  • mutation: We randomly sum one or subtract one from the first component of the vector.
  • crossover: We make a new vector with the first component of the first parent and the second component of the second parent.
  • score: If f(x)<0f(x) < 0, then the score of xx will be f(x)109|f(x)| * 10^{-9}. Otherwise, it’ll be f(x)f(x). As we know there are solutions ss so that f(s)>0f(s) > 0, we know that any value of ff that’s less than 00 isn’t the solution we’re looking for. But the score should always be positive, so we need to make sure any negative solution is worse than any positive one, which is why we add the 10910^{-9} factor.

Let’s see the genetic algorithm implementation.

Get hands-on with 1400+ tech skills courses.