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

📄 evaluate.c

📁 很好的一个约束遗传算法优化程序
💻 C
📖 第 1 页 / 共 4 页
字号:
#include "genocop.h"/* Cumulative probability on crossover *//* Random probability on mutation       *//* NO multiple hits per agent possible  *//********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   optimization()                               *//*                                                                              *//*           SYNOPSIS          :   void optimization(X,x1,x2,fin_mat,rc,tot_eq) *//*                                                                              *//*           DESCRIPTION       :   This procedure initializes the population    *//*                                  with the values X passed from main, and     *//*                                  evaluates them.  After assigning weight     *//*                                  for each member of the populaiton, a group  *//*                                  of them are chosen to reproduce and a group *//*                                  is chosen to die.  Genetic operators are    *//*                                  applied and a new generation is produced    *//*                                  to replace the members that died.  This     *//*                                  cycle continues for the number of times,    *//*                                  user specifies in the input file            *//*                                                                              *//*           FUNCTIONS CALLED  :   assign_probab(),                             *//*                                 evaluate(),                                  *//*                                 find_cum_probab(),                           *//*                                 find_live_die(),                             *//*                                 find_parent(),                               *//*                                 ivector(),                                   *//*                                 matrix(),                                    *//*                                 oper1(),                                     *//*                                 oper2(),                                     *//*                                 oper3(),                                     *//*                                 oper4(),                                     *//*                                 oper5(),                                     *//*                                 oper6(),                                     *//*                                 print_matrix(),                              *//*                                 print_population(),                          *//*                                 sort(),                                      *//*                                 vector().                                    *//*                                                                              *//*           CALLING FUNCITONS :   main()                                       *//*                                                                              *//*                                                                              *//********************************************************************************/voidoptimization(X, x1, x2, fin_mat, rc, tot_eq, a1_b)    MATRIX          fin_mat;    VECTOR          X, a1_b;    IVECTOR         x1, x2;    INDEX           rc;    int             tot_eq;{    MATRIX          new_genera,		    /* Temporary storage for the new					     * generation */                    population,		    /* Population of x2 variables */                    ref_population,	    /* pop. of ref. vectors */                    print_pop, temp;    VECTOR          probab,		    /* Probability of agents to die					     * or live */		    ref_probab,             /* prob. dist. of ref pop */		                        cum_probab,		    /* Cumulative probability of					     * agents */                    t_vec, temp_vec;    IVECTOR         live, die;    unsigned long   generations;	    /* Total number of Generations */    unsigned long   count_gener = 1;	    /* counter : number of					     * generations */    unsigned long   peak_cnt;    int             P1, P2, P3, P4, P5, P6, P7,	/* # of parents for each op. */                    j1, j2, j3, j4, j5, j6, j7,	/* j vars */                    pop_size,		    /* Population size */                    MinMax,		    /* Min or Max problem : 0 or 1 */                    init_val,		    /* Single or multiple point init.					     * population? */                    P,			    /* Total number of agents chosen					     * to reproduce */                    oper,		    /**/                    B,			    /* Parm. for op. 3 - nonuniform					     * mutation */                    STEP,		    /* for op. 5 - simple					     * arithmetical xover */                    x2_vari = rc.c - 2,	    /**/                    first,		    /* Index of the parent to mutate */                    first_live,		    /* Index of the two parents for					     * xover parents */                    second_live, first_die, /* Index of the two parents for					     * xover death */                    second_die, tot,	    /* total number of chosen parents					     * not used */                    i, j, k, dup_count,	    /* replication count */                    nonlinear_check;	    /* NL check flag while moving */    float           Q,			    /* Probability of the best agent */                    A,			    /* Parm. for op. 4 - whole					     * arithmetical xover */                    Teval,		    /* Evaluation of the best agent */                    R_Teval,		    /* Evaluation of best ref. agent */                    TR_Teval, initial_val;  /* Initial value of the					     * population */    float           peak_val;    float           amove;		    /* const. used for moving */    int             x, y;    FLAG            same;    FLAG            _PROGEND;    float         **new;    char            response_char;    int             nlcflag = TRUE;	    /* flag for NL constraints */    float           p_x_z = 0.0;    int             amovecount = 0;    int             rindex = 0;		    /* random index */    float	    pr_x_z_replace=0.0;     /* prob. of repl. of x by Z */    int		    NI_NUMBER = 0;	    /* no. of NI constraints */    int		    NE_NUMBER = 0;	    /* no. of NE constraints */    float	    EPSILON=0.0;	    /* precision input */    int		    prob_dist_mode=0;       /* 0 for random, 1 for distrib.    					       for ref. pop. choice */    float	    temp_prob=0.0;    /*     * Reading from the file the population size, total number of     * generations, total number of times each of the 5 operators to be     * applied, probability of the best agent, minimization or maximization     * problem, the parameters for the operators     */    fscanf(input, "%d %lU %d %d %d %d %d %d %d", &pop_size, &generations, &P1, &P2, &P3, &P4, &P5, &P6, &P7);    fscanf(input, "%f ", &Q);    fscanf(input, "%d ", &MinMax);    fscanf(input, "%d ", &init_val);    fscanf(input, "%d ", &B);    fscanf(input, "%d ", &STEP);    fscanf(input, "%d ", &test_num);    fscanf(input, "%f ", &pr_x_z_replace);    fscanf(input, "%d ", &NI_NUMBER);    NE_NUMBER = 0;    EPSILON = 0.0;    fscanf(input, "%f ", &prob_dist_mode);            fclose(input);    fprintf(output, "\n\n");    fprintf(output, "Test case number           : %d\n", test_num);    fprintf(output, "Number of operators        : %d  %d  %d  %d  %d  %d  %d\n", P1, P2, P3, P4, P5, P6, P7);    fprintf(output, "Number of generations      : %lu\n", generations);    fprintf(output, "Population size            : %d\n", pop_size);    fprintf(output, "Parameter B                : %d\n", B);    fprintf(output, "Parameter Q                : %f\n", Q);    fprintf(output, "Max/Min                    : %d\n", MinMax);    fprintf(output, "Probability of replacement : %f\n", pr_x_z_replace);    fprintf(output, "Initialization mode        : %d\n", init_val);    fprintf(output, "Selection of refer. point  : %d\n", prob_dist_mode);    /* P is the total number of parents needed for applying all the operators */    P = P1 + P2 + P3 + P4 + P5 + P6 + P7;    if (P > pop_size)    {	printf("The total number of operators greater than population\n");	fprintf(output, "The total number of operators greater than population\n");	fclose(output);	exit(1);    }    peak_val = 0;    peak_cnt = 0;    /* Space allocation for all the vectors and matrices involved */    population = matrix(1, pop_size, 0, x2_vari + 1);    ref_population = matrix(1, pop_size, 0, x2_vari + 1);    print_pop = matrix(1, pop_size, 0, x2_vari + tot_eq + 1);    new_genera = matrix(1, pop_size, 0, x2_vari + 1);    temp = matrix(1, 2, 1, x2_vari);    probab = vector(1, pop_size);    ref_probab = vector(1, pop_size);    t_vec = vector(1, x2_vari);    temp_vec = vector(0, x2_vari+1);    cum_probab = vector(1, pop_size);    live = ivector(1, pop_size);    die = ivector(1, pop_size);    /*     * Initial population with all identical agents, whose values were got     * randomly - ditto for initial reference population     */    if (init_val == 1)    {	fprintf(output, "\n\nUSING SINGLE POINT INITIAL POPULATION...\n\n");	_PROGEND = initialize_x2(fin_mat, rc, x1, x2, tot_eq, X, a1_b, FALSE,					NI_NUMBER, NE_NUMBER, EPSILON);	for (j = 1; j <= pop_size; j++)	{	    for (i = 1; i <= x2_vari; i++)	    {		population[j][i] = X[x2[i]];		population[j][x2_vari + 1] = 0;	    }	    population[j][0] = evaluate(X);	}	fprintf(output, "\nThe initial point of the population is\n");	print_vector(X, 1, tot_eq + x2_vari);	_PROGEND = initialize_x2(fin_mat, rc, x1, x2, tot_eq, X, a1_b, TRUE,					NI_NUMBER, NE_NUMBER, EPSILON);	for (j = 1; j <= pop_size; j++)	{	    for (i = 1; i <= x2_vari; i++)	    {		ref_population[j][i] = X[x2[i]];		ref_population[j][x2_vari + 1] = 0;	    }	    ref_population[j][0] = evaluate(X);	}	fprintf(output, "\nThe initial point of the ref. population is\n");	print_vector(X, 1, tot_eq + x2_vari);    }    /* end of if init_val == 1 ...else starts below */    /*    ** MULTIPLE POINT INITIALIZATION FOR BOTH POPULATIONS    */    else    {	fprintf(output, "\n\nUSING MULTIPLE POINT INITIAL POPULATION...\n\n");	j = 1;	while (j <= pop_size)	{	    _PROGEND = initialize_x2(fin_mat, rc, x1, x2, tot_eq, X, a1_b,				     FALSE, NI_NUMBER, NE_NUMBER, EPSILON);	    if (_PROGEND == TRUE)	    {		for (i = 1; i <= x2_vari; i++)		{		    population[j][i] = X[x2[i]];		    population[j][x2_vari + 1] = 0;		}		population[j][0] = evaluate(X);		j++;		/*printf("Linear Gen. ok\n");*/	    } else	    {		printf("Vector is feasible w.r.t linear constraints.\nDo you wish to include/replicate this vector in the population? (y/n)");		fflush(stdin);		response_char = getchar();		fflush(stdin);		if (response_char == 'Y' || response_char == 'y')		{		    do		    {			printf("How many copies (min. 1, max. %d) :", pop_size -			       j + 1);			scanf("%d", &dup_count);			if ((dup_count < 1) || (dup_count > (pop_size - j + 1)))			    printf("\nInvalid entry.  Must be in the range 1 to %d\n",				   pop_size - j + 1);		    } while ((dup_count < 1) || (dup_count > (pop_size - j + 1)));		    for (k = 1; k <= dup_count; k++)		    {			for (i = 1; i <= x2_vari; i++)			{			    population[j][i] = X[x2[i]];			    population[j][x2_vari + 1] = 0;			}		    	population[j][0] = evaluate(X);			j++;		    }			    /* end of for dup_count.... */		}			    /* end of if response_char...... */	    }				    /* end of if					     * _PROGEND...else.............. */	}				    /* end of do loop for multiple					     * pt. */	/* Now for the reference pop. multiple init */	j = 1;	dup_count = 0;	while (j <= pop_size)	{	    _PROGEND = initialize_x2(fin_mat, rc, x1, x2, tot_eq, X, a1_b,				     TRUE, NI_NUMBER, NE_NUMBER, EPSILON);	    if (_PROGEND == TRUE)	    {		for (i = 1; i <= x2_vari; i++)		{		    ref_population[j][i] = X[x2[i]];		    ref_population[j][x2_vari + 1] = 0;		}	    	ref_population[j][0] = evaluate(X);		j++;	    } else 	    {		printf("Vector is feasible w.r.t all constraints.\nDo you wish to include/replicate this vector in the population? (y/n)");		fflush(stdin);		response_char = getchar();		fflush(stdin);		if (response_char == 'Y' || response_char == 'y')		{		    do		    {			printf("How many copies (min. 1, max. %d) :", pop_size - j + 1);			scanf("%d", &dup_count);			if ((dup_count < 1) || (dup_count > (pop_size - j + 1)))			    printf("\nInvalid entry.  Must be in the range 1 to %d\n",				   pop_size - j + 1);		    } while ((dup_count < 1) || (dup_count > (pop_size - j + 1)));		    for (k = 1; k <= dup_count; k++)		    {			for (i = 1; i <= x2_vari; i++)			{			    ref_population[j][i] = X[x2[i]];			    ref_population[j][x2_vari + 1] = 0;			}	    		ref_population[j][0] = evaluate(X);						j++;		    }			    /* end of for dup_count.... */		}			    /* end of if response_char...... */	    }				    /* end of if					     * _PROGEND...else.............. */	}				    /* end of while */    }					    /* end of if init_val block */

⌨️ 快捷键说明

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