Creating Alogrithm for calculating fitness function