(1) Let represent a new generated harmony vector
 // Calculate the total weight of knapsack according to
(2) // denotes the total weight
(3) if then
(4) // The “DROP” phase
(5) for to do
(6)   // Compute the profit density value of items loaded
(7) end for
(8) Sort items in increasing order of , and let represent the result sorted,
   and denotes the original index of each
(9) for to do
(10)  if then
(11)   Continue
(12)  end if
(13)   // Unload the th item from the knapsack
(14)  
(15)  if then
(16)   Break // Terminate the “DROP” phase of repair process
(17)  end if
(18) end for
(19) end if
(20)
(21) if then
(22) // The “ADD” phase
(23) Calculate the profit density ratio according to (4)
(24) Sort all the items in decreasing order of , and let represent the result sorted,
  and denotes the original index of each
(25) for to do
(26)  if then
(27)   Let represent a temporary harmony vector, and set
(28)   Set //Try to load the th item
(29)   if  ≤  then
(30)     // Load the th item into knapsack
(31)   end if
(32)   end if
(33) end for
(34) end if
Algorithm 4: The two-phase repair operator.