COPASI API  4.16.103
COptProblem.h
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) 2002 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 /**
16  * File name: COptProblem.h
17  *
18  * Programmer: Yongqun He
19  * Contact email: yohe@vt.edu
20  * Purpose: This is the header file of the COptProblem class.
21  * It specifies the optimization problem with its own members and
22  * functions. It's used by COptAlgorithm class and COptimization class
23  */
24 
25 #ifndef COPTPROBLEM_H
26 #define COPTPROBLEM_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include "report/CCopasiTimer.h"
32 
34 #include "utilities/CVector.h"
36 
37 #include "function/CExpression.h"
38 
39 class CSteadyStateTask;
40 class CTrajectoryTask;
41 class COptItem;
42 
44 {
47 };
48 
49 /** @dia:pos -4.4,4.15 */
50 class COptProblem : public CCopasiProblem
51 {
52  // Implementation
53 
54 public:
55  /**
56  * The methods which can be selected for performing this task.
57  */
58  static const unsigned int ValidSubtasks[];
59 
60  /**
61  * Default constructor
62  * @param const CCopasiTask::Type & type (default: optimization)
63  * @param const CCopasiContainer * pParent (default: NULL)
64  */
66  const CCopasiContainer * pParent = NULL);
67 
68  /**
69  * Copy constructor.
70  * @param const COptProblem & src
71  * @param const CCopasiContainer * pParent (default: NULL)
72  */
73  COptProblem(const COptProblem & src,
74  const CCopasiContainer * pParent = NULL);
75 
76  /**
77  * Destructor
78  */
79  virtual ~COptProblem();
80 
81  /**
82  * This methods must be called to elevate subgroups to
83  * derived objects. The default implementation does nothing.
84  * @return bool success
85  */
86  virtual bool elevateChildren();
87 
88  /**
89  * Set the model of the problem
90  * @param CModel * pModel
91  * @result bool success
92  */
93  virtual bool setModel(CModel * pModel);
94 
95  /**
96  * Set the call back of the problem
97  * @param CProcessReport * pCallBack
98  * @result bool success
99  */
100  virtual bool setCallBack(CProcessReport * pCallBack);
101 
102  /**
103  * Do all necessary initialization so that calls to calculate will
104  * be successful. This is called once from CCopasiTask::process()
105  * @result bool success
106  */
107  virtual bool initialize();
108 
109  /**
110  * perform at least the initializations of the subtask that
111  * must be done before the output is initialized.
112  */
113  virtual bool initializeSubtaskBeforeOutput();
114 
115  /**
116  * Do the calculating based on CalculateVariables and fill
117  * CalculateResults with the results.
118  * @result bool continue
119  */
120  virtual bool calculate();
121 
122  /**
123  * Reset counters and objective value.
124  */
125  void reset();
126 
127  /**
128  * Do all necessary restore procedures for the model
129  * is in the same state as before.
130  * @param const bool & updateModel
131  */
132  void restoreModel(const bool & updateModel);
133 
134  /**
135  * Do all necessary restore procedures so that the
136  * model and task are in the same state as before.
137  * @param const bool & updateModel
138  * @result bool success
139  */
140  virtual bool restore(const bool & updateModel);
141 
142  /**
143  * Check whether all parameters are within their boundaries.
144  * @result bool within
145  */
146  virtual bool checkParametricConstraints();
147 
148  /**
149  * Check whether all functional constraints are fulfilled.
150  * @result bool fulfilled
151  */
152  virtual bool checkFunctionalConstraints();
153 
154  /**
155  * Calculate the statistics for the problem
156  * @param const C_FLOAT64 & factor (Default: 1.0e-003)
157  * @param const C_FLOAT64 & resolution (Default: 1.0e-009)
158  */
159  virtual bool calculateStatistics(const C_FLOAT64 & factor = 1.0e-003,
160  const C_FLOAT64 & resolution = 1.0e-009);
161 
162  /**
163  * Retrieve the size of the variable vectors.
164  * @result size_t VariableSize
165  */
166  size_t getVariableSize() const;
167 
168  /**
169  * Retrieve the list of optimization parameters.
170  * @return const std::vector< COptItem * > & optItemList
171  */
172  const std::vector< COptItem * > & getOptItemList() const;
173 
174  /**
175  * Retrieve the list of constraints.
176  * @return const std::vector< COptItem * > & constraintList
177  */
178  const std::vector< COptItem * > & getConstraintList() const;
179 
180  /**
181  * Retrieve the update methods for the variables for calculation.
182  * @return const std::vector< UpdateMethod * > & updateMethods
183  */
184  const std::vector< UpdateMethod * > & getCalculateVariableUpdateMethods() const;
185 
186  /**
187  * Retrieve the result of a calculation
188  */
189  const C_FLOAT64 & getCalculateValue() const;
190 
191  /**
192  * Retrieve the solution variables
193  */
195 
196  /**
197  * Retrieve the gradients for each solution variable.
198  * @return const CVector< C_FLOAT64 > & variableGradients
199  */
201 
202  /**
203  * Set the solution.
204  * @param const C_FLOAT64 & value
205  * @param const CVector< C_FLOAT64 > & variables
206  * @return bool continue;
207  */
208  virtual bool setSolution(const C_FLOAT64 & value,
209  const CVector< C_FLOAT64 > & variables);
210 
211  /**
212  * Retrieve the result for the solution
213  */
214  const C_FLOAT64 & getSolutionValue() const;
215 
216  /**
217  * Retrieve the 'index' optimization item.
218  * @param const size_t & index
219  * @return COptItem optItem
220  */
221  COptItem & getOptItem(const size_t & index);
222 
223  /**
224  * Retrieve the number of optimization items.
225  * @return const size_t size
226  */
227  size_t getOptItemSize() const;
228 
229  /**
230  * Add an optimization item to the problem.
231  * @param const CCopasiObjectName & objectCN
232  * @return COptItem optItemAdded
233  */
234  COptItem & addOptItem(const CCopasiObjectName & objectCN);
235 
236  /**
237  * Remove an optimization items.
238  * @param const size_t & index
239  * @return bool success
240  */
241  bool removeOptItem(const size_t & index);
242 
243  /**
244  * Swap two optimization items.
245  * @param const size_t & iFrom
246  * @param const size_t & iTo
247  * @return bool success
248  */
249  bool swapOptItem(const size_t & iFrom,
250  const size_t & iTo);
251 
252  /**
253  * Set optimization function
254  * @param const std::string & infix
255  * @return bool success
256  */
257  bool setObjectiveFunction(const std::string & infix);
258 
259  /**
260  * Retrieve the objective function.
261  * @return const std::string infix.
262  */
263  const std::string getObjectiveFunction();
264 
265  /**
266  * Set subtask type
267  * @param const CCopasiTask::Type & subtaskType
268  * @return success
269  */
270  bool setSubtaskType(const CCopasiTask::Type & subtaskType);
271 
272  /**
273  * Retrieve the subtask type
274  * @return CCopasiTask::Type subtaskType
275  */
277 
278  /**
279  * Set whether we have to maximize the objective function
280  * @param const bool & maximize
281  */
282  void setMaximize(const bool & maximize);
283 
284  /**
285  * Check whether we have to maximize
286  * @return const bool & maximize
287  */
288  const bool & maximize() const;
289 
290  /**
291  * Set whether we have to randomize start values
292  * @param const bool & randomize
293  */
294  void setRandomizeStartValues(const bool & randomize);
295 
296  /**
297  * Retrieve whether we have to calculate statistics
298  * @return const bool & randomize
299  */
300  const bool & getRandomizeStartValues() const;
301 
302  /**
303  * Randomize the start values if requested
304  */
305  void randomizeStartValues();
306 
307  /**
308  * Remember the start values;
309  */
310  void rememberStartValues();
311 
312  /**
313  * Set whether we have to calculate statistics
314  * @param const bool & calculate
315  */
316  void setCalculateStatistics(const bool & calculate);
317 
318  /**
319  * Retrieve whether we have to calculate statistics
320  * @return const bool & maximize
321  */
322  const bool & getCalculateStatistics() const;
323 
324  /**
325  * Retrieve the evaluation counter.
326  * @return const unsigned C_INT32 & functionEvaluations
327  */
328  const unsigned C_INT32 & getFunctionEvaluations() const;
329 
330  /**
331  * Adds increment to the function evaluation counter
332  * @param unsigned C_INT32 increment
333  */
334  void incrementEvaluations(unsigned C_INT32 increment);
335 
336  /**
337  * Resets the function evaluation counter
338  */
339  void resetEvaluations();
340 
341  /**
342  * Retrieve the objective function.
343  * @return const C_FLOAT64 & executionTime
344  */
345  const C_FLOAT64 & getExecutionTime() const;
346 
347  /**
348  * This is the output method for any object. The default implementation
349  * provided with CCopasiObject uses the ostream operator<< of the object
350  * to print the object.To override this default behavior one needs to
351  * reimplement the virtual print function.
352  * @param std::ostream * ostream
353  */
354  virtual void print(std::ostream * ostream) const;
355 
356  /**
357  * Output stream operator
358  * @param ostream & os
359  * @param const COptProblem & A
360  * @return ostream & os
361  */
362  friend std::ostream &operator<<(std::ostream &os, const COptProblem & o);
363 
364  /**
365  * This is the output method for any result of a problem. The default implementation
366  * provided with CCopasiProblem. Does only print "Not implemented." To override this
367  * default behavior one needs to reimplement the virtual printResult function.
368  * @param std::ostream * ostream
369  */
370  virtual void printResult(std::ostream * ostream) const;
371 
372 private:
373  /**
374  * Allocates all group parameters and assures that they are
375  * properly initialized.
376  */
377  void initializeParameter();
378 
379  void initObjects();
380 
381  //data member
382 protected:
383  /**
384  * A static value containing Infinity.
385  */
387 
388  /**
389  * A pointer to the value of the CCopasiParameter holding the CN for the subtask
390  */
391  std::string * mpParmSubtaskCN;
392 
393  /**
394  * A pointer to the value of the CCopasiParameter holding the ObjectiveFunctionKey
395  */
396  // std::string * mpParmObjectiveFunctionKey;
397 
398  /**
399  * A pointer to the value of the CCopasiParameter holding the infix expression
400  * of the objective function
401  */
402  std::string * mpParmObjectiveExpression;
403 
404  /**
405  * A pointer to the value of the CCopasiParameter holding Maximize
406  */
407  bool * mpParmMaximize;
408 
409  /**
410  * A pointer to the value of the CCopasiParameter holding Randomize Start Values
411  */
413 
414  /**
415  * A pointer to the value of the CCopasiParameter holding Calculate Statistics
416  */
418 
419  /**
420  * A pointer to the value of the CCopasiParameterGroup holding the OptimizationItems
421  */
423 
424  /**
425  * A pointer to the value of the CCopasiParameter holding the OptimizationConstraints
426  */
428 
429  /**
430  * A pointer to the vector of optimization items
431  */
432  std::vector<COptItem *> * mpOptItems;
433 
434  /**
435  * A pointer to the vector of optimization constraints
436  */
437  std::vector<COptItem *> * mpConstraintItems;
438 
439  /**
440  * Pointer to the subtask to be used in the optimization
441  */
442  mutable CCopasiTask * mpSubtask;
443 
444  /**
445  * The objective function which should be minimized or maximized.
446  */
448 
449  /**
450  * A vector of update method to the values of the optimization items.
451  */
452  std::vector< UpdateMethod * > mUpdateMethods;
453 
454  /**
455  * A vector of refresh methods which need to be called update all initial
456  * values which depend on the optimization items.
457  */
458  std::vector< Refresh * > mInitialRefreshMethods;
459 
460  /**
461  * A vector of refresh methods which need to be called retrieve the value
462  * of the objective function.
463  */
464  std::vector< Refresh * > mRefreshMethods;
465 
466  /**
467  * A vector of refresh methods which need to be called retrieve the values
468  * of constraints.
469  */
470  std::vector< Refresh * > mRefreshConstraints;
471 
472  /**
473  * A vector of results for calculate
474  */
476 
477  /**
478  * A vector of solution variables
479  */
481 
482  /**
483  * A vector of solution variables
484  */
486 
487  /**
488  * A vector of solution results
489  */
491 
492  /**
493  * Counter of evaluations
494  */
495  unsigned C_INT32 mCounter;
496 
497  /**
498  * Counter of failed evaluations
499  */
500  unsigned C_INT32 mFailedCounter;
501 
502  /**
503  * Counter of constraint checks
504  */
505  unsigned C_INT32 mConstraintCounter;
506 
507  /**
508  * Counter of failed constraint checks
509  */
511 
512  /**
513  * A CPU Timer
514  */
516 
517  /**
518  * Handle of "Best Value" process report item
519  */
520  size_t mhSolutionValue;
521 
522  /**
523  * Handle of "Function Evaluations" process report item
524  */
525  size_t mhCounter;
526 
527  /**
528  * Indicates whether the results shall be stored. The default
529  * is false.
530  */
531  bool mStoreResults;
532 
533  /**
534  * Indicates whether the statistics have been calculated for
535  * the current result
536  */
537  bool mHaveStatistics;
538 
539  /**
540  * The gradient vector for the parameters
541  */
543 };
544 
545 #endif // the end
Header file of class CExpression.
CCopasiParameterGroup * mpGrpItems
Definition: COptProblem.h:421
CCopasiTask::Type getSubtaskType() const
CVector< C_FLOAT64 > mSolutionVariables
Definition: COptProblem.h:479
C_FLOAT64 mSolutionValue
Definition: COptProblem.h:489
size_t getVariableSize() const
void incrementEvaluations(unsigned C_INT32 increment)
std::vector< Refresh * > mInitialRefreshMethods
Definition: COptProblem.h:457
friend std::ostream & operator<<(std::ostream &os, const COptProblem &o)
bool swapOptItem(const size_t &iFrom, const size_t &iTo)
COptItem & getOptItem(const size_t &index)
unsigned C_INT32 mCounter
Definition: COptProblem.h:494
void restoreModel(const bool &updateModel)
bool * mpParmCalculateStatistics
Definition: COptProblem.h:416
std::vector< COptItem * > * mpConstraintItems
Definition: COptProblem.h:436
std::vector< UpdateMethod * > mUpdateMethods
Definition: COptProblem.h:451
CVector< C_FLOAT64 > mGradient
Definition: COptProblem.h:541
unsigned C_INT32 mFailedCounter
Definition: COptProblem.h:499
const std::vector< COptItem * > & getConstraintList() const
virtual bool initialize()
size_t getOptItemSize() const
const CVector< C_FLOAT64 > & getVariableGradients() const
virtual bool setModel(CModel *pModel)
const std::vector< UpdateMethod * > & getCalculateVariableUpdateMethods() const
bool removeOptItem(const size_t &index)
const bool & getRandomizeStartValues() const
virtual bool calculateStatistics(const C_FLOAT64 &factor=1.0e-003, const C_FLOAT64 &resolution=1.0e-009)
#define C_INT32
Definition: copasi.h:90
bool mHaveStatistics
Definition: COptProblem.h:536
unsigned C_INT32 mConstraintCounter
Definition: COptProblem.h:504
virtual void print(std::ostream *ostream) const
const bool & maximize() const
void resetEvaluations()
unsigned C_INT32 mFailedConstraintCounter
Definition: COptProblem.h:509
virtual ~COptProblem()
size_t mhCounter
Definition: COptProblem.h:524
virtual bool restore(const bool &updateModel)
virtual bool setCallBack(CProcessReport *pCallBack)
C_FLOAT64 mCalculateValue
Definition: COptProblem.h:474
const C_FLOAT64 & getSolutionValue() const
virtual bool calculate()
std::vector< COptItem * > * mpOptItems
Definition: COptProblem.h:431
const C_FLOAT64 & getExecutionTime() const
std::vector< Refresh * > mRefreshConstraints
Definition: COptProblem.h:469
const std::vector< COptItem * > & getOptItemList() const
CCopasiTask * mpSubtask
Definition: COptProblem.h:441
void initializeParameter()
bool mStoreResults
Definition: COptProblem.h:530
CCopasiParameterGroup * mpGrpConstraints
Definition: COptProblem.h:426
size_t mhSolutionValue
Definition: COptProblem.h:519
std::string * mpParmSubtaskCN
Definition: COptProblem.h:390
void rememberStartValues()
void randomizeStartValues()
const CVector< C_FLOAT64 > & getSolutionVariables() const
CCopasiTimer mCPUTime
Definition: COptProblem.h:514
std::vector< Refresh * > mRefreshMethods
Definition: COptProblem.h:463
virtual bool setSolution(const C_FLOAT64 &value, const CVector< C_FLOAT64 > &variables)
std::string * mpParmObjectiveExpression
Definition: COptProblem.h:401
virtual void printResult(std::ostream *ostream) const
COptItem & addOptItem(const CCopasiObjectName &objectCN)
ProblemType
Definition: COptProblem.h:43
virtual bool initializeSubtaskBeforeOutput()
virtual bool checkFunctionalConstraints()
virtual bool elevateChildren()
bool * mpParmMaximize
Definition: COptProblem.h:406
static const unsigned int ValidSubtasks[]
Definition: COptProblem.h:57
void setRandomizeStartValues(const bool &randomize)
#define C_FLOAT64
Definition: copasi.h:92
bool * mpParmRandomizeStartValues
Definition: COptProblem.h:411
const std::string getObjectiveFunction()
C_FLOAT64 mWorstValue
Definition: COptProblem.h:385
CVector< C_FLOAT64 > mOriginalVariables
Definition: COptProblem.h:484
virtual bool checkParametricConstraints()
const bool & getCalculateStatistics() const
bool setSubtaskType(const CCopasiTask::Type &subtaskType)
Definition: CModel.h:50
void setMaximize(const bool &maximize)
const unsigned C_INT32 & getFunctionEvaluations() const
const C_FLOAT64 & getCalculateValue() const
COptProblem(const CCopasiTask::Type &type=CCopasiTask::optimization, const CCopasiContainer *pParent=NULL)
Definition: COptProblem.cpp:67
CExpression * mpObjectiveExpression
Definition: COptProblem.h:446
void setCalculateStatistics(const bool &calculate)
bool setObjectiveFunction(const std::string &infix)
void initObjects()