COPASI API  4.16.103
COptTask.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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 /**
16  * COptTask class.
17  *
18  * This class implements a optimization task which is comprised of a
19  * of a problem and a method.
20  *
21  */
22 #include "copasi.h"
23 
24 #include "COptTask.h"
25 #include "COptProblem.h"
26 #include "COptMethod.h"
27 #include "report/CKeyFactory.h"
28 #include "report/CReport.h"
29 
30 #include "model/CModel.h"
31 #include "model/CState.h"
32 
33 //#include "trajectory/CTrajectoryTask.h"
34 //#include "trajectory/CTrajectoryProblem.h"
35 //#include "steadystate/CSteadyStateTask.h"
36 //#include "steadystate/CSteadyStateProblem.h"
37 //#include "utilities/COutputHandler.h"
38 
39 const unsigned int COptTask::ValidMethods[] =
40 {
42 #ifdef COPASI_DEBUG
44 #endif // COPASI_DEBUG
61 };
62 
64  const CCopasiContainer * pParent):
65  CCopasiTask(type, pParent)
66 {
67  mpProblem = new COptProblem(type, this);
69  this->add(mpMethod, true);
70  // mpMethod->setObjectParent(this);
71  ((COptMethod *) mpMethod)->setProblem((COptProblem *) mpProblem);
72 }
73 
75  const CCopasiContainer * pParent):
76  CCopasiTask(src, pParent)
77 {
78  mpProblem = new COptProblem(*(COptProblem *) src.mpProblem, this);
80  this->add(mpMethod, true);
81  // mpMethod->setObjectParent(this);
82  ((COptMethod *) mpMethod)->setProblem((COptProblem *) mpProblem);
83 }
84 
86 {cleanup();}
87 
89 
91 {
92  bool success = CCopasiTask::setCallBack(pCallBack);
93 
94  if (!mpProblem->setCallBack(pCallBack)) success = false;
95 
96  if (!mpMethod->setCallBack(pCallBack)) success = false;
97 
98  return success;
99 }
100 
102  COutputHandler * pOutputHandler,
103  std::ostream * pOstream)
104 {
105  COptProblem * pProblem = dynamic_cast<COptProblem *>(mpProblem);
106  COptMethod * pMethod = dynamic_cast<COptMethod *>(mpMethod);
107 
108  if (!pProblem || !pMethod) return false;
109 
110  //initialize reporting
111  bool success = true;
112 
113  //do the part of the initialization of the subtask that needs to be
114  //performed before the output is initialized. This is kind of a hack,
115  //we need to find a more general solution for this
116  if (!pProblem->initializeSubtaskBeforeOutput()) success = false;
117 
118  if (!CCopasiTask::initialize(of, pOutputHandler, pOstream)) success = false;
119 
120  //if (!mReport.open(pOstream)) success = false;
121  //if (!mReport.compile()) success = false;
122 
123  if (!pProblem->initialize()) success = false;
124 
125  pMethod->setProblem(pProblem);
126  // if (!pMethod->initialize()) return false;
127 
128  return success;
129 }
130 
131 bool COptTask::process(const bool & useInitialValues)
132 {
133  COptProblem * pProblem = dynamic_cast<COptProblem *>(mpProblem);
134  COptMethod * pMethod = dynamic_cast<COptMethod *>(mpMethod);
135 
136  if (!pProblem || !pMethod) return false;
137 
139 
140  pProblem->randomizeStartValues();
141  pProblem->rememberStartValues();
142 
143  if (useInitialValues) pProblem->resetEvaluations();
144 
146 
147  bool success = pMethod->optimise();
148 
149  pProblem->calculateStatistics();
150 
152 
153  return success;
154 }
155 
156 bool COptTask::setMethodType(const int & type)
157 {
159 
160  if (mpMethod->getSubType() == Type) return true;
161 
162  pdelete(mpMethod);
163 
164  mpMethod = createMethod(Type);
165  this->add(mpMethod, true);
166 
167  return true;
168 }
169 
170 // virtual
171 CCopasiMethod * COptTask::createMethod(const int & type) const
172 {
174 
175  return COptMethod::createMethod(Type);
176 }
virtual bool setCallBack(CProcessReport *pCallBack)
CCopasiMethod * mpMethod
Definition: CCopasiTask.h:239
#define pdelete(p)
Definition: copasi.h:215
void setProblem(COptProblem *problem)
Definition: COptMethod.cpp:170
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
static COptMethod * createMethod(CCopasiMethod::SubType subType=CCopasiMethod::RandomSearch)
Definition: COptMethod.cpp:50
virtual bool initialize()
virtual CCopasiMethod * createMethod(const int &type) const
Definition: COptTask.cpp:171
virtual bool setCallBack(CProcessReport *pCallBack)
virtual bool calculateStatistics(const C_FLOAT64 &factor=1.0e-003, const C_FLOAT64 &resolution=1.0e-009)
virtual void output(const COutputInterface::Activity &activity)
const CCopasiMethod::SubType & getSubType() const
~COptTask()
Definition: COptTask.cpp:85
static const unsigned int ValidMethods[]
Definition: COptTask.h:50
void resetEvaluations()
virtual bool isValidProblem(const CCopasiProblem *pProblem)
virtual bool setCallBack(CProcessReport *pCallBack)
Definition: COptTask.cpp:90
CCopasiProblem * mpProblem
Definition: CCopasiTask.h:234
virtual bool initialize(const OutputFlag &of, COutputHandler *pOutputHandler, std::ostream *pOstream)
Definition: COptTask.cpp:101
void rememberStartValues()
void randomizeStartValues()
virtual bool process(const bool &useInitialValues)
Definition: COptTask.cpp:131
virtual bool initializeSubtaskBeforeOutput()
virtual bool setMethodType(const int &type)
Definition: COptTask.cpp:156
virtual bool optimise()
Definition: COptMethod.cpp:184
void cleanup()
Definition: COptTask.cpp:88
virtual bool add(CCopasiObject *pObject, const bool &adopt=true)
virtual bool setCallBack(CProcessReport *pCallBack)
COptTask(const CCopasiTask::Type &type=CCopasiTask::optimization, const CCopasiContainer *pParent=NULL)
Definition: COptTask.cpp:63