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

#include <COptMethodHGASA.h>

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

Public Member Functions

 COptMethodHGASA (const COptMethodHGASA &src)
 
virtual void copy (int o, int d)
 
virtual void crossover (int p1, int p2, int c1, int c2)
 
virtual void exchange (int o, int d)
 
virtual int fittest (void)
 
double Get_BestFoundSoFar_candidate ()
 
int Get_NumGeneration ()
 
int Get_NumParameter ()
 
int Get_PopulationSize ()
 
virtual bool optimise ()
 
virtual bool optimise (int index)
 
virtual void replicate (void)
 
virtual void select (int method)
 
void Set_BestFoundSoFar (int num)
 
void Set_CandidateValue (int i, double num)
 
void Set_individual (int i, int j, double num)
 
void Set_Maximum (double num)
 
void Set_Minimum (double num)
 
void Set_murvar (double num)
 
void Set_NumGeneration (int num)
 
void Set_NumParameter (int num)
 
void Set_PopulationSize (int num)
 
virtual void swap (int o, int d)
 
virtual void TrackDataFile (int i)
 
virtual ~COptMethodHGASA ()
 
- Public Member Functions inherited from COptMethod
 COptMethod (const COptMethod &src, const CCopasiContainer *pParent=NULL)
 
virtual bool initialize ()
 
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

 COptMethodHGASA ()
 

Private Attributes

int BestFoundSoFar
 
double * CandidateValue
 
int * CrossPoint
 
double ** individual
 
double Maximum
 
double Minimum
 
int NumCrossPoint
 
int NumGeneration
 
int NumParameter
 
int PopulationSize
 
int * WinScore
 

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
virtual bool cleanup ()
 
 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 43 of file COptMethodHGASA.h.

Constructor & Destructor Documentation

COptMethodHGASA::COptMethodHGASA ( )
private

Default Constructor

Definition at line 52 of file COptMethodHGASA.cpp.

References CCopasiParameterGroup::addParameter(), C_INT32, CCopasiParameter::INT, CRandom::mt19937, and CCopasiParameter::UINT.

