return (-1);
}
- num_tries = (int) ceil (log (p->individuals_num) / log (2.0));
- for (i = 0; i < num_tries; i++)
+ do
{
size_t j;
+ int chance_j;
+ int chance_pi;
+ int chance;
+
j = (size_t) (((double) p->individuals_num) * (rand() / (RAND_MAX + 1.0)));
if (p->individuals[j].ptr == NULL)
break;
}
- if (pi_rating < p->individuals[j].rating)
+ /* large distance from fittest => high probability of losing. */
+ chance_j = 1 + p->individuals[j].rating - p->fittest.rating;
+ chance_pi = 1 + pi_rating - p->fittest.rating;
+
+ chance = (int) (((double) (chance_j + chance_pi))
+ * (rand() / (RAND_MAX + 1.0)));
+ if (chance < chance_j) /* j looses ;) */
{
void *temp0;
int temp1;
p->individuals[j].rating = pi_rating;
pi_rating = temp1;
}
- }
+ } while (0);
pthread_mutex_unlock (&p->lock);