double computeCoeffs(struct Complex** c, int nTerms, int mid, double stddev) { int i, j; for (i = 0; i < nTerms; i++) { for (j = mid; j < nTerms; j++) { c[i][j].real = randGauss(0, stddev); c[i][j].imag = randGauss(0, stddev); c[2*mid-i][2*mid-j].real = c[i][j].real; c[2*mid-i][2*mid-j].imag = -c[i][j].imag; } } c[mid][mid].real = randGauss(0, 2*stddev*stddev); c[mid][mid].imag = 0.0; }
void NewDev2Population::updatePopulation() { //normalize dapVals int tempI = 0; double tempD = 0; double tempD2 = 0; for(int i = 0 ; i < mDSize ; ++i) { tempD += mDAPValues[i]; } if(tempD == 0) tempD = 1; for(int i = 0 ; i < mDSize ; ++i) { mDAPValues[i] = mDAPValues[i] / tempD; //scale values to sum to 1 } tempI = mSize - mDSize;//tempI = pool size mPoolIndices[0] = 0; for(int i = 1 ; i <= mDSize ; ++i) { mPoolIndices[i] = mPoolIndices[i-1] + floor(mDAPValues[i-1] * tempI) + 1; } mPoolIndices[mDSize] = mSize; double* tempPm = new double[mDim]; double* tempMean = new double[mDim]; double* tempVariance = new double[mDim]; for(int i = 0 ; i < mDim ; ++i) { tempMean[i] = 0; for(int j = 0 ; j < mDSize ; ++j) { tempMean[i] += mDPositions[(j*mDim) + i]; } tempMean[i] /= mDSize; } for(int i = 0 ; i < mDim ; ++i) { tempVariance[i] = 0; for(int j = 0 ; j < mDSize ; ++j) { tempVariance[i] += (mDPositions[(j*mDim) + i] - tempMean[i]) * (mDPositions[(j*mDim) + i] - tempMean[i]); } tempVariance[i] = sqrt(tempVariance[i]) / mDSize; //tempVariance[i] /= mDSize; } bool pointsEqual = false; for(int i = 0 ; i < mDSize ; ++i) //Generate points { pointsEqual = (i==mBestPointIndex); if(!pointsEqual) { for(int j = 0 ; j < mDim ; ++j) { if(!(mDPositions[(i * mDim) + j] == mDPositions[( mBestPointIndex * mDim ) + j])) { pointsEqual = false; break; } } } for(int j = 0 ; j < mDim ; ++j) { tempPm[j] = (mDPositions[(i * mDim) + j] + mDPositions[( mBestPointIndex * mDim ) + j]) / 2; tempD = mDPositions[(i * mDim) + j] - mDPositions[( mBestPointIndex * mDim ) + j]; if(pointsEqual) { tempD = tempVariance[j]; } for( int k = mPoolIndices[i] ; k < mPoolIndices[i + 1] ; ++k) { mPositions[(k * mDim) + j] = tempPm[j] + (randGauss() * tempD); if(mPositions[(k * mDim) + j] > mBounds[mDim + j]) { mPositions[(k * mDim) + j] = mBounds[mDim + j]; } else if(mPositions[(k * mDim) + j] < mBounds[j]) { mPositions[(k * mDim) + j] = mBounds[j]; } } } } for(int i = 0 ; i < mSize ; ++i)// evaluate points mOptFunc->evaluate(&(mPositions[i * mDim]), mValues[i]); int bestInd; for(int i = 0 ; i < mDSize ; ++i) //update points { bestInd = -1; mDAPValues[i] = 0; for( int k = mPoolIndices[i] ; k < mPoolIndices[i + 1] ; ++k) { //mDAPValues[i] += mValues[k]; if(mValues[k] < mDValues[i]) { if(mValues[k] < mDValues[mBestPointIndex]) mBestPointIndex = i; bestInd = k; mDValues[i] = mValues[k]; mDAPValues[i] = mDValues[i]; } } //mDAPValues[i] /= (double)(mPoolIndices[i + 1]-mPoolIndices[i]); if(bestInd != -1) { for(int j = 0 ; j < mDim ; ++j) { mDPositions[(i*mDim) + j] = mPositions[(bestInd * mDim) + j]; } } } delete[] tempPm; delete[] tempMean; delete[] tempVariance; }