53 {
54  addParameter("HybridGASA.Iterations",
56  (unsigned C_INT32) 10000);
57  addParameter("HybridGASA.PopulationSize",
59  (unsigned C_INT32) 600);
60  addParameter("HybridGASA.RandomGenerator.Type",
63  addParameter("HybridGASA.RandomGenerator.Seed",
65  (C_INT32) 0);
66 }
#define C_INT32
Definition: copasi.h:90
bool addParameter(const CCopasiParameter &parameter)
COptMethodHGASA::COptMethodHGASA ( const COptMethodHGASA src)

Copy Constructor

Parameters
constCOptMethodHGASA & src

Definition at line 48 of file COptMethodHGASA.cpp.

48  :
49  COptMethod(src)
50 {}
COptMethodHGASA::~COptMethodHGASA ( )
virtual

Destructor

Definition at line 71 of file COptMethodHGASA.cpp.

72 {}

Member Function Documentation

void COptMethodHGASA::copy ( int  o,
int  d 
)
virtual

Definition at line 585 of file COptMethodHGASA.cpp.

References CandidateValue, individual, and NumParameter.

Referenced by replicate().

586 {
587  int i;
588 
589  for (i = 0; i < NumParameter; i++)
590  {
591  individual[d][i] = individual[o][i];
592  }
593 
595 }
double * CandidateValue
double ** individual
void COptMethodHGASA::crossover ( int  p1,
int  p2,
int  c1,
int  c2 
)
virtual

Definition at line 637 of file COptMethodHGASA.cpp.

References CrossPoint, individual, NumCrossPoint, and NumParameter.

Referenced by replicate().

638 {
639  int i, j, s, e;
640  int pp1, pp2, tmp, l;
641 
642  //srand(time(NULL)); rand();
643 
644  try
645  {
646  if (NumParameter > 1)
647  {
648  // get a random number of crossover points, always less than half the number of genes
649  NumCrossPoint = (int)fabs(floor((NumParameter / 2) * rand() / RAND_MAX));
650  // NumCrossPoint = (int)floor((NumParameter/2)*pRand->getRandomCC());
651  }
652  else NumCrossPoint = 0;
653 
654  // if less than 0 just copy parent to child
655  if (NumCrossPoint == 0)
656  {
657  for (j = 0; j < NumParameter; j++)
658  {
659  individual[c1][j] = individual[p1][j];
660  individual[c2][j] = individual[p2][j];
661  }
662 
663  return;
664  }
665 
666  // chose first point
667  CrossPoint[0] = 1 + (int)fabs(floor((NumParameter - NumCrossPoint) * rand() / RAND_MAX));
668 
669  //CrossPoint[0] = 1 + (int)floor((NumParameter-NumCrossPoint)*pRand->getRandomCC());
670  // chose the others
671  for (i = 1; i < NumCrossPoint; i++)
672  {
673  l = NumParameter - NumCrossPoint + i - 1 - CrossPoint[i - 1];
674  CrossPoint[i] = 1 + CrossPoint[i - 1] + (l == 0 ? 0 : (int)fabs(floor(l * rand() / RAND_MAX)));
675  //CrossPoint[i] = 1 + CrossPoint[i-1] + (l==0 ? 0 : (int)floor(l*pRand->getRandomCC()));
676  }
677 
678  // copy segments
679  pp1 = p2; pp2 = p1;
680 
681  for (i = 0; i <= NumCrossPoint; i++)
682  {
683  // swap the indexes
684  tmp = pp1;
685  pp1 = pp2;
686  pp2 = tmp;
687  if (i == 0) s = 0; else s = CrossPoint[i - 1];
688  if (i == NumCrossPoint) e = NumParameter; else e = CrossPoint[i];
689 
690  for (j = s; j < e; j++)
691  {
692  individual[c1][j] = individual[pp1][j];
693  individual[c2][j] = individual[pp2][j];
694  }
695  }
696  }
697  catch (int)
698  {}}
double ** individual
void COptMethodHGASA::exchange ( int  o,
int  d 
)
virtual

Definition at line 620 of file COptMethodHGASA.cpp.

References CandidateValue, individual, and NumParameter.

Referenced by optimise(), and select().

621 {
622  int i;
623  double tmp;
624 
625  for (i = 0; i < NumParameter; i++)
626  {
627  tmp = individual[d][i];
628  individual[d][i] = individual[o][i];
629  individual[o][i] = tmp;
630  }
631 
632  tmp = CandidateValue[d];
634  CandidateValue[o] = tmp;
635 }
double * CandidateValue
double ** individual
int COptMethodHGASA::fittest ( void  )
virtual

Definition at line 794 of file COptMethodHGASA.cpp.

References CandidateValue, and PopulationSize.

Referenced by optimise().

795 {
796  int i, b;
797  double f;
798  f = CandidateValue[0];
799  b = 0;
800 
801  for (i = 1; i < PopulationSize; i++)
802  {
803  if (CandidateValue[i] < f)
804  {
805  b = i;
806  f = CandidateValue[i];
807  }
808  }
809 
810  return b;
811 }
double * CandidateValue
double COptMethodHGASA::Get_BestFoundSoFar_candidate ( )
inline

Definition at line 203 of file COptMethodHGASA.h.

References BestFoundSoFar, and CandidateValue.

Referenced by optimise().

204 {
206 }
double * CandidateValue
int COptMethodHGASA::Get_NumGeneration ( )
inline

Definition at line 208 of file COptMethodHGASA.h.

References NumGeneration.

209 {
210  return NumGeneration;
211 }
int COptMethodHGASA::Get_NumParameter ( )
inline

Definition at line 198 of file COptMethodHGASA.h.

References NumParameter.

199 {
200  return NumParameter;
201 }
int COptMethodHGASA::Get_PopulationSize ( )
inline

Definition at line 213 of file COptMethodHGASA.h.

References PopulationSize.

214 {
215  return PopulationSize;
216 }
C_INT32 COptMethodHGASA::optimise ( void  )
virtual

GA Optimizer Function: Returns: nothing

Reimplemented from COptMethod.

Definition at line 78 of file COptMethodHGASA.cpp.

References CVectorCore< CType >::array(), BestFoundSoFar, C_INT32, CandidateValue, CRandom::createGenerator(), CrossPoint, FALSE, fittest(), Get_BestFoundSoFar_candidate(), CRandom::getRandomCC(), CCopasiParameter::getValue(), individual, linear(), max, Maximum, min, Minimum, NumGeneration, NumParameter, PopulationSize, replicate(), select(), TrackDataFile(), TRUE, and WinScore.

79 {
80  NumGeneration = (C_INT32) getValue("HybridGASA.Iterations");
81  PopulationSize = (C_INT32) getValue("HybridGASA.PopulationSize");
82  NumParameter = mOptProblem->getVariableSize();
83 
84  /* Create a random number generator */
86  Type = (CRandom::Type)(C_INT32) getValue("HybridGASA.RandomGenerator.Type");
87  C_INT32 Seed;
88  Seed = (C_INT32) getValue("HybridGASA.RandomGenerator.Seed");
89  CRandom * pRand = CRandom::createGenerator(Type, Seed);
90 
91  assert(pRand);
92 
93  const double ** Minimum = mOptProblem->getParameterMin().array();
94  const double ** Maximum = mOptProblem->getParameterMax().array();
95 
96  CVector< C_FLOAT64 > & Parameter = mOptProblem->getCalculateVariables();
97 
98 #ifdef XXXX
99  double **Parameter;
100  Parameter = new double * [2 * PopulationSize];
101 
102  for (int ii = 0; ii < 2*PopulationSize; ii++)
103  {
104  Parameter[ii] = new double[NumParameter];
105  }
106 
107  for (int dd = 0; dd < 2*PopulationSize; dd++)
108  Parameter[dd] = mParameters->array();
109 
110 #endif // XXXX
111 
112  double current_best_value, la;
113  int i, j, last_update, u10, u30, u50;
114  bool linear;
115 
116  // create array for function value of candidate
117  CandidateValue = new double[2 * PopulationSize];
118 
119  // create array for tournament competition strategy
120  WinScore = new int[2 * PopulationSize];
121 
122  // create array for crossover points
123  CrossPoint = new int[NumParameter];
124 
125  // create the population array
126  individual = new double * [2 * PopulationSize];
127 
128  // create the individuals
129  for (i = 0; i < 2*PopulationSize; i++)
130  {
131  individual[i] = new double[NumParameter];
132  }
133 
134  // Prepare inital population
135  //Generate the initial population at random
136  for (i = 0; i < PopulationSize; i++)
137  {
138  for (j = 0; j < NumParameter; j++)
139  {
140  try
141  {
142  // determine if linear or log scale
143  linear = FALSE; la = 1.0;
144 
145  if ((*Maximum[j] <= 0.0) || (*Minimum[j] < 0.0)) linear = TRUE;
146  else
147  {
148  la = log10(*Maximum[j]) - log10(std::min(*Minimum[j], std::numeric_limits< C_FLOAT64 >::epsilon()));
149 
150  if (la < 1.8) linear = TRUE;
151  }
152 
153  // set it to a random value within the interval
154  if (linear)
155  individual[i][j] = *Minimum[j] + pRand->getRandomCC() * (*Maximum[j] - *Minimum[j]);
156  else
157  individual[i][j] = *Minimum[j] * pow(10.0, la * pRand->getRandomCC());
158  }
159  catch (int)
160  {
161  individual[i][j] = (*Maximum[j] - *Minimum[j]) * 0.5 + *Minimum[j];
162  }
163  }
164 
165  try
166  {
167  // calculate its fitness value
168  for (int kk = 0; kk < NumParameter; kk++) {Parameter[kk] = individual[i][kk];}
169 
170  CandidateValue[i] = mOptProblem->calculate();
171  }
172  catch (int)
173  {
175  }
176  } // initialization ends
177 
178  // get the index of the fittest
180 
181  // initialise the update registers
182  last_update = 0;
183  u10 = u30 = u50 = 0;
184 
185  // store that value
186  current_best_value = Get_BestFoundSoFar_candidate();
187 
188  std::ofstream finalout("debugopt.dat");
189 
190  if (!finalout)
191  {
192  std::cout << "debugopt.dat cannot be opened!" << std::endl;
193  exit(1);
194  }
195 
196  finalout << "----------------------------- the best result at each generation---------------------" << std::endl;
197  finalout << "Generation\t" << "Best candidate value for object function\t" << "Display " << NumParameter << " parameters" << std::endl;
198  finalout << std::endl;
199  finalout.close();
200 
201  srand(time(NULL)); rand();
202 
203  //double a1=0.00000000001;
204  //double b1=0.9;
205 
206  // Iterations of GA
207  for (i = 0; i < NumGeneration; i++)
208  {
209  std::cout << std::endl;
210  std::cout << "GA is processing at generation " << i << std::endl;
211 
212  TrackDataFile(i);
213  // replicate the individuals
214  replicate();
215 
216  //Mutating some offspring
217  for (int nn = PopulationSize; nn < 2*PopulationSize; nn++)
218  {
219  double mut;
220 
221  // mutate the parameters
222  // calculate the mutatated parameter, only some of offspring mutated
223  //if (floor(10*rand()/RAND_MAX) >= 2)
224 
225  if (pRand->getRandomCC() >= 0.15)
226  {
227  for (int j = 0; j < NumParameter; j++)
228  {
229  if (floor(10*rand() / RAND_MAX) > 5)
230  {
231  //mut = individual[nn][j] + floor(1000000*rand()/RAND_MAX)/1000000;
232  // if(i<5000) mut = individual[nn][j] + pRand->getRandomCC()*(*Maximum[j]-*Minimum[j])/(i*i);
233 
234  // if(i<NumGeneration/2) mut = individual[nn][j]*(1 + pRand->getRandomCC());
235  // mut = individual[nn][j]+(*Maximum[j]-individual[nn][j])*(1-pow((pRand->getRandomCC()),pow((1-i/NumGeneration),2)));
236  // if(i>=NumGeneration/2) mut = individual[nn][j] *(1+ (1-pow((pRand->getRandomCC()),pow((1-i/NumGeneration),2))));
237 
238  // if(i>=5000) mut = individual[nn][j] + pRand->getRandomCC()*(*Maximum[j]-*Minimum[j])/sqrt(i);
239  // if(i>=5000) mut = individual[nn][j] + pRand->getRandomCC()*(*Maximum[j]-*Minimum[j])/sqrt(i);
240  // if(i<5000) mut = individual[nn][j] *(1+ (1-pow((pRand->getRandomCC()),pow((1-i/NumGeneration),2))));
241 
242  //if(i < NumGeneration/16) mut = individual[nn][j]*(1 + pRand->getRandomCC());
243  // if(i < NumGeneration/16) mut = individual[nn][j]+ pRand->getRandomCC();
244  //else mut = individual[nn][j]*(1 + pRand->getRandomCC()*(pow((a1+1-b1),i/NumGeneration)-(1-b1)));
245  // else mut = individual[nn][j]+ pRand->getRandomCC()*(pow((a1+1-b1),i/NumGeneration)-(1-b1));
246 
247  if (i < NumGeneration*0.5) mut = individual[nn][j] * (1 + pRand->getRandomCC());
248  else
249  {
250  if (i < NumGeneration*0.6) mut = individual[nn][j] * (1 + 0.5 * pRand->getRandomCC());
251  else
252  {
253  if (i < NumGeneration*0.7) mut = individual[nn][j] * (1 + 0.25 * pRand->getRandomCC());
254  else
255  {
256  if (i < NumGeneration*0.8) mut = individual[nn][j] * (1 + 0.1 * pRand->getRandomCC());
257  else
258  {
259  if (i < NumGeneration*0.9) mut = individual[nn][j] * (1 + 0.01 * pRand->getRandomCC());
260  else
261  {
262  if (i < NumGeneration*0.95) mut = individual[nn][j] * (1 + 0.001 * pRand->getRandomCC());
263  else mut = individual[nn][j] * (1 + 0.0001 * pRand->getRandomCC());
264  }
265  }
266  }
267  }
268  }
269  }
270  else
271  {
272  //mut = individual[nn][j] - floor(1000000*rand()/RAND_MAX)/1000000;
273  // if(i<5000) mut = individual[nn][j] - pRand->getRandomCC()*(*Maximum[j]-*Minimum[j])/(i*i);
274  // if(i>=5000) mut = individual[nn][j] - pRand->getRandomCC()*(*Maximum[j]-*Minimum[j])/sqrt(i);
275 
276  // mut = individual[nn][j]+(individual[nn][j]-*Minimum[j])*(1-pow((pRand->getRandomCC()),pow((1-i/NumGeneration),2)));
277  // if(i< NumGeneration/2) mut = individual[nn][j]*(1 - pRand->getRandomCC());
278  // if(i>= NumGeneration/2) mut = individual[nn][j] *(1- (1-pow((pRand->getRandomCC()),pow((1-i/NumGeneration),2))));
279 
280  //if(i< NumGeneration/16) mut = individual[nn][j]*(1 - pRand->getRandomCC());
281  // if(i< NumGeneration/16) mut = individual[nn][j] - pRand->getRandomCC();
282  //else mut = individual[nn][j]*(1 - pRand->getRandomCC()*(pow((a1+1-b1),i/NumGeneration)-(1-b1)));
283  // else mut = individual[nn][j] - pRand->getRandomCC()*(pow((a1+1-b1),i/NumGeneration)-(1-b1));
284 
285  if (i < NumGeneration*0.5) mut = individual[nn][j] * (1 - pRand->getRandomCC());
286  else
287  {
288  if (i < NumGeneration*0.6) mut = individual[nn][j] * (1 - 0.5 * pRand->getRandomCC());
289  else
290  {
291  if (i < NumGeneration*0.7) mut = individual[nn][j] * (1 - 0.25 * pRand->getRandomCC());
292  else
293  {
294  if (i < NumGeneration*0.8) mut = individual[nn][j] * (1 - 0.1 * pRand->getRandomCC());
295  else
296  {
297  if (i < NumGeneration*0.9) mut = individual[nn][j] * (1 - 0.01 * pRand->getRandomCC());
298  else
299  {
300  if (i < NumGeneration*0.95) mut = individual[nn][j] * (1 - 0.001 * pRand->getRandomCC());
301  else mut = individual[nn][j] * (1 - 0.0001 * pRand->getRandomCC());
302  }
303  }
304  }
305  }
306  }
307 
308  // mut = individual[nn][j] * (1 + floor(1000000*rand()/RAND_MAX)/1000000);
309  //mut = individual[nn][j] * (1 + pRand->getRandomCC());
310  }
311 
312  // check boundary and force it to be within the bounds
313  if (mut <= *Minimum[j]) mut = *Minimum[j] + std::numeric_limits< C_FLOAT64 >::epsilon();
314  else
315  {
316  if (mut < *Minimum[j]) mut = *Minimum[j];
317  }
318 
319  if (mut >= *Maximum[j]) mut = *Maximum[j] - std::numeric_limits< C_FLOAT64 >::epsilon();
320  else
321  {
322  if (mut > *Maximum[j]) mut = *Maximum[j];
323  }
324 
325  // store it
326  individual[nn][j] = mut;
327  }
328  }
329 
330  // evaluate the fitness
331  for (int kk = 0; kk < NumParameter; kk++)
332  {
333  Parameter[kk] = individual[nn][kk];
334  }
335 
336  CandidateValue[nn] = mOptProblem->calculate();
337  }
338 
339  // select approprate individuals as new population
340  // select(1);
341  //select(2);
342  if ((i % 2) == 0) select(2);
343  else select(3);
344 
345  // if((i%2)==0) select(3);
346  // else select(4);
347 
348  // Here introduce SA to optimise some top indiviudals
349  //if(i>=0.5*NumGeneration)
350  if ((i >= 1) && (i % 1000 == 0))
351  {
352  for (int optKK = 0; optKK < 6; optKK++)
353  {
354  optimise(optKK);
355  }
356  }
357 
358  // get the index of the fittest
360 
361  if (Get_BestFoundSoFar_candidate() != current_best_value)
362  {
363  last_update = i;
364  current_best_value = Get_BestFoundSoFar_candidate();
365  }
366 
367  if (u10) u10--;
368 
369  if (u30) u30--;
370 
371  if (u50) u50--;
372 
373  if ((u50 == 0) && (i - last_update > 50))
374  //if((u50==0)&&(i-last_update>150))
375  {
376  for (int mm = PopulationSize / 2; mm < PopulationSize; mm++)
377  {
378  for (int jj = 0; jj < NumParameter; jj++)
379  {
380  try
381  {
382  // determine if linear or log scale
383  linear = FALSE; la = 1.0;
384 
385  if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE;
386  else
387  {
388  la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon()));
389 
390  if (la < 1.8) linear = TRUE;
391  }
392 
393  // set it to a random value within the interval
394  if (linear)
395  individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]);
396  else
397  individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC());
398  }
399  catch (int)
400  {
401  individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj];
402  }
403  }
404 
405  try
406  {
407  // calculate its fitness
408  for (int kk = 0; kk < NumParameter; kk++) {Parameter[kk] = individual[mm][kk];}
409 
410  CandidateValue[mm] = mOptProblem->calculate();
411  }
412  catch (int)
413  {
415  }
416  }
417 
418  //end external for loop
420  u50 = 50; u30 = 30; u10 = 10;
421  //u50=150; u30=100; u10=50;
422  }
423  else
424  {
425  if ((u30 == 0) && (i - last_update > 30))
426  //if((u30==0)&&(i-last_update>100))
427  {
428  for (int mm = (int)floor(PopulationSize * 0.7); mm < PopulationSize; mm++)
429  {
430  for (int jj = 0; jj < NumParameter; jj++)
431  {
432  try
433  {
434  // determine if linear or log scale
435  linear = FALSE; la = 1.0;
436 
437  if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE;
438  else
439  {
440  la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon()));
441 
442  if (la < 1.8) linear = TRUE;
443  }
444 
445  // set it to a random value within the interval
446  if (linear)
447  individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]);
448  else
449  individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC());
450  }
451  catch (int)
452  {
453  individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj];
454  }
455  }
456 
457  try
458  {
459  // calculate its fitness
460  for (int kk = 0; kk < NumParameter; kk++) {Parameter[kk] = individual[mm][kk];}
461 
462  CandidateValue[mm] = mOptProblem->calculate();
463  }
464  catch (int)
465  {
467  }
468  }
469 
470  //end external for loop
472  u30 = 30; u10 = 10;
473  //u30=100; u10=50;
474  }
475  else
476  {
477  if ((u10 == 0) && (i - last_update > 10))
478  //if((u10==0)&&(i-last_update>50))
479  {
480  for (int mm = (int) floor(PopulationSize * 0.9); mm < PopulationSize; mm++)
481  {
482  for (int jj = 0; jj < NumParameter; jj++)
483  {
484  try
485  {
486  // determine if linear or log scale
487  linear = FALSE; la = 1.0;
488 
489  if ((*Maximum[jj] <= 0.0) || (*Minimum[jj] < 0.0)) linear = TRUE;
490  else
491  {
492  la = log10(*Maximum[jj]) - log10(std::min(*Minimum[jj], std::numeric_limits< C_FLOAT64 >::epsilon()));
493 
494  if (la < 1.8) linear = TRUE;
495  }
496 
497  // set it to a random value within the interval
498  if (linear)
499  individual[mm][jj] = *Minimum[jj] + pRand->getRandomCC() * (*Maximum[jj] - *Minimum[jj]);
500  else
501  individual[mm][jj] = *Minimum[jj] * pow(10.0, la * pRand->getRandomCC());
502  }
503  catch (int)
504  {
505  individual[mm][jj] = (*Maximum[jj] - *Minimum[jj]) * 0.5 + *Minimum[jj];
506  }
507  }
508 
509  try
510  {
511  // calculate its fitness
512  for (int kk = 0; kk < NumParameter; kk++) {Parameter[kk] = individual[mm][kk];}
513 
514  CandidateValue[mm] = mOptProblem->calculate();
515  }
516  catch (int)
517  {
519  }
520  }
521 
522  //end external for loop
524  u10 = 10;
525  //u10=50;
526  }
527  }
528  }
529  } // end iteration of generations
530 
531  for (int kk = 0; kk < NumParameter; kk++)
532  {
533  Parameter[kk] = individual[BestFoundSoFar][kk];
534  }
535 
536  //set the BestFoundSoFar function value
537  mOptProblem->setSolutionValue(Get_BestFoundSoFar_candidate());
538 
539  //store the combination of the BestFoundSoFar parameter values found so far
540  mOptProblem->getSolutionVariables() = Parameter;
541 
542  //free memory space
543  delete individual;
544  delete CrossPoint;
545  delete WinScore;
546  delete CandidateValue;
547 
548  std::cout << std::endl;
549  std::cout << "GA has successfully done!" << std::endl;
550 
551  return 0;
552 }
double * CandidateValue
virtual void select(int method)
qreal linear(qreal a, qreal b, qreal t)
virtual int fittest(void)
#define C_INT32
Definition: copasi.h:90
double ** individual
virtual bool optimise()
virtual void replicate(void)
double Get_BestFoundSoFar_candidate()
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
virtual C_FLOAT64 getRandomCC()
Definition: CRandom.cpp:235
#define FALSE
const Value & getValue() const
#define TRUE
CType * array()
Definition: CVector.h:139
#define min(a, b)
Definition: f2c.h:175
virtual void TrackDataFile(int i)
#define max(a, b)
Definition: f2c.h:176
C_INT32 COptMethodHGASA::optimise ( int  index)
virtual

