3737
3838COptMethodEP::COptMethodEP (const CDataContainer * pParent,
3939 const CTaskEnum::Method & methodType,
40- const CTaskEnum::Task & taskType):
41- COptPopulationMethod(pParent, methodType, taskType, false ),
42- mBestIndex(C_INVALID_INDEX),
43- mLosses(0 ),
44- mEvaluationValue(std::numeric_limits< C_FLOAT64 >::max()),
45- mStopAfterStalledGenerations(0 ),
46- mVariance(0 )
40+ const CTaskEnum::Task & taskType)
41+ : COptPopulationMethod(pParent, methodType, taskType, true )
42+ , mBestIndex(C_INVALID_INDEX)
43+ , mLosses(0 )
44+ , mStopAfterStalledGenerations(0 )
45+ , mVariance(0 )
4746{
4847 assertParameter (" Number of Generations" , CCopasiParameter::Type::UINT, (unsigned C_INT32) 200 );
4948 assertParameter (" Population Size" , CCopasiParameter::Type::UINT, (unsigned C_INT32) 20 );
@@ -56,13 +55,14 @@ COptMethodEP::COptMethodEP(const CDataContainer * pParent,
5655
5756COptMethodEP::COptMethodEP (const COptMethodEP & src,
5857 const CDataContainer * pParent)
59- : COptPopulationMethod(src, pParent),
60- mBestIndex(C_INVALID_INDEX),
61- mLosses(0 ),
62- mEvaluationValue(std::numeric_limits< C_FLOAT64 >::max()),
63- mStopAfterStalledGenerations(0 ),
64- mVariance(0 )
65- {initObjects ();}
58+ : COptPopulationMethod(src, pParent)
59+ , mBestIndex(C_INVALID_INDEX)
60+ , mLosses(0 )
61+ , mStopAfterStalledGenerations(0 )
62+ , mVariance(0 )
63+ {
64+ initObjects ();
65+ }
6666
6767COptMethodEP::~COptMethodEP ()
6868{
@@ -88,18 +88,16 @@ bool COptMethodEP::optimise()
8888 )
8989 );
9090
91- bool Continue = true ;
92-
9391 // Initialize the population
94- Continue = creation ();
92+ creation ();
9593
9694 // get the index of the fittest
9795 mBestIndex = fittest ();
9896
9997 if (mBestIndex != C_INVALID_INDEX)
10098 setSolution (mValues [mBestIndex ], *mIndividuals [mBestIndex ], true );
10199
102- if (!Continue )
100+ if (!proceed () )
103101 {
104102 if (mLogVerbosity > 0 )
105103 mMethodLog .enterLogEntry (COptLogEntry (" Algorithm was terminated by user after initial population creation." ));
@@ -116,18 +114,18 @@ bool COptMethodEP::optimise()
116114
117115 // iterate over Generations
118116 for (mCurrentGeneration = 2 ;
119- mCurrentGeneration <= mGenerations && Continue ;
117+ mCurrentGeneration <= mGenerations && proceed () ;
120118 mCurrentGeneration ++, Stalled++)
121119 {
122120
123121 if (mStopAfterStalledGenerations != 0 && Stalled > mStopAfterStalledGenerations )
124122 break ;
125123
126124 // replicate the individuals
127- Continue = replicate ();
125+ replicate ();
128126
129127 // select the most fit
130- Continue = select ();
128+ select ();
131129
132130 // get the index of the fittest
133131 mBestIndex = fittest ();
@@ -139,8 +137,9 @@ bool COptMethodEP::optimise()
139137 setSolution (mValues [mBestIndex ], *mIndividuals [mBestIndex ], true );
140138 }
141139
142- if (mProcessReport )
143- Continue = mProcessReport .progressItem (mhGenerations);
140+ if (mProcessReport
141+ && !mProcessReport .progressItem (mhGenerations))
142+ signalStop ();
144143
145144 // use a different output channel. It will later get a proper enum name
146145 mpParentTask->output (COutputInterface::MONITORING);
@@ -241,7 +240,8 @@ bool COptMethodEP::creation()
241240 // set the other half to random values within the boundaries
242241 // for(i=half; i<mVariableSizeze; i++)
243242
244- for (i = 1 ; i < mPopulationSize && proceed (); i++)
243+ #pragma omp parallel for schedule(runtime)
244+ for (i = 1 ; i < mPopulationSize ; i++)
245245 {
246246 createIndividual (i, COptItem::CheckPolicyFlag::All);
247247 mValues [i] = evaluate ({EvaluationPolicy::Constraints});
@@ -291,21 +291,10 @@ bool COptMethodEP::select()
291291
292292bool COptMethodEP::swap (size_t from, size_t to)
293293{
294- CVector< C_FLOAT64 > * pTmp = mIndividuals [to];
295- mIndividuals [to] = mIndividuals [from];
296- mIndividuals [from] = pTmp;
297-
298- pTmp = mVariance [to];
299- mVariance [to] = mVariance [from];
300- mVariance [from] = pTmp;
301-
302- C_FLOAT64 dTmp = mValues [to];
303- mValues [to] = mValues [from];
304- mValues [from] = dTmp;
305-
306- size_t iTmp = mLosses [to];
307- mLosses [to] = mLosses [from];
308- mLosses [from] = iTmp;
294+ std::swap (mIndividuals [to], mIndividuals [from]);
295+ std::swap (mVariance [to], mVariance [from]);
296+ std::swap (mValues [to], mValues [from]);
297+ std::swap (mLosses [to], mLosses [from]);
309298
310299 return true ;
311300}
@@ -332,16 +321,13 @@ bool COptMethodEP::replicate()
332321 bool Continue = true ;
333322
334323 // iterate over parents
335- for (i = 0 ; i < mPopulationSize && Continue; i++)
324+ #pragma omp parallel for schedule(runtime)
325+ for (i = 0 ; i < mPopulationSize ; i++)
336326 {
337327 // replicate them
338- for (j = 0 ; j < mVariableSize ; j++)
339- {
340- (*mIndividuals [mPopulationSize + i])[j] = (*mIndividuals [i])[j];
341- (*mVariance [mPopulationSize + i])[j] = (*mVariance [i])[j];
342- }
343-
344- mValues [mPopulationSize + i] = mValues [i];
328+ *mIndividuals [mPopulationSize + i] = *mIndividuals [i];
329+ *mVariance [mPopulationSize + i] = *mVariance [i];
330+ mValues [mPopulationSize + i] = std::numeric_limits< C_FLOAT64 >::infinity ();
345331
346332 // possibly mutate the offspring
347333 Continue = mutate (mPopulationSize + i);
0 commit comments