COPASI API  4.16.103
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
COptMethodGASR Class Reference

#include <COptMethodGASR.h>

Inheritance diagram for COptMethodGASR:
Inheritance graph
[legend]
Collaboration diagram for COptMethodGASR:
Collaboration graph
[legend]

Public Member Functions

 COptMethodGASR (const COptMethodGASR &src, const CCopasiContainer *pParent=NULL)
 
virtual bool optimise ()
 
virtual ~COptMethodGASR ()
 
- Public Member Functions inherited from COptMethod
 COptMethod (const COptMethod &src, const CCopasiContainer *pParent=NULL)
 
bool isBounded (void)
 
virtual bool isValidProblem (const CCopasiProblem *pProblem)
 
void setProblem (COptProblem *problem)
 
virtual ~COptMethod ()
 
- Public Member Functions inherited from CCopasiMethod
 CCopasiMethod (const CCopasiMethod &src, const CCopasiContainer *pParent=NULL)
 
const CCopasiMethod::SubTypegetSubType () const
 
const CCopasiTask::TypegetType () const
 
virtual void load (CReadConfig &configBuffer, CReadConfig::Mode mode=CReadConfig::SEARCH)
 
virtual void print (std::ostream *ostream) const
 
virtual void printResult (std::ostream *ostream) const
 
virtual bool setCallBack (CProcessReport *pCallBack)
 
virtual ~CCopasiMethod ()
 
- Public Member Functions inherited from CCopasiParameterGroup
bool addGroup (const std::string &name)
 
bool addParameter (const CCopasiParameter &parameter)
 
bool addParameter (const std::string &name, const CCopasiParameter::Type type)
 
template<class CType >
bool addParameter (const std::string &name, const CCopasiParameter::Type type, const CType &value)
 
void addParameter (CCopasiParameter *pParameter)
 
CCopasiParameterGroupassertGroup (const std::string &name)
 
template<class CType >
CCopasiParameterassertParameter (const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
 
index_iterator beginIndex () const
 
name_iterator beginName () const
 
 CCopasiParameterGroup (const CCopasiParameterGroup &src, const CCopasiContainer *pParent=NULL)
 
 CCopasiParameterGroup (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &objectType="ParameterGroup")
 
void clear ()
 
virtual bool elevateChildren ()
 
index_iterator endIndex () const
 
name_iterator endName () const
 
CCopasiParameterGroupgetGroup (const std::string &name)
 
const CCopasiParameterGroupgetGroup (const std::string &name) const
 
CCopasiParameterGroupgetGroup (const size_t &index)
 
const CCopasiParameterGroupgetGroup (const size_t &index) const
 
size_t getIndex (const std::string &name) const
 
std::string getKey (const std::string &name) const
 
std::string getKey (const size_t &index) const
 
virtual const std::string & getName (const size_t &index) const
 
virtual const CObjectInterfacegetObject (const CCopasiObjectName &cn) const
 
CCopasiParametergetParameter (const std::string &name)
 
const CCopasiParametergetParameter (const std::string &name) const
 
CCopasiParametergetParameter (const size_t &index)
 
const CCopasiParametergetParameter (const size_t &index) const
 
CCopasiParameter::Type getType (const std::string &name) const
 
CCopasiParameter::Type getType (const size_t &index) const
 
std::string getUniqueParameterName (const CCopasiParameter *pParameter) const
 
const CCopasiParameter::ValuegetValue (const std::string &name) const
 
const CCopasiParameter::ValuegetValue (const size_t &index) const
 
CCopasiParameter::ValuegetValue (const std::string &name)
 
CCopasiParameter::ValuegetValue (const size_t &index)
 
CCopasiParameterGroupoperator= (const CCopasiParameterGroup &rhs)
 
bool removeParameter (const std::string &name)
 
bool removeParameter (const size_t &index)
 
template<class CType >
bool setValue (const std::string &name, const CType &value)
 
template<class CType >
bool setValue (const size_t &index, const CType &value)
 
size_t size () const
 
bool swap (const size_t &iFrom, const size_t &iTo)
 
bool swap (index_iterator &from, index_iterator &to)
 
virtual ~CCopasiParameterGroup ()
 
- Public Member Functions inherited from CCopasiParameter
 CCopasiParameter (const CCopasiParameter &src, const CCopasiContainer *pParent=NULL)
 
 CCopasiParameter (const std::string &name, const Type &type, const void *pValue=NULL, const CCopasiContainer *pParent=NULL, const std::string &objectType="Parameter")
 
virtual CCopasiObjectName getCN () const
 
virtual const std::string & getKey () const
 
virtual std::string getObjectDisplayName (bool regular=true, bool richtext=false) const
 
const CCopasiParameter::TypegetType () const
 
const ValuegetValue () const
 
ValuegetValue ()
 
virtual voidgetValuePointer () const
 
CCopasiObjectgetValueReference () const
 
bool isValidValue (const C_FLOAT64 &value) const
 
bool isValidValue (const C_INT32 &value) const
 
bool isValidValue (const unsigned C_INT32 &value) const
 
bool isValidValue (const bool &value) const
 
bool isValidValue (const std::string &value) const
 
bool isValidValue (const CCopasiObjectName &value) const
 
bool isValidValue (const std::vector< CCopasiParameter * > &value) const
 
CCopasiParameteroperator= (const CCopasiParameter &rhs)
 
template<class CType >
bool setValue (const CType &value)
 
bool setValue (const std::vector< CCopasiParameter * > &value)
 
virtual ~CCopasiParameter ()
 
- Public Member Functions inherited from CCopasiContainer
virtual bool add (CCopasiObject *pObject, const bool &adopt=true)
 
 CCopasiContainer (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &type="CN", const unsigned C_INT32 &flag=CCopasiObject::Container)
 
 CCopasiContainer (const CCopasiContainer &src, const CCopasiContainer *pParent=NULL)
 
virtual std::string getChildObjectUnits (const CCopasiObject *pObject) const
 
virtual const objectMapgetObjects () const
 
virtual std::string getUnits () const
 
virtual const CCopasiObjectgetValueObject () const
 
virtual bool remove (CCopasiObject *pObject)
 
virtual ~CCopasiContainer ()
 
- Public Member Functions inherited from CCopasiObject
void addDirectDependency (const CCopasiObject *pObject)
 
 CCopasiObject (const CCopasiObject &src, const CCopasiContainer *pParent=NULL)
 
void clearDirectDependencies ()
 
void clearRefresh ()
 
bool dependsOn (DataObjectSet candidates, const DataObjectSet &context=DataObjectSet()) const
 
void getAllDependencies (DataObjectSet &dependencies, const DataObjectSet &context) const
 
virtual const DataObjectSetgetDirectDependencies (const DataObjectSet &context=DataObjectSet()) const
 
CCopasiContainergetObjectAncestor (const std::string &type) const
 
CCopasiDataModelgetObjectDataModel ()
 
const CCopasiDataModelgetObjectDataModel () const
 
const std::string & getObjectName () const
 
CCopasiContainergetObjectParent () const
 
const std::string & getObjectType () const
 
virtual const
CObjectInterface::ObjectSet
getPrerequisites () const
 
virtual RefreshgetRefresh () const
 
UpdateMethodgetUpdateMethod () const
 
bool hasCircularDependencies (DataObjectSet &candidates, DataObjectSet &verified, const DataObjectSet &context) const
 
bool hasUpdateMethod () const
 
bool isArray () const
 
bool isContainer () const
 
bool isDataModel () const
 
bool isMatrix () const
 
bool isNameVector () const
 
bool isNonUniqueName () const
 
virtual bool isPrerequisiteForContext (const CObjectInterface *pObject, const CMath::SimulationContextFlag &context, const CObjectInterface::ObjectSet &changedObjects) const
 
bool isReference () const
 
bool isRoot () const
 
bool isSeparator () const
 
bool isStaticString () const
 
bool isValueBool () const
 
bool isValueDbl () const
 
bool isValueInt () const
 
bool isValueInt64 () const
 
bool isValueString () const
 
bool isVector () const
 
virtual bool mustBeDeleted (const DataObjectSet &deletedObjects) const
 
void removeDirectDependency (const CCopasiObject *pObject)
 
void setDirectDependencies (const DataObjectSet &directDependencies)
 
bool setObjectName (const std::string &name)
 
virtual bool setObjectParent (const CCopasiContainer *pParent)
 
void setObjectValue (const C_FLOAT64 &value)
 
void setObjectValue (const C_INT32 &value)
 
void setObjectValue (const bool &value)
 
template<class CType >
void setRefresh (CType *pType, void(CType::*method)(void))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const C_FLOAT64 &))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const C_INT32 &))
 
