///////////////////////////////////////////////////////////// // // // Copyright (c) 2007-2017 by The University of Queensland // // Centre for Geoscience Computing // // http://earth.uq.edu.au/centre-geoscience-computing // // // // Primary Business: Brisbane, Queensland, Australia // // Licensed under the Open Software License version 3.0 // // http://www.apache.org/licenses/LICENSE-2.0 // // // ///////////////////////////////////////////////////////////// template simplex_method::simplex_method(nfunction* f) { m_func=f; } template nvector simplex_method::reflect(int idx) { nvector sum=nvector(T(0)); for(int i=0;i void simplex_method::insert(const nvector& vert,T val,int idx) { bool up=true; bool down=true; m_vert[idx]=vert; m_val[idx]=val; int i=idx; while(up && (im_val[i+1]){ up=false; } else { // swap nvector h_vert=m_vert[i]; T h_val=m_val[i]; m_vert[i]=m_vert[i+1]; m_val[i]=m_val[i+1]; m_vert[i+1]=h_vert; m_val[i+1]=h_val; i++; } } while(down && (i>0)){ if(m_val[i] h_vert=m_vert[i]; T h_val=m_val[i]; m_vert[i]=m_vert[i-1]; m_val[i]=m_val[i-1]; m_vert[i-1]=h_vert; m_val[i-1]=h_val; i--; } } } template void simplex_method::shrink() { nvector center=m_vert[0]; for(int i=1;i void simplex_method::sort() { for(int i=0;i h_vert=m_vert[j]; T h_val=m_val[j]; m_vert[j]=m_vert[j+1]; m_val[j]=m_val[j+1]; m_vert[j+1]=h_vert; m_val[j+1]=h_val; } } } } template nvector simplex_method::solve(T lim, const nvector & start,int max) { int idx; for(int i=0;i temp_vert=start+nvector::unit(i); T temp_val=(*m_func)(temp_vert); insert(temp_vert,temp_val,i+1); } T eps=m_val[0]; int count=0; while((eps>lim)&&((count temp_vert=reflect(idx); T temp_val=(*m_func)(temp_vert); if(temp_val