Definition at line 838 of file COptMethodHGASA.cpp.

References BESTFOUNDSOFAR, C_INT32, CandidateValue, CRandom::createGenerator(), exchange(), FALSE, CRandom::getRandomCC(), CCopasiParameter::getValue(), individual, Maximum, Minimum, NumDirection, NumParameter, pdelete, PI, and TRUE.

839 {
840  //C_INT32 NumSignificantPoint, NumTempChange, NumIteration = (C_INT32) getValue("SimulatedAnnealing.Iterations");
841  C_INT32 NumSignificantPoint, NumTempChange, NumIteration = 1000;
842  C_INT32 j, NumParameter = mOptProblem->getVariableSize();
843 
844  //variable settings neccessary for SA
845  CVector<double> candparameter(NumParameter); //one-dimentional array of candidate value for parameters
846  double candFuncValue; // candidate value of objective function
847 
848  CVector<double> thisparameter(NumParameter); //current parameters
849  double thisFuncValue; //current value of the objective function
850 
851  CVector <double> newparameter(NumParameter); //new parameter value
852  double newFuncValue; //function value of new point
853 
854  CVector <double> step(NumParameter); //array of maximum steps for each parameter
855  CVector <int> NumStepAccep(NumParameter); //number of steps accepted
856 
857  double TempDecreaseRate = 0.85; //temperature decrease rate
858  double BoltzmannConstant = 1.0; //Boltzmann constant
859 
860  double InitialTemp = 1.0; //initial temperature
861  double t; //current temperature
862 
863  double ConvgCriterion = 0.01; // convergence criterion or program termination criterion
864  double ChangeValue, EnergyDifference;
865 
867  bool ready;
868 
869  /* Create a random number generator */
871  Type = (CRandom::Type)(C_INT32) getValue("HybridGASA.RandomGenerator.Type");
872  unsigned C_INT32 Seed;
873  Seed = (unsigned C_INT32) getValue("HybridGASA.RandomGenerator.Seed");
874  CRandom * pRandSA = CRandom::createGenerator(Type, Seed);
875 
876  assert(pRandSA);
877 
878  const double ** Minimum = mOptProblem->getParameterMin().array();
879  const double ** Maximum = mOptProblem->getParameterMax().array();
880 
881  CVector< C_FLOAT64 > & SAParameter = mOptProblem->getCalculateVariables();
882 
883  //double * Parameter;
884 
885  //dump_datafile_init()
886 
887  //inital temperature
888  t = InitialTemp;
889 
890  //inital point
891  NumSignificantPoint = 0;
892 
893  /*
894  //generate initial parameters randomly
895  for (j = 0; j < NumParameter; j++)
896  {
897  linear = false;
898  la = 1.0;
899 
900  if (*Minimum[j] == 0.0) *Minimum[j] = std::numeric_limits< C_FLOAT64 >::epsilon();
901 
902  if ((*Maximum[j] <= 0.0) || (*Minimum[j] <= 0.0)) linear = true;
903 
904  else
905  {
906  la = log10(*Maximum[j]) - log10(*Minimum[j]);
907  if (la < 1.8) linear = true;
908  }
909 
910  if (linear)
911  Parameter[j] =
912  *Minimum[j] + pRandSA->getRandomCC() * (*Maximum[j] - *Minimum[j]);
913  else
914  Parameter[j] = *Minimum[j] * pow(10.0, la * pRandSA->getRandomCC());
915  } // Initialization ends
916  */
917 
918  for (j = 0; j < NumParameter; j++)
919  {
920  SAParameter[j] = individual[index][j];
921  }
922 
923  for (int kk = 0; kk < NumParameter; kk++)
924  candparameter[kk] = thisparameter[kk] = newparameter[kk] = SAParameter[kk];
925 
926  // calculate the function fitness value
927  double FitnessValue = mOptProblem->calculate();
928  thisFuncValue = candFuncValue = FitnessValue;
929 
930  //Remember them
931  for (int mm = 0; mm < BESTFOUNDSOFAR; mm++) fk[mm] = thisFuncValue;
932 
933  //initial step sizes
934  for (int jj = 0; jj < NumParameter; jj++)
935  {
936  NumStepAccep[jj] = 0;
937  step[jj] = fabs(candparameter[jj]);
938  }
939 
940  //no temperature reductions yet
941  NumTempChange = 0;
942 
943  do
944  {
945  for (int ff = 0; ff < NumIteration; ff++) //step adjustments
946  {
947  std::cout << "New individual begins ......" << index << std::endl;
948  std::cout << "New iteration begins ......" << std::endl;
949  std::cout << "Current Temperature: " << t << std::endl;
950  std::cout << "Number of Temperature Change: " << NumTempChange << std::endl;
951 
952  for (j = 0; j < NumDirection; j++) // adjustment in all directions
953  {
954  for (int hh = 0; hh < NumParameter; hh++)
955  {
956  // ChangeValue=tan(2*PI*rand()/RAND_MAX)*(t/pow(pow(2,2.0)+t*t,(NumParameter+1)/2.0));
957  ChangeValue = tan(2 * PI * pRandSA->getRandomCC()) * (t / pow(pow(2, 2.0) + t * t, (NumParameter + 1) / 2.0));
958  newparameter[hh] = thisparameter[hh] + step[hh] * ChangeValue;
959 
960  if (newparameter[hh] < *Minimum[hh]) newparameter[hh] = *Minimum[hh] + pRandSA->getRandomCC() * (*Maximum[hh] - *Minimum[hh]);
961 
962  if (newparameter[hh] > *Maximum[hh]) newparameter[hh] = *Minimum[hh] + pRandSA->getRandomCC() * (*Maximum[hh] - *Minimum[hh]);
963 
964  for (int exchange = 0; exchange < NumParameter; exchange++)
965  {
966  SAParameter[exchange] = newparameter[exchange];
967  }
968 
969  // calculate the function value
970  double FitnessValue = mOptProblem->calculate();
971  newFuncValue = FitnessValue;
972 
973  //Calculate the energy difference
974  EnergyDifference = newFuncValue - thisFuncValue;
975 
976  //keep newparameter if energy is reduced
977  if (newFuncValue <= thisFuncValue)
978  {
979  for (int exchange = 0; exchange < NumParameter; exchange++)
980  {
981  thisparameter[exchange] = newparameter[exchange];
982  }
983 
984  thisFuncValue = newFuncValue;
985 
986  NumSignificantPoint++; // a new point counted
987  NumStepAccep[hh]++; //a new point in this coordinate counted
988 
989  if (thisFuncValue < candFuncValue)
990  {
991  for (int aa = 0; aa < NumParameter; aa++)
992  individual[index][aa] = SAParameter[aa] = candparameter[aa] = thisparameter[aa];
993 
994  CandidateValue[index] = candFuncValue = thisFuncValue;
995 
996  if (!mOptProblem->checkFunctionalConstraints())
997  continue;
998 
999  //set the best function value
1000  //mOptProblem->setBestValue(candFuncValue);
1001  //std::cout << "the Best value (" << NumSignificantPoint << "): " << candFuncValue << std::endl;
1002 
1003  //store the combination of the best parameter values found so far at current temperature
1004  //mOptProblem->getBestParameter() = *mParameters;
1005 
1006  //std::cout << "the Best Parameters: (";
1007  //for (int kk = 0; kk < mOptProblem->getParameterNum(); kk++)
1008  // std::cout << mOptProblem->getParameter(kk) << ", ";
1009 
1010  // std::cout << ")" << std::endl;
1011  }
1012  }
1013  else
1014  {
1015  //keep newparameter with probability, if newFuncValue is increased
1016  double Probability = exp(-(newFuncValue - thisFuncValue) / (BoltzmannConstant * t));
1017 
1018  if (Probability > pRandSA->getRandomCC())
1019  {
1020  //Keep the new point
1021  for (int exchange = 0; exchange < NumParameter; exchange++)
1022  {
1023  thisparameter[exchange] = newparameter[exchange];
1024  }
1025 
1026  thisFuncValue = newFuncValue;
1027  NumSignificantPoint++; // a new point counted
1028  NumStepAccep[hh]++; //a new point in this coordinate counted
1029  }
1030  }
1031  }
1032  }
1033 
1034  //update the step sizes
1035  for (int nn = 0; nn < NumParameter; nn++)
1036  {
1037  double StepAdjustment = (double) NumStepAccep[nn] / (double)NumDirection;
1038 
1039  if (StepAdjustment > 0.6) step[nn] *= 1 + 5 * (StepAdjustment - 0.6);
1040 
1041  if (StepAdjustment < 0.4) step[nn] /= 1 + 5 * (0.4 - StepAdjustment);
1042 
1043  NumStepAccep[nn] = 0;
1044  }
1045  }
1046 
1047  //update the temperature
1048  t *= TempDecreaseRate;
1049  NumTempChange++;
1050 
1051  // if this is the first cycle then ignore the convergence test
1052  if (NumTempChange == 1) ready = FALSE;
1053  else
1054  {
1055  ready = TRUE;
1056 
1057  //check if there is not much change for termination criterion since last BESTFOUNDSOFAR times
1058  for (int ii = 0; ii < BESTFOUNDSOFAR; ii++)
1059  if (fabs(fk[ii] - thisFuncValue) > ConvgCriterion)
1060  {
1061  ready = FALSE;
1062  break;
1063  }
1064 
1065  if (!ready)
1066  {
1067  for (int aa = 0; aa < BESTFOUNDSOFAR - 1; aa++)
1068  fk[aa] = fk[aa + 1];
1069 
1070  fk[BESTFOUNDSOFAR - 1] = thisFuncValue;
1071  }
1072  else
1073 
1074  //check the termination criterion of not much larger than last optimal
1075  if (fabs(thisFuncValue - candFuncValue) > ConvgCriterion)ready = FALSE;
1076  }
1077 
1078  if (!ready)
1079  {
1080  NumSignificantPoint++;
1081 
1082  for (int kk = 0; kk < NumParameter; kk++)
1083  thisparameter[kk] = candparameter[kk];
1084 
1085  thisFuncValue = candFuncValue;
1086  }
1087  }
1088  while (!ready); // do-while loop ends
1089 
1090  pdelete(pRandSA);
1091  return 0;
1092 }
virtual void exchange(int o, int d)
double * CandidateValue
#define pdelete(p)
Definition: copasi.h:215
#define C_INT32
Definition: copasi.h:90
double ** individual
static CRandom * createGenerator(CRandom::Type type=CRandom::mt19937, unsigned C_INT32 seed=0)
Definition: CRandom.cpp:49
virtual C_FLOAT64 getRandomCC()
Definition: CRandom.cpp:235
#define FALSE
const Value & getValue() const
#define NumDirection
#define PI
#define BESTFOUNDSOFAR
#define TRUE
void COptMethodHGASA::replicate ( void  )
virtual

