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

📄 nsga2r.c

📁 nsga2具体算法代码,用来多目标遗传算法的学习和研究
💻 C
📖 第 1 页 / 共 2 页
字号:
        {            printf("\n Could not open a pipe to gnuplot, check the definition of GNUPLOT_COMMAND in file global.h\n");            printf("\n Edit the string to suit your system configuration and rerun the program\n");            exit(1);        }        if (nobj==2)        {            printf("\n Enter the objective for X axis display : ");            scanf("%d",&obj1);            if (obj1<1 || obj1>nobj)            {                printf("\n Wrong value of X objective entered, value entered was %d\n",obj1);                exit(1);            }            printf("\n Enter the objective for Y axis display : ");            scanf("%d",&obj2);            if (obj2<1 || obj2>nobj)            {                printf("\n Wrong value of Y objective entered, value entered was %d\n",obj2);                exit(1);            }            obj3 = -1;        }        else        {            printf("\n #obj > 2, 2D display or a 3D display ?, enter 2 for 2D and 3 for 3D :");            scanf("%d",&choice);            if (choice!=2 && choice!=3)            {                printf("\n Entered the wrong choice, hence exiting, choice entered was %d\n",choice);                exit(1);            }            if (choice==2)            {                printf("\n Enter the objective for X axis display : ");                scanf("%d",&obj1);                if (obj1<1 || obj1>nobj)                {                    printf("\n Wrong value of X objective entered, value entered was %d\n",obj1);                    exit(1);                }                printf("\n Enter the objective for Y axis display : ");                scanf("%d",&obj2);                if (obj2<1 || obj2>nobj)                {                    printf("\n Wrong value of Y objective entered, value entered was %d\n",obj2);                    exit(1);                }                obj3 = -1;            }            else            {                printf("\n Enter the objective for X axis display : ");                scanf("%d",&obj1);                if (obj1<1 || obj1>nobj)                {                    printf("\n Wrong value of X objective entered, value entered was %d\n",obj1);                    exit(1);                }                printf("\n Enter the objective for Y axis display : ");                scanf("%d",&obj2);                if (obj2<1 || obj2>nobj)                {                    printf("\n Wrong value of Y objective entered, value entered was %d\n",obj2);                    exit(1);                }                printf("\n Enter the objective for Z axis display : ");                scanf("%d",&obj3);                if (obj3<1 || obj3>nobj)                {                    printf("\n Wrong value of Z objective entered, value entered was %d\n",obj3);                    exit(1);                }                printf("\n You have chosen 3D display, hence location of eye required \n");                printf("\n Enter the first angle (an integer in the range 0-180) (if not known, enter 60) :");                scanf("%d",&angle1);                if (angle1<0 || angle1>180)                {                    printf("\n Wrong value for first angle entered, hence exiting \n");                    exit(1);                }                printf("\n Enter the second angle (an integer in the range 0-360) (if not known, enter 30) :");                scanf("%d",&angle2);                if (angle2<0 || angle2>360)                {                    printf("\n Wrong value for second angle entered, hence exiting \n");                    exit(1);                }            }        }    }    printf("\n Input data successfully entered, now performing initialization \n");    fprintf(fpt5,"\n Population size = %d",popsize);    fprintf(fpt5,"\n Number of generations = %d",ngen);    fprintf(fpt5,"\n Number of objective functions = %d",nobj);    fprintf(fpt5,"\n Number of constraints = %d",ncon);    fprintf(fpt5,"\n Number of real variables = %d",nreal);    if (nreal!=0)    {        for (i=0; i<nreal; i++)        {            fprintf(fpt5,"\n Lower limit of real variable %d = %e",i+1,min_realvar[i]);            fprintf(fpt5,"\n Upper limit of real variable %d = %e",i+1,max_realvar[i]);        }        fprintf(fpt5,"\n Probability of crossover of real variable = %e",pcross_real);        fprintf(fpt5,"\n Probability of mutation of real variable = %e",pmut_real);        fprintf(fpt5,"\n Distribution index for crossover = %e",eta_c);        fprintf(fpt5,"\n Distribution index for mutation = %e",eta_m);    }    fprintf(fpt5,"\n Number of binary variables = %d",nbin);    if (nbin!=0)    {        for (i=0; i<nbin; i++)        {            fprintf(fpt5,"\n Number of bits for binary variable %d = %d",i+1,nbits[i]);            fprintf(fpt5,"\n Lower limit of binary variable %d = %e",i+1,min_binvar[i]);            fprintf(fpt5,"\n Upper limit of binary variable %d = %e",i+1,max_binvar[i]);        }        fprintf(fpt5,"\n Probability of crossover of binary variable = %e",pcross_bin);        fprintf(fpt5,"\n Probability of mutation of binary variable = %e",pmut_bin);    }    fprintf(fpt5,"\n Seed for random number generator = %e",seed);    bitlength = 0;    if (nbin!=0)    {        for (i=0; i<nbin; i++)        {            bitlength += nbits[i];        }    }    fprintf(fpt1,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation, rank, crowding_distance\n",nobj,ncon,nreal,bitlength);    fprintf(fpt2,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation, rank, crowding_distance\n",nobj,ncon,nreal,bitlength);    fprintf(fpt3,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation, rank, crowding_distance\n",nobj,ncon,nreal,bitlength);    fprintf(fpt4,"# of objectives = %d, # of constraints = %d, # of real_var = %d, # of bits of bin_var = %d, constr_violation, rank, crowding_distance\n",nobj,ncon,nreal,bitlength);    nbinmut = 0;    nrealmut = 0;    nbincross = 0;    nrealcross = 0;    parent_pop = (population *)malloc(sizeof(population));    child_pop = (population *)malloc(sizeof(population));    mixed_pop = (population *)malloc(sizeof(population));    allocate_memory_pop (parent_pop, popsize);    allocate_memory_pop (child_pop, popsize);    allocate_memory_pop (mixed_pop, 2*popsize);    randomize();    initialize_pop (parent_pop);    printf("\n Initialization done, now performing first generation");    decode_pop(parent_pop);    evaluate_pop (parent_pop);    assign_rank_and_crowding_distance (parent_pop);    report_pop (parent_pop, fpt1);    fprintf(fpt4,"# gen = 1\n");    report_pop(parent_pop,fpt4);    printf("\n gen = 1");    fflush(stdout);    if (choice!=0)    onthefly_display (parent_pop,gp,1);    fflush(fpt1);    fflush(fpt2);    fflush(fpt3);    fflush(fpt4);    fflush(fpt5);    sleep(1);    for (i=2; i<=ngen; i++)    {        selection (parent_pop, child_pop);        mutation_pop (child_pop);        decode_pop(child_pop);        evaluate_pop(child_pop);        merge (parent_pop, child_pop, mixed_pop);        fill_nondominated_sort (mixed_pop, parent_pop);        /* Comment following four lines if information for all        generations is not desired, it will speed up the execution */        fprintf(fpt4,"# gen = %d\n",i);        report_pop(parent_pop,fpt4);        fflush(fpt4);        if (choice!=0)    onthefly_display (parent_pop,gp,i);        printf("\n gen = %d",i);    }    printf("\n Generations finished, now reporting solutions");    report_pop(parent_pop,fpt2);    report_feasible(parent_pop,fpt3);    if (nreal!=0)    {        fprintf(fpt5,"\n Number of crossover of real variable = %d",nrealcross);        fprintf(fpt5,"\n Number of mutation of real variable = %d",nrealmut);    }    if (nbin!=0)    {        fprintf(fpt5,"\n Number of crossover of binary variable = %d",nbincross);        fprintf(fpt5,"\n Number of mutation of binary variable = %d",nbinmut);    }    fflush(stdout);    fflush(fpt1);    fflush(fpt2);    fflush(fpt3);    fflush(fpt4);    fflush(fpt5);    fclose(fpt1);    fclose(fpt2);    fclose(fpt3);    fclose(fpt4);    fclose(fpt5);    if (choice!=0)    {        pclose(gp);    }    if (nreal!=0)    {        free (min_realvar);        free (max_realvar);    }    if (nbin!=0)    {        free (min_binvar);        free (max_binvar);        free (nbits);    }    deallocate_memory_pop (parent_pop, popsize);    deallocate_memory_pop (child_pop, popsize);    deallocate_memory_pop (mixed_pop, 2*popsize);    free (parent_pop);    free (child_pop);    free (mixed_pop);    printf("\n Routine successfully exited \n");    return (0);}

⌨️ 快捷键说明

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