문제

I'm still trying to get my genetic algorithm to work (yesterday I had a problem with memory allocation and consequently an awful error when attempting to free it) but today I have this piece of code

while (iCurrentGen <= data->m_iMaxGenerations)
{
    arrfSelectedChromosomes = selection(&data[0], szChromosomes);
    iSelectedLen = order_descending_grid(arrfSelectedChromosomes);
    szAuxGen = crossover(&data[0], arrfSelectedChromosomes, szChromosomes, iSelectedLen);
    //szChromosomes is what I need to free after that call returns
    free_generation(&data[0], szChromosomes);//Code Explotion
    szChromosomes = szAuxGen;
    szAuxGen = NULL;
    ++iCurrentGen;
}

I was checking its content before the free_generation() function call and It was something like the picture below:

(Variable first four values)

Variable first four values

But inside the free_generation() function, the same variable loses some of its values (specifically when i takes value 2, i being the for loop index), as shown below:

(Variable values inside the function)

Variable values inside the function

I post the free_generation code below:

void free_generation(struct INPUT_DATA* d, char** szChromosomes)
{
    int i;

    for (i = 0; i < d->m_iPopulationSize; ++i)
    {
        free(szChromosomes[i]);
    }

    free(szChromosomes);
    szChromosomes = NULL;
}

The definition of szChromosomes is as follows:

char** szChromosomes = (char**)malloc(d->m_iPopulationSize * sizeof(char*));

srand(time(NULL));
for (i = 0; i < d->m_iPopulationSize; ++i)
{
    szChromosomes[i] = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));
    for (j = 0; j < d->m_iBitsPChromosome; ++j)
    {
        szChromosomes[i][j] = rand_1_0(0.0, 1.0) == 1? '1' : '0';
    }
    szChromosomes[i][j] = '\0';
}

I need to clarify that this value's loss occurs only after the second iteration of the while loop posted on top. I mean on the first run everything works perfectly but after this iteration, the second one behaves as described above.

EDIT:

I forgot to include the increment of the loop control variable (thanks for pointing that out! and no, it isn't global xD). I'm including parts of the crossover code:

    char** crossover(struct INPUT_DATA* d, float** arrfSelectedChromosomes, char** arrszChromosomes, int iChromosomesInGrid)
{
    int i;
    int iTIndex = 0, iRPos = 0;
    char* szFirstChromosome = NULL;
    char* szSecondChromosome = NULL;
    char* szFirstNewChrom = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));
    char* szSecondNewChrom = (char*)malloc((d->m_iBitsPChromosome + 1) * sizeof(char));

    char** arrszNewPop = (char**)malloc(d->m_iPopulationSize * sizeof(char*));

    int iSplitPoint = (int)(d->m_iBitsPChromosome / 4);

    float fCrossOverProb = CROSSOVER_PROBABILITY; 

    srand(time(NULL));
    for (i = 0; i < d->m_iPopulationSize; i += 2)
    {
        iRPos = rand() % iChromosomesInGrid;
        iTIndex = (int)arrfSelectedChromosomes[iRPos][0];
        szFirstChromosome = arrszChromosomes[iTIndex];
        iRPos =  rand() % iChromosomesInGrid;
        iTIndex = (int)arrfSelectedChromosomes[iRPos][0];
        szSecondChromosome = arrszChromosomes[iTIndex];

        if (is_same_chromosome(szFirstChromosome, szSecondChromosome))
        {
            i -= 2;
            continue;
        }

        if (fCrossOverProb < CROSSOVER_PROBABILITY)
        {
            //if probability is lower than the defined prob. we keep both chromosomes
            strcpy(szFirstNewChrom, szFirstChromosome);
            strcpy(szSecondNewChrom, szSecondChromosome);
        }
        else
        {

            strcpy(szFirstNewChrom, szFirstChromosome);
            szFirstNewChrom[iSplitPoint] = '\0';
            strcat(szFirstNewChrom, &szSecondChromosome[iSplitPoint]);

            //Para crear el segundo hijo se realiza una operacion similar
            strcpy(szSecondNewChrom, szSecondChromosome);
            szSecondNewChrom[iSplitPoint] = '\0';
            strcat(szSecondNewChrom, &szFirstChromosome[iSplitPoint]);
        }

        arrszNewPop[i] = szFirstNewChrom;
        arrszNewPop[i + 1] = szSecondNewChrom;
    }

    return arrszNewPop;
}
도움이 되었습니까?

해결책

Since feature requests to mark a comment as an answer remain declined, I copy the above solution here.

n.m You're the man! thanks so much.. It was a problem of duplicated pointers, and it was due to wrong memory allocation in crossover function. I was allocating memory for szFirstNewChrom and szSecondNewChrom at the beginning of the function but that memory was used for 30 different strings. Because of that terrible mistake, the free_generation function kept failing since it was trying to free a pointer freed previously. Thank you all! – Jorge Cespedes

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top