Definition at line 701 of file COptMethodHGASA.cpp.

References copy(), crossover(), and PopulationSize.

Referenced by optimise().

702 {
703  int i, parent1, parent2;
704 
705  // reproduce in consecutive pairs
706  for (i = 0; i < PopulationSize / 2; i++)
707  {
708  parent1 = (int)fabs(floor(PopulationSize * rand() / RAND_MAX));
709  parent2 = (int)fabs(floor(PopulationSize * rand() / RAND_MAX));
710  crossover(parent1, parent2, PopulationSize + i*2, PopulationSize + i*2 + 1);
711  }
712 
713  // check if there is one left over and just copy it
714  if (PopulationSize % 2 > 0) copy(PopulationSize - 1, 2*PopulationSize - 1);
715 }
virtual void copy(int o, int d)
virtual void crossover(int p1, int p2, int c1, int c2)
void COptMethodHGASA::select ( int  method)
virtual

Definition at line 718 of file COptMethodHGASA.cpp.

References CandidateValue, exchange(), PopulationSize, swap(), and WinScore.

Referenced by optimise().

719 {
720  int i, j, TournamentSize, RandomRival;
721  int RandomIndividual;
722  //srand(time(NULL)); rand();
723 
724  switch (SelectionStrategy)
725  {
726  case 1: // parent-offspring competition
727 
728  for (i = PopulationSize; i < 2*PopulationSize; i++)
729  {
730  // if offspring is fitter keep it
731  for (j = 0; j < PopulationSize; j++)
732  {
733  if (CandidateValue[i] < CandidateValue[j]) exchange(i, j);
734  }
735  }
736 
737  break;
738  case 2: // tournament competition
739  // compete with 20% of the population
740  TournamentSize = PopulationSize / 5;
741 
742  // but at least one
743  if (TournamentSize < 1) TournamentSize = 1;
744 
745  // parents and offspring are all in competition
746  for (i = 0; i < 2*PopulationSize; i++)
747  {
748  WinScore[i] = 0;
749 
750  for (j = 0; j < TournamentSize; j++)
751  {
752  // get random rival
753  RandomRival = (int)fabs(floor((PopulationSize * 2 - 1) * rand() / RAND_MAX));
754 
755  if (CandidateValue[i] <= CandidateValue[RandomRival]) WinScore[i]++;
756  }
757  }
758 
759  // selection of top PopulationSize winners
760  for (i = 0; i < PopulationSize; i++)
761  {
762  for (j = i + 1; j < 2*PopulationSize; j++)
763  {if (WinScore[i] < WinScore[j]) swap(i, j);}
764  }
765 
766  break;
767 
768  // ranking strategy without proportionate-fitness
769  case 3:
770 
771  for (i = 0; i < PopulationSize; i++)
772  {
773  for (j = i + 1; j < 2*PopulationSize; j++)
774  {
775  if (CandidateValue[i] > CandidateValue[j]) exchange(i, j);
776  }
777  }
778 
779  break;
780  // Randomly select P individuals from population of 2P
781  case 4:
782 
783  for (i = 0; i < PopulationSize; i++)
784  {
785  RandomIndividual = (int)fabs(floor((PopulationSize * 2 - 1) * rand() / RAND_MAX));
786  exchange(i, RandomIndividual);
787  }
788 
789  break;
790  }
791 }
virtual void exchange(int o, int d)
double * CandidateValue
virtual void swap(int o, int d)
void COptMethodHGASA::Set_BestFoundSoFar ( int  num)
inline

