123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- #include "optimization/DownhillSimplexOptimizer.h"
- #include "optimization/Opt_Namespace.h"
- #include "mateigen.h"
- #include <iostream>
- using namespace optimization;
- DownhillSimplexOptimizer::DownhillSimplexOptimizer(OptLogBase *loger): SuperClass(loger)
- {
- m_abort = false;
- m_simplexInitialized = false;
- m_alpha = 1.0;
- m_beta = 0.5;
- m_gamma = 1.0;
- m_rankdeficiencythresh= 0.01;
- m_rankcheckenabled= false;
- }
- DownhillSimplexOptimizer::DownhillSimplexOptimizer(const DownhillSimplexOptimizer &opt) : SuperClass(opt)
- {
- m_abort = opt.m_abort;
- m_simplexInitialized = opt.m_simplexInitialized;
- m_vertices = opt.m_vertices;
- m_y = opt.m_y;
- m_alpha = opt.m_alpha;
- m_beta = opt.m_beta;
- m_gamma = opt.m_gamma;
- m_rankdeficiencythresh= 0.01;
- m_rankcheckenabled= false;
- }
- DownhillSimplexOptimizer::~DownhillSimplexOptimizer()
- {
- }
- bool DownhillSimplexOptimizer::setWholeSimplex(const matrix_type &simplex)
- {
- if(simplex.rows() == static_cast<int>(m_numberOfParameters) && simplex.cols() == static_cast<int>(m_numberOfParameters + 1))
- {
-
- if(m_rankcheckenabled)
- {
- int rank = getRank(simplex, m_rankdeficiencythresh);
-
- if(rank < static_cast<int>(m_numberOfParameters))
- {
- m_simplexInitialized = false;
- return false;
- }
- }
- m_vertices = simplex;
- m_simplexInitialized = true;
-
- for (int k = 0; k < static_cast<int>(m_numberOfParameters +1); k++)
- {
- m_y[k][0] = evaluateCostFunction(m_vertices(0,k,m_numberOfParameters-1,k));
- }
-
- return true;
- }
- else
- {
- m_simplexInitialized = false;
- return false;
- }
- }
- void DownhillSimplexOptimizer::init()
- {
- SuperClass::init();
-
-
- m_vertices = matrix_type(m_numberOfParameters, m_numberOfParameters + 1);
- m_y = matrix_type(m_numberOfParameters + 1,1);
- m_abort=false;
- m_simplexInitialized == false;
-
-
-
- if (m_simplexInitialized == false)
- {
- bool abort = false;
- while(abort == false)
- {
- matrix_type simplex(m_numberOfParameters,m_numberOfParameters+1);
-
-
- for(int i = 0; i < static_cast<int>(m_numberOfParameters); i++)
- {
- for(int j = 0; j < static_cast<int>(m_numberOfParameters+1); j++)
- {
- simplex[i][j] = m_parameters[i][0];
-
- if( j == i+1 )
- {
- double tmpRand = m_scales[i][0];
- simplex[i][j] += tmpRand;
- }
- }
- }
- if(this->setWholeSimplex(simplex) == true)
- {
-
- m_simplexInitialized = false;
- abort =true;
- }
- }
- }
- for (int k = 0; k < static_cast<int>(m_numberOfParameters +1); k++)
- {
- m_y[k][0] = evaluateCostFunction(m_vertices(0,k,m_numberOfParameters-1,k));
- }
- }
- int DownhillSimplexOptimizer::optimize()
- {
-
-
- this->init();
-
-
- if(m_loger)
- m_loger->logTrace("Starting Downhill Simplex Optimization\n");
- int tmp=amoeba();
- m_parameters = m_vertices(0,tmp,m_numberOfParameters-1,tmp);
-
- m_currentCostFunctionValue = evaluateCostFunction(m_parameters);
- return m_returnReason;
- }
- int DownhillSimplexOptimizer::amoeba()
- {
-
- double tol =m_funcTol;
- const int ndim=m_numberOfParameters;
-
-
-
-
-
- const int spts=ndim+1;
- int i,j, max_val;
-
- int ilo;
- int ihi;
-
- int inhi;
-
- double rtol,ytry;
-
- double ysave;
- matrix_type psum(1,ndim);
-
-
- m_startTime = clock();
-
- for (j=0;j<ndim;j++)
- {
- double sum=0.0;
-
- for (i=0;i<spts;i++)
- sum += m_vertices[j][i];
- psum[0][j]=sum;
- }
-
- for (;;)
- {
- if(m_verbose)
- {
- for(int u = 0; u < ndim+1; u++)
- {
- for(int v = 0; v < ndim ; v++)
- {
- std::cout << m_vertices[v][u] << " ";
- }
- std::cout<< " " << m_y[u][0] << std::endl;
- }
- std::cout << std::endl;
- }
- ilo = 0;
-
-
-
-
-
-
- if(ndim >= 2)
- {
- ihi = m_y[1][0]>m_y[2][0] ? (inhi=1,2) : (inhi=2,1);
- for (i=0;i<spts;i++)
- {
- if (m_y[i][0] <= m_y[ilo][0]) ilo=i;
- if (m_y[i][0] > m_y[ihi][0]) {
- inhi=ihi;
- ihi=i;
- }
- else
- if (m_y[i][0] > m_y[inhi][0])
- if (i != ihi)
- inhi=i;
- }
- }
- else
- {
- if(m_y[0][0]>m_y[1][0])
- {
- ilo = 1;
- inhi = 1;
- ihi = 0;
- }
- else
- {
- ilo = 0;
- inhi = 0;
- ihi = 1;
- }
- }
-
-
- if(m_loger)
- {
-
- matrix_type optparams(m_vertices.rows(),1,0);
- for(int i= 0; i< m_vertices.rows(); ++i)
- {
- optparams[i][0]= m_vertices[i][ilo];
- }
- matrix_type fullparams= m_costFunction->getFullParamsFromSubParams(optparams);
- m_loger->writeParamsToFile(fullparams);
- }
-
-
- if(m_abort == true)
- {
- break;
- }
-
- if(m_maxSecondsActive)
- {
- m_currentTime = clock();
-
- if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
- {
-
- m_returnReason = SUCCESS_TIMELIMIT;
- break;
- }
- }
-
-
- if(m_funcTolActive == true)
- {
- rtol=2.0*fabs(m_y[ihi][0]-m_y[ilo][0])/(fabs(m_y[ihi][0])+fabs(m_y[ilo][0])+1e-10);
-
-
-
- #ifdef OPT_DEBUG
- std::cout<<"rtol"<<" "<<rtol<< std::endl;
- #endif
-
- if (rtol<tol)
- {
-
- max_val=(int)m_y[ilo][0];
- m_returnReason = SUCCESS_FUNCTOL;
- break;
- }
- }
-
-
- if (m_paramTolActive == true)
- {
-
-
-
- if ( (m_vertices(0,ihi,m_numberOfParameters-1,ihi) -
- m_vertices(0,ilo,m_numberOfParameters-1,ilo)).Norm(0) < m_paramTol)
- {
-
- m_returnReason = SUCCESS_PARAMTOL;
- break;
- }
- }
-
-
-
- m_numIter++;
-
-
- if(m_maxNumIterActive == true)
- {
- if (m_numIter >= m_maxNumIter)
- {
-
- m_returnReason = SUCCESS_MAXITER;
- break;
- }
- }
-
-
-
-
-
-
-
-
-
- ytry=amotry(psum,ihi,-m_alpha);
- if (ytry < m_y[ilo][0])
- {
-
-
-
-
- ytry=amotry(psum,ihi,m_gamma);
-
- #ifdef OPT_DEBUG
- std::cout<<"Case one .. reflected highest through simplex" << std::endl;
- #endif
- }
- else
- {
-
- if (ytry >= m_y[inhi][0])
- {
-
-
-
- ysave=m_y[ihi][0];
-
-
- ytry=amotry(psum,ihi,m_beta);
- #ifdef OPT_DEBUG
- std::cout<<"Case two .. looking for intermediate point" << std::endl;
- #endif
- if (ytry >= ysave)
- {
-
- #ifdef OPT_DEBUG
- std::cout<<"Case three .. contract around lowest point" << std::endl;
- #endif
- for (i=0;i<spts;i++)
- {
-
- if (i!=ilo)
- {
- for (j=0;j<ndim;j++)
- {
- psum[0][j]=0.5*(m_vertices[j][i]+m_vertices[j][ilo]);
- #ifdef OPT_DEBUG
- printf("psum(%d)=%f\n",j,psum[0][j]);
- #endif
- m_vertices[j][i]=psum[0][j];
- }
- if (checkParameters(!psum))
- {
- m_y[i][0]= evaluateCostFunction(!psum);
-
- }
- else
- {
- m_returnReason = ERROR_XOUTOFBOUNDS;
- break;
- }
- }
- for (j=0;j<ndim;j++)
- {
- double sum=0.0;
- for (int ii=0;ii<spts;ii++)
- sum += m_vertices[j][ii];
- psum[0][j]=sum;
- }
- }
- }
- }
- }
-
- }
-
- return ilo;
- }
- double DownhillSimplexOptimizer::amotry(matrix_type & psum, int ihi, double fac)
- {
-
-
-
-
- const double maxreal= (m_maximize == true)? -1.0e+300 : 1.0e+300;
- double fac1,fac2,ytry;
- int ndim=m_numberOfParameters;
- matrix_type ptry(1,ndim);
- fac1=(1.0-fac)/ndim;
- fac2=fac1-fac;
-
- for (int j=0;j<ndim;j++)
- ptry[0][j]=psum[0][j]*fac1-m_vertices[j][ihi]*fac2;
-
- if (checkParameters(!ptry))
- {
- ytry=evaluateCostFunction(!ptry);
-
- if (ytry<m_y[ihi][0]) {
-
- m_y[ihi][0]=ytry;
- for (int j=0; j<ndim;j++) {
- psum[0][j] = psum[0][j] + ptry[0][j]-m_vertices[j][ihi];
- m_vertices[j][ihi]=ptry[0][j];
- }
- }
- }
- else
- {
- ytry=maxreal;
- m_abort = true;
- m_returnReason = ERROR_XOUTOFBOUNDS;
-
- }
- return ytry;
- }
- void DownhillSimplexOptimizer::setDownhillParams(double alpha, double beta, double gamma)
- {
- m_alpha = alpha;
- m_beta = beta;
- m_gamma = gamma;
- }
- void DownhillSimplexOptimizer::setRankDeficiencyThresh(float rankdeficiencythresh)
- {
- m_rankdeficiencythresh= rankdeficiencythresh;
- }
- void DownhillSimplexOptimizer::setRankCheckStatus(bool status)
- {
- m_rankcheckenabled= status;
- }
- bool DownhillSimplexOptimizer::getRankCheckStatus()
- {
- return m_rankcheckenabled;
- }
- unsigned int getRank(const matrix_type &A,double numZero)
- {
- unsigned int tmpCount = 0;
- matrix_type U,s,Vt;
-
- if(A.rows() < A.cols())
- {
- SingularValueDcmp(!A, U, s, Vt);
- }
- else
- {
- SingularValueDcmp(A, U, s, Vt);
- }
-
- for(int i= 0; i < s.rows();i++)
- {
- if( s[i][i] > numZero )
- {
- tmpCount++;
- }
- }
-
- return tmpCount;
- }
|