This program uses an evolutionary algorithm to solve the multiple knapsack problem (MKP). In this problem, n items, each with weights wi and values vi, are packed into a set of K knapsacks, each of which has capacity C. The goal of the problem is to pack the items such that the value contained in the set of knapsacks is maximized while ensuring that the weight does not exceed the capacity of any of the knapsacks. As a trivial example, suppose that there are two knapsacks, each with a maximum capacity of 5, and four items with (weight, value) pairs of I1 = (5, 16), I2 = (5, 20), I3 = (2, 10), I4 = (2, 12). One way to pack the items into the knapsack is to put I1 into the first knapsack and I2 into the second, in which case the knapsacks contain a total value of 36. However, if I2 is packed in one knapsack, and items I3 and I4 are packed in the other, the total value is 42.
Solutions to the MKP are encoded using a string of n integers from 0 to K. Each integer's position is associated with an item, and the value of an integer determines which knapsack the item is assigned to. A value of 0 means an item is not included in any knapsack; any other value from 1 to K represents the knapsack the item is assigned to. For example, the string 302 means that item 1 is assigned to knapsack 3, item 2 is not assigned to any knapsack, and item 3 is assigned to knapsack 2.
The algorithm solves the MKP by reading in a list of (weight, value) pairs from a file, where each pair represents an item. The initial population is then established by randomly generating chromosome encodings and assigning items to knapsacks accordingly. Once the initial population has been established, each generation evolves in the same way: two parents are selected, two children are created from recombination, and both children have a chance for mutation to occur. When there are as many children as the previous generation, the parent generation is replaced with the child generation and the process repeats.