Skip to content

Commit 10965e4

Browse files
committed
COptMethodEP parallelized
1 parent a44de70 commit 10965e4

3 files changed

Lines changed: 36 additions & 52 deletions

File tree

copasi/optimization/COptMethod.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ const CVector< C_FLOAT64 > * COptMethod::getCurrentParameters() const
217217
// virtual evaluate the fitness of one individual
218218
C_FLOAT64 COptMethod::evaluate(const EvaluationPolicyFlag & policy)
219219
{
220+
if (!mProceed)
221+
return std::numeric_limits< C_FLOAT64 >::infinity();
222+
220223
// We do not need to check whether the parametric constraints are fulfilled
221224
// since the parameters are created within the bounds.
222225
COptProblem *& pOptProblem = mProblemContext.active();

copasi/optimization/COptMethodEP.cpp

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@
3737

3838
COptMethodEP::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

5756
COptMethodEP::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

6767
COptMethodEP::~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

292292
bool 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);

copasi/optimization/COptMethodEP.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ private :
150150
*/
151151
CVector< size_t > mLosses;
152152

153-
/**
154-
* The value of the last evaluation.
155-
*/
156-
C_FLOAT64 mEvaluationValue;
157-
158153
/**
159154
* if no improvement was made after # stalled generations
160155
* stop

0 commit comments

Comments
 (0)