template<class CType >
void setUpdateMethod (CType *pType, void(CType::*method)(const bool &))
 
virtual ~CCopasiObject ()
 
- Public Member Functions inherited from CObjectInterface
 CObjectInterface ()
 
virtual ~CObjectInterface ()
 

Private Member Functions

virtual bool cleanup ()
 
 COptMethodGASR (const CCopasiContainer *pParent=NULL)
 
bool creation (size_t first, size_t last=std::numeric_limits< size_t >::max())
 
bool crossover (const CVector< C_FLOAT64 > &parent1, const CVector< C_FLOAT64 > &parent2, CVector< C_FLOAT64 > &child1, CVector< C_FLOAT64 > &child2)
 
bool evaluate (const CVector< C_FLOAT64 > &individual)
 
size_t fittest ()
 
virtual bool initialize ()
 
void initObjects ()
 
bool mutate (CVector< C_FLOAT64 > &individual)
 
C_FLOAT64 phi (size_t indvNum)
 
bool replicate ()
 
bool select ()
 
bool swap (size_t from, size_t to)
 

Private Attributes

size_t mBestIndex
 
C_FLOAT64 mBestValue
 
CVector< bool > mCrossOver
 
CVector< bool > mCrossOverFalse
 
C_FLOAT64 mEvaluationValue
 
unsigned C_INT32 mGeneration
 
unsigned C_INT32 mGenerations
 
size_t mhGenerations
 
std::vector< CVector
< C_FLOAT64 > * > 
mIndividual
 
C_FLOAT64 mMutationVarians
 
C_FLOAT64 mPf
 
CVector< C_FLOAT64mPhi
 
unsigned C_INT32 mPopulationSize
 
CPermutationmpPermutation
 
CRandommpRandom
 
CVector< C_FLOAT64mValue
 
size_t mVariableSize
 
CVector< size_t > mWins
 

Friends

COptMethodCOptMethod::createMethod (CCopasiMethod::SubType subType)
 

Additional Inherited Members

- Public Types inherited from CCopasiMethod
enum  SubType {
  unset = 0, RandomSearch, RandomSearchMaster, SimulatedAnnealing,
  CoranaWalk, DifferentialEvolution, ScatterSearch, GeneticAlgorithm,
  EvolutionaryProgram, SteepestDescent, HybridGASA, GeneticAlgorithmSR,
  HookeJeeves, LevenbergMarquardt, NelderMead, SRES,
  Statistics, ParticleSwarm, Praxis, TruncatedNewton,
  Newton, deterministic, LSODAR, directMethod,
  stochastic, tauLeap, adaptiveSA, hybrid,
  hybridLSODA, hybridODE45, DsaLsodar, tssILDM,
  tssILDMModified, tssCSP, mcaMethodReder, scanMethod,
  lyapWolf, sensMethod, EFMAlgorithm, EFMBitPatternTreeAlgorithm,
  EFMBitPatternAlgorithm, Householder, crossSectionMethod, linearNoiseApproximation
}
 
- Public Types inherited from CCopasiParameterGroup
typedef parameterGroup::iterator index_iterator
 
typedef
CCopasiContainer::objectMap::iterator 
name_iterator
 
typedef std::vector
< CCopasiParameter * > 
parameterGroup
 
- Public Types inherited from CCopasiParameter
enum  Type {
  DOUBLE = 0, UDOUBLE, INT, UINT,
  BOOL, GROUP, STRING, CN,
  KEY, FILE, EXPRESSION, INVALID
}
 
- Public Types inherited from CCopasiContainer
typedef std::multimap
< std::string, CCopasiObject * > 
objectMap
 
