Mating algorithm python implementation