我仍在尝试让我的遗传算法工作(昨天我遇到了内存分配问题,因此在尝试释放它时出现了一个可怕的错误)但今天我有这段代码
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;
}
free_generation()
我在函数调用之前检查了它的内容,如下图所示:
(变量前四个值)
但是在free_generation()
函数内部,同一个变量会丢失一些值(特别是当i
取值为 2 时,i
即 for 循环索引),如下所示:
(函数内部的变量值)
我在下面发布了 free_generation 代码:
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;
}
的定义szChromosomes
如下:
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';
}
我需要澄清的是,这个值的丢失仅发生在顶部发布的 while 循环的第二次迭代之后。我的意思是在第一次运行时一切正常,但在这次迭代之后,第二次运行的行为如上所述。
编辑:
我忘了包括循环控制变量的增量(感谢指出!不,它不是全局 xD)。我包括部分交叉代码:
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;
}