⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eps-dom.cpp

📁 epsMOEA
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*############################################################################****************************************************************************** * This is a Multi-Objective GA program.                                     * * This program is the implementation of the EPSILON Dominance Idea          * *     Refer : Towards a Quick Computation                                   * *             of Well Spread Pareto-Optimal Solution                        * *                - Deb, Mohan and Mishra (EMO-2003 Proceedings)             * *                  Also available from http://www.iitk.ac.in/kangal/pub.htm * *                   (KanGAL Report Number 2003002)                          * *                                                                           * *  (copyright) Kalyanmoy Deb                                                * ***************************************************************************** * * * This code is a generic one, for any number of objective functions and decision variables. * However, the code uses real-parameter variables only. * SBX and polynomial mutation (See Deb's (2001 Wiley) book for details * * The code is written for minimization of all objectives. For handling *  maximization problems, multiply the objective by -1. * * Here it is illstrated for the DTLZ - 1 Problem * (refer:Scalable multi-objective optimization test problems, *   - K. Deb and L. Thiele and M. Laumanns and E. Zitzler *     http://www.iitk.ac.in/kangal/pub.htm (Report No. 2001001) * * Constraint Handling is done by using a new constraint violation parameter (cv) * * Please refer to the Readme file for further details * * Please send any comments or information about any bug to *   deb@iitk.ac.in * ###############################################################################*/#include <iostream.h>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#include "random.h"#define INFINITY  1e7#define DELTA 1e-6// Number of decision variables#define N_of_x 7// MAX is the number of objective functions#define MAX 3//MAX_CONSTRAINT is the maximum number of constraints#define MAX_CONSTRAINT 0//MAX_ARCHIVE_SIZE is the maximum size possible (the upper bound) of the archive#define MAX_ARCHIVE_SIZE 1000// Define the EPSILON matrixdouble EPSILON[MAX] = { 0.5 / 24, 0.5 / 24, 0.5 / 10 };// any additional constants etc. are defined heredouble PI =  asin (1) *  2.0;/* declaring the global variables*/// Define the initial Population Size (to be kept constant throughout the algo.)     const int pop_size = 100;     int max_no_gen;// bool RIGID = 1;     double pxover;     double eeta, n_distribution_m, p_mutation_real;     int archive_size = 0;	//starting archive size = 0     double seed;     double infimumx[N_of_x];     double supremumx[N_of_x];// Class definitions for an individual     class individual     {     public:double xreal[N_of_x];       // Real Coded Decision Variable       double gxm;		// value of the g(x) function for the DTLZ-1 problem       double f[MAX];		// the objective functions       double cv;		// cv - parameter for constraint violation       double box[MAX];		// box vectors for the individual       double constraint[MAX_CONSTRAINT];	// defining the constraint values       double gx ()       {	 double gx_val;	 double sum = 0;	 for (int i = MAX - 1; i < N_of_x; i++)	   {	     sum +=	       (xreal[i] - 0.5) * (xreal[i] - 0.5) -	       cos (20 * PI * (xreal[i] - 0.5));	   }	 gx_val = 100 * ((N_of_x - (MAX - 1)) + sum);	// g(x) as needed for this problem	 return (gx_val);       }       // Function Definitions       double function1 ()       {	 double f11;	 f11 = 0.5 * xreal[0] * xreal[1] * (1 + gxm);	 return f11;       }              double function2 ()       {	 double f22;	 f22 = 0.5 * xreal[0] * (1 - xreal[1]) * (1 + gxm);	 return f22;       }              double function3 ()       {	 double f33;	 f33 = 0.5 * (1 - xreal[0]) * (1 + gxm);	 return f33;       }       /* Define your Constraints here  like the example shown below,          make sure you have also set your MAX_CONSTRAINT to the required number.          This part is commented as this problem is not constrained. The lines below          illustrate a possible set of constraints.          double constr1 ()          {          return (f[3] + (4.0 * f[1]) - 1.0);          }	            double constr2 ()          {          return (f[3] + (4.0 * f[2]) - 1.0);          }	            double constr3 ()          {          return ((2.0 * f[3]) + f[1] + f[2] - 1.0);          }       */                     // Initialize the individual       void init ()       {	 gxm = gx ();	 cv = 0;	 	 f[0] = function1 ();	 f[1] = function2 ();	 f[2] = function3 ();	 /* Uncomment this if Constraints are needed and add required number of constraint functions.	    constraint[0] = constr1 ();	    constraint[1] = constr2 ();	    constraint[2] = constr3 ();	 */	 for (int i = 0; i < MAX_CONSTRAINT; i++)	   {	     if (constraint[i] < 0.0)	       cv += (-1.0 * constraint[i]);	   }       }            };individual population[pop_size];individual archive[MAX_ARCHIVE_SIZE];individual newchild, newchild1, newchild2;// Header Files for the real-cross-over, real mutation and inputs#include "realmut.h"#include "realcross.h"#include "input.h"void box_func (individual * ind1);	// prototype of the box function// Create the random popvoidcreate_random_pop (){  double ran;  for (int i = 0; i < pop_size; i++)    {      for (int j = 0; j < N_of_x; j++)	{	  ran = randomperc ();	  population[i].xreal[j] =	    ran * supremumx[j] + (1.0 - ran) * infimumx[j];	}      population[i].init ();    }}// Strict Domination check function for population members indexed by m and n// returns 1 ( 0 ) meaning m dominates (does not dominate) n in minimization sense// only for population members _to_be_called_in_  create_archive()intstrict_dom_check (int m, int n){  int flag = 1;  for (int p = 0; p < MAX; p++)    {      if ((flag == 1) && (population[m].f[p] < population[n].f[p]))	flag = 1;      else	{	  flag = 0;	  break;	}    }  return (flag);}// Create the initial archive// Consists of the non-dominated (strict domination) members// of the populationvoidcreate_archive (){  int flag = 0;  for (int i = 0; i < pop_size; i++)    {      flag = 0;      for (int j = 0; j < pop_size; j++)	{	  if (strict_dom_check (j, i) == 1)	    flag = -1;	}      if (flag == 0)	{	  archive[archive_size] = population[i];	  archive_size++;	}    }}// Prints the population members, whenever calledvoidprint_pop (){  cout << "\n Printing real values of the function values\n";  for (int i = 0; i < pop_size; i++)    {      for (int j = 0; j < MAX; j++)	cout << population[i].f[j] << "\t";      cout << "\n";    }}// Prints the function values of the archive members, when calledvoidprint_archive (){  cout << "\n Printing the function values of the archive members\n";  for (int i = 0; i < archive_size; i++)    {      for (int j = 0; j < MAX; j++)	cout << archive[i].f[j] << "\t";      cout << "\n";    }}// Prints into files the population and archive members// Also prints the Box vectors and Decision Variables of the archive membersvoidprint_func_values (){  FILE *fpop, *farch, *fvar, *fbox;  fpop = fopen ("popltn.out", "w");  farch = fopen ("archive.out", "w");  fvar = fopen ("ar_variables.out", "w");  fbox = fopen ("box_vectors.out", "w");  cout << "\n\n The size of the archive is " << archive_size << "\n";  cout << " The final archive is in file archive.out\n\n";  for (int i = 0; i < pop_size; i++)    {      for (int j = 0; j < MAX; j++)	fprintf (fpop, "%f\t", population[i].f[j]);      fprintf (fpop, "\n");    }  for (int i = 0; i < archive_size; i++)    {      for (int j = 0; j < MAX; j++)	{	  fprintf (farch, "%f\t", archive[i].f[j]);	  fprintf (fbox, "%f\t", archive[i].box[j] * EPSILON[j]);	}      for (int j = 0; j < N_of_x; j++)	fprintf (fvar, "%f\t", archive[i].xreal[j]);      fprintf (farch, "\n");      fprintf (fbox, "\n");      fprintf (fvar, "\n");    }}// Main function starts herevoidmain (){  void generate_replace ();  void compete ();  int update ();  // returns 0 if not accepted and 1 if the child is accepted  int con_update ();  time_t start, end;  time (&start);  // Take the start time reading  int child_flag1 = 0, flg = 0, child_flag2 = 0;  input_param ();  // Input the necessary parameters  cout << "\n Enter the seed : ";  cin >> seed;  warmup_random (seed);  // set up the random no. generator  create_random_pop ();  create_archive ();  cout << "\n Enter the No of Generations Needed : ";  // No of function evaluations = max_no_gen * 2  cin >> max_no_gen;  cout << "\n Starting loop........";  for (int no_gen = 0; no_gen < max_no_gen; no_gen++)    {      generate_replace ();      cout << "\n GEN = " << no_gen + 1;      cout << "\n NO of members in Archive at present :" << archive_size <<	"\n";      newchild = newchild1;      child_flag1 = con_update ();      compete ();      newchild = newchild2;      child_flag2 = con_update ();      compete ();    }  /* printing the values of the functions */  print_func_values ();  time (&end);  cout << "\n\n Total Time taken == " << difftime (end,						   start) << " seconds.\n\n";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -