COPASI API  4.16.103
CFitItem.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2005 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <limits>
16 #include <cmath>
17 
18 #include "copasi.h"
19 
20 #include "CFitItem.h"
21 
22 #include "report/CKeyFactory.h"
26 
28  const std::string & name):
29  COptItem(pParent, name),
30  mpGrpAffectedExperiments(NULL),
31  mpGrpAffectedCrossValidations(NULL),
32  mLocalValue(0),
33  mpLocalMethod(new SpecificUpdateMethod<CFitItem, C_FLOAT64>(this, &CFitItem::setLocalValue))
35 
37  const CCopasiContainer * pParent):
38  COptItem(src, pParent),
39  mpGrpAffectedExperiments(NULL),
40  mpGrpAffectedCrossValidations(NULL),
41  mLocalValue(0),
42  mpLocalMethod(new SpecificUpdateMethod<CFitItem, C_FLOAT64>(this, &CFitItem::setLocalValue))
44 
46  const CCopasiContainer * pParent):
47  COptItem(group, pParent),
48  mpGrpAffectedExperiments(NULL),
49  mpGrpAffectedCrossValidations(NULL),
50  mLocalValue(0),
51  mpLocalMethod(new SpecificUpdateMethod<CFitItem, C_FLOAT64>(this, &CFitItem::setLocalValue))
53 
56 
58 {
59  mpGrpAffectedExperiments = assertGroup("Affected Experiments");
60 
61  mpGrpAffectedCrossValidations = assertGroup("Affected Cross Validation Experiments");
62 
64 }
65 
67 {
68  // The functionality of SavedValue is no handled more transparently
69  // through the StartValue. Therefore, in case we encounter an old file
70  // we need to copy its value.
71  CCopasiParameter *pSavedValue = getParameter("SavedValue");
72 
73  if (pSavedValue)
74  {
75  setStartValue(*pSavedValue->getValue().pDOUBLE);
76  removeParameter("SavedValue");
77  }
78 
80  elevate<CCopasiParameterGroup, CCopasiParameterGroup>(mpGrpAffectedExperiments);
81 
82  if (!mpGrpAffectedExperiments) return false;
83 
85  elevate<CCopasiParameterGroup, CCopasiParameterGroup>(mpGrpAffectedCrossValidations);
86 
87  if (!mpGrpAffectedCrossValidations) return false;
88 
89  return true;
90 }
91 
92 bool CFitItem::isValid() const
93 {return COptItem::isValid();}
94 
96 {
97  CFitItem tmp(group);
98 
99  return tmp.isValid();
100 }
101 
102 bool CFitItem::compile(const std::vector< CCopasiContainer * > listOfContainer)
103 {
104  bool success = COptItem::compile(listOfContainer);
105 
107 
108  return success;
109 }
110 
112 {
113  if (*mpLowerBound > mLocalValue) return - 1;
114 
115  if (mLocalValue > *mpUpperBound) return 1;
116 
117  return 0;
118 }
119 
121 {
122  switch (checkConstraint())
123  {
124  case - 1:
125  return *mpLowerBound - mLocalValue;
126  break;
127 
128  case 1:
129  return mLocalValue - *mpUpperBound;
130  break;
131  }
132 
133  return 0.0;
134 }
135 
136 std::ostream &operator<<(std::ostream &os, const CFitItem & o)
137 {
138  os << * static_cast<const COptItem *>(&o) << std::endl;
139 
140  size_t i, imax = o.mpGrpAffectedExperiments->size();
141 
142  os << " Affected Experiments:" << std::endl << " ";
143 
144  if (imax == 0) os << "all";
145 
146  for (i = 0; i < imax; i++)
147  {
148  if (i) os << ", ";
149 
150  os << o.getExperiment(i);
151  }
152 
154 
155  os << " Affected Validation Experiments:" << std::endl << " ";
156 
157  if (imax == 0) os << "all";
158 
159  for (i = 0; i < imax; i++)
160  {
161  if (i) os << ", ";
162 
163  os << o.getCrossValidation(i);
164  }
165 
166  return os;
167 }
168 
170 {
171  mLocalValue = value;
172  return;
173 }
174 
176 {return mLocalValue;}
177 
179 {return & mLocalValue;}
180 
182 {return mpLocalMethod;}
183 
184 bool CFitItem::addExperiment(const std::string & key)
185 {
186  size_t i, imax = mpGrpAffectedExperiments->size();
187 
188  for (i = 0; i < imax; i++)
189  if (*mpGrpAffectedExperiments->getValue(i).pKEY == key) return false; // The key already exists.
190 
191  return mpGrpAffectedExperiments->addParameter("Experiment Key", CCopasiParameter::KEY, key);
192 }
193 
194 const std::string & CFitItem::getExperiment(const size_t & index) const
195 {
196  static const std::string Empty("");
197 
198  if (index < mpGrpAffectedExperiments->size())
199  return *mpGrpAffectedExperiments->getValue(index).pKEY;
200 
201  return Empty;
202 }
203 
204 bool CFitItem::removeExperiment(const size_t & index)
206 
208 {return mpGrpAffectedExperiments->size();}
209 
210 std::string CFitItem::getExperiments() const
211 {
212  std::string Experiments;
213  size_t i, imax = mpGrpAffectedExperiments->size();
214  const CCopasiObject * pObject;
215 
216  for (i = 0; i < imax; i++)
217  {
219 
220  if (pObject != NULL)
221  {
222  if (i != 0)
223  {
224  Experiments += ", ";
225  }
226 
227  Experiments += pObject->getObjectName();
228  }
229  }
230 
231  return Experiments;
232 }
233 
234 bool CFitItem::addCrossValidation(const std::string & key)
235 {
236  size_t i, imax = mpGrpAffectedCrossValidations->size();
237 
238  for (i = 0; i < imax; i++)
239  if (*mpGrpAffectedCrossValidations->getValue(i).pKEY == key) return false; // The key already exists.
240 
241  return mpGrpAffectedCrossValidations->addParameter("Experiment Key", CCopasiParameter::KEY, key);
242 }
243 
244 const std::string & CFitItem::getCrossValidation(const size_t & index) const
245 {
246  static const std::string Empty("");
247 
248  if (index < mpGrpAffectedCrossValidations->size())
250 
251  return Empty;
252 }
253 
254 bool CFitItem::removeCrossValidation(const size_t & index)
256 
259 
260 std::string CFitItem::getCrossValidations() const
261 {
262  std::string CrossValidations;
263  size_t i, imax = mpGrpAffectedCrossValidations->size();
264  const CCopasiObject * pObject;
265 
266  for (i = 0; i < imax; i++)
267  {
269 
270  if (i && pObject)
271  CrossValidations += ", ";
272 
273  CrossValidations += pObject->getObjectName();
274  }
275 
276  return CrossValidations;
277 }
278 
279 bool CFitItem::updateBounds(std::vector<COptItem * >::iterator it)
280 {
281  while (*it != this)
282  {
283  if (mpLowerObject && (getLowerBound() == (*it)->getObjectCN()))
284  mpLowerBound = &static_cast<CFitItem *>(*it)->getLocalValue();
285 
286  if (mpUpperObject && (getUpperBound() == (*it)->getObjectCN()))
287  mpUpperBound = &static_cast<CFitItem *>(*it)->getLocalValue();
288 
289  ++it;
290  }
291 
292  return true;
293 }
294 
296  const std::string & name):
297  CFitItem(pParent, name),
298  mCheckConstraint(0),
299  mConstraintViolation(0.0)
300 {}
301 
303  const CCopasiContainer * pParent):
304  CFitItem(src, pParent),
305  mCheckConstraint(src.mCheckConstraint),
306  mConstraintViolation(src.mConstraintViolation)
307 {}
308 
310  const CCopasiContainer * pParent):
311  CFitItem(group, pParent),
312  mCheckConstraint(0),
313  mConstraintViolation(0.0)
314 {}
315 
317 
319 {
320  mCheckConstraint = 0;
321  mConstraintViolation = 0.0;
322 }
323 
325 {
327  else if (*mpObjectValue > *mpUpperBound) mCheckConstraint = 1;
328  else mCheckConstraint = 0;
329 
330  switch (mCheckConstraint)
331  {
332  case - 1:
334  break;
335 
336  case 1:
337  mConstraintViolation += *mpObjectValue - *mpUpperBound;
338  break;
339 
340  default:
341  break;
342  }
343 }
344 
346 {return mCheckConstraint;}
347 
349 {return mConstraintViolation;}
const std::string & getExperiment(const size_t &index) const
Definition: CFitItem.cpp:194
virtual const C_FLOAT64 * getObjectValue() const
Definition: CFitItem.cpp:178
CCopasiParameterGroup * mpGrpAffectedCrossValidations
Definition: CFitItem.h:230
CCopasiParameterGroup * mpGrpAffectedExperiments
Definition: CFitItem.h:224
#define pdelete(p)
Definition: copasi.h:215
virtual ~CFitConstraint()
Definition: CFitItem.cpp:316
C_FLOAT64 mLocalValue
Definition: CFitItem.h:235
const std::string & getObjectName() const
UpdateMethod * mpLocalMethod
Definition: CFitItem.h:240
CCopasiObject * get(const std::string &key)
const std::string getUpperBound() const
Definition: COptItem.cpp:189
const CCopasiObject * mpLowerObject
Definition: COptItem.h:303
bool removeCrossValidation(const size_t &index)
Definition: CFitItem.cpp:254
void setLocalValue(const C_FLOAT64 &value)
Definition: CFitItem.cpp:169
virtual UpdateMethod * getUpdateMethod() const
Definition: CFitItem.cpp:181
void initializeParameter()
Definition: CFitItem.cpp:57
virtual C_INT32 checkConstraint() const
Definition: CFitItem.cpp:111
bool addExperiment(const std::string &key)
Definition: CFitItem.cpp:184
void resetConstraintViolation()
Definition: CFitItem.cpp:318
#define C_INT32
Definition: copasi.h:90
const C_FLOAT64 * mpUpperBound
Definition: COptItem.h:323
const C_FLOAT64 & getLocalValue() const
Definition: CFitItem.cpp:175
void calculateConstraintViolation()
Definition: CFitItem.cpp:324
virtual bool elevateChildren()
Definition: CFitItem.cpp:66
std::string getCrossValidations() const
Definition: CFitItem.cpp:260
bool removeParameter(const std::string &name)
virtual C_FLOAT64 getConstraintViolation() const
Definition: CFitItem.cpp:348
const Value & getValue() const
C_FLOAT64 * mpParmStartValue
Definition: COptItem.h:283
virtual C_INT32 checkConstraint() const
Definition: CFitItem.cpp:345
C_FLOAT64 mConstraintViolation
Definition: CFitItem.h:318
CCopasiParameterGroup * assertGroup(const std::string &name)
std::ostream & operator<<(std::ostream &os, const CFitItem &o)
Definition: CFitItem.cpp:136
bool setStartValue(const C_FLOAT64 &value)
Definition: COptItem.cpp:192
CCopasiParameter * getParameter(const std::string &name)
const CCopasiObject * mpUpperObject
Definition: COptItem.h:318
virtual bool compile(const std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
Definition: COptItem.cpp:336
const C_FLOAT64 * mpLowerBound
Definition: COptItem.h:308
size_t getCrossValidationCount() const
Definition: CFitItem.cpp:257
static CKeyFactory * getKeyFactory()
const std::string getLowerBound() const
Definition: COptItem.cpp:164
#define C_FLOAT64
Definition: copasi.h:92
bool updateBounds(std::vector< COptItem * >::iterator it)
Definition: CFitItem.cpp:279
bool addParameter(const CCopasiParameter &parameter)
virtual C_FLOAT64 getConstraintViolation() const
Definition: CFitItem.cpp:120
const CCopasiParameter::Value & getValue(const std::string &name) const
bool removeExperiment(const size_t &index)
Definition: CFitItem.cpp:204
virtual bool isValid() const
Definition: COptItem.cpp:316
const C_FLOAT64 * mpObjectValue
Definition: COptItem.h:298
std::string getExperiments() const
Definition: CFitItem.cpp:210
virtual bool compile(const std::vector< CCopasiContainer * > listOfContainer=CCopasiContainer::EmptyList)
Definition: CFitItem.cpp:102
C_INT32 mCheckConstraint
Definition: CFitItem.h:313
virtual ~CFitItem()
Definition: CFitItem.cpp:54
const std::string & getCrossValidation(const size_t &index) const
Definition: CFitItem.cpp:244
size_t getExperimentCount() const
Definition: CFitItem.cpp:207
bool addCrossValidation(const std::string &key)
Definition: CFitItem.cpp:234
virtual bool isValid() const
Definition: CFitItem.cpp:92