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

📄 nsga2r.c

📁 多目标遗传算法的程序
💻 C
字号:
/* NSGA-II routine (implementation of the 'main' function) */# include <stdio.h># include <stdlib.h># include <math.h>
# include "time.h"# include "global.h"# include "rand.h"int nreal;int nobj;int ncon;int popsize;long double pcross_real;long double pmut_real;long double eta_c;long double eta_m;int ngen;int nrealmut;int nrealcross;int *nbits;long double *min_realvar;long double *max_realvar;int main (int argc, char **argv){    int i;    FILE *fpt1;    FILE *fpt2;    FILE *fpt3;    FILE *fpt4;    FILE *fpt5;
	FILE *fpt6;    population *parent_pop;    population *child_pop;    population *mixed_pop;
	int random;    /*if (argc<2)    {        printf("\n Usage ./nsga2r random_seed \n");        exit(1);    }    seed = (long double)atof(argv[1]);*/
	srand( (unsigned)time( NULL ) ); 
	random = rand()%1000;
    seed=(float) random/1000.0;    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("初始种群.txt","w");    fpt2 = fopen("最终种群.txt","w");    fpt3 = fopen("pareto最优解(best_pop).txt","w");    //fpt4 = fopen("all_pop.out","w");    fpt5 = fopen("参数设置.txt","w");
	fpt6 = fopen("中间代最优解.txt","w");

	popsize=500;  
	ngen=500;  
	nobj=3; 	ncon=4;	nreal=2; 
	    if (nreal != 0)    {        min_realvar = (long double *)malloc(nreal*sizeof(long double)); /* 用于存储相对应实数变量的下限 */        max_realvar = (long double *)malloc(nreal*sizeof(long double)); /* 用于存储相对应实数变量的上限 *///        for (i=0; i<nreal; i++)
//        {
//			min_realvar[i]=0.0; 
//			max_realvar[i]=1.0; 			 	
//         }
		min_realvar[0]=94000.0; 
		max_realvar[0]=104000.0; 
		min_realvar[1]=9901.0; 
		max_realvar[1]=12272.0;		pcross_real=0.9; 		pmut_real=0.1; 			eta_c=10; 
		eta_m=10;     }    nrealmut = 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");    evaluate_pop (parent_pop);    assign_rank_and_crowding_distance (parent_pop);    report_pop (parent_pop, fpt1);    printf("\n 正在进化第  1  代......");    fflush(stdout);    fflush(fpt1);    fflush(fpt2);    fflush(fpt3);    //fflush(fpt4);    fflush(fpt5);    for (i=2; i<=ngen; i++)    {        selection (parent_pop, child_pop);        mutation_pop (child_pop);        evaluate_pop(child_pop);        merge (parent_pop, child_pop, mixed_pop);        fill_nondominated_sort (mixed_pop, parent_pop);	        printf("\n 正在进化第  %d  代......",i);    }    printf("\n Generations finished, now reporting solutions");    report_pop(parent_pop,fpt2);    report_feasible(parent_pop,fpt3);
	report_orginal (parent_pop, fpt6);    fflush(stdout);    fflush(fpt1);    fflush(fpt2);    fflush(fpt3);    //fflush(fpt4);    fflush(fpt5);
	fflush(fpt6);    fclose(fpt1);    fclose(fpt2);    fclose(fpt3);    //fclose(fpt4);    fclose(fpt5);
	fclose(fpt6);    if (nreal!=0)    {        free (min_realvar);        free (max_realvar);    }    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 + -