00001
00002
00004
00005 #include "SharedBFactorNetworkMinimizable.h"
00006
00008
00010
00011 SharedBFactorNetworkMinimizable::SharedBFactorNetworkMinimizable()
00012 {
00013
00014 }
00015
00016 SharedBFactorNetworkMinimizable::SharedBFactorNetworkMinimizable(std::vector<Experiment *> experiments,std::vector<CReactionMover *> reactionMovers, bool rateConstantsOptimizable, bool initialChemConcOptimizable, bool logsInObjectiveFunction, double timeSeriesWeight, double rateConstantsWeight, double initialChemConcWeight, int nCells)
00017 {
00018 Initialize(experiments,reactionMovers,nCells,rateConstantsOptimizable,initialChemConcOptimizable,logsInObjectiveFunction,timeSeriesWeight,rateConstantsWeight,initialChemConcWeight);
00019
00020 }
00021
00022 SharedBFactorNetworkMinimizable::~SharedBFactorNetworkMinimizable()
00023 {
00024
00025
00026 for (int expCounter=0; expCounter < nExp; expCounter++) {
00027 this->reactionMovers[expCounter]->Detach(cellObservers[expCounter]);
00028 delete cellObservers[expCounter];
00029 }
00030 for(int i = 0; i < m_pConversionFactors.size(); i++)
00031 {
00032 delete m_pConversionFactors[i];
00033 }
00034 }
00035
00036 void SharedBFactorNetworkMinimizable::Initialize(std::vector<Experiment *> experiments, std::vector<CReactionMover *> reactionMovers, int nCells, bool rateConstantsOptimizable, bool initialChemConcOptimizable, bool logsInObjectiveFunction, double timeSeriesWeight, double rateConstantsWeight, double initialChemConcWeight)
00037 {
00038 this->nExp = experiments.size();
00039 this->experiments = experiments;
00040 this->reactionMovers = reactionMovers;
00041 this->nCells = nCells;
00042 this->rateConstantsOptimizable = rateConstantsOptimizable;
00043 this->initialChemConcOptimizable = initialChemConcOptimizable;
00044 this->logsInObjectiveFunction = logsInObjectiveFunction;
00045
00046 this->SetTimeSeriesWeight(timeSeriesWeight);
00047 this->SetRateConstantsWeight(rateConstantsWeight);
00048 this->SetInitialChemConcWeight(initialChemConcWeight);
00049
00050 for (int i=0; i < nExp; i++) {
00051
00052 this->experiments[i]->ReadData();
00053
00054 this->experiments[i]->ReadForcingData();
00055 }
00056
00057
00058
00059 int nChem = this->experiments[0]->GetReactionNetwork()->GetNumberOfChemicals();
00060 int nRateConstants = this->experiments[0]->GetReactionNetwork()->GetNumberOfRateConstants();
00061
00062
00063 for(int expCounter=0; expCounter < nExp; expCounter++) {
00064 assert(nChem == this->experiments[expCounter]->GetReactionNetwork()->GetNumberOfChemicals());
00065 assert(nRateConstants == this->experiments[expCounter]->GetReactionNetwork()->GetNumberOfRateConstants() );
00066 int intTime = ((int) this->experiments[expCounter]->GetChemicalTimeSeriesData()->GetLatestDataTime()) + 2;
00067
00068 this->cellObservers.push_back(new CellAverageObserver(nChem,intTime,nCells));
00069
00070 this->reactionMovers[expCounter]->Attach(this->cellObservers[expCounter]);
00071 }
00072
00073 initialNetworkData.reserve(nExp);
00074 currentNetworkData.reserve(nExp);
00075
00076 for (int expCounter=0; expCounter<nExp; expCounter++) {
00077 currentNetworkData.push_back(new std::vector<double *>);
00078 (*currentNetworkData[expCounter]).reserve(nRateConstants + nChem);
00079 initialNetworkData.push_back(new std::vector<double *>);
00080 (*initialNetworkData[expCounter]).reserve(nRateConstants + nChem);
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 for(int l = 0; l < nChem; l++)
00093 {
00094 bool needFactorI = false;
00095 for (int i = 0; i < nExp; i++) {
00096 if((experiments[i]->GetChemicalTimeSeriesData()->GetTimeSeries(l)).size() > 0 )
00097 {
00098 needFactorI = true;
00099 break;
00100 }
00101 }
00102 if (needFactorI) {
00103 m_pConversionFactors.push_back(new CConversionFactor(true,false,1.0));
00104 }
00105 else
00106 {
00107 m_pConversionFactors.push_back(new CConversionFactor(false));
00108 }
00109 }
00110
00111 m_iNBFactors = 0;
00112
00113
00114
00115 for (int expCounter=0; expCounter < nExp; expCounter++) {
00116 for(int i = 0; i < nRateConstants; i++)
00117 {
00118 (*initialNetworkData[expCounter]).push_back(new double);
00119 (*(*initialNetworkData[expCounter])[i]) = this->experiments[expCounter]->GetReactionNetwork()->GetRateConstant(i)->GetInitialValue();
00120 (*currentNetworkData[expCounter]).push_back(new double);
00121 (*(*currentNetworkData[expCounter])[i]) = (*(*initialNetworkData[expCounter])[i]);
00122 }
00123 for(int i = nRateConstants; i < nRateConstants + nChem; i++)
00124 {
00125 (*initialNetworkData[expCounter]).push_back(new double);
00126 (*(*initialNetworkData[expCounter])[i]) = this->experiments[expCounter]->GetReactionNetwork()->GetChemical(i-nRateConstants)->GetInitialAmount();
00127 (*currentNetworkData[expCounter]).push_back(new double);
00128 (*(*currentNetworkData[expCounter])[i]) = (*(*initialNetworkData[expCounter])[i]);
00129
00130 }
00131 }
00132
00133 int localResidualsSize = 0;
00134 if(this->timeSeriesWeight > 0.0) {
00135 for (int expCounter=0; expCounter < nExp; expCounter++) {
00136 localResidualsSize += this->experiments[expCounter]->GetChemicalTimeSeriesData()->GetNDataPoints();
00137 }
00138 }
00139 if(this->rateConstantsWeight > 0.0) {localResidualsSize += nExp*nRateConstants;}
00140 if(this->initialChemConcWeight > 0.0) {localResidualsSize += nExp*nChem;}
00141
00142 Allocate(localResidualsSize);
00143
00144 }
00145
00146 double SharedBFactorNetworkMinimizable::ComputeES(double *parameters, double T)
00147 {
00148 m_dEnergyLastComputed = this->ComputeResiduals(parameters);
00149 double S = this->EntropyShift(T) - 0.5*m_dEntropyLastComputed;
00150 m_dEntropyLastComputed = S;
00151 return m_dEnergyLastComputed;
00152 }
00153
00154
00155
00156
00157
00158
00159 double SharedBFactorNetworkMinimizable::F(double *parameters, double T)
00160 {
00161 m_dEnergyLastComputed = this->ComputeResiduals(parameters);
00162 double S = -0.5*m_dEntropyLastComputed;
00163 m_dEntropyLastComputed = S;
00164 return (m_dEnergyLastComputed - T*m_dEntropyLastComputed);
00165 }
00166
00167 double SharedBFactorNetworkMinimizable::F0(double *parameters, double T)
00168 {
00169 m_dEnergyLastComputed = this->ComputeResiduals(parameters);
00170 double S = this->EntropyShift(T) - 0.5*m_dEntropyLastComputed;
00171 m_dEntropyLastComputed = S;
00172 return (m_dEnergyLastComputed - T*m_dEntropyLastComputed);
00173 }
00174
00175 double SharedBFactorNetworkMinimizable::ComputeResiduals(double *parameters)
00176 {
00177 m_iNBFactors = 0;
00178 m_dEntropyLastComputed = 0.0;
00179 double partialEntropy = 0.0;
00180
00181 double objFuncVal = 0.0;
00182
00183 int nRateConstants = experiments[0]->GetReactionNetwork()->GetNumberOfRateConstants();
00184 int nChem = experiments[0]->GetReactionNetwork()->GetNumberOfChemicals();
00185
00186
00187
00188
00189
00190
00191
00192
00193 if(rateConstantsOptimizable && initialChemConcOptimizable)
00194 {
00195 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00196 for(int i = 0; i < nRateConstants; i++)
00197 {
00198 experiments[expCounter]->GetReactionNetwork()->GetRateConstant(i)->SetRateConstant(parameters[i]);
00199 (*(*currentNetworkData[expCounter])[i]) = parameters[i];
00200 }
00201 for(int i = nRateConstants; i < nRateConstants + nChem; i++)
00202 {
00203 experiments[expCounter]->GetReactionNetwork()->GetChemical(i - nRateConstants)->SetAmount(parameters[i]);
00204 (*(*currentNetworkData[expCounter])[i]) = parameters[i];
00205 }
00206 }
00207 }
00208 else if(rateConstantsOptimizable)
00209 {
00210 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00211 for(int i = 0; i < nRateConstants; i++)
00212 {
00213 experiments[expCounter]->GetReactionNetwork()->GetRateConstant(i)->SetRateConstant(parameters[i]);
00214 (*(*currentNetworkData[expCounter])[i]) = parameters[i];
00215 }
00216 experiments[expCounter]->GetReactionNetwork()->ChemicalReset();
00217 }
00218 }
00219 else if(initialChemConcOptimizable)
00220 {
00221 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00222 for(int i = 0; i < nChem; i++)
00223 {
00224 experiments[expCounter]->GetReactionNetwork()->GetChemical(i)->SetAmount(parameters[i]);
00225 (*(*currentNetworkData[expCounter])[i+nRateConstants]) = parameters[i];
00226 }
00227 experiments[expCounter]->GetReactionNetwork()->RateConstantReset();
00228 }
00229 }
00230 else
00231 {
00232
00233
00234
00235
00236
00237 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00238 experiments[expCounter]->GetReactionNetwork()->RateConstantReset();
00239 experiments[expCounter]->GetReactionNetwork()->ChemicalReset();
00240 }
00241 }
00242
00243
00244 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00245 cellObservers[expCounter]->ZeroConcentrations();
00246 }
00247 for (int expCounter = 0; expCounter < nExp; expCounter++) {
00248
00249 for(int cellCounter = 1; cellCounter <= nCells; cellCounter++)
00250 {
00251
00252
00253
00254
00255 if(initialChemConcOptimizable)
00256 {
00257 for(int i = nRateConstants; i < nRateConstants + nChem; i++)
00258 {
00259 experiments[expCounter]->GetReactionNetwork()->GetChemical(i - nRateConstants)->SetAmount(parameters[i]);
00260 }
00261 }
00262 else
00263 {
00264 experiments[expCounter]->GetReactionNetwork()->ChemicalReset();
00265 }
00266
00267
00268
00269
00270
00271
00272 reactionMovers[expCounter]->ResetStepStatistics(experiments[expCounter]->GetChemicalTimeSeriesData()->GetLatestDataTime());
00273
00274
00275 if(experiments[expCounter]->GetForcingData()->GetForcingVector().size() == 0)
00276 {
00277
00278 reactionMovers[expCounter]->Move(0.0,0.0,experiments[expCounter]->GetReactionNetwork());
00279 reactionMovers[expCounter]->Move(0.0,experiments[expCounter]->GetChemicalTimeSeriesData()->GetLatestDataTime(),experiments[expCounter]->GetReactionNetwork());
00280 }
00281 else
00282 {
00283 int forceCount = 0;
00284 double time = 0.0;
00285 int lastForce = experiments[expCounter]->GetForcingData()->GetForcingVector().size();
00286 double finaltime = experiments[expCounter]->GetChemicalTimeSeriesData()->GetLatestDataTime();
00287 double finalForceTime = experiments[expCounter]->GetForcingData()->GetLatestForcingTime();
00288 int index;
00289 int start,stop;
00290 double t1,t2,value;
00291 while(time < finalForceTime)
00292 {
00293 start = forceCount;
00294 stop = forceCount;
00295 t1 = experiments[expCounter]->GetForcingData()->GetForcingVector()[start]->GetTime();
00296
00297 for(int i = start; i < lastForce; i++)
00298 {
00299 t2 = experiments[expCounter]->GetForcingData()->GetForcingVector()[i]->GetTime();
00300 if(t1 == t2)
00301 {
00302 }
00303 else
00304 {
00305 stop = i-1;
00306 for(int k = start; k <= stop; k++)
00307 {
00308 index = experiments[expCounter]->GetForcingData()->GetForcingVector()[k]->GetChemNumber();
00309 value = experiments[expCounter]->GetForcingData()->GetForcingVector()[k]->GetValue();
00310 experiments[expCounter]->GetReactionNetwork()->GetChemical(index)->SetAmount(value);
00311 }
00312 break;
00313 }
00314 }
00315 forceCount = stop+1;
00316
00317 reactionMovers[expCounter]->Move(time,t1,experiments[expCounter]->GetReactionNetwork());
00318 time = t1;
00319 }
00320
00321
00322 t2 = finalForceTime;
00323 stop = lastForce-1;
00324 for(int i = lastForce-1; i > 0; i--)
00325 {
00326 t1 = experiments[expCounter]->GetForcingData()->GetForcingVector()[i]->GetTime();
00327 if(t1 == t2)
00328 {
00329 }
00330 else
00331 {
00332 start = i+1;
00333 for(int k = start; k <= stop; k++)
00334 {
00335 index = experiments[expCounter]->GetForcingData()->GetForcingVector()[k]->GetChemNumber();
00336 value = experiments[expCounter]->GetForcingData()->GetForcingVector()[k]->GetValue();
00337 experiments[expCounter]->GetReactionNetwork()->GetChemical(index)->SetAmount(value);
00338 }
00339 break;
00340 }
00341 }
00342
00343 if(finaltime > time)
00344 {
00345 reactionMovers[expCounter]->Move(time,finaltime,experiments[expCounter]->GetReactionNetwork());
00346 }
00347 time = finaltime;
00348 }
00349 }
00350 }
00351
00352
00353
00354
00355
00356 for(int chemCount = 0; chemCount < nChem; chemCount++)
00357 {
00358 double num = 0.0;
00359 double den = 0.0;
00360
00361
00362 if(m_pConversionFactors[chemCount]->IsFactorNeeded())
00363 {
00364 if(m_pConversionFactors[chemCount]->IsFactorFixed())
00365 {
00366 num = m_pConversionFactors[chemCount]->GetFactorValue();
00367 den = 1.0;
00368 }
00369 else
00370 {
00371 m_iNBFactors++;
00372 for (int expCounter=0; expCounter < nExp; expCounter++) {
00373 int dataSize = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(chemCount)).size();
00374
00375 for(int nData = 0; nData < dataSize; nData++)
00376 {
00377 double time = experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(chemCount)[nData]->GetTime();
00378 double data = experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(chemCount)[nData]->GetDatum();
00379 double error = experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(chemCount)[nData]->GetError();
00380 double sim = (cellObservers[expCounter]->GetAverageConcentration())[chemCount][int(time)];
00381 num += ((2*sim)/(error*error))*data;
00382 den += ((2*sim)/(error*error))*sim;
00383
00384 partialEntropy += ((sim*sim)/(error*error));
00385 }
00386 m_dEntropyLastComputed += log(partialEntropy);
00387 }
00388 }
00389 }
00390 else
00391 {
00392 num = 1.0;
00393 den = 1.0;
00394 }
00395 double factor = num/den;
00396
00397 m_pConversionFactors[chemCount]->SetFactorValue(factor);
00398
00399 }
00400
00401
00402 int nextAvailableIndex = 0;
00403 if(timeSeriesWeight > 0.0)
00404 {
00405 for (int expCounter=0; expCounter<nExp; expCounter++) {
00406
00407 for(int chemIt = 0; chemIt < (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeVector()).size(); chemIt++)
00408 {
00409
00410 double targetTime = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeVector())[chemIt]->GetTime();
00411 int targetChem = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeVector())[chemIt]->GetChemNumber();
00412 int vectorIndex = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeVector())[chemIt]->GetVectorIndex();
00413
00414 double data = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(targetChem))[vectorIndex]->GetDatum();
00415 double sigma = (experiments[expCounter]->GetChemicalTimeSeriesData()->GetTimeSeries(targetChem))[vectorIndex]->GetError();
00416 double sim = (cellObservers[expCounter]->GetAverageConcentration())[targetChem][int(targetTime)];
00417
00418 if(m_pConversionFactors[targetChem]->IsFactorNeeded())
00419 {
00420 sim = sim*(m_pConversionFactors[targetChem]->GetFactorValue());
00421 }
00422
00423 residuals[nextAvailableIndex] = (sqrt(timeSeriesWeight/2.0))*((data - sim)/sigma);
00424 objFuncVal += residuals[nextAvailableIndex]*residuals[nextAvailableIndex];
00425 nextAvailableIndex++;
00426 }
00427 }
00428 }
00429
00430 if(rateConstantsWeight > 0.0)
00431 {
00432 for (int expCounter=0; expCounter<nExp; expCounter++) {
00433
00434 if(logsInObjectiveFunction)
00435 {
00436 for(int i = 0; i < nRateConstants; i++)
00437 {
00438 double sigma = experiments[expCounter]->GetReactionNetwork()->GetRateConstant(i)->GetErrorInInitialValue();
00439 double sigmalog = sigma/(*(*initialNetworkData[expCounter])[i]);
00440 residuals[nextAvailableIndex] = (sqrt(rateConstantsWeight/2.0))*(log((*(*initialNetworkData[expCounter])[i])/(*(*currentNetworkData[expCounter])[i]))/sigmalog);
00441 objFuncVal += residuals[nextAvailableIndex]*residuals[nextAvailableIndex];
00442 nextAvailableIndex++;
00443 }
00444 }
00445 else
00446 {
00447 for(int i = 0; i < nRateConstants; i++)
00448 {
00449 double sigma = experiments[expCounter]->GetReactionNetwork()->GetRateConstant(i)->GetErrorInInitialValue();
00450 residuals[nextAvailableIndex] = (sqrt(rateConstantsWeight/2.0))*((initialNetworkData[i+expCounter*(nRateConstants+nChem)] - currentNetworkData[i+expCounter*(nRateConstants+nChem)])/sigma);
00451 objFuncVal += residuals[nextAvailableIndex]*residuals[nextAvailableIndex];
00452 nextAvailableIndex++;
00453 }
00454 }
00455 }
00456 }
00457
00458 if(initialChemConcWeight > 0.0)
00459 {
00460 for (int expCounter=0; expCounter<nExp; expCounter++) {
00461
00462 for(int i = 0; i < nChem; i++)
00463 {
00464 double sigma = experiments[expCounter]->GetReactionNetwork()->GetChemical(i)->GetErrorInInitialAmount();
00465 residuals[nextAvailableIndex] = (sqrt(initialChemConcWeight/2.0))*((initialNetworkData[i+nRateConstants+expCounter*(nRateConstants+nChem)] - currentNetworkData[i+nRateConstants+expCounter*(nRateConstants+nChem)])/sigma);
00466 objFuncVal += residuals[nextAvailableIndex]*residuals[nextAvailableIndex];
00467 nextAvailableIndex++;
00468 }
00469 }
00470 }
00471
00472
00473 Notify();
00474
00475 return objFuncVal;
00476 }
00477
00478 int SharedBFactorNetworkMinimizable::GetNParameters()
00479 {
00480
00481
00482
00483
00484 int nRateConstants = experiments[0]->GetReactionNetwork()->GetNumberOfRateConstants();
00485 int nChem = experiments[0]->GetReactionNetwork()->GetNumberOfChemicals();
00486 int nParameters;
00487
00488
00489 if(rateConstantsOptimizable && initialChemConcOptimizable)
00490 {
00491 nParameters = nRateConstants + nChem;
00492 }
00493 else if(rateConstantsOptimizable)
00494 {
00495 nParameters = nRateConstants;
00496 }
00497 else if(initialChemConcOptimizable)
00498 {
00499 nParameters = nChem;
00500 }
00501 else
00502 {
00503 nParameters = 0;
00504 }
00505
00506 return nParameters;
00507 }
00508
00509 double SharedBFactorNetworkMinimizable::GetParameter(int parIndex)
00510 {
00511 int nP = GetNParameters();
00512
00513 if(parIndex > nP)
00514 {
00515 throw std::runtime_error("Parameter index is off parameter list.");
00516 }
00517 else
00518 {
00519 return (*(*currentNetworkData[0])[parIndex]);
00520 }
00521
00522 return 0.0;
00523 }