Xmipp  v3.23.11-Nereus
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
DESolver Class Referenceabstract

#include <numerical_tools.h>

Inheritance diagram for DESolver:
Inheritance graph
[legend]

Public Member Functions

 DESolver (int dim, int popSize)
 Empty constructor. More...
 
 DESolver (const DESolver &)=delete
 
DESolveroperator= (const DESolver &)=delete
 
virtual ~DESolver (void)
 Destructor. More...
 
void Setup (double min[], double max[], int deStrategy, double diffScale, double crossoverProb)
 Setup() must be called before solve to set min, max, strategy etc. More...
 
virtual bool Solve (int maxGenerations)
 
virtual double EnergyFunction (double testSolution[], bool &bAtSolution)=0
 
int Dimension () const
 Return dimension. More...
 
int Population () const
 Return population. More...
 
double Energy () const
 Call these functions after Solve() to get results. More...
 
double * Solution (void)
 Return best solution. More...
 
int Generations () const
 Return the number of generations. More...
 

Protected Member Functions

void SelectSamples (int candidate, int *r1, int *r2=nullptr, int *r3=nullptr, int *r4=nullptr, int *r5=nullptr)
 

Protected Attributes

int nDim
 
int nPop
 
int generations
 
int strategy
 
StrategyFunction calcTrialSolution
 
double scale
 
double probability
 
double trialEnergy
 
double bestEnergy
 
double * trialSolution
 
double * bestSolution
 
double * popEnergy
 
double * population
 

Detailed Description

Differential evolution class. For further information visit: http://www.icsi.berkeley.edu/~storn/code.html Example of use:

// Declaration of the optimizer
class AlignmentSolver : public DESolver {
public:
AlignmentSolver(int dim,int pop) : DESolver(dim,pop), count(0) {;}
double EnergyFunction(double trial[],bool &bAtSolution) {
double result=wrapper_fitness_individual(trial);
if (count++ % (5*nPop) == 0)
std::cout << "Evaluations= " << count/nPop
<< " energy= " << Energy()
<< " axisRot= " << Solution()[0]
<< " axisTilt= " << Solution()[1]
<< " axisShiftX= " << Solution()[2]
<< " axisShiftY= " << Solution()[3]
<< " axisShiftZ= " << Solution()[4]
<< std::endl;
bAtSolution=false;
return(result);
}
private:
int count;
};
// Optimizer use:
AlignmentSolver solver(length,length*Npop);
solver.Setup(MULTIDIM_ARRAY(min_allowed),
MULTIDIM_ARRAY(max_allowed), stBest2Bin, 0.5, 0.8);
solver.Solve(NGenerations);
double current_energy=solver.Energy();
double* bestSolution=solver.Solution();

Definition at line 312 of file numerical_tools.h.

Constructor & Destructor Documentation

◆ DESolver() [1/2]

DESolver::DESolver ( int  dim,
int  popSize 
)

Empty constructor.

Definition at line 387 of file numerical_tools.cpp.

387  :
388 nDim(dim), nPop(popSize),
390 scale(0.7), probability(0.5), bestEnergy(0.0),
392 popEnergy(0), population(0)
393 {
394  trialSolution = new double[nDim];
395  bestSolution = new double[nDim];
396  popEnergy = new double[nPop];
397  population = new double[nPop * nDim];
398 
400 }
double probability
double * population
double * bestSolution
double * trialSolution
double bestEnergy
double scale
double * popEnergy
constexpr int stRand1Exp
unsigned int randomize_random_generator()

◆ DESolver() [2/2]

DESolver::DESolver ( const DESolver )
delete

◆ ~DESolver()

DESolver::~DESolver ( void  )
virtual

Destructor.

Definition at line 402 of file numerical_tools.cpp.

403 {
404  if (trialSolution)
405  delete [] trialSolution;
406  if (bestSolution)
407  delete [] bestSolution;
408  if (popEnergy)
409  delete [] popEnergy;
410  if (population)
411  delete [] population;
412 
414  return;
415 }
double * population
double * bestSolution
double * trialSolution
double * popEnergy

Member Function Documentation

◆ Dimension()

int DESolver::Dimension ( ) const
inline

Return dimension.

Definition at line 344 of file numerical_tools.h.

345  {
346  return nDim;
347  }

◆ Energy()

double DESolver::Energy ( ) const
inline

Call these functions after Solve() to get results.

Definition at line 356 of file numerical_tools.h.

357  {
358  return bestEnergy;
359  }
double bestEnergy

◆ EnergyFunction()

virtual double DESolver::EnergyFunction ( double  testSolution[],
bool &  bAtSolution 
)
pure virtual

EnergyFunction must be overridden for problem to solve testSolution[] is nDim array for a candidate solution setting bAtSolution = true indicates solution is found and Solve() immediately returns true.

Implemented in WedgeSolver, and EulerSolver.

◆ Generations()

int DESolver::Generations ( ) const
inline

Return the number of generations.

Definition at line 368 of file numerical_tools.h.

369  {
370  return generations;
371  }

◆ operator=()

DESolver& DESolver::operator= ( const DESolver )
delete

◆ Population()

int DESolver::Population ( ) const
inline

Return population.

Definition at line 350 of file numerical_tools.h.

351  {
352  return nPop;
353  }

◆ SelectSamples()

void DESolver::SelectSamples ( int  candidate,
int *  r1,
int *  r2 = nullptr,
int *  r3 = nullptr,
int *  r4 = nullptr,
int *  r5 = nullptr 
)
protected

Definition at line 749 of file numerical_tools.cpp.

751 {
752  if (r1)
753  {
754  do
755  {
756  *r1 = (int)rnd_unif(0.0, (double)nPop);
757  }
758  while (*r1 == candidate);
759  } else
760  return;
761 
762  if (r2)
763  {
764  do
765  {
766  *r2 = (int)rnd_unif(0.0, (double)nPop);
767  }
768  while ((*r2 == candidate) || (*r2 == *r1));
769  } else
770  return;
771 
772  if (r3)
773  {
774  do
775  {
776  *r3 = (int)rnd_unif(0.0, (double)nPop);
777  }
778  while ((*r3 == candidate) || (*r3 == *r2) || (*r3 == *r1));
779  } else
780  return;
781 
782  if (r4)
783  {
784  do
785  {
786  *r4 = (int)rnd_unif(0.0, (double)nPop);
787  }
788  while ((*r4 == candidate) || (*r4 == *r3) || (*r4 == *r2) || (*r4 == *r1));
789  } else
790  return;
791 
792  if (r5)
793  {
794  do
795  {
796  *r5 = (int)rnd_unif(0.0, (double)nPop);
797  }
798  while ((*r5 == candidate) || (*r5 == *r4) || (*r5 == *r3)
799  || (*r5 == *r2) || (*r5 == *r1));
800  }
801 }
double rnd_unif()
float r3
float r2
float r1

◆ Setup()

void DESolver::Setup ( double  min[],
double  max[],
int  deStrategy,
double  diffScale,
double  crossoverProb 
)

Setup() must be called before solve to set min, max, strategy etc.

Definition at line 417 of file numerical_tools.cpp.

419 {
420  int i;
421  int j;
422 
423  strategy = deStrategy;
424  scale = diffScale;
425  probability = crossoverProb;
426  bestEnergy = 1.0E20;
427 
428  for (i = 0; i < nPop; i++)
429  {
430  for (j = 0; j < nDim; j++)
431  {
432  population[i*nDim+j] = rnd_unif(min[j], max[j]);
433  //Element(population, i, j) = rnd_unif(min[j], max[j]);
434  }
435 
436  popEnergy[i] = 1.0E20;
437  }
438 
439  for (i = 0; i < nDim; i++)
440  bestSolution[i] = (min[i] + max[i]) / 2.0;
441 
442  switch (strategy)
443  {
444  case stBest1Exp:
445  calcTrialSolution = &DESolver::Best1Exp;
446  break;
447 
448  case stRand1Exp:
449  calcTrialSolution = &DESolver::Rand1Exp;
450  break;
451 
452  case stRandToBest1Exp:
453  calcTrialSolution = &DESolver::RandToBest1Exp;
454  break;
455 
456  case stBest2Exp:
457  calcTrialSolution = &DESolver::Best2Exp;
458  break;
459 
460  case stRand2Exp:
461  calcTrialSolution = &DESolver::Rand2Exp;
462  break;
463 
464  case stBest1Bin:
465  calcTrialSolution = &DESolver::Best1Bin;
466  break;
467 
468  case stRand1Bin:
469  calcTrialSolution = &DESolver::Rand1Bin;
470  break;
471 
472  case stRandToBest1Bin:
473  calcTrialSolution = &DESolver::RandToBest1Bin;
474  break;
475 
476  case stBest2Bin:
477  calcTrialSolution = &DESolver::Best2Bin;
478  break;
479 
480  case stRand2Bin:
481  calcTrialSolution = &DESolver::Rand2Bin;
482  break;
483  }
484 
485  return;
486 }
double probability
void min(Image< double > &op1, const Image< double > &op2)
double * population
double * bestSolution
constexpr int stBest1Exp
constexpr int stBest2Exp
constexpr int stBest1Bin
constexpr int stRand2Bin
StrategyFunction calcTrialSolution
#define i
double rnd_unif()
constexpr int stBest2Bin
void max(Image< double > &op1, const Image< double > &op2)
constexpr int stRand2Exp
double bestEnergy
#define j
constexpr int stRand1Bin
constexpr int stRandToBest1Exp
double scale
constexpr int stRandToBest1Bin
double * popEnergy
constexpr int stRand1Exp

◆ Solution()

double* DESolver::Solution ( void  )
inline

Return best solution.

Definition at line 362 of file numerical_tools.h.

363  {
364  return bestSolution;
365  }
double * bestSolution

◆ Solve()

bool DESolver::Solve ( int  maxGenerations)
virtual

Solve() returns true if EnergyFunction() returns true. Otherwise it runs maxGenerations generations and returns false.

Definition at line 488 of file numerical_tools.cpp.

489 {
490  bool bAtSolution = false;
491  int generation;
492 
493  for (generation = 0;(generation < maxGenerations) && !bAtSolution;generation++)
494  for (int candidate = 0; candidate < nPop; candidate++)
495  {
496  (this->*calcTrialSolution)(candidate);
497  trialEnergy = EnergyFunction(trialSolution, bAtSolution);
498 
499  if (trialEnergy < popEnergy[candidate])
500  {
501  // New low for this candidate
502  popEnergy[candidate] = trialEnergy;
504 
505  // Check if all-time low
506  if (trialEnergy < bestEnergy)
507  {
510  }
511  }
512  }
513 
514  generations = generation;
515  return bAtSolution;
516 }
double * population
#define RowVector(a, b)
double * bestSolution
StrategyFunction calcTrialSolution
virtual double EnergyFunction(double testSolution[], bool &bAtSolution)=0
double trialEnergy
double * trialSolution
double bestEnergy
double * popEnergy
#define CopyVector(a, b)

Member Data Documentation

◆ bestEnergy

double DESolver::bestEnergy
protected

Definition at line 391 of file numerical_tools.h.

◆ bestSolution

double* DESolver::bestSolution
protected

Definition at line 394 of file numerical_tools.h.

◆ calcTrialSolution

StrategyFunction DESolver::calcTrialSolution
protected

Definition at line 386 of file numerical_tools.h.

◆ generations

int DESolver::generations
protected

Definition at line 383 of file numerical_tools.h.

◆ nDim

int DESolver::nDim
protected

Definition at line 381 of file numerical_tools.h.

◆ nPop

int DESolver::nPop
protected

Definition at line 382 of file numerical_tools.h.

◆ popEnergy

double* DESolver::popEnergy
protected

Definition at line 395 of file numerical_tools.h.

◆ population

double* DESolver::population
protected

Definition at line 396 of file numerical_tools.h.

◆ probability

double DESolver::probability
protected

Definition at line 388 of file numerical_tools.h.

◆ scale

double DESolver::scale
protected

Definition at line 387 of file numerical_tools.h.

◆ strategy

int DESolver::strategy
protected

Definition at line 385 of file numerical_tools.h.

◆ trialEnergy

double DESolver::trialEnergy
protected

Definition at line 390 of file numerical_tools.h.

◆ trialSolution

double* DESolver::trialSolution
protected

Definition at line 393 of file numerical_tools.h.


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