⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nmsearch.cc

📁 在linux下面实现的单纯性算法的源代码
💻 CC
📖 第 1 页 / 共 2 页
字号:
         (*plex)[i][j] = (*plex)[0][j] + q;      } // inner for 1      j = i - 1;      (*plex)[i][j] = (*plex)[0][j] + p;      for(j = i; j < dimensions; j++) {            (*plex)[i][j] = (*plex)[0][j] + q;      } // inner for 2   } // outer for   InitGeneralSimplex(plex);   delete plex;} // InitRegularTriangularSimplex()void NMSearch::InitFixedLengthRightSimplex(const Vector<double> *basePoint,                                           const double edgeLength){  // to take advantage of code reuse, this function simply turns  // edgeLength into a vector of dimensions length, and then  // calls InitVariableLengthRightSimplex()   double* edgeLengths = new double[dimensions];   for( int i = 0; i < dimensions; i++ ) {      edgeLengths[i] = edgeLength;   }   InitVariableLengthRightSimplex(basePoint,edgeLengths);   delete [] edgeLengths;} // InitFixedLengthRightSimplex()void NMSearch::InitVariableLengthRightSimplex(const Vector<double> *basePoint,                                              const double* edgeLengths){   Matrix<double> *plex = new Matrix<double>(dimensions+1,dimensions,0.0);   for( int i = 0; i < dimensions; i++ ) {      // we're building the basePoint component-by-component into      // the (n+1)st row      (*plex)[dimensions][i] = (*basePoint)[i];      // now fill in the ith row with the proper point      for( int j = 0; j < dimensions; j++ ) {         (*plex)[i][j] = (*basePoint)[j];         if( i == j )            (*plex)[i][j] += edgeLengths[i];      }   } // for   InitGeneralSimplex(plex);   delete plex;} // InitVariableLengthRightSimplex()void NMSearch::InitGeneralSimplex(const Matrix<double> *plex){   functionCalls = 0;   if( simplex != NULL ) { delete simplex; }   if( simplexValues != NULL ) { delete [] simplexValues;}   simplex = new Matrix<double>((*plex));   simplexValues = new double[dimensions+1];   int success;   for( int i = 0; i <= dimensions; i++ ) {      *scratch = (*plex).row(i);      fcnCall(dimensions, (*scratch).begin(), simplexValues[i], success);      if(!success) cerr<<"Error with point #"<<i<<" in initial simplex.\n";   } // for   FindMinMaxIndices();} // InitGeneralSimplex()void NMSearch::ReadSimplexFile(istream& fp){   if(fp == NULL) {      cerr<<"No Input Stream in ReadSimplexFile()!\n";      return; // There's no file handle!!   }   Matrix<double> *plex = new Matrix<double>(dimensions+1,dimensions);   for( int i = 0; i <= dimensions; i++ ) {      for ( int j = 0; j < dimensions; j++ ) {         fp >> (*plex)[i][j];      } // inner for   } // outer for   InitGeneralSimplex(plex);   delete plex;} // ReadSimplexFile()// Query functionsint NMSearch::GetFunctionCalls() const{   return functionCalls;} // GetFunctionCalls()void NMSearch::GetMinPoint(Vector<double>* &minimum) const{   minimum = new Vector<double>((*simplex).row(minIndex));} // GetMinPoint()double NMSearch::GetMinVal() const{   return simplexValues[minIndex];} // GetMinVal()void NMSearch::GetCurrentSimplex(Matrix<double>* &plex) const{   plex = new Matrix<double>((*simplex));} // GetCurrentSimplex()void NMSearch::GetCurrentSimplexValues(double* &simValues) const{   simValues = new double[dimensions+1];   for( int i = 0; i <= dimensions; i++ ) {      simValues[i] = simplexValues[i];   } // for} // GetCurrentSimplexValues()int NMSearch::GetVarNo() const{   return dimensions;} // GetVarNo()int NMSearch::GetTolHit() const{   return toleranceHit;} // GetTolHit()// private functionsvoid NMSearch::FindMinMaxIndices(){   if(simplexValues == NULL) {      cerr << "Error in FindMinMaxIndices() - "           << "The vector of simplexValues is NULL!!\n";      return;   }   minIndex = 0;   maxIndex = dimensions;   double min = simplexValues[0];   double max = simplexValues[dimensions];   for( int i = 1; i <= dimensions; i++ ) {      if( simplexValues[i] < min ) {         min = simplexValues[i];         minIndex = i;      } // if      if( simplexValues[dimensions-i] > max ) {         max = simplexValues[dimensions-i];         maxIndex = dimensions - i;      } // if   } // for} // FindMinMaxIndices()int NMSearch::SecondHighestPtIndex(){   if(simplexValues == NULL) {      cerr << "Error in SecondHighestPtValue() - "           << "The vector of simplexValues is NULL!!\n";      return -1;   }   int secondMaxIndex = minIndex;   double secondMax = simplexValues[minIndex];   for( int i = 0; i <= dimensions; i++ ) {      if(i != maxIndex) {         if( simplexValues[i] > secondMax ) {            secondMax = simplexValues[i];            secondMaxIndex = i;         } // inner if      } // outer if   } // for   return secondMaxIndex;} // SecondHighestPtValue()void NMSearch::FindCentroid(){   (*centroid) = 0.0;   for( int i = 0; i <= dimensions; i++ ) {      if( i != maxIndex ) {         (*centroid) = (*centroid) + (*simplex).row(i);      } // if   } // for   (*centroid) = (*centroid) * ( 1.0 / (double)dimensions );} // FindCentroid()void NMSearch::FindReflectionPt(){    (*reflectionPt) = 0.0;   (*reflectionPt) = ( (*centroid) * (1.0 + alpha) ) -                     ( alpha * (*simplex).row(maxIndex) );   int success;   fcnCall(dimensions, (*reflectionPt).begin(), reflectionPtValue, success);   if(!success) {      cerr << "Error finding f(x) for reflection point at"           << "function call #" << functionCalls << ".\n";   } // if} // FindReflectionPt()void NMSearch::FindExpansionPt(){   (*expansionPt) = 0.0;   (*expansionPt) = ( (*centroid) * (1.0 - gamma) ) +                    ( gamma * (*reflectionPt) );   int success;   fcnCall(dimensions, (*expansionPt).begin(), expansionPtValue, success);   if(!success) {      cerr << "Error finding f(x) for expansion point at"           << "function call #" << functionCalls << ".\n";   } // if} // FindExpansionPt()void NMSearch::FindContractionPt(){   // need to first define maxPrimePt   Vector<double> *maxPrimePt = scratch;   if(simplexValues[maxIndex] <= reflectionPtValue) {      *maxPrimePt = (*simplex).row(maxIndex);      maxPrimePtValue = simplexValues[maxIndex];      maxPrimePtId = 1;   } // if   else {      maxPrimePt = reflectionPt;      maxPrimePtValue = reflectionPtValue;      maxPrimePtId = 0;   } // else   (*contractionPt) = ( (*centroid) * (1.0 - beta) ) +                      ( beta * (*maxPrimePt) );   int success;   fcnCall(dimensions, (*contractionPt).begin(), contractionPtValue, success);   if(!success) {      cerr << "Error finding f(x) for contraction point at"           << "function call #" << functionCalls << ".\n";   } // if} // FindContractionPt()void NMSearch::ShrinkSimplex(){   // stop if at maximum function calls  // changed 5/01 to reflect maxcalls = -1 possibility ---pls  if ( (maxCalls != (-1))        && (functionCalls >= maxCalls) ) {return;}   Vector<double> *lowestPt = scratch;   *lowestPt = (*simplex).row(minIndex);   Vector<double> *tempPt = scratch2;   int success;   for( int i = 0; i <= dimensions; i++ ) {      if( i != minIndex ) {         *tempPt = (*simplex).row(i);         (*tempPt) = (*tempPt) + ( sigma * ( (*lowestPt)-(*tempPt) ) );         for( int j = 0; j < dimensions; j++ ) {            (*simplex)[i][j] = (*tempPt)[j];         } // inner for         fcnCall(dimensions,(*tempPt).begin(),simplexValues[i],success);         if (!success) cerr << "Error shrinking the simplex.\n";                  // stop if at maximum function calls 	 // changed 5/01 to reflect maxcalls = -1 possibility ---pls	 if ( (maxCalls != (-1)) 	      && (functionCalls >= maxCalls) ) {return;}	       } // if   } // outer for} // ShrinkSimplex()void NMSearch::printSimplex() const{  for( int i = 0; i <= dimensions; i++ ) {     cout << "   Point:";     for ( int j = 0; j < dimensions; j++ ) {        cout << (*simplex)[i][j] << "\t";     } // inner for     cout << "Value:" << simplexValues[i] << "\n";  } // outer for  cout << "\nFCalls: " << functionCalls << endl << endl;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -