我正在编写一个遗传算法程序。我使用 Linux(操作系统)、c++(语言)和 g++(编译器)。下面的代码生成一个段错误。问题是它有时会运行 50 多次,有时甚至没有。此外,错误发生在任何 tmp[ k ][ p ] 或 tmp[ k + 1 ][ p ] 分配行中。我在这里错过了什么吗?
int** GeneticAlgorythm::newGeneration( int** parents )
{
int** tmp = new int*[ population ];
int p = 0;
for( int k = 0; k < population; k += 2 )
{
tmp[ k ] = new int[ nGenes ];
tmp[ k + 1 ] = new int[ nGenes ];
setLikelihood( parents );
int parent1 = getParent( likelyhood );
int parent2 = getParent( likelyhood );
while( parent1 == parent2 )
{
parent2 = getParent( likelyhood );
}
for( p = 0; p < crossOverPoint; p++ )
{
tmp[ k ][ p ] = parents[ parent1 ][ p ];
tmp[ k + 1 ][ p ] = parents[ parent2 ][ p ];
}
for( p = crossOverPoint; p < nGenes; p++ )
{
tmp[ k ][ p ] = parents[ parent2 ][ p ];
tmp[ k + 1 ][ p ] = parents[ parent1 ][ p ];
}
}
currGeneration++;
return tmp;
}
int GeneticAlgorythm::getParent( double* lh )
{
int randVal = rand( ) % 100;
int* choose = new int[ 100 ];
int counter = 0;
for( int k = 0; k < population; k++ )
{
for( int j = 0; j < (int)likelyhood[ k ]; j++ )
{
choose[ counter++ ] = j;
}
}
counter = choose[ randVal ];
delete[] choose;
return counter;
}
void GeneticAlgorythm::setLikelihood( int** pg )
{
multipleInverse = 0;
double one = 1.00;
for( int mi = 0; mi < population; mi++ )
{
multipleInverse += one/checkFitness( pg[ mi ] );
}
for( int lh = 0; lh < population; lh++ )
{
likelyhood[ lh ] = round(((one/checkFitness( pg[ lh ] ))/multipleInverse) * 100);
}
}
变量值: 人口 = 20;nGenes = 3; 交叉点 = 1;
可能性是根据他们的健康水平选择父母的可能性。pg 是亲本基因。[人口][nGenes]。
提前致谢。