Your solution was well written and the code readable and understandable and it was a good idea to use a mask in order to compute the solution and using itertools built-in function (I might implement it myself), none the less i have some suggestion for your code:
Suggestions
GA main function:
You don't need to check all the individuals after each generation to check if you have found an optimal solution, since the list is sorted by te fitness if the first one is not an optimal solution is impossible that an individual with a lower fitness score will be optimal. This will improve the runtime also beacause after each generation you recalculate the fitness for each individual but you could simply access it without recalculating with i[1].
check_sol:
this function could be improved by using list comprehension in order to improve runtime for example:
return len(set([loci for gene in sol for loci in gene])) == N
fitness:
in the same way as for check_sol you could have used list comprehension
General:
Could have reported the result in the README for better understanding and ease of use
General
Your solution was well written and the code readable and understandable and it was a good idea to use a mask in order to compute the solution and using itertools built-in function (I might implement it myself), none the less i have some suggestion for your code:
Suggestions
GA main function: You don't need to check all the individuals after each generation to check if you have found an optimal solution, since the list is sorted by te fitness if the first one is not an optimal solution is impossible that an individual with a lower fitness score will be optimal. This will improve the runtime also beacause after each generation you recalculate the fitness for each individual but you could simply access it without recalculating with
i[1]
.check_sol: this function could be improved by using list comprehension in order to improve runtime for example:
fitness: in the same way as for
check_sol
you could have used list comprehensionGeneral: Could have reported the result in the README for better understanding and ease of use