c - 该值未正确保存到数组中
问题描述
我试图计算随机生成的值的适应度并将每个值保存在名为适应度 [] 的数组中。显然,代码显示保存在数组中的所有值实际上是相同的,因此计算的最小值和最大值是相同的。我在这里错过了一些重要的事情吗?
int main()
{
//Declaration of variables
//Variable 'numPoints' will hold the number of of points
//Variable 'order' will hold the order of the polynomial
//Variable 'numPopulation' will hold the number of the population
int numPoints, order, numPopulation;
//Variable 'upper' and 'lower' will be used for limiting the coefficient values to certain boundaries
int upper, lower;
//Variable 'population' will hold the coefficients of the population in 1D array converted from 2D array
int* coefficient;
//Declare indexes for the loops
int i, j, k, n;
int row, col;
//Variable 'iterCnt' will count the iterations, or so-called generations of the AI
int iterCnt;
//Initialize random number gerator by using the time of system clock in seconds
srand(time(0));
//Input order of the polynomial fit
printf("Select the order of the polynomial fit.: ");
scanf_s("%i", &order);
printf("The fit will be drawn in the order of %u.\n\n", order);
//Increment 1 from the value of order to include the constant
int numCoefficient = order + 1;
//Input number of population
printf("Select the desired number of population: ");
scanf_s("%i", &numPopulation);
if (numPopulation > 20)
{
printf("WARNING: population that is too large can result in slower compilation\n");
}
printf("The function will now generate the %u numbers of points.\n\n", numPopulation);
//Input number of points
printf("How many points will be provided?: ");
scanf_s("%i", &numPoints);
printf("The function will now generate the %u numbers of points.\n\n", numPoints);
//Initiailize heap location for the coordinates
int* xData = (int*)malloc(sizeof(int) * numPoints);
int* yData = (int*)malloc(sizeof(int) * numPoints);
//can be replaced with the line below for stack:
//int xData[100000];
//Initialize heap location for the population which contains coefficient information in array
coefficient = (int*)malloc(numPopulation * numCoefficient * sizeof(int));
for (i = 0; i < numPoints; i++)
{
printf("Please enter the x coordinate of P(%i).\n", i + 1);
scanf_s("%i", &xData[i]);
printf("Please enter the y coordinate of P(%i).\n", i + 1);
scanf_s("%i", &yData[i]);
}
//Get the estimated coefficient value boundary
printf("What is the estimated absolute value of the coefficient?: ");
scanf_s("%i", &upper);
lower = -upper;
printf("The coefficient will be initialized between %i and %i.\n\n", lower, upper);
//Generate an initial population
for (row = 0; row < numPopulation; row++)
{
for (col = 0; col < numCoefficient; col++)
{
n = (row * numCoefficient) + col;
coefficient[n] = (int)(rand() % (upper - lower + 1)) + lower;
}
}
//Print the initial coefficient array in 2D format
printf("\nCoefficient Array: \n\n");
for (i = 0; i < (numPopulation * numCoefficient); i++)
{
//Convert the coefficient[] into a shape of 2D by entering new line every 'numCoefficient'
j = i % numCoefficient;
if (j == 0)
{
printf("\n");
printf("Population %i: ", (i/numCoefficient)+1);
printf("%i ", coefficient[i]);
}
else {
printf("%i ", coefficient[i]);
}
}
//
int totSqDiff = 0;
int minLocation = 0, maxLocation = 0;
int* fitness = (int*)malloc(sizeof(int) * numPopulation);
//Initialize the
int* yCalculated = (int*)malloc(sizeof(int) * numPoints);
int* sqDiff = (int*)malloc(sizeof(int) * numPoints);
//Calculate fitness of the initial population
for (i = 0; i < numPopulation; i++)
{
for (j = 0; j < numPoints; j++)
{
for (k = 0; k < order; k++)
{
yCalculated[j] = coefficient[k] * pow(xData[j], k);
sqDiff[j] = pow((yData[j] - yCalculated[j]), 2);
totSqDiff += sqDiff[j];
}
}
fitness[i] = totSqDiff/numPoints;
totSqDiff = 0;
printf("\n%i", fitness[i]);
if(fitness[i] < fitness[minLocation])
{
minLocation = i;
}
if(fitness[i] > fitness[maxLocation])
{
maxLocation = i;
}
}
printf("\n\nCurrent minimum and maximum fitness present is %i and %i.\n", fitness[minLocation], fitness[maxLocation]);
free(yCalculated, sqDiff);
}
示例输出如下:
Select the order of the polynomial fit.: 2
The fit will be drawn in the order of 2.
Select the desired number of population: 5
The function will now generate the 5 numbers of points.
How many points will be provided?: 1
The function will now generate the 1 numbers of points.
Please enter the x coordinate of P(1).
1
Please enter the y coordinate of P(1).
1
What is the estimated absolute value of the coefficient?: 3
The coefficient will be initialized between -3 and 3.
Coefficient Array:
Population 1: -1 -3 2
Population 2: -3 1 -3
Population 3: 2 -3 -1
Population 4: -1 -3 2
Population 5: 0 -2 0
20
20
20
20
20
Current minimum and maximum fitness present is 20 and 20.
解决方案
推荐阅读
- java - 这个先决条件是否违反了里氏替换原则
- java - 为 Projet Euler 问题 8 尝试不同的方法
- snowflake-cloud-data-platform - 雪花 - 转义字符参数在复制语句中不起作用
- python - 从子进程发送和接收变量
- c++ - 当我想覆盖一个值时,为什么不能使用统一初始化?
- python - 输出链接到相同关键字的所有行
- python - 在 x 轴上绘制正确的点
- regex - 正则表达式:每个组都是可选的,至少必须存在一个,之后允许其他任何内容
- html - 如何在列表中每个元素(字符串)的末尾添加一个链接?使用百里香叶
- python - 在 Python 中加入 dict 和数据框