00001 // NetworkMinimizableDirector.cpp: implementation of the NetworkMinimizableDirector class. 00002 // 00004 00005 #include "NetworkMinimizableDirector.h" 00006 00008 // Construction/Destruction 00010 00011 NetworkMinimizableDirector::NetworkMinimizableDirector() 00012 { 00013 00014 } 00015 00016 NetworkMinimizableDirector::~NetworkMinimizableDirector() 00017 { 00018 int i; 00019 for(i = 0; i < _minimizableList.size(); i++) 00020 { 00021 delete _minimizableList[i]; 00022 } 00023 for(i = 0; i < _networkList.size(); i++) 00024 { 00025 delete _networkList[i]; 00026 } 00027 for(i = 0; i < _experimentList.size(); i++) 00028 { 00029 delete _experimentList[i]; 00030 } 00031 for(i = 0; i < _moverList.size(); i++) 00032 { 00033 delete _moverList[i]; 00034 } 00035 } 00036 00037 void NetworkMinimizableDirector::DumpResidualInfo() 00038 { 00039 fstream *pFile = new fstream("resinfo.par",ios::out); 00040 *pFile << "##################################################################" << endl; 00041 *pFile << "# RESIDUALS PRINTED IN THE ORDER THEY ARE STORED INTERNALLY" << endl; 00042 *pFile << "# C1 IS MINIMIZABLE NUM., C2 IS CHEMICAL NUM., C3 IS TIME" << endl; 00043 *pFile << "##################################################################" << endl; 00044 00045 for(int i = 0; i < _experimentList.size(); i++) 00046 { 00047 int tvecsize = _experimentList[i]->GetChemicalTimeSeriesData()->GetTimeVector().size(); 00048 for(int j = 0; j < tvecsize; j++) 00049 { 00050 int cnum = _experimentList[i]->GetChemicalTimeSeriesData()->GetTimeVector()[j]->GetChemNumber(); 00051 double time = _experimentList[i]->GetChemicalTimeSeriesData()->GetTimeVector()[j]->GetTime(); 00052 *pFile << i << "\t\t" << cnum << "\t\t" << time << endl; 00053 } 00054 } 00055 delete pFile; 00056 } 00057 00058 double NetworkMinimizableDirector::ObjectiveFunction(double *parameters) 00059 { 00060 double summedCost = 0.0; 00061 summedCost = this->ComputeResiduals(parameters); 00062 return summedCost; 00063 } 00064 00065 double NetworkMinimizableDirector::ComputeES(double *parameters, double T) 00066 { 00067 m_dEnergyLastComputed = this->ComputeResiduals(parameters); 00068 double S = 0; 00069 for(int i = 0; i < _minimizableList.size(); i++) 00070 { 00071 S += _minimizableList[i]->EntropyShift(T) - 0.5*(_minimizableList[i]->GetEntropyLastComputed()); 00072 } 00073 m_dEntropyLastComputed = S; 00074 return m_dEnergyLastComputed; 00075 } 00076 00077 double NetworkMinimizableDirector::EntropyShift(double T) 00078 { 00079 // total up the net entropy shift 00080 double S = 0; 00081 for(int i = 0; i < _minimizableList.size(); i++) 00082 { 00083 S += 0.5*(_minimizableList[i]->GetNBFactors())*log(2*3.14159265358979323846*T); 00084 } 00085 return S; 00086 } 00087 00088 double NetworkMinimizableDirector::F(double *parameters, double T) 00089 { 00090 m_dEnergyLastComputed = this->ComputeResiduals(parameters); 00091 double S = 0; 00092 for(int i = 0; i < _minimizableList.size(); i++) 00093 { 00094 S += -0.5*(_minimizableList[i]->GetEntropyLastComputed()); 00095 } 00096 m_dEntropyLastComputed = S; 00097 return (m_dEnergyLastComputed - T*m_dEntropyLastComputed); 00098 } 00099 00100 double NetworkMinimizableDirector::F0(double *parameters, double T) 00101 { 00102 m_dEnergyLastComputed = this->ComputeResiduals(parameters); 00103 double S = 0; 00104 for(int i = 0; i < _minimizableList.size(); i++) 00105 { 00106 S += _minimizableList[i]->EntropyShift(T) - 0.5*(_minimizableList[i]->GetEntropyLastComputed()); 00107 } 00108 m_dEntropyLastComputed = S; 00109 return (m_dEnergyLastComputed - T*m_dEntropyLastComputed); 00110 } 00111 00112 double NetworkMinimizableDirector::ComputeResiduals(double *parameters) 00113 { 00114 int i; 00115 // just sum up the costs from each director and then add the 00116 // integration penalty, = (gamma^2/2)*(sum_ti)^2 00117 double summedCost = 0.0; 00118 double timeCost = 0.0; 00119 double pcost = 0.0; 00120 00121 // compute the residuals/cost from each minimizable 00122 for(i = 0; i < _minimizableList.size(); i++) 00123 { 00124 summedCost += _minimizableList[i]->ObjectiveFunction(parameters); 00125 } 00126 00127 // load up the master list with each set of residuals 00128 int nextResidual = 0; 00129 for(i = 0; i < _minimizableList.size(); i++) 00130 { 00131 int nRes = _minimizableList[i]->GetNResiduals(); 00132 for(int j = 0; j < nRes; j++) 00133 { 00134 residuals[nextResidual+j] = (_minimizableList[i]->GetResiduals())[j]; 00135 } 00136 nextResidual += nRes; 00137 } 00138 00139 // now compute the cost for deviation from guessed values, if any 00140 for(i = 0; i < m_iPriorList.size(); i++) 00141 { 00142 int which = m_iPriorList[i]; 00143 //double presid = parameters[which] - _networkList[0]->GetRateConstant(which)->GetInitialValue(); 00144 double presid = _networkList[0]->GetRateConstant(which)->GetValue() - _networkList[0]->GetRateConstant(which)->GetInitialValue(); 00145 presid = presid/(sqrt(2.0)*_networkList[0]->GetRateConstant(which)->GetErrorInInitialValue()); 00146 residuals[nextResidual + i] = presid; 00147 pcost += presid*presid; 00148 } 00149 nextResidual += m_iPriorList.size(); 00150 00151 // if the prefactor for the integration cost is > 0, compute its residual (otherwise 00152 // that residual shouldn't exist) 00153 if(m_dGammaSquared > 0.0) 00154 { 00155 double totalSteps = 0.0; 00156 for(int l = 0; l < _moverList.size(); l++) 00157 { 00158 totalSteps += _moverList[l]->GetTotalStepsTaken(); 00159 } 00160 residuals[nResiduals - 1] = sqrt(m_dGammaSquared/2.0)*totalSteps; 00161 timeCost = residuals[nResiduals - 1]*residuals[nResiduals - 1]; 00162 } 00163 return summedCost + pcost + timeCost; 00164 }