Definition at line 191 of file COptMethodHGASA.h.

References BestFoundSoFar.

192 {
193  BestFoundSoFar = num;
194 }
void COptMethodHGASA::Set_CandidateValue ( int  i,
double  num 
)
inline

Definition at line 186 of file COptMethodHGASA.h.

References CandidateValue.

187 {
188  CandidateValue[i] = num;
189 }
double * CandidateValue
void COptMethodHGASA::Set_individual ( int  i,
int  j,
double  num 
)
inline

Definition at line 181 of file COptMethodHGASA.h.

References individual.

182 {
183  individual[i][j] = num;
184 }
double ** individual
void COptMethodHGASA::Set_Maximum ( double  num)
inline

Definition at line 176 of file COptMethodHGASA.h.

References Maximum.

177 {
178  Maximum = num;
179 }
void COptMethodHGASA::Set_Minimum ( double  num)
inline

Definition at line 172 of file COptMethodHGASA.h.

References Minimum.

173 {
174  Minimum = num;
175 }
void COptMethodHGASA::Set_murvar ( double  num)
void COptMethodHGASA::Set_NumGeneration ( int  num)
inline

Definition at line 160 of file COptMethodHGASA.h.

References NumGeneration.

161 {
162  NumGeneration = num;
163 }
void COptMethodHGASA::Set_NumParameter ( int  num)
inline

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.

Definition at line 150 of file COptMethodHGASA.h.

References NumParameter.

151 {
152  NumParameter = num;
153 }
void COptMethodHGASA::Set_PopulationSize ( int  num)
inline

Definition at line 155 of file COptMethodHGASA.h.

References PopulationSize.

156 {
157  PopulationSize = num;
158 }
void COptMethodHGASA::swap ( int  o,
int  d 
)
virtual

Definition at line 598 of file COptMethodHGASA.cpp.

References CandidateValue, individual, NumParameter, and WinScore.

Referenced by select().

599 {
600  int i;
601  double tmp;
602 
603  for (i = 0; i < NumParameter; i++)
604  {
605  tmp = individual[d][i];
606  individual[d][i] = individual[o][i];
607  individual[o][i] = tmp;
608  }
609 
610  tmp = CandidateValue[d];
612  CandidateValue[o] = tmp;
613 
614  i = WinScore[d];
615  WinScore[d] = WinScore[o];
616  WinScore[o] = i;
617 }
double * CandidateValue
double ** individual
void COptMethodHGASA::TrackDataFile ( int  i)
virtual

Definition at line 813 of file COptMethodHGASA.cpp.

References BestFoundSoFar, CandidateValue, individual, and NumParameter.

Referenced by optimise().

814 {
815  std::ofstream finalout("debugopt.dat", std::ios::app);
816 
817  if (!finalout)
818  {
819  std::cout << "debugopt.dat cannot be opened!" << std::endl;
820  exit(1);
821  }
822 
823  finalout << "#" << i << "\t" << std::setprecision(8) << CandidateValue[BestFoundSoFar] << std::endl;
824 
825  for (int j = 0; j < NumParameter; j++)
826  {
827  finalout << std::setprecision(8) << individual[BestFoundSoFar][j] << "\t";
828  }
829 
830  finalout << std::endl;
831  finalout << std::endl;
832 
833  finalout.close();
834 }
double * CandidateValue
double ** individual

Friends And Related Function Documentation

Member Data Documentation

int COptMethodHGASA::BestFoundSoFar
private
double* COptMethodHGASA::CandidateValue
private
int* COptMethodHGASA::CrossPoint
private

Definition at line 62 of file COptMethodHGASA.h.

Referenced by crossover(), and optimise().

double** COptMethodHGASA::individual
private

Definition at line 59 of file COptMethodHGASA.h.

Referenced by copy(), crossover(), exchange(), optimise(), Set_individual(), swap(), and TrackDataFile().

double COptMethodHGASA::Maximum
private

Definition at line 54 of file COptMethodHGASA.h.

Referenced by optimise(), and Set_Maximum().

double COptMethodHGASA::Minimum
private

Definition at line 54 of file COptMethodHGASA.h.

Referenced by optimise(), and Set_Minimum().

int COptMethodHGASA::NumCrossPoint
private

Definition at line 52 of file COptMethodHGASA.h.

Referenced by crossover().

int COptMethodHGASA::NumGeneration
private

Definition at line 50 of file COptMethodHGASA.h.

Referenced by Get_NumGeneration(), optimise(), and Set_NumGeneration().

int COptMethodHGASA::NumParameter
private
int COptMethodHGASA::PopulationSize
private
int* COptMethodHGASA::WinScore
private

Definition at line 63 of file COptMethodHGASA.h.

Referenced by optimise(), select(), and swap().


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