Solution to Exercise 2: Assigning Tasks

Solve Exercise 2 of this chapter.

We'll cover the following

The remaining exercises are challenging to solve with PSO. When we think of solutions to these problems, it’s hard to define a velocity for them. Using a genetic algorithm is more natural—and that’s why we’ll do just that.

Exercise 2

In this problem, we have nn tasks and mm employees. The matrix WW tells us how much time each employee needs to do each task. What would a solution for this problem look like?

In this case, a solution is an assignment of each task to some worker. There could be workers with no tasks assigned and workers with multiple tasks assigned. Mathematically, we can think of a solution as a matrix SS with size m×n,m \times n, where Si,j=1S_{i,j} = 1 if worker ii has the task jj assigned and Si,j=0S_{i,j} = 0 otherwise.

For example, consider that SS is:

(1001)\left(\begin{array}{cc} 1 & 0\\ 0 & 1 \end{array}\right)

Then we assign task 1 to worker 1 and task 2 to worker 2. The total time is the longest time that it takes for a worker to complete their tasks. We can calculate those times using matrix WW.

We’re going to use the following operations in our genetic algorithm:

  • mutation: We reassign a task to another worker.

  • crossover: We combine the first columns of one solution with the last columns of the other.

  • score: The score will be the inverse of the total time.

To check how good our solution is, we can use the function exact_solution that’s already defined. This function is hidden since we’ll only use it to compare our results.

Get hands-on with 1400+ tech skills courses.