|
@@ -1,16 +1,20 @@
|
|
|
|
|
|
*
|
|
|
-* @file: DownhillSimplexOptimizer.cpp: implementation of the downhill Simplex Optimier
|
|
|
+* @file: DownhillSimplexOptimizer.cpp: implementation of the downhill Simplex Optimier
|
|
|
*
|
|
|
-* @author: Matthias Wacker, Esther Platzer
|
|
|
+* @author: Matthias Wacker, Esther Platzer, Alexander Freytag
|
|
|
*
|
|
|
*/
|
|
|
|
|
|
+#include <iostream>
|
|
|
+
|
|
|
+#include "mateigen.h"
|
|
|
+
|
|
|
#include "optimization/DownhillSimplexOptimizer.h"
|
|
|
#include "optimization/Opt_Namespace.h"
|
|
|
-#include "mateigen.h"
|
|
|
|
|
|
-#include <iostream>
|
|
|
+
|
|
|
+
|
|
|
|
|
|
using namespace optimization;
|
|
|
|
|
@@ -29,15 +33,15 @@ DownhillSimplexOptimizer::DownhillSimplexOptimizer(OptLogBase *loger): SuperClas
|
|
|
|
|
|
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;
|
|
|
+ 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()
|
|
@@ -46,163 +50,173 @@ 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))
|
|
|
- {
|
|
|
-
|
|
|
- * Check if simplex has rank(simplex)=numberOfParameters
|
|
|
- * otherwise return false because of bad posed simplex
|
|
|
- */
|
|
|
-
|
|
|
- if(m_rankcheckenabled)
|
|
|
+ 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))
|
|
|
{
|
|
|
- int rank = getRank(simplex, m_rankdeficiencythresh);
|
|
|
-
|
|
|
- if(rank < static_cast<int>(m_numberOfParameters))
|
|
|
- {
|
|
|
- m_simplexInitialized = false;
|
|
|
- return false;
|
|
|
- }
|
|
|
+ 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));
|
|
|
- }
|
|
|
+ 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;
|
|
|
- }
|
|
|
+ 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;
|
|
|
-
|
|
|
-
|
|
|
- compute the function values corresponding to the simplex
|
|
|
- */
|
|
|
-
|
|
|
- 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));
|
|
|
- }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ else
|
|
|
+ {
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-* Numerical Recipies in C
|
|
|
+* Taken from Numerical Recipies in C
|
|
|
*/
|
|
|
int DownhillSimplexOptimizer::amoeba()
|
|
|
{
|
|
|
-
|
|
|
- double tol =m_funcTol;
|
|
|
-
|
|
|
- const int ndim=m_numberOfParameters;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ double tol = m_funcTol;
|
|
|
|
|
|
+
|
|
|
+ const int ndim=m_numberOfParameters;
|
|
|
if (m_verbose)
|
|
|
{
|
|
|
std::cerr << "ndim: " << ndim << std::endl;
|
|
|
- }
|
|
|
+ }
|
|
|
|
|
|
- const int spts=ndim+1;
|
|
|
- int i,j, max_val;
|
|
|
+
|
|
|
+ const int spts=ndim+1;
|
|
|
+ int i,j, max_val;
|
|
|
|
|
|
- int ilo;
|
|
|
- int ihi;
|
|
|
-
|
|
|
- int inhi;
|
|
|
-
|
|
|
- double rtol,ytry;
|
|
|
+
|
|
|
+ int ilo;
|
|
|
+
|
|
|
+ int ihi;
|
|
|
+
|
|
|
+ int inhi;
|
|
|
+
|
|
|
+ double rtol,ytry;
|
|
|
|
|
|
- double ysave;
|
|
|
- matrix_type psum(1,ndim);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- start time criteria
|
|
|
- */
|
|
|
- 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;
|
|
|
- std::cerr << sum << " " ;
|
|
|
- }
|
|
|
- std::cerr << std::endl;
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
|
|
|
if (m_verbose)
|
|
|
{
|
|
@@ -213,293 +227,291 @@ int DownhillSimplexOptimizer::amoeba()
|
|
|
}
|
|
|
std::cerr << std::endl;
|
|
|
}
|
|
|
-
|
|
|
- for (;;)
|
|
|
- {
|
|
|
- if(m_verbose)
|
|
|
- {
|
|
|
- for(int u = 0; u < ndim+1; u++)
|
|
|
- {
|
|
|
- for(int v = 0; v < ndim ; v++)
|
|
|
- {
|
|
|
- std::cerr << m_vertices[v][u] << " ";
|
|
|
- }
|
|
|
- std::cerr<< " " << m_y[u][0] << std::endl;
|
|
|
- }
|
|
|
- std::cerr << std::endl;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- ilo = 0;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- this works only, if m_numberOfParameters=ndim >= 2
|
|
|
- */
|
|
|
- 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)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for (;;)
|
|
|
+ {
|
|
|
+
|
|
|
+ if(m_verbose)
|
|
|
+ {
|
|
|
+ for(int u = 0; u < ndim+1; u++)
|
|
|
+ {
|
|
|
+ for(int v = 0; v < ndim ; v++)
|
|
|
{
|
|
|
-
|
|
|
- matrix_type optparams(m_vertices.rows(),1,0);
|
|
|
+ std::cerr << m_vertices[v][u] << " ";
|
|
|
+ }
|
|
|
+ std::cerr<< " " << m_y[u][0] << std::endl;
|
|
|
+ }
|
|
|
+ std::cerr << std::endl;
|
|
|
+ }
|
|
|
|
|
|
- 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);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- Check m_abort .. outofbounds may have occurred in amotry() last iteration
|
|
|
- */
|
|
|
- if(m_abort == true)
|
|
|
- {
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- Check time criterion
|
|
|
- */
|
|
|
- if(m_maxSecondsActive)
|
|
|
- {
|
|
|
- m_currentTime = clock();
|
|
|
-
|
|
|
-
|
|
|
- if(((float)(m_currentTime - m_startTime )/CLOCKS_PER_SEC) >= m_maxSeconds )
|
|
|
- {
|
|
|
-
|
|
|
- m_returnReason = SUCCESS_TIMELIMIT;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- check functol criterion
|
|
|
- */
|
|
|
- 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;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- check param tol criterion
|
|
|
- */
|
|
|
- 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)
|
|
|
- {
|
|
|
-
|
|
|
- set according return reason and end optimization
|
|
|
- */
|
|
|
- m_returnReason = SUCCESS_PARAMTOL;
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- m_numIter++;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- check max num iter criterion
|
|
|
- */
|
|
|
- if(m_maxNumIterActive == true)
|
|
|
- {
|
|
|
- if (m_numIter >= m_maxNumIter)
|
|
|
- {
|
|
|
-
|
|
|
- m_returnReason = SUCCESS_MAXITER;
|
|
|
- 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)
|
|
|
+ {
|
|
|
+
|
|
|
+ set according return reason and end optimization
|
|
|
+ */
|
|
|
+ m_returnReason = SUCCESS_PARAMTOL;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ m_numIter++;
|
|
|
+
|
|
|
+
|
|
|
+ if(m_maxNumIterActive == true)
|
|
|
+ {
|
|
|
+ if (m_numIter >= m_maxNumIter)
|
|
|
+ {
|
|
|
+
|
|
|
+ m_returnReason = SUCCESS_MAXITER;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
if (m_verbose)
|
|
|
{
|
|
|
std::cerr << "start new iteration with amotry -alpha, i.e., reflect worst point through simplex" << std::endl;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- ytry=amotry(psum,ihi,-m_alpha);
|
|
|
- if (ytry < m_y[ilo][0])
|
|
|
- {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ ytry=amotry(psum,ihi,-m_alpha);
|
|
|
+
|
|
|
+ if (ytry < m_y[ilo][0])
|
|
|
+ {
|
|
|
|
|
|
if (m_verbose)
|
|
|
{
|
|
|
std::cerr << "reflected point is better than best point, perform further extrapolation with gamma" << std::endl;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- 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])
|
|
|
- {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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])
|
|
|
+ {
|
|
|
|
|
|
if (m_verbose)
|
|
|
{
|
|
|
std::cerr << "reflected point is worse then second worst, looking for intermediate point with beta" << std::endl;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- 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
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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
|
|
|
|
|
|
if (m_verbose)
|
|
|
{
|
|
|
std::cerr << "Intermediate point is also worse, contract around current best point with factor 0.5." << std::endl;
|
|
|
}
|
|
|
- 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;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+ 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;
|
|
@@ -519,27 +531,41 @@ double DownhillSimplexOptimizer::amotry(matrix_type & psum, int ihi, double fac)
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
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];
|
|
|
- }
|
|
|
- }
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
-
|
|
|
- }
|
|
|
+ {
|
|
|
+ ytry=maxreal;
|
|
|
+ m_abort = true;
|
|
|
+ m_returnReason = ERROR_XOUTOFBOUNDS;
|
|
|
+ }
|
|
|
return ytry;
|
|
|
}
|
|
|
|
|
@@ -587,26 +613,28 @@ bool DownhillSimplexOptimizer::getRankCheckStatus()
|
|
|
|
|
|
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;
|
|
|
+ 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;
|
|
|
}
|