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

📄 nsga2r.c

📁 nsga2具体算法代码,用来多目标遗传算法的学习和研究
💻 C
📖 第 1 页 / 共 2 页
字号:
/* NSGA-II routine (implementation of the 'main' function) */# include <stdio.h># include <stdlib.h># include <math.h># include <unistd.h># include "global.h"# include "rand.h"int nreal;int nbin;int nobj;int ncon;int popsize;double pcross_real;double pcross_bin;double pmut_real;double pmut_bin;double eta_c;double eta_m;int ngen;int nbinmut;int nrealmut;int nbincross;int nrealcross;int *nbits;double *min_realvar;double *max_realvar;double *min_binvar;double *max_binvar;int bitlength;int choice;int obj1;int obj2;int obj3;int angle1;int angle2;int main (int argc, char **argv){    int i;    FILE *fpt1;    FILE *fpt2;    FILE *fpt3;    FILE *fpt4;    FILE *fpt5;    FILE *gp;    population *parent_pop;    population *child_pop;    population *mixed_pop;    if (argc<2)    {        printf("\n Usage ./nsga2r random_seed \n");        exit(1);    }    seed = (double)atof(argv[1]);    if (seed<=0.0 || seed>=1.0)    {        printf("\n Entered seed value is wrong, seed value must be in (0,1) \n");        exit(1);    }    fpt1 = fopen("initial_pop.out","w");    fpt2 = fopen("final_pop.out","w");    fpt3 = fopen("best_pop.out","w");    fpt4 = fopen("all_pop.out","w");    fpt5 = fopen("params.out","w");    fprintf(fpt1,"# This file contains the data of initial population\n");    fprintf(fpt2,"# This file contains the data of final population\n");    fprintf(fpt3,"# This file contains the data of final feasible population (if found)\n");    fprintf(fpt4,"# This file contains the data of all generations\n");    fprintf(fpt5,"# This file contains information about inputs as read by the program\n");    printf("\n Enter the problem relevant and algorithm relevant parameters ... ");    printf("\n Enter the population size (a multiple of 4) : ");    scanf("%d",&popsize);    if (popsize<4 || (popsize%4)!= 0)    {        printf("\n population size read is : %d",popsize);        printf("\n Wrong population size entered, hence exiting \n");        exit (1);    }    printf("\n Enter the number of generations : ");    scanf("%d",&ngen);    if (ngen<1)    {        printf("\n number of generations read is : %d",ngen);        printf("\n Wrong nuber of generations entered, hence exiting \n");        exit (1);    }    printf("\n Enter the number of objectives : ");    scanf("%d",&nobj);    if (nobj<1)    {        printf("\n number of objectives entered is : %d",nobj);        printf("\n Wrong number of objectives entered, hence exiting \n");        exit (1);    }    printf("\n Enter the number of constraints : ");    scanf("%d",&ncon);    if (ncon<0)    {        printf("\n number of constraints entered is : %d",ncon);        printf("\n Wrong number of constraints enetered, hence exiting \n");        exit (1);    }    printf("\n Enter the number of real variables : ");    scanf("%d",&nreal);    if (nreal<0)    {        printf("\n number of real variables entered is : %d",nreal);        printf("\n Wrong number of variables entered, hence exiting \n");        exit (1);    }    if (nreal != 0)    {        min_realvar = (double *)malloc(nreal*sizeof(double));        max_realvar = (double *)malloc(nreal*sizeof(double));        for (i=0; i<nreal; i++)        {            printf ("\n Enter the lower limit of real variable %d : ",i+1);            scanf ("%lf",&min_realvar[i]);            printf ("\n Enter the upper limit of real variable %d : ",i+1);            scanf ("%lf",&max_realvar[i]);            if (max_realvar[i] <= min_realvar[i])            {                printf("\n Wrong limits entered for the min and max bounds of real variable, hence exiting \n");                exit(1);            }        }        printf ("\n Enter the probability of crossover of real variable (0.6-1.0) : ");        scanf ("%lf",&pcross_real);        if (pcross_real<0.0 || pcross_real>1.0)        {            printf("\n Probability of crossover entered is : %e",pcross_real);            printf("\n Entered value of probability of crossover of real variables is out of bounds, hence exiting \n");            exit (1);        }        printf ("\n Enter the probablity of mutation of real variables (1/nreal) : ");        scanf ("%lf",&pmut_real);        if (pmut_real<0.0 || pmut_real>1.0)        {            printf("\n Probability of mutation entered is : %e",pmut_real);            printf("\n Entered value of probability of mutation of real variables is out of bounds, hence exiting \n");            exit (1);        }        printf ("\n Enter the value of distribution index for crossover (5-20): ");        scanf ("%lf",&eta_c);        if (eta_c<=0)        {            printf("\n The value entered is : %e",eta_c);            printf("\n Wrong value of distribution index for crossover entered, hence exiting \n");            exit (1);        }        printf ("\n Enter the value of distribution index for mutation (5-50): ");        scanf ("%lf",&eta_m);        if (eta_m<=0)        {            printf("\n The value entered is : %e",eta_m);            printf("\n Wrong value of distribution index for mutation entered, hence exiting \n");            exit (1);        }    }    printf("\n Enter the number of binary variables : ");    scanf("%d",&nbin);    if (nbin<0)    {        printf ("\n number of binary variables entered is : %d",nbin);        printf ("\n Wrong number of binary variables entered, hence exiting \n");        exit(1);    }    if (nbin != 0)    {        nbits = (int *)malloc(nbin*sizeof(int));        min_binvar = (double *)malloc(nbin*sizeof(double));        max_binvar = (double *)malloc(nbin*sizeof(double));        for (i=0; i<nbin; i++)        {            printf ("\n Enter the number of bits for binary variable %d : ",i+1);            scanf ("%d",&nbits[i]);            if (nbits[i] < 1)            {                printf("\n Wrong number of bits for binary variable entered, hence exiting");                exit(1);            }            printf ("\n Enter the lower limit of binary variable %d : ",i+1);            scanf ("%lf",&min_binvar[i]);            printf ("\n Enter the upper limit of binary variable %d : ",i+1);            scanf ("%lf",&max_binvar[i]);            if (max_binvar[i] <= min_binvar[i])            {                printf("\n Wrong limits entered for the min and max bounds of binary variable entered, hence exiting \n");                exit(1);            }        }        printf ("\n Enter the probability of crossover of binary variable (0.6-1.0): ");        scanf ("%lf",&pcross_bin);        if (pcross_bin<0.0 || pcross_bin>1.0)        {            printf("\n Probability of crossover entered is : %e",pcross_bin);            printf("\n Entered value of probability of crossover of binary variables is out of bounds, hence exiting \n");            exit (1);        }        printf ("\n Enter the probability of mutation of binary variables (1/nbits): ");        scanf ("%lf",&pmut_bin);        if (pmut_bin<0.0 || pmut_bin>1.0)        {            printf("\n Probability of mutation entered is : %e",pmut_bin);            printf("\n Entered value of probability  of mutation of binary variables is out of bounds, hence exiting \n");            exit (1);        }    }    if (nreal==0 && nbin==0)    {        printf("\n Number of real as well as binary variables, both are zero, hence exiting \n");        exit(1);    }    choice=0;    printf("\n Do you want to use gnuplot to display the results realtime (0 for NO) (1 for yes) : ");    scanf("%d",&choice);    if (choice!=0 && choice!=1)    {        printf("\n Entered the wrong choice, hence exiting, choice entered was %d\n",choice);        exit(1);    }    if (choice==1)    {        gp = popen(GNUPLOT_COMMAND,"w");        if (gp==NULL)

⌨️ 快捷键说明

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