Home>

I want to make sure that the parent doesn't suffer from two parents in selecting the parent roulette in the genetic algorithm.
I'm writing the code below, but an infinite loop (or a tremendous number of repetitions) has occurred and the result is not good.

Other selection methods are not considered for convenience.
I'd like to choose my parents without suffering, but what should I do?

Problems i am experiencing

Infinite loop (repetitive selection of parents a great number of times)

Partial source code
void GA (int Agent, int AS [N_MAX], int N_AS, int N_TS, int cs [N_MAX], int cs_a [N_MAX], double cs_p [N_MAX], double cs_e [N_MAX], double cs_k [N_MAX], double cs_F [N_MAX], int cs_exp [N_MAX], double cs_as [N_MAX], int cs_ts [N_MAX])
{
 Int v [2] = {0, 0};
    double s, k, F_sum = 0;
    / * Select first parent * /
    j = 0;
    k = 0;
    s = MT () * F_sum;
 / * Calculate the total fitness within AS * /
    for (i = 0;i<n_cs;i ++)
    {
        / * cs [i] and cs_F [i] are arguments * /
        if (AS [i] == 1&&cs [i]! = 0)
            F_sum + = cs_F [i];
    }
    / * Determine parent index * /
    while (k<s) {
        if (AS [j] == 1&&cs [j]! = 0)
            k + = cs_F [j];
        / * Record parent index * /
        v [0] = j;
        j ++;
        if (j == n_cs)
            j = 0;
    }
    / * Select second parent to avoid duplication * /
    while (1)
    {
        j = 0;
        k = 0;
        s = MT () * F_sum;
        / * Determine parent index * /
        while (k<s) {
            if (AS [j] == 1&&cs [j]! = 0)
                k + = cs_F [j];
            / * Record parent index * /
            if (v [1]! = v [0])
                v [1] = j;
            j ++;
            if (j == n_cs)
                j = 0;
        }
        / * End if parent is not covered * /
        if (v [0]! = v [1])
            break;
    }
}
Supplement

MT () is a function that generates a random number of (0,1).

c++
  • Answer # 1

    The source has not been confirmed, but how about the following method?

    If the parent generation isNpeople

    A random number "x" is obtained from 0 to (N−1). x = the absolute position of the first parent

    'x'th is the first parent.

    A random number "y" is obtained from 1 to (N−1). y = relative position from the first parent

    (x + y)% The Nth parent is the second parent.

  • Answer # 2

    S is always 0 because F_sum is always 0
    Perhaps this is not what you intended


    The quote that was said to be not the actual code,
    Since the variable name is encrypted, guess the operation appropriately and answer.

    Add cs_F [] to k to create a cumulative distribution,
    I think I'm looking for the first place where k exceeds s = [0, F_sum) and selecting the parent,
    As far as the code is concerned, use the same distribution for the first and second time,
    I think that the second time will be a lottery if you suffer.
    Using the same distribution in this way makes it easier for the parent selected in the first time to be selected in the second time.
    In particular, GAs tend to remarkably increase the fitness of a particular individual as the generation progresses, so redraws often occur.

    In the second lottery, let's make a cumulative distribution by removing the fitness of the individual drawn in the first lottery.