00001 // SharedParametersMinimizableDirector.cpp: implementation of the CSharedParametersMinimizableDirector class. 00002 // 00004 00005 #include "SharedParametersMinimizableDirector.h" 00006 00008 // Construction/Destruction 00010 00011 CSharedParametersMinimizableDirector::CSharedParametersMinimizableDirector() 00012 { 00013 00014 } 00015 00016 CSharedParametersMinimizableDirector::~CSharedParametersMinimizableDirector() 00017 { 00018 delete m_pReplicaOne; 00019 delete m_pReplicaTwo; 00020 } 00021 00022 int CSharedParametersMinimizableDirector::GetNParameters() 00023 { 00024 int n1 = m_pReplicaOne->GetNParameters(); 00025 int n2 = m_pReplicaTwo->GetNParameters(); 00026 int n1intn2 = m_viIntersectOne.size(); 00027 00028 return n1 + n2 - n1intn2; 00029 } 00030 00031 double CSharedParametersMinimizableDirector::GetParameter(int parIndex) 00032 { 00033 // it's a parameter either in the intersection or 00034 // unique to the first network 00035 int firstNP = m_pReplicaOne->GetNParameters(); 00036 int intSize = m_viIntersectTwo.size(); 00037 if(parIndex < firstNP) 00038 { 00039 return m_pReplicaOne->GetParameter(parIndex); 00040 } 00041 else // it's a parameter unique to the second network 00042 { 00043 return m_pReplicaTwo->GetParameter(parIndex - intSize); 00044 } 00045 00046 } 00047 00048 void CSharedParametersMinimizableDirector::SetIntersectionLists() 00049 { 00050 // look at all the parameters in the first minimizable/replica and determine 00051 // if they have counterparts in the second network. When they are found, push 00052 // them onto the intersection lists 00053 for(int i = 0; i < m_pReplicaOne->GetReactionNetwork(0)->GetNumberOfRateConstants(); i++) 00054 { 00055 std::string firstName = m_pReplicaOne->GetReactionNetwork(0)->GetRateConstant(i)->GetName(); 00056 for(int j = 0; j < m_pReplicaTwo->GetReactionNetwork(0)->GetNumberOfRateConstants(); j++) 00057 { 00058 if(firstName == m_pReplicaTwo->GetReactionNetwork(0)->GetRateConstant(j)->GetName()) 00059 { 00060 // put i on the first intersection list and j on the second 00061 00062 // intersection list 00063 m_viIntersectOne.push_back(i); 00064 m_viIntersectTwo.push_back(j); 00065 } 00066 } 00067 } 00068 00069 // before returning, if the intersection list is null print a warning 00070 if(m_viIntersectOne.size() == 0) 00071 { 00072 cout << "WARNING : Models have no shared parameters." << endl; 00073 } 00074 } 00075 00076 double CSharedParametersMinimizableDirector::ComputeResiduals(double *parameters) 00077 { 00078 double summedCost = 0.0; 00079 int nP1 = m_pReplicaOne->GetNParameters(); 00080 int nP2 = m_pReplicaTwo->GetNParameters(); 00081 int intSize = m_viIntersectOne.size(); 00082 // create dummy arrays to pass appropriate sized parameter lists 00083 double *pOne = new double[nP1]; 00084 double *pTwo = new double[nP2]; 00085 // fill the arrays with the appropriate parameters 00086 for(int i = 0; i < intSize; i++) 00087 { 00088 pOne[i] = parameters[i]; 00089 pTwo[i] = parameters[i]; 00090 } 00091 // now fill in the parameters unique to model 1 00092 for(int i = intSize; i < nP1; i++) 00093 { 00094 pOne[i] = parameters[i]; 00095 } 00096 // now the ones unique to model 2 00097 for(int i = nP1; i < nP2; i++) 00098 { 00099 pOne[intSize + (i - nP1)] = parameters[i]; 00100 } 00101 // compute the residuals/cost from each replica 00102 summedCost = m_pReplicaOne->ObjectiveFunction(pOne) + m_pReplicaTwo->ObjectiveFunction(pTwo); 00103 00104 // fill in the residuals list 00105 int ResOne = m_pReplicaOne->GetNResiduals(); 00106 int ResTwo = m_pReplicaTwo->GetNResiduals(); 00107 for(int j = 0; j < ResOne; j++) 00108 { 00109 residuals[j] = (m_pReplicaOne->GetResiduals())[j]; 00110 } 00111 for(int j = ResOne; j < ResOne + ResTwo; j++) 00112 { 00113 residuals[j] = (m_pReplicaTwo->GetResiduals())[j-ResOne]; 00114 } 00115 00116 delete [] pOne; 00117 delete [] pTwo; 00118 00119 return summedCost; 00120 }