- Public Types inherited from CCopasiObject
typedef std::set< const
CCopasiObject * > 
DataObjectSet
 
typedef std::vector< Refresh * > DataUpdateSequence
 
- Public Types inherited from CObjectInterface
typedef std::set< const
CObjectInterface * > 
ObjectSet
 
typedef std::vector
< CObjectInterface * > 
UpdateSequence
 
- Static Public Member Functions inherited from COptMethod
static COptMethodcreateMethod (CCopasiMethod::SubType subType=CCopasiMethod::RandomSearch)
 
- Static Public Member Functions inherited from CCopasiObject
static std::vector< Refresh * > buildUpdateSequence (const DataObjectSet &objects, const DataObjectSet &uptoDateObjects, const DataObjectSet &context=DataObjectSet())
 
static void setRenameHandler (CRenameHandler *rh)
 
- Static Public Attributes inherited from CCopasiMethod
static const std::string SubTypeName []
 
static const char * XMLSubType []
 
- Static Public Attributes inherited from CCopasiParameter
static const std::string TypeName []
 
static const char * XMLType []
 
- Static Public Attributes inherited from CCopasiContainer
static const std::vector
< CCopasiContainer * > 
EmptyList
 
- Protected Types inherited from CCopasiObject
enum  Flag {
  Container = 0x1, Vector = 0x2, Matrix = 0x4, NameVector = 0x8,
  Reference = 0x10, ValueBool = 0x20, ValueInt = 0x40, ValueInt64 = 0x80,
  ValueDbl = 0x100, NonUniqueName = 0x200, StaticString = 0x400, ValueString = 0x800,
  Separator = 0x1000, ModelEntity = 0x2000, Array = 0x4000, DataModel = 0x8000,
  Root = 0x10000, Gui = 0x20000
}
 
- Protected Member Functions inherited from COptMethod
 COptMethod (const CCopasiTask::Type &taskType, const SubType &subType, const CCopasiContainer *pParent=NULL)
 
- Protected Member Functions inherited from CCopasiMethod
 CCopasiMethod (const CCopasiTask::Type &taskType, const SubType &subType, const CCopasiContainer *pParent=NULL)
 
- Protected Member Functions inherited from CCopasiParameterGroup
 CCopasiParameterGroup ()
 
