COPASI API  4.16.103
CRandomSearch.cpp
Go to the documentation of this file.
1 // Begin CVS Header
2 // $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/optimization/CRandomSearch.cpp,v $
3 // $Revision: 1.43 $
4 // $Name: $
5 // $Author: shoops $
6 // $Date: 2012/06/20 21:16:37 $
7 // End CVS Header
8 
9 // Copyright (C) 2012 - 2010 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc., University of Heidelberg, and The University
11 // of Manchester.
12 // All rights reserved.
13 
14 // Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual
15 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
16 // and The University of Manchester.
17 // All rights reserved.
18 
19 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
20 // Properties, Inc. and EML Research, gGmbH.
21 // All rights reserved.
22 
23 /***************************************************************************
24  CRandomSearch.cpp - Random Optimizer
25  -------------------
26 
27 Programmer : Rohan Luktuke
28 email : rluktuke@vt.edu
29  ***************************************************************************/
30 
31 /***************************************************************************
32  * This is the implementation of the Random Algorithm for Optimization. The
33  * class is inherited from the COptAlgorithm class
34  ***************************************************************************/
35 
36 #include "copasi.h"
37 
38 #include "COptMethod.h"
39 #include "COptProblem.h"
40 #include "COptItem.h"
41 #include "COptTask.h"
42 
43 #include "CRandomSearch.h"
44 
47 
49  COptMethod(CCopasiTask::optimization, CCopasiMethod::RandomSearch)
50 {
51  addParameter("Number of Iterations", CCopasiParameter::UINT, (unsigned C_INT32) 100000);
52  addParameter("Random Number Generator", CCopasiParameter::UINT, (unsigned C_INT32) CRandom::mt19937);
53  addParameter("Seed", CCopasiParameter::UINT, (unsigned C_INT32) 0);
54 
55  initObjects();
56 }
57 
59  COptMethod(src)
60 {initObjects();}
61 
62 /**
63  * Destructor
64  */
66 {//*** added similar to coptga
67  cleanup();
68 }
69 
71 {
73 }
74 
75 /**
76  * Optimizer Function
77  * Returns: true if properly initialized
78  * should return a boolean
79  */
81 {
82  cleanup();
83 
84  if (!COptMethod::initialize()) return false;
85 
86  mIterations = * getValue("Number of Iterations").pUINT;
87  mpRandom = CRandom::createGenerator(* (CRandom::Type *) getValue("Random Number Generator").pUINT,
88  * getValue("Seed").pUINT);
89 
90  mBestValue = std::numeric_limits<C_FLOAT64>::infinity();
91 
92  mVariableSize = mpOptItem->size();
94 
95  return true;
96 }
97 
98 /**
99  * Optimizer Function
100  * Returns: nothing
101  * should return a boolean
102  */
103 //C_INT32 CRandomSearch::optimize()
105 {
106  bool Continue = true;
107 
108  if (!initialize()) return false;
109 
110  unsigned C_INT32 j;
111 
112  // current value is the initial guess
113  for (j = 0; j < mVariableSize; j++)
114  {
115  C_FLOAT64 & mut = mIndividual[j];
116  COptItem & OptItem = *(*mpOptItem)[j];
117 
118  mut = OptItem.getStartValue();
119 
120  // force it to be within the bounds
121  switch (OptItem.checkConstraint(mut))
122  {
123  case - 1:
124  mut = *OptItem.getLowerBoundValue();
125  break;
126 
127  case 1:
128  mut = *OptItem.getUpperBoundValue();
129  break;
130  }
131 
132  // We need to set the value here so that further checks take
133  // account of the value.
134  (*(*mpSetCalculateVariable)[j])(mut);
135  }
136 
137  Continue = evaluate(mIndividual);
138  mBestValue = mValue;
140 
141  // We found a new best value lets report it.
142  //if (mpReport) mpReport->printBody();
144 
146  {
147  for (j = 0; j < mVariableSize && Continue; j++)
148  {
149  // CALCULATE lower and upper bounds
150  COptItem & OptItem = *(*mpOptItem)[j];
151  C_FLOAT64 & mut = mIndividual[j];
152 
153  mut = OptItem.getRandomValue(mpRandom);
154 
155  // force it to be within the bounds
156  switch (OptItem.checkConstraint(mut))
157  {
158  case - 1:
159  mut = *OptItem.getLowerBoundValue();
160 
161  if (!OptItem.checkLowerBound(mut)) // Inequality
162  {
163  if (mut == 0.0)
165  else
166  mut += mut * std::numeric_limits< C_FLOAT64 >::epsilon();
167  }
168 
169  break;
170 
171  case 1:
172  mut = *OptItem.getUpperBoundValue();
173 
174  if (!OptItem.checkUpperBound(mut)) // Inequality
175  {
176  if (mut == 0.0)
178  else
179  mut -= mut * std::numeric_limits< C_FLOAT64 >::epsilon();
180  }
181 
182  break;
183  }
184 
185  // We need to set the value here so that further checks take
186  // account of the value.
187  (*(*mpSetCalculateVariable)[j])(mut);
188  }
189 
190  Continue = evaluate(mIndividual);
191 
192  // COMPARE
193  if (mValue < mBestValue)
194  {
195  mBestValue = mValue;
197 
198  // We found a new best value lets report it.
199  //if (mpReport) mpReport->printBody();
201  }
202  }
203 
204  return true;
205 }
206 
207 // evaluate the fitness of one individual
208 bool CRandomSearch::evaluate(const CVector< C_FLOAT64 > & /* individual */)
209 {
210  bool Continue = true;
211 
212  // We do not need to check whether the parametric constraints are fulfilled
213  // since the parameters are created within the bounds.
214 
215  // evaluate the fitness
216  Continue = mpOptProblem->calculate();
217 
218  // check wheter the functional constraints are fulfilled
220  mValue = std::numeric_limits<C_FLOAT64>::infinity();
221  else
223 
224  return Continue;
225 }
CVector< C_FLOAT64 > mIndividual
Definition: CRandomSearch.h:84
virtual C_INT32 checkConstraint() const
Definition: COptItem.cpp:401
COptTask * mpParentTask
Definition: COptMethod.h:58
virtual bool initialize()
Definition: COptMethod.cpp:189
virtual bool optimise()
bool checkLowerBound(const C_FLOAT64 &value) const
Definition: COptItem.cpp:435
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
COptProblem * mpOptProblem
Definition: COptMethod.h:56
virtual void output(const COutputInterface::Activity &activity)
#define C_INT32
Definition: copasi.h:90
bool checkUpperBound(const C_FLOAT64 &value) const
Definition: COptItem.cpp:438
unsigned C_INT32 mCurrentIteration
Definition: CRandomSearch.h:79
virtual bool cleanup()
Definition: COptMethod.cpp:215
size_t mVariableSize
Definition: CRandomSearch.h:99
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
virtual bool calculate()
CRandom * mpRandom
Definition: CRandomSearch.h:94
virtual bool initialize()
const Value & getValue() const
const C_FLOAT64 * getLowerBoundValue() const
Definition: COptItem.h:191
C_FLOAT64 mValue
Definition: CRandomSearch.h:89
unsigned C_INT32 * pUINT
virtual bool setSolution(const C_FLOAT64 &value, const CVector< C_FLOAT64 > &variables)
virtual bool checkFunctionalConstraints()
const C_FLOAT64 & getStartValue() const
Definition: COptItem.cpp:199
const std::vector< COptItem * > * mpOptItem
Definition: COptMethod.h:70
#define C_FLOAT64
Definition: copasi.h:92
const C_FLOAT64 * getUpperBoundValue() const
Definition: COptItem.h:198
bool addParameter(const CCopasiParameter &parameter)
C_FLOAT64 mBestValue
C_FLOAT64 getRandomValue(CRandom *pRandom=NULL)
Definition: COptItem.cpp:230
const C_FLOAT64 & getCalculateValue() const
virtual ~CRandomSearch()
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
#define min(a, b)
Definition: f2c.h:175
unsigned C_INT32 mIterations
Definition: CRandomSearch.h:74
bool evaluate(const CVector< C_FLOAT64 > &individual)