我正在编写一个程序,这是一个优化问题。作为问题的一部分,我定义了一个保存优化参数的类。它由一些向量组成,我调整它们并在函数中为它们提供初始值。课程如下:
typedef class Chrom
{
public:
vector<vector <short int>> bit;
vector<vector <float>> WaitingTime;
vector <short int> WaitingJob;
vector<vector <float>> StartTime;
vector<vector <float>> finish;
//short int FinishTime;
float fit;
void variablesresize(){
WaitingTime.resize(Jobs);
WaitingJob.resize(Jobs);
StartTime.resize(Jobs);
finish.resize(Jobs);
bit.resize(Jobs);
//cout << "machine size is:" << Machines;
for (int i = 0; i < Machines - 1; ++i)
{
WaitingTime[i].resize(Jobs);
}
for (int i = 0; i < Machines; ++i)
{
StartTime[i].resize(Jobs);
finish[i].resize(Jobs);
bit[i].resize(Jobs);
}
}
} chrom;
然后我定义了这个类型的两个类(popcurrent和popnext),并在main函数之前运行y和x:
chrom popcurrent[populationsize];
chrom popnext[populationsize];
void *initialize(chrom popcurrent[populationsize]); //defining the
functions that we will use
chrom x(chrom popcurrent,int pindex);
float y(chrom chromp);
void *selection(chrom popcurrent[populationsize]);
void *crossover(chrom popnext[populationsize]);
void *mutation(chrom popnext[populationsize]);
Problem ProblemConstraint;
void SetProblemSize(short int &Machines, short int &Jobs);
void vectordimension(chrom ch[populationsize]);
void ytest(chrom popcurrent);
void main()
{
我的问题是每当我在其他函数中调用x和y并在其他函数中分配一些值时,它只会为这些类的成员返回零。例如,下面是函数x中赋值的一部分:
chrom x(chrom popnext,int pindex)
{
int z = 0, i, j, k, tempindex, previousjobindex, l;
float temp;
//sorting chromosoms based on the population
//if
cout << '\n';
cout << "popcurrent.bit[0][0] =" << popnext.bit[0][0] << '\n';
cout << "popcurrent.bit[0][1] =" << popnext.bit[0][1] << '\n';
//for the first machine
//for (int i = 0; i < populationsize;i++)
for (i = 0; i < Machines; i++)//**just for test
for (j = 0; j < Jobs; j++)//**just for test
{
cout << "popcurrent.bit[" << i << "][" << j << "] =" << popnext.bit[i]
[j] << '\n';
}
for (int j = 0; j < Machines; j++)//after first machine so j=1 and not 0
{
for (int k = 0; k < Jobs; k++)
{
for (l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1)
{
cout << "j= " << j << "k = " << k;
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
popnext.StartTime[j][k] = popnext.finish[j - 1][k] +
popnext.WaitingTime[j - 1][k];
popnext.finish[j][k] = popnext.WaitingTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
cout << "
cout << "popcurrent.StartTime[" << j << "][" << k << "]= " << popnext.StartTime[j][k] << " popcurrent.finish[j - 1][k]" << popnext.finish[j - 1][k] << '\n';
cout << " popcurrent.WaitingTime[" << j - 1 << "][" << k << "]= " << popnext.WaitingTime[j - 1][k] << '\n';//start time of machine j is equal to finish time of machine j-1
}
}
for (k = 1; k < Jobs; k++)
{
if (popnext.bit[j][k] == k + 1)
{
for (int l = 0; l < Jobs; l++)
if (popnext.bit[j][l] == 1 + k)
{
temp = popnext.finish[j][l];//finish time of previous job
tempindex = l;
}
popnext.StartTime[j][k] = temp;
if (popnext.StartTime[j][k] < popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k]);
popnext.StartTime[j][k] = popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k];//we need to update the start time and also we need to update w as well.
else if (popnext.StartTime[j][k] > popnext.finish[j - 1][k] + popnext.WaitingTime[j - 1][k])
popnext.WaitingTime[j - 1][k] = popnext.StartTime[j][k] - popnext.finish[j - 1][k];//we need to update the waiting time which is greater than t1 here.
popnext.finish[j][k] = popnext.StartTime[j][k] + ProblemConstraint.Processing[j][k] + popnext.StartTime[j][k];
popnext.WaitingTime[j][k] = ProblemConstraint.t1[j][k];
cout << "popcurrent.finish[" << j << "][" << k << "]= " << popnext.finish[j][k] << '\n';//debugging code
}//start of '{' is: else if (popcurrent.bit[j][k] >0)
}
}
cout << "In x functionn, popcurrent.finish[Machines - 1][k]= " <<
popnext.finish[1][2] << '\n';//debugging cout
return(popnext);
}
该函数返回popnext,但我用x(popcurrent,pindex)和x(popnext,pindex)调用它。问题是,在调用之后,我希望成员的值如完成更改,但它们不会改变,因此我有 x(popcurrent); 由于x函数中的最后一个命令,它打印出popcurrent.finish [Machines - 1] [k] = 32但是当我在另一个函数中有一个cout如main时它给了我popcurrent.finish [Machines - 1] [k] = 0;
它只给我所有值的零(我在定义它们时为另一个函数赋值为零,这是向量的初始值)。我想知道为什么在定义popcurrent和popnext全局类时会发生这种情况?