/*************************************************************************
* This is an implementation of MOPSO-CD,a multiobjective particle swarm *
* optimization algorithm using crowding distance *
* *
* For details please see: *
* C.R. Raquel and P.C. Naval, Jr., An Effective Use of Crowding *
* Distance in Multiobjective Particle Swarm Optimization. *
* In Proc. of Genetic and Evolutionary Computation Conference *
* (GECCO 2005), Washington DC, June 2005. *
* *
* E-mail address : cvmig@engg.upd.edu.ph *
* Version : mopsocd05b *
* Last updated : Fri Feb 17, 2006 *
* *
* Random Generator Source code has been taken from Random Library found *
* at http://www.swin.edu.au/astronomy/pbourke/software/random/ *
* *
* Permission to use MOPSO-CD codes is hereby granted for academic and *
* research purposes only. Commercial usage of these codes is prohibited *
* without prior knowledge of the authors. In no way will the authors *
* be held responsible for any possible faulty operation of *
* software/hardware arising from the use of these codes. *
*************************************************************************/
/*************************************************
FUNCTION CODE OPTIMIZATION OBJECTIVE VARIABLES
Kita 100 maximize (1) 2 2
Kursawe 200 minimize (0) 2 3
Deb 300 minimize (0) 2 2
DTLZ6 500 minimize (0) 3 22
<- put yours here and in test-fun.h
/* Initialize your function in initialize_pop() */
/* Put your function in evaluate() */
/* Put your constraints, if any, in check_constraints(...) */
/* See also maintain_particles() routine */
/**************************************************/
#define function 500 /* set functions code */
#define popsize 100 /* set number of particles in the population */
#define maxgen 1000 /* set maximum number of generations */
#define optimization 0 /* set optimization type, 0 for min, 1 for max */
#define archive_size 500 /* set capacity of archive */
#define maxfun 3 /* set maximum number of objective functions */
#define maxvar 22 /* set maximum number of variables */
#define verbose 1 /* verbosity level 0,1 */
#define printevery 10 /* how frequently should output be generated */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include "randomlib.h"
double PI;
void initialize_rand(void);
void initialize_pop(void);
void initialize_vel(void);
void evaluate(void);
double kita_f1(unsigned int);
double kita_f2(unsigned int);
double kursawe_f1(unsigned int);
double kursawe_f2(unsigned int);
double deb_f1(unsigned int);
double deb_f2(unsigned int);
void store_pbests(void);
void insert_nondom(void);
void delete_particle(unsigned int);
unsigned int compare_particles(unsigned int, unsigned int);
unsigned int check_constraints(double *);
void crowding(void);
void qsortFitness(unsigned int, unsigned int, unsigned int);
void qsortCrowd(unsigned int, unsigned int);
void compute_distance(unsigned int);
void mutate(unsigned int);
void get_ranges(double *, double *);
void maintain_particles(void);
void compute_velocity(void);
void update_archive(void);
unsigned int check_nondom(unsigned int);
void update_pbests(void);
void save_results(char *);
double DTLZ6_f1(unsigned int);
double DTLZ6_f2(unsigned int);
double DTLZ6_f3(unsigned int);
double archiveVar[archive_size][maxvar]; /* variable values of particles in the archive */
double archiveFit[archive_size][maxfun]; /* fitness values of particles in the archive */
double popVar[popsize][maxvar]; /* variable values of particles in the population */
double popFit[popsize][maxfun]; /* fitness values of particles in the population */
double pbestsVar[popsize][maxvar]; /* personal bests of particles in the population */
double pbestsFit[popsize][maxfun]; /* personal bests of particles in the population */
double velocity[popsize][maxvar]; /* velocity of particles in the population */
double crowdDist[archive_size]; /* crowding distance values of particles in archive */
double pMut=0.5; /* probability of mutation */
unsigned int nondomCtr = 0; /* number of nondominated solutions in archive */
int main(int argc, char *argv[])
{
char archiveName[20];
unsigned int i, j, t;
clock_t startTime, endTime;
double duration, clocktime;
FILE *outfile,*plotfile;
PI = 4.0*atan(1.0);
sprintf(archiveName,"archive.out");
outfile = fopen("output.out","w");
plotfile = fopen("plot.out","w");
/* Initialize random number generator */
initialize_rand();
startTime = clock();
/* Initialize generation counter */
t = 0;
/* Initialize population with random values */
initialize_pop();
/* Initialize velocity */
initialize_vel();
/* Evaluate particles in population */
evaluate();
/* Store initial personal bests (both variable and fitness values) of particles */
store_pbests();
/* Insert nondominated particles in population into the archive */
insert_nondom();
/******* MAIN LOOP *********/
while(t <= maxgen){
clocktime = (clock() - startTime)/(double)CLOCKS_PER_SEC;
if(verbose > 0 && t%printevery==0 || t == maxgen) {
fprintf(stdout,"Generation %d Time: %.2f sec\n",t,clocktime);
fflush(stdout);
}
if(t%printevery==0 || t == maxgen) {
fprintf(outfile,"Generation %d Time: %.2f sec\n",t,clocktime);
}
/* Compute crowding distance of each particle in the archive */
/* Only when there are at least 3 particles in the archive */
if(nondomCtr > 2)
crowding();
/* Compute new velocity of each particle in the population */
compute_velocity();
/* Maintain particles in the population within the search space */
maintain_particles();
/* Mutate particles in the population */
if(t < maxgen * pMut)
mutate(t);
/* Evaluate particles in the population */
evaluate();
/* Insert new nondominated particles in pop into archive */
update_archive();
/* Update personal bests of particles in the population */
update_pbests();
/* Print Best So Far */
if(t%printevery==0 || t == maxgen) {
fprintf(outfile, "Size of Pareto Set: %d\n", nondomCtr);
for(i = 0; i < nondomCtr; i++) {
fprintf(outfile, "Function Values: ");
for(j = 0; j < maxfun; j++)
fprintf(outfile, "%.6f ", archiveFit[i][j]);
fprintf(outfile, "\n");
for(j = 0; j < maxvar; j++)
fprintf(outfile, "%.6f ", archiveVar[i][j]);
fprintf(outfile, "\n");
}
fprintf(outfile, "\n\n");
fflush(outfile);
}
if(t == maxgen) {
fprintf(plotfile, "# GNU Plot\n");
for(i = 0; i < nondomCtr; i++) {
for(j = 0; j < maxfun; j++)
fprintf(plotfile, "%.4f ", archiveFit[i][j]);
fprintf(plotfile, "\n");
}
fflush(plotfile);
}
/* Increment generation counter */
t++;
}
/* Write results to file */
save_results(archiveName);
/* Compute time duration */
评论30
最新资源