1
/////////////////////////////////////////////////////////////
3
// Copyright (c) 2007-2011 by The University of Queensland //
4
// Earth Systems Science Computational Centre (ESSCC) //
5
// http://www.uq.edu.au/esscc //
7
// Primary Business: Brisbane, Queensland, Australia //
8
// Licensed under the Open Software License version 3.0 //
9
// http://www.opensource.org/licenses/osl-3.0.php //
11
/////////////////////////////////////////////////////////////
13
template<class T,int n>
14
simplex_method<T,n>::simplex_method(nfunction<T,n>* f)
20
template<class T,int n>
21
nvector<T,n> simplex_method<T,n>::reflect(int idx)
23
nvector<T,n> sum=nvector<T,n>(T(0));
24
for(int i=0;i<n+1;i++){
31
return sum-m_vert[idx];
34
template<class T,int n>
35
void simplex_method<T,n>::insert(const nvector<T,n>& vert,T val,int idx)
45
if(m_val[i]>m_val[i+1]){
49
nvector<T,n> h_vert=m_vert[i];
51
m_vert[i]=m_vert[i+1];
59
if(m_val[i]<m_val[i-1]){
63
nvector<T,n> h_vert=m_vert[i];
65
m_vert[i]=m_vert[i-1];
74
template<class T,int n>
75
void simplex_method<T,n>::shrink()
77
nvector<T,n> center=m_vert[0];
78
for(int i=1;i<n+1;i++){
82
for(int i=0;i<n+1;i++){
83
m_vert[i]=center+(m_vert[i]-center)/T(2);
84
m_val[i]=(*m_func)(m_vert[i]);
89
template<class T,int n>
90
void simplex_method<T,n>::sort()
92
for(int i=0;i<n-1;i++){
94
if(m_val[j]<m_val[j+1]){
95
nvector<T,n> h_vert=m_vert[j];
97
m_vert[j]=m_vert[j+1];
106
template<class T,int n>
107
nvector<T,n> simplex_method<T,n>::solve(T lim, const nvector<T,n> & start,int max)
110
for(int i=0;i<n+1;i++){
113
// set initial simplex vertices
114
insert(start,(*m_func)(start),0);
115
for(int i=0;i<n;i++){
116
nvector<T,n> temp_vert=start+nvector<T,n>::unit(i);
117
T temp_val=(*m_func)(temp_vert);
118
insert(temp_vert,temp_val,i+1);
122
while((eps>lim)&&((count<max)||(max==-1))){
125
while((idx<n+1) && !found){
126
nvector<T,n> temp_vert=reflect(idx);
127
T temp_val=(*m_func)(temp_vert);
128
if(temp_val<m_val[idx]){
129
insert(temp_vert,temp_val,idx);
135
if(idx==n+1) shrink();
136
//for(int i=0;i<n+1;i++){
137
// cout << m_vert[i] << " " << m_val[i] << endl;
139
//cout << "------------" << endl;
142
//std::cout << "step : " << count << " eps : " << eps << std::endl;