- Protected Member Functions inherited from CCopasiContainer
template<class CType >
CCopasiObjectaddMatrixReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
template<class CType >
CCopasiObjectaddObjectReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
template<class CType >
CCopasiObjectaddVectorReference (const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
 
void initObjects ()
 
- Protected Member Functions inherited from CCopasiObject
 CCopasiObject ()
 
 CCopasiObject (const std::string &name, const CCopasiContainer *pParent=NULL, const std::string &type="CN", const unsigned C_INT32 &flag=0)
 
- Protected Attributes inherited from COptMethod
const bool mBounds
 
const std::vector< COptItem * > * mpOptContraints
 
const std::vector< COptItem * > * mpOptItem
 
COptProblemmpOptProblem
 
COptTaskmpParentTask
 
const std::vector
< UpdateMethod * > * 
mpSetCalculateVariable
 
- Protected Attributes inherited from CCopasiMethod
CProcessReportmpCallBack
 
- Protected Attributes inherited from CCopasiParameter
std::string mKey
 
CCopasiObjectmpValueReference
 
size_t mSize
 
Value mValue
 
- Protected Attributes inherited from CCopasiContainer
objectMap mObjects
 
- Static Protected Attributes inherited from CCopasiObject
static CRenameHandlersmpRenameHandler = NULL
 

Detailed Description

Definition at line 30 of file COptMethodGASR.h.

Constructor & Destructor Documentation

COptMethodGASR::COptMethodGASR ( const COptMethodGASR src,
const CCopasiContainer pParent = NULL 
)

Copy Constructor

Parameters
constCOptMethodGASR & src
constCCopasiContainer * pParent (default: NULL)

Definition at line 57 of file COptMethodGASR.cpp.

References initObjects().

58  :
59  COptMethod(src, pParent),
60  mGenerations(0),
61  mPopulationSize(0),
62  mpRandom(NULL),
63  mVariableSize(0),
64  mIndividual(0),
65  mCrossOverFalse(0),
66  mCrossOver(0),
67  mValue(0),
68  mpPermutation(NULL),
69  mWins(0),
70  mMutationVarians(0.1),
74  mGeneration(0)
75 {initObjects();}
C_FLOAT64 mBestValue
CRandom * mpRandom
unsigned C_INT32 mPopulationSize
#define C_INVALID_INDEX
Definition: copasi.h:222
CPermutation * mpPermutation
std::vector< CVector< C_FLOAT64 > * > mIndividual
unsigned C_INT32 mGenerations
C_FLOAT64 mEvaluationValue
CVector< C_FLOAT64 > mValue
C_FLOAT64 mMutationVarians
CVector< bool > mCrossOver
CVector< size_t > mWins
CVector< bool > mCrossOverFalse
unsigned C_INT32 mGeneration
#define max(a, b)
Definition: f2c.h:176
COptMethodGASR::~COptMethodGASR ( )
virtual

Destructor

Definition at line 77 of file COptMethodGASR.cpp.

References cleanup().

78 {cleanup();}
virtual bool cleanup()
COptMethodGASR::COptMethodGASR ( const CCopasiContainer pParent = NULL)
private

Default Constructor

Parameters
constCCopasiContainer * pParent (default: NULL)

Definition at line 29 of file COptMethodGASR.cpp.

References CCopasiParameterGroup::addParameter(), C_FLOAT64, C_INT32, CCopasiParameter::DOUBLE, initObjects(), CRandom::mt19937, and CCopasiParameter::UINT.

29  :
31  mGenerations(0),
32  mPopulationSize(0),
33  mpRandom(NULL),
34  mVariableSize(0),
35  mIndividual(0),
36  mCrossOverFalse(0),
37  mCrossOver(0),
38  mValue(0),
39  mpPermutation(NULL),
40  mWins(0),
41  mMutationVarians(0.1),
45  mGeneration(0)
46 
47 {
48  addParameter("Number of Generations", CCopasiParameter::UINT, (unsigned C_INT32) 200);
49  addParameter("Population Size", CCopasiParameter::UINT, (unsigned C_INT32) 20);
50  addParameter("Random Number Generator", CCopasiParameter::UINT, (unsigned C_INT32) CRandom::mt19937);
51  addParameter("Seed", CCopasiParameter::UINT, (unsigned C_INT32) 0);
52  addParameter("Pf", CCopasiParameter::DOUBLE, (C_FLOAT64) 0.475); //*****ADDED for SR
53 
54  initObjects();
55 }
C_FLOAT64 mBestValue
CRandom * mpRandom
unsigned C_INT32 mPopulationSize
#define C_INVALID_INDEX
Definition: copasi.h:222
#define C_INT32
Definition: copasi.h:90
CPermutation * mpPermutation
std::vector< CVector< C_FLOAT64 > * > mIndividual
unsigned C_INT32 mGenerations
C_FLOAT64 mEvaluationValue
CVector< C_FLOAT64 > mValue
C_FLOAT64 mMutationVarians
CVector< bool > mCrossOver
CVector< size_t > mWins
#define C_FLOAT64
Definition: copasi.h:92
bool addParameter(const CCopasiParameter &parameter)
CVector< bool > mCrossOverFalse
unsigned C_INT32 mGeneration
#define max(a, b)
Definition: f2c.h:176

Member Function Documentation

bool COptMethodGASR::cleanup ( )
privatevirtual

Cleanup arrays and pointers.

Returns
bool success

Reimplemented from COptMethod.

Definition at line 455 of file COptMethodGASR.cpp.

References mIndividual, mpPermutation, mpRandom, and pdelete.

Referenced by initialize(), optimise(), and ~COptMethodGASR().

456 {
457  size_t i;
458 
459  pdelete(mpRandom);
461 
462  for (i = 0; i < mIndividual.size(); i++)
463  pdelete(mIndividual[i]);
464 
465  return true;
466 }
#define pdelete(p)
Definition: copasi.h:215
CRandom * mpRandom
CPermutation * mpPermutation
std::vector< CVector< C_FLOAT64 > * > mIndividual
bool COptMethodGASR::creation ( size_t  first,
size_t  last = std::numeric_limits<size_t>::max() 
)
private

Initialise the population

Parameters
size_tfirst
size_tlast (default: population size)
Returns
bool success

Definition at line 332 of file COptMethodGASR.cpp.

References C_FLOAT64, evaluate(), COptItem::getLowerBoundValue(), CRandom::getRandomCC(), COptItem::getUpperBoundValue(), max, mEvaluationValue, min, mIndividual, mPhi, mPopulationSize, mpRandom, mValue, mVariableSize, and phi().

Referenced by optimise().

334 {
335  size_t Last = std::min< size_t >(last, mPopulationSize);
336 
337  size_t i;
338  size_t j;
339 
340  C_FLOAT64 mn;
341  C_FLOAT64 mx;
342  C_FLOAT64 la;
343 
344  bool Continue = true;
345 
346  for (i = first; i < Last && Continue; i++)
347  {
348  for (j = 0; j < mVariableSize; j++)
349  {
350  // calculate lower and upper bounds
351  COptItem & OptItem = *(*mpOptItem)[j];
352  mn = *OptItem.getLowerBoundValue();
353  mx = *OptItem.getUpperBoundValue();
354 
355  C_FLOAT64 & mut = (*mIndividual[i])[j];
356 
357  try
358  {
359  // determine if linear or log scale
360  if ((mn < 0.0) || (mx <= 0.0))
361  mut = mn + mpRandom->getRandomCC() * (mx - mn);
362  else
363  {
364  la = log10(mx) - log10(std::max(mn, std::numeric_limits< C_FLOAT64 >::min()));
365 
366  if (la < 1.8)
367  mut = mn + mpRandom->getRandomCC() * (mx - mn);
368  else
369  mut = pow(10.0, log10(std::max(mn, std::numeric_limits< C_FLOAT64 >::min())) + la * mpRandom->getRandomCC());
370  }
371  }
372 
373  catch (...)
374  {
375  mut = (mx + mn) * 0.5;
376  }
377 
378  // We need to set the value here so that further checks take
379  // account of the value.
380  (*(*mpSetCalculateVariable)[j])(mut);
381  }
382 
383  // calculate its fitness
384  Continue = evaluate(*mIndividual[i]);
386 
387  /* Calculate the phi value of the individual for SR*/
388  mPhi[i] = phi(i);
389  }
390 
391  return Continue;
392 }
C_FLOAT64 phi(size_t indvNum)
CVector< C_FLOAT64 > mPhi
CRandom * mpRandom
unsigned C_INT32 mPopulationSize
std::vector< CVector< C_FLOAT64 > * > mIndividual
virtual C_FLOAT64 getRandomCC()
Definition: CRandom.cpp:235
C_FLOAT64 mEvaluationValue
CVector< C_FLOAT64 > mValue
const C_FLOAT64 * getLowerBoundValue() const
Definition: COptItem.h:191
#define C_FLOAT64
Definition: copasi.h:92
const C_FLOAT64 * getUpperBoundValue() const
Definition: COptItem.h:198
bool evaluate(const CVector< C_FLOAT64 > &individual)
#define min(a, b)
Definition: f2c.h:175
#define max(a, b)
Definition: f2c.h:176
bool COptMethodGASR::crossover ( const CVector< C_FLOAT64 > &  parent1,
const CVector< C_FLOAT64 > &  parent2,
CVector< C_FLOAT64 > &  child1,
CVector< C_FLOAT64 > &  child2 
)
private
Parameters
constCVector< C_FLOAT64 > & parent1
constCVector< C_FLOAT64 > & parent2
CVector<C_FLOAT64 > & child1
CVector<C_FLOAT64 > & child2

Definition at line 144 of file COptMethodGASR.cpp.

References C_INT32, CRandom::getRandomU(), mCrossOver, mCrossOverFalse, mpRandom, and mVariableSize.

Referenced by replicate().

148 {
149  size_t i, crp;
150  size_t nCross = 0;
151 
153 
154  if (mVariableSize > 1)
155  nCross = mpRandom->getRandomU((unsigned C_INT32)(mVariableSize / 2));
156 
157  if (nCross == 0)
158  {
159  // if less than 0 just copy parent to child
160  child1 = parent1;
161  child2 = parent2;
162 
163  return true;
164  }
165 
166  // choose cross over points;
167  // We do not mind if a crossover point gets drawn twice
168  for (i = 0; i < nCross; i++)
169  {
170  crp = mpRandom->getRandomU((unsigned C_INT32)(mVariableSize - 1));
171  mCrossOver[crp] = true;
172  }
173 
174  const CVector< C_FLOAT64 > * pParent1 = & parent1;
175 
176  const CVector< C_FLOAT64 > * pParent2 = & parent2;
177 
178  const CVector< C_FLOAT64 > * pTmp;
179 
180  for (i = 0; i < mVariableSize; i++)
181  {
182  if (mCrossOver[i])
183  {
184  pTmp = pParent1;
185  pParent1 = pParent2;
186  pParent2 = pTmp;
187  }
188 
189  child1[i] = (*pParent1)[i];
190  child2[i] = (*pParent2)[i];
191  }
192 
193  return true;
194 }
CRandom * mpRandom
#define C_INT32
Definition: copasi.h:90
virtual unsigned C_INT32 getRandomU()
Definition: CRandom.cpp:173
CVector< bool > mCrossOver
CVector< bool > mCrossOverFalse
bool COptMethodGASR::evaluate ( const CVector< C_FLOAT64 > &  individual)
private

Evaluate the fitness of one individual

Parameters
constCVector< C_FLOAT64 > & individual
Returns
bool continue

Definition at line 81 of file COptMethodGASR.cpp.

References COptProblem::calculate(), COptProblem::getCalculateValue(), mEvaluationValue, and COptMethod::mpOptProblem.

Referenced by creation(), optimise(), and replicate().

82 {
83  bool Continue = true;
84 
85  // We do not need to check whether the parametric constraints are fulfilled
86  // since this method allows for parameters outside the bounds
87 
88  // evaluate the fitness
89  Continue = mpOptProblem->calculate();
90 
91  // We do not need to check whether the functional constraints are fulfilled
92  // since this method allows for solutions outside the bounds.
93 
95 
96  return Continue;
97 }
COptProblem * mpOptProblem
Definition: COptMethod.h:56
virtual bool calculate()
C_FLOAT64 mEvaluationValue
const C_FLOAT64 & getCalculateValue() const
size_t COptMethodGASR::fittest ( void  )
private

Find the best individual at this generation

Returns
size_t fittest

Definition at line 316 of file COptMethodGASR.cpp.

References C_FLOAT64, C_INVALID_INDEX, max, mPhi, mPopulationSize, and mValue.

Referenced by optimise().

317 {
318  size_t i, BestIndex = C_INVALID_INDEX;
320 
321  for (i = 0; i < mPopulationSize; i++)
322  if (mValue[i] < BestValue && !(mPhi[i] != 0))
323  {
324  BestIndex = i;
325  BestValue = mValue[i];
326  }
327 
328  return BestIndex;
329 }
CVector< C_FLOAT64 > mPhi
unsigned C_INT32 mPopulationSize
#define C_INVALID_INDEX
Definition: copasi.h:222
CVector< C_FLOAT64 > mValue
#define C_FLOAT64
Definition: copasi.h:92
#define max(a, b)
Definition: f2c.h:176
bool COptMethodGASR::initialize ( )
privatevirtual

Initialize arrays and pointer.

Returns
bool success

Reimplemented from COptMethod.

Definition at line 399 of file COptMethodGASR.cpp.

References CProcessReport::addItem(), C_FLOAT64, cleanup(), CRandom::createGenerator(), CCopasiParameter::getValue(), COptMethod::initialize(), mCrossOver, mCrossOverFalse, mGeneration, mGenerations, mhGenerations, mIndividual, mMutationVarians, CCopasiMethod::mpCallBack, mPf, mPhi, COptMethod::mpOptItem, mPopulationSize, mpPermutation, mpRandom, mValue, mVariableSize, mWins, CCopasiParameter::Value::pDOUBLE, CCopasiParameter::Value::pUINT, CVector< CType >::resize(), and CCopasiParameterGroup::setValue().

Referenced by optimise().

400 {
401  cleanup();
402 
403  size_t i;
404 
405  if (!COptMethod::initialize()) return false;
406 
407  mGeneration = 0;
408  mGenerations = * getValue("Number of Generations").pUINT;
409 
410  if (mpCallBack)
411  mhGenerations =
412  mpCallBack->addItem("Current Generation",
413  mGeneration,
414  & mGenerations);
415 
416  mGeneration++;
417 
418  mPopulationSize = * getValue("Population Size").pUINT;
419  mPf = *(C_FLOAT64*) getValue("Pf").pDOUBLE;
420 
421  if (mPf < 0.0 || 1.0 < mPf)
422  {
423  mPf = 0.475;
424  setValue("Pf", mPf);
425  }
426 
427  mpRandom =
428  CRandom::createGenerator(* (CRandom::Type *) getValue("Random Number Generator").pUINT,
429  * getValue("Seed").pUINT);
430 
431  mVariableSize = mpOptItem->size();
432 
433  mIndividual.resize(2 * mPopulationSize);
435 
436  for (i = 0; i < 2 * mPopulationSize; i++)
438 
440  mCrossOverFalse = false;
442 
443  mValue.resize(2 * mPopulationSize);
444 
445  mpPermutation = new CPermutation(mpRandom, mPopulationSize);
446 
447  mWins.resize(2 * mPopulationSize);
448 
449  // initialise the variance for mutations
450  mMutationVarians = 0.1;
451 
452  return true;
453 }
virtual bool cleanup()
virtual bool initialize()
Definition: COptMethod.cpp:189
CVector< C_FLOAT64 > mPhi
CRandom * mpRandom
unsigned C_INT32 mPopulationSize
void resize(size_t size, const bool &copy=false)
Definition: CVector.h:301
CPermutation * mpPermutation
std::vector< CVector< C_FLOAT64 > * > mIndividual
unsigned C_INT32 mGenerations
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
size_t addItem(const std::string &name, const std::string &value, const std::string *pEndValue=NULL)
const Value & getValue() const
CVector< C_FLOAT64 > mValue
bool setValue(const std::string &name, const CType &value)
unsigned C_INT32 * pUINT
C_FLOAT64 mMutationVarians
CVector< bool > mCrossOver
CVector< size_t > mWins
const std::vector< COptItem * > * mpOptItem
Definition: COptMethod.h:70
#define C_FLOAT64
Definition: copasi.h:92
CVector< bool > mCrossOverFalse
unsigned C_INT32 mGeneration
CProcessReport * mpCallBack
void COptMethodGASR::initObjects ( )
private

Initialize contained objects.

Definition at line 394 of file COptMethodGASR.cpp.

References CCopasiContainer::addObjectReference(), mGeneration, and CCopasiObject::ValueInt.

Referenced by COptMethodGASR().

395 {
397 }
unsigned C_INT32 mGeneration
CCopasiObject * addObjectReference(const std::string &name, CType &reference, const unsigned C_INT32 &flag=0)
bool COptMethodGASR::mutate ( CVector< C_FLOAT64 > &  individual)
private

fm one individual

Parameters
CVector<C_FLOAT64 > & individual
Returns
bool success

Definition at line 122 of file COptMethodGASR.cpp.

References C_FLOAT64, CRandom::getRandomNormal(), mMutationVarians, mpRandom, and mVariableSize.

Referenced by replicate().

123 {
124  size_t j;
125 
126  // mutate the parameters
127  for (j = 0; j < mVariableSize; j++)
128  {
129  C_FLOAT64 & mut = individual[j];
130 
131  // calculate the mutatated parameter
133 
134  // for SR do not force to be within bounds
135 
136  // We need to set the value here so that further checks take
137  // account of the value.
138  (*(*mpSetCalculateVariable)[j])(mut);
139  }
140 
141  return true;
142 }
virtual C_FLOAT64 getRandomNormal(const C_FLOAT64 &mean, const C_FLOAT64 &sd)
Definition: CRandom.cpp:292
CRandom * mpRandom
C_FLOAT64 mMutationVarians
#define C_FLOAT64
Definition: copasi.h:92
bool COptMethodGASR::optimise ( void  )
virtual

Execute the optimization algorithm calling simulation routine when needed. It is noted that this procedure can give feedback of its progress by the callback function set with SetCallback. @ return success;

Reimplemented from COptMethod.

Definition at line 468 of file COptMethodGASR.cpp.

References C_INVALID_INDEX, cleanup(), creation(), COutputInterface::DURING, evaluate(), CProcessReport::finishItem(), fittest(), initialize(), mBestIndex, mBestValue, mEvaluationValue, mGeneration, mGenerations, mhGenerations, mIndividual, CCopasiMethod::mpCallBack, mPhi, COptMethod::mpOptProblem, mPopulationSize, COptMethod::mpParentTask, COptMethod::mpSetCalculateVariable, mValue, mVariableSize, CCopasiTask::output(), phi(), CProcessReport::progressItem(), replicate(), select(), and COptProblem::setSolution().

469 {
470  bool Continue = true;
471 
472  if (!initialize())
473  {
474  if (mpCallBack)
476 
477  return false;
478  }
479 
480  // Counters to determine whether the optimization process has stalled
481  // They count the number of generations without advances.
482  size_t Stalled, Stalled10, Stalled30, Stalled50;
483  Stalled = Stalled10 = Stalled30 = Stalled50 = 0;
484 
485  size_t i;
486 
487  // initialise the population
488  // first individual is the initial guess
489  for (i = 0; i < mVariableSize; i++)
490  (*mIndividual[0])[i] = (*mpOptItem)[i]->getStartValue();
491 
492  // calculate the fitness
493  size_t j;
494  std::vector< UpdateMethod *>::const_iterator itMethod = mpSetCalculateVariable->begin();
495 
496  // set the paramter values
497  for (j = 0; j < mVariableSize; j++, ++itMethod)
498  (**itMethod)((*mIndividual[0])[j]);
499 
500  Continue = evaluate(*mIndividual[0]);
502 
503  /* Calculate the phi value of the individual for SR*/
504  mPhi[0] = phi(0);
505 
506  // the others are random
507  Continue = creation(1, mPopulationSize);
508 
509  // get the index of the fittest
510  mBestIndex = fittest();
511 
513  {
514  // and store that value
517 
518  // We found a new best value lets report it.
520  }
521 
522  if (!Continue)
523  {
524  if (mpCallBack)
526 
527  cleanup();
528  return true;
529  }
530 
531  // ITERATE FOR gener GENERATIONS
532  for (mGeneration = 2;
533  mGeneration <= mGenerations && Continue;
534  mGeneration++, Stalled++, Stalled10++, Stalled30++, Stalled50++)
535  {
536  // perturb the population if we have stalled for a while
537  if (Stalled > 50 && Stalled50 > 50)
538  {
539  Continue = creation((size_t)(mPopulationSize * 0.5),
541  Stalled10 = Stalled30 = Stalled50 = 0;
542  }
543  else if (Stalled > 30 && Stalled30 > 30)
544  {
545  Continue = creation((size_t)(mPopulationSize * 0.7),
547  Stalled10 = Stalled30 = 0;
548  }
549  else if (Stalled > 10 && Stalled10 > 10)
550  {
551  Continue = creation((size_t)(mPopulationSize * 0.9),
553  Stalled10 = 0;
554  }
555  // replicate the individuals
556  else
557  Continue = replicate();
558 
559  // select the most fit
560  Continue = select();
561 
562  // get the index of the fittest
563  mBestIndex = fittest();
564 
565  if (mBestIndex != C_INVALID_INDEX &&
566  mValue[mBestIndex] < mBestValue)
567  {
568  Stalled = Stalled10 = Stalled30 = Stalled50 = 0;
570 
571  Continue = mpOptProblem->setSolution(mBestValue, *mIndividual[mBestIndex]);
572 
573  // We found a new best value lets report it.
575  }
576 
577  if (mpCallBack)
579  }
580 
581  if (mpCallBack)
583 
584  cleanup();
585 
586  return true;
587 }
COptTask * mpParentTask
Definition: COptMethod.h:58
virtual bool cleanup()
C_FLOAT64 mBestValue
C_FLOAT64 phi(size_t indvNum)
CVector< C_FLOAT64 > mPhi
unsigned C_INT32 mPopulationSize
#define C_INVALID_INDEX
Definition: copasi.h:222
COptProblem * mpOptProblem
Definition: COptMethod.h:56
virtual void output(const COutputInterface::Activity &activity)
virtual bool progressItem(const size_t &handle)
std::vector< CVector< C_FLOAT64 > * > mIndividual
unsigned C_INT32 mGenerations
C_FLOAT64 mEvaluationValue
const std::vector< UpdateMethod * > * mpSetCalculateVariable
Definition: COptMethod.h:65
CVector< C_FLOAT64 > mValue
virtual bool setSolution(const C_FLOAT64 &value, const CVector< C_FLOAT64 > &variables)
virtual bool finishItem(const size_t &handle)
virtual bool initialize()
bool evaluate(const CVector< C_FLOAT64 > &individual)
unsigned C_INT32 mGeneration
CProcessReport * mpCallBack
bool creation(size_t first, size_t last=std::numeric_limits< size_t >::max())
C_FLOAT64 COptMethodGASR::phi ( size_t  indvNum)
private

For Stochastic Ranking, evaluate the distance ofparameters to boundaries

Returns
C_FLOAT64 phiVal

Definition at line 276 of file COptMethodGASR.cpp.

References C_FLOAT64, mIndividual, COptMethod::mpOptContraints, and COptMethod::mpOptItem.

Referenced by creation(), optimise(), and replicate().

277 {
278  C_FLOAT64 phiVal = 0.0;
279  C_FLOAT64 phiCalc;
280 
281  std::vector< COptItem * >::const_iterator it = mpOptItem->begin();
282  std::vector< COptItem * >::const_iterator end = mpOptItem->end();
283  C_FLOAT64 * pValue = mIndividual[indivNum]->array();
284 
285  for (; it != end; ++it, pValue++)
286  {
287  switch ((*it)->checkConstraint())
288  {
289  case - 1:
290  phiCalc = *(*it)->getLowerBoundValue() - *pValue;
291  phiVal += phiCalc * phiCalc;
292  break;
293 
294  case 1:
295  phiCalc = *pValue - *(*it)->getUpperBoundValue();
296  phiVal += phiCalc * phiCalc;
297  break;
298  }
299  }
300 
301  it = mpOptContraints->begin();
302  end = mpOptContraints->end();
303 
304  for (; it != end; ++it)
305  {
306  phiCalc = (*it)->getConstraintViolation();
307 
308  if (phiCalc > 0.0)
309  phiVal += phiCalc * phiCalc;
310  }
311 
312  return phiVal;
313 }
const std::vector< COptItem * > * mpOptContraints
Definition: COptMethod.h:75
std::vector< CVector< C_FLOAT64 > * > mIndividual
const std::vector< COptItem * > * mpOptItem
Definition: COptMethod.h:70
#define C_FLOAT64
Definition: copasi.h:92
bool COptMethodGASR::replicate ( void  )
private

Replicate the individuals with crossover

Returns
bool continue

Definition at line 196 of file COptMethodGASR.cpp.

References crossover(), evaluate(), mEvaluationValue, mIndividual, mPhi, mPopulationSize, mpPermutation, mutate(), mValue, CPermutation::next(), phi(), and CPermutation::shuffle().

Referenced by optimise().

197 {
198  size_t i;
199  bool Continue = true;
200 
201  // generate a random order for the parents
203 
204  // reproduce in consecutive pairs
205  for (i = 0; i < mPopulationSize / 2; i++)
208  *mIndividual[mPopulationSize + i * 2],
209  *mIndividual[mPopulationSize + i * 2 + 1]);
210 
211  // check if there is one left over and just copy it
212  if (mPopulationSize % 2 > 0)
213  *mIndividual[2 * mPopulationSize - 1] = *mIndividual[mPopulationSize - 1];
214 
215  // mutate the offspring
216  for (i = mPopulationSize; i < 2 * mPopulationSize && Continue; i++)
217  {
218  mutate(*mIndividual[i]);
219  Continue = evaluate(*mIndividual[i]);
221 
222  /* Calculate the phi value of the individual for SR*/
223  mPhi[i] = phi(i);
224  }
225 
226  return Continue;
227 }
bool mutate(CVector< C_FLOAT64 > &individual)
C_FLOAT64 phi(size_t indvNum)
CVector< C_FLOAT64 > mPhi
const size_t & next()
unsigned C_INT32 mPopulationSize
CPermutation * mpPermutation
std::vector< CVector< C_FLOAT64 > * > mIndividual
C_FLOAT64 mEvaluationValue
void shuffle(const size_t &swaps=C_INVALID_INDEX)
CVector< C_FLOAT64 > mValue
bool evaluate(const CVector< C_FLOAT64 > &individual)
bool crossover(const CVector< C_FLOAT64 > &parent1, const CVector< C_FLOAT64 > &parent2, CVector< C_FLOAT64 > &child1, CVector< C_FLOAT64 > &child2)
bool COptMethodGASR::select ( )
private

Select surviving population

Returns
bool success

Definition at line 230 of file COptMethodGASR.cpp.

References CRandom::getRandomOO(), mPf, mPhi, mPopulationSize, mpRandom, mValue, and swap().

Referenced by optimise().

231 {
232  size_t i, j;
233  size_t TotalPopulation = 2 * mPopulationSize;
234  bool wasSwapped;
235  size_t sweepNum = TotalPopulation; // This is default based on paper
236 
237  // Selection Method for Stochastic Ranking
238  // stochastic ranking "bubble sort"
239 
240  for (i = 0; i < sweepNum; i++) // Here sweepNum is optimal number of sweeps from paper
241  {
242  wasSwapped = false;
243 
244  // :TODO: since we are only interested in mPopulationSize highest ranked
245  // individuals the upper limit of the loop can be improved.
246  for (j = 0; j < TotalPopulation - 1; j++) // lambda is number of individuals
247  {
248  if ((mPhi[j] == 0 && mPhi[j + 1] == 0) || // within bounds
249  (mpRandom->getRandomOO() < mPf)) // random chance to compare values outside bounds
250  {
251  // compare obj fcn using mValue alternative code
252  if (mValue[j] > mValue[j + 1])
253  {
254  swap(j, j + 1);
255  wasSwapped = true;
256  }
257  }
258  else //mPhi values are not equal and not within boundary
259  {
260  if (mPhi[j] > mPhi[j + 1]) // j further outside then j+1
261  {
262  swap(j, j + 1);
263  wasSwapped = true;
264  }
265  }
266  }
267 
268  // if no swap then break
269  if (wasSwapped == false) break;
270  }
271 
272  return true;
273 }
CVector< C_FLOAT64 > mPhi
virtual C_FLOAT64 getRandomOO()
Definition: CRandom.cpp:254
CRandom * mpRandom
unsigned C_INT32 mPopulationSize
CVector< C_FLOAT64 > mValue
bool swap(size_t from, size_t to)
bool COptMethodGASR::swap ( size_t  from,
size_t  to 
)
private

Swap individuals from and to

Parameters
size_tfrom
size_tto
Returns
bool success

Definition at line 99 of file COptMethodGASR.cpp.

References C_FLOAT64, mIndividual, mPhi, mValue, and mWins.

Referenced by select().

100 {
101  CVector< C_FLOAT64 > * pTmp = mIndividual[to];
102  mIndividual[to] = mIndividual[from];
103  mIndividual[from] = pTmp;
104 
105  //**** Added for swapping individual Phi values also for Stochastic Ranking
106  C_FLOAT64 dTmp = mPhi[to];
107  mPhi[to] = mPhi[from];
108  mPhi[from] = dTmp;
109 
110  dTmp = mValue[to];
111  mValue[to] = mValue[from];
112  mValue[from] = dTmp;
113 
114  size_t iTmp = mWins[to];
115  mWins[to] = mWins[from];
116  mWins[from] = iTmp;
117 
118  return true;
119 }
CVector< C_FLOAT64 > mPhi
std::vector< CVector< C_FLOAT64 > * > mIndividual
CVector< C_FLOAT64 > mValue
CVector< size_t > mWins
#define C_FLOAT64
Definition: copasi.h:92

Friends And Related Function Documentation

Member Data Documentation

size_t COptMethodGASR::mBestIndex
private

Definition at line 227 of file COptMethodGASR.h.

Referenced by optimise().

C_FLOAT64 COptMethodGASR::mBestValue
private

The best value so far.

Definition at line 226 of file COptMethodGASR.h.

Referenced by optimise().

CVector< bool > COptMethodGASR::mCrossOver
private

Vector of crossover points.

Definition at line 196 of file COptMethodGASR.h.

Referenced by crossover(), and initialize().

CVector< bool > COptMethodGASR::mCrossOverFalse
private

Vector used to initialize the crossover point to false

Definition at line 191 of file COptMethodGASR.h.

Referenced by crossover(), and initialize().

C_FLOAT64 COptMethodGASR::mEvaluationValue
private

The value of the last evaluation.

Definition at line 221 of file COptMethodGASR.h.

Referenced by creation(), evaluate(), optimise(), and replicate().

unsigned C_INT32 COptMethodGASR::mGeneration
private

Definition at line 228 of file COptMethodGASR.h.

Referenced by initialize(), initObjects(), and optimise().

unsigned C_INT32 COptMethodGASR::mGenerations
private

number of generations

Definition at line 151 of file COptMethodGASR.h.

Referenced by initialize(), and optimise().

size_t COptMethodGASR::mhGenerations
private

Handle to the process report item "Current Generation"

Definition at line 156 of file COptMethodGASR.h.

Referenced by initialize(), and optimise().

std::vector< CVector < C_FLOAT64 > * > COptMethodGASR::mIndividual
private

for array of individuals w/ candidate values for the parameters

Definition at line 181 of file COptMethodGASR.h.

Referenced by cleanup(), creation(), initialize(), optimise(), phi(), replicate(), and swap().

C_FLOAT64 COptMethodGASR::mMutationVarians
private

variance for mutations

Definition at line 216 of file COptMethodGASR.h.

Referenced by initialize(), and mutate().

C_FLOAT64 COptMethodGASR::mPf
private

Probability value used for SR ****

Definition at line 171 of file COptMethodGASR.h.

Referenced by initialize(), and select().

CVector< C_FLOAT64 > COptMethodGASR::mPhi
private

For Stochastic Ranking. Used for array of Phi values for the individuals

Definition at line 186 of file COptMethodGASR.h.

Referenced by creation(), fittest(), initialize(), optimise(), replicate(), select(), and swap().

unsigned C_INT32 COptMethodGASR::mPopulationSize
private

size of the population

Definition at line 161 of file COptMethodGASR.h.

Referenced by creation(), fittest(), initialize(), optimise(), replicate(), and select().

CPermutation* COptMethodGASR::mpPermutation
private

A permutation of integers used to create the informants;

Definition at line 206 of file COptMethodGASR.h.

Referenced by cleanup(), initialize(), and replicate().

CRandom* COptMethodGASR::mpRandom
private

a pointer to the randomnumber generator.

Definition at line 166 of file COptMethodGASR.h.

Referenced by cleanup(), creation(), crossover(), initialize(), mutate(), and select().

CVector< C_FLOAT64 > COptMethodGASR::mValue
private

array of values of objective function f/ individuals

Definition at line 201 of file COptMethodGASR.h.

Referenced by creation(), fittest(), initialize(), optimise(), replicate(), select(), and swap().

size_t COptMethodGASR::mVariableSize
private

number of parameters

Definition at line 176 of file COptMethodGASR.h.

Referenced by creation(), crossover(), initialize(), mutate(), and optimise().

CVector< size_t > COptMethodGASR::mWins
private

number of wins of each individual in the tournament

Definition at line 211 of file COptMethodGASR.h.

Referenced by initialize(), and swap().


The documentation for this class was generated from the following files: