I am writing a sorting algorithm in Python.
For example,
Lets say I have 10 child generated from 1 parent. Each child has two attributes "Fitness" and "Noise".
Lets say, I calculate the "Fitness" for 10 children first and select only the 3 best children (higher "fitness" are chosen as best). For example let us take 60,50,40 as fitness values for the 3 best children.
Now I proceed to calculate the "noise" of the 3 children. Again for example, lets assume I get the "noise" for 3 children as 5,8,6. (Less "noise" is considered better).
How do I find a sorting algorithm to choose based on best "fitness" and "noise". I would ideally require a child with high "fitness" and low "noise". I would need to sort them in a way I take the best child out of 3 children.
Initial 3 children:
Fitness {60,50,40} Noise {5,8,6}
My Ideal 3 children after sorting should be:
Fitness {60,40,50} Noise {5,6,8}
I am also not sure how to give the weighting for selecting noise as a secondary variable.
Basically I am looking for a good sorting algorithm in python.
You can use the builtin
sorted
function, and pass a lambda as the key. The lambda returns the object used for comparison. If we return a tuple, it will sort based on the first element of the tuple, then the second, etc.If we make the tuple first be the noise, then the negative of the fitness, it will give the order you want: