Evolutionary Algorithm for Adaptive Phase Estimation  1.0.2
mpi_optalg.h
1 #ifndef MPIOPTALG_H
2 #define MPIOPTALG_H
3 #include <mpi.h>
4 #include <cstdlib>
5 #include <stdexcept>
6 #include <cmath>
7 
8 #include "problem.h"
9 #include "candidate.h"
10 #include "rng.h"
11 #include "aux_functions.h"
12 
17 class OptAlg {
18  public:
19  OptAlg() {};
20  OptAlg(Problem *problem_ptr, Rng *gaussian_rng, int pop_size): gaussian_rng(gaussian_rng) {
21  prob = problem_ptr; //Store the pointer to the problem object.
22  num = prob->num; //Store the number of variables.
23  num_fit = prob->num_fit; //Store the number of fitness values.
24 
25  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
26  MPI_Comm_size(MPI_COMM_WORLD, &nb_proc);
27  total_pop = pop_size; //Store the population size.
28  }
29  virtual ~OptAlg() {
30  delete[] pop;
31  };
32 
33  /*List of functions which are specific to the optimization functions*/
34  virtual void put_to_best() {};
36  virtual void combination() {};
38  virtual void selection() {};
39  virtual void write_param(double *param_array) {};
40  virtual void read_param(double *param_array) {};
41  virtual void fit_to_global() {};
42  virtual void find_global() {};
45  Problem* prob;
46 
47  //functions and variables that are not algorithm specific
48  void Init_population(int psize);
49  void Init_previous(double prev_dev, double new_dev, int psize, double *prev_soln);
51  void Cont_fitness(int p);
52  void Best_fitness(int p);
53  void update_popfit();
55  void set_success(int iter, bool goal);
56  bool check_success(int t, double *current_fitarray, double *memory_fitarray, int data_size, double t_goal, bool *mem_ptype, int *numvar, int N_cut, double *memory_forT);
58  //Selecting solution
59  double Final_select(double *fit, double *solution, double *fitarray);
60  double avg_Final_select(double* solution, int repeat, double *soln_fit, double *fitarray);
62  void dev_gen(double *dev_array, double prev_dev, double new_dev, int cut_off);
64  int find_max(double *fit);
67  bool success;
68  bool policy_type;
70  protected:
71  int num;
72  int num_fit;
73  Rng *gaussian_rng;
74  int pop_size;
75  int T;
76  int t;
78  bool goal;
80  int total_pop;
81  int my_rank;
82  int nb_proc;
84  };
85 
88 class DE : public OptAlg {
89  public:
90  DE() {};
91  DE(Problem *problem_ptr, Rng *gaussian_rng, int pop_size): OptAlg(problem_ptr, gaussian_rng, pop_size), F(0.1), Cr(0.6) {};
92  ~DE() {};
93 
94  void put_to_best();
95  void combination();
96  void selection();
97  void fit_to_global();
98  void find_global() {};
99  void write_param(double *param_array);
100  void read_param(double *param_array);
101 
102  private:
103  double F;
104  double Cr;
106  void family_gen(int* fam, int p, int fam_size);
109  };
110 
113 class PSO : public OptAlg {
114  public:
115  PSO() {};
116  PSO(Problem *problem_ptr, Rng *gaussian_rng, int pop_size): OptAlg(problem_ptr, gaussian_rng, pop_size), w(0.8), phi1(0.6), phi2(1.0), v_max(0.2) {};
117  ~PSO() {};
118 
119  void put_to_best();
120  void combination();
121  void selection();
122  void write_param(double *param_array);
123  void read_param(double *param_array);
124  void fit_to_global() {};
125  void find_global();
126 
127  private:
128  inline void find_index(int *prev, int *forw, int p);
129  inline int find_fitness(int prev, double prev_fit, int forw, double forw_fit, int p, double fit);
130  double w;
131  double phi1;
132  double phi2;
133  double v_max;
134  };
135 
136 #endif // OPTALG_H
void write_param(double *param_array)
Definition: mpi_pso.cpp:10
void combination()
Definition: mpi_pso.cpp:178
void Best_fitness(int p)
Definition: mpi_optalg.cpp:80
void read_param(double *param_array)
Definition: mpi_pso.cpp:3
void Cont_fitness(int p)
Definition: mpi_optalg.cpp:64
virtual void write_param(double *param_array)
Definition: mpi_optalg.h:39
void find_global()
Definition: mpi_optalg.h:98
double Final_select(double *fit, double *solution, double *fitarray)
Definition: mpi_optalg.cpp:98
void fit_to_global()
Definition: mpi_optalg.h:124
virtual void read_param(double *param_array)
Definition: mpi_optalg.h:40
DE class contains the functions that are specific to this particular optimization algorithm.
Definition: mpi_optalg.h:88
void read_param(double *param_array)
Definition: mpi_de.cpp:14
int num
Definition: problem.h:39
Problem class contains the prototype of the functions in the optimization problem that OptAlg class n...
Definition: problem.h:9
bool success
Definition: mpi_optalg.h:67
int total_pop
Definition: mpi_optalg.h:80
void update_popfit()
Definition: mpi_optalg.cpp:89
int t
Definition: mpi_optalg.h:76
int find_max(double *fit)
Definition: mpi_optalg.cpp:288
int my_rank
Definition: mpi_optalg.h:81
PSO class contains the functions that are specific to this particular optimization algorithm.
Definition: mpi_optalg.h:113
void write_param(double *param_array)
Definition: mpi_de.cpp:9
void put_to_best()
Definition: mpi_pso.cpp:145
bool goal
Definition: mpi_optalg.h:78
Candidate class contains arrays to be used by optimization algorithm to store data of a solution cand...
Definition: candidate.h:11
int nb_proc
Definition: mpi_optalg.h:82
double avg_Final_select(double *solution, int repeat, double *soln_fit, double *fitarray)
Definition: mpi_optalg.cpp:190
Candidate * pop
Definition: mpi_optalg.h:77
int pop_size
Definition: mpi_optalg.h:74
int num_fit
Definition: mpi_optalg.h:72
void combination()
Definition: mpi_de.cpp:36
void Init_population(int psize)
Definition: mpi_optalg.cpp:5
virtual void find_global()
Definition: mpi_optalg.h:42
Problem * prob
Definition: mpi_optalg.h:42
void selection()
Definition: mpi_de.cpp:25
Rng * gaussian_rng
Definition: mpi_optalg.h:73
int num
Definition: mpi_optalg.h:71
virtual void combination()
Definition: mpi_optalg.h:36
virtual void selection()
Definition: mpi_optalg.h:38
bool policy_type
Definition: mpi_optalg.h:68
void Init_previous(double prev_dev, double new_dev, int psize, double *prev_soln)
Definition: mpi_optalg.cpp:31
bool check_success(int t, double *current_fitarray, double *memory_fitarray, int data_size, double t_goal, bool *mem_ptype, int *numvar, int N_cut, double *memory_forT)
Definition: mpi_optalg.cpp:316
void dev_gen(double *dev_array, double prev_dev, double new_dev, int cut_off)
Definition: mpi_optalg.cpp:341
int T
Definition: mpi_optalg.h:75
void find_global()
Definition: mpi_pso.cpp:17
void set_success(int iter, bool goal)
Definition: mpi_optalg.cpp:307
OptAlg class contains the functions that can be used to contruct an optimization algorithm in the mai...
Definition: mpi_optalg.h:17
void selection()
Definition: mpi_pso.cpp:165
void fit_to_global()
Definition: mpi_de.cpp:19
virtual void fit_to_global()
Definition: mpi_optalg.h:41