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

📄 evaluate.c

📁 很好的一个约束遗传算法优化程序
💻 C
📖 第 1 页 / 共 4 页
字号:
{    int             i, temp, t1, tot = 0;    /* Finding the total number of parents to reproduce */    for (i = 1; i <= pop_size; i++)	tot = tot + live[i];    if (tot == 0)    {	printf("No agents to select\n");	exit(1);    }    /* Choosing one of them randomly */    temp = irange_ran(1, tot);    tot = 0;    i = 1;    do    {	if (live[i] != 0)	    t1 = i;	tot = tot + live[i++];    } while (tot < temp);    /*     * Decrementing the number of times the parent chosen is going to     * reproduce     */    live[t1]--;    return (t1);}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   assign_probab()                              *//*                                                                              *//*           SYNOPSIS          :   void assign_probab(probab,pop_size,Q)        *//*                                                                              *//*           DESCRIPTION       :   This function assigns probability of survival*//*                                  to each of the agents determined by the     *//*                                  value provided by the user for the          *//*                                  probability of the best agnet.              *//*                                                                              *//*           FUNCTIONS CALLED  :   None                                         *//*                                                                              *//*           CALLING FUNCITONS :   optimization()                               *//*                                                                              *//*           AUTHOR            :   Swarnalatha Swaminathan                      *//*                                                                              *//*           DATE              :   1/17/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/voidassign_probab(probab, pop_size, Q)    int             pop_size;		    /* Population size */    double          Q;			    /* The probability of survival of					     * the best agent */    VECTOR          probab;		    /* Array to contain the					     * probability of survival */ /* of each of the agents */{    int             i;    double          Q1;    /* Q, Q(1-Q)^1, Q(1-Q)^2 ... Q(1-Q)^n */    Q1 = Q / (1 - x_pow_y(1 - Q, pop_size));    for (i = 1; i <= pop_size; i++)	probab[i] = Q1 * x_pow_y(1 - Q, i - 1);}voidassign_ref_probab(ref_probab, pop_size, Q)    int             pop_size;		    /* Population size */    double          Q;			    /* The probability of survival of					     * the best agent */    VECTOR          ref_probab;		    /* Array to contain the					     * probability of survival */ /* of each of the agents */{    int             i;    double          Q1;    /* Q, Q(1-Q)^1, Q(1-Q)^2 ... Q(1-Q)^n */    Q1 = Q / (1 - x_pow_y(1 - Q, pop_size));    for (i = 1; i <= pop_size; i++)	ref_probab[i] = Q1 * x_pow_y(1 - Q, i - 1);}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   x_pow_y()                                    *//*                                                                              *//*           SYNOPSIS          :   float x_pow_y(x,y)                           *//*                                                                              *//*           DESCRIPTION       :   This function returns the value of x to the  *//*                                  power of y.                                 *//*                                                                              *//*           FUNCTIONS CALLED  :   None                                         *//*                                                                              *//*           CALLING FUNCITONS :   assign_probab()                              *//*                                                                              *//*           AUTHOR            :   Swarnalatha Swaminathan                      *//*                                                                              *//*           DATE              :   1/17/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/floatx_pow_y(x, y)    float           x;    int             y;{    int             i;    float           tot = 1.0;    for (i = 0; i < y; i++)	tot = tot * x;    return (tot);}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   find_cum__probab()                           *//*                                                                              *//*           SYNOPSIS          :   void find_cum__probab(cum_probab,probab,     *//*                                                                     pop_size)*//*                                                                              *//*           DESCRIPTION       :   This function finds the cumulative           *//*                                  probability of each of the agents, from the *//*                                  individual probability found earlier.       *//*                                                                              *//*           FUNCTIONS CALLED  :   None                                         *//*                                                                              *//*           CALLING FUNCITONS :   optimization()                               *//*                                                                              *//*           AUTHOR            :   Swarnalatha Swaminathan                      *//*                                                                              *//*           DATE              :   1/17/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/voidfind_cum_probab(cum_probab, probab, pop_size)    int             pop_size;		    /* Population size */    VECTOR          probab,		    /* Individual probability of					     * survival of each of the agent */                    cum_probab;		    /* Cumulative probability of					     * survival of each of the agent */{    int             i;    cum_probab[1] = probab[1];    for (i = 2; i <= pop_size; i++)	cum_probab[i] = cum_probab[i - 1] + probab[i];}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   find_live()                                  *//*                                                                              *//*           SYNOPSIS          :   void find_live(cum_probab,live,pop_size,P4+P5*//*                                                                              *//*           DESCRIPTION       :   This function finds the agents from the      *//*                                  population, who are going to live - those   *//*                                  who are going to reproduce, which is done   *//*                                  based on the cumulative probability of      *//*                                  survival of each of the agents.             *//*                                                                              *//*           FUNCTIONS CALLED  :   frange_ran()                                 *//*                                                                              *//*           CALLING FUNCITONS :   optimization()                               *//*                                                                              *//*           AUTHOR            :   Tom Logan                                    *//*                                                                              *//*           DATE              :   9/15/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/voidfind_live(cum_probab, live, pop_size, P)    VECTOR          cum_probab;		    /* Cumulative probability */    IVECTOR         live;		    /* Agents that are going to					     * reproduce */    int             pop_size,		    /* Population size */                    P;			    /* Total number of parents needed					     * to reproduce */{    float           random;    int             count = 0,		    /* Count of the number of agents					     * chosen to live */                    i;    do    {	/* Choosing a random cumulative probability */	random = frange_ran(0.0, 1.0);	i = 0;	/* Finding the agent with the chosen cumulative probability */	do	{	    i++;	} while ((random > cum_probab[i]) && (i < pop_size));	/* Chosing the parent with that probability to reproduce */	if (count < P)	{	    live[i]++;	    count++;	}    } while (count < P);}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   find_die()                                   *//*                                                                              *//*           SYNOPSIS          :   void find_die(cum_probab,die,pop_size,P4+P5) *//*                                                                              *//*           DESCRIPTION       :   This function finds the agents from the      *//*                                  population, who are going to die.            //**//*           FUNCTIONS CALLED  :   frange_ran()                                 *//*                                                                              *//*           CALLING FUNCITONS :   optimization()                               *//*                                                                              *//*           AUTHOR            :   Tom Logan                                    *//*                                                                              *//*           DATE              :   9/15/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/intfind_die(cum_probab, die, pop_size)    VECTOR          cum_probab;		    /* Cumulative probability */    IVECTOR         die;		    /* Agents that are going to die */    int             pop_size;		    /* Population size */{    float           random;    int             i;    int             done = FALSE;    do    {	/* Choosing a random cumulative probability */	random = frange_ran(0.0, 1.0);	i = 0;	/* Finding the agent with the chosen cumulative probability */	do	{	    i++;	}	while ((random > cum_probab[i]) && (i < pop_size));	/* Chosing the agent to die */	if ((die[pop_size + 1 - i] == 0) && (i < pop_size))	    done = TRUE;    }    while (!done);    return (pop_size + 1 - i);}/********************************************************************************//*                                                                              *//*           FUNCTION NAME     :   find_X()                                     *//*                                                                              *//*           SYNOPSIS          :   void find_X(final,agen.X,x2_vari,tot_eq,x1,  *//*                                                                           x2)*//*                                                                              *//*           DESCRIPTION       :   This function finds the value of all the     *//*                                  variables in the original problem, with     *//*                                  the values of the remaining variables.      *//*                                                                              *//*           FUNCTIONS CALLED  :   none                                         *//*                                                                              *//*           CALLING FUNCITONS :   optimization()                               *//*                                                                              *//*           AUTHOR            :   Swarnalatha Swaminathan                      *//*                                                                              *//*           DATE              :   1/17/92                                      *//*                                                                              *//*                                                                              *//*           REV            DATE            BY           DESCRIPTION            *//*           ---            ----            --           -----------            *//*                                                                              *//*                                                                              *//********************************************************************************/voidfind_X(final, agent, X, x2_vari, tot_eq, x1, x2, a1_b)    MATRIX          final;    VECTOR          agent, X, a1_b;    IVECTOR         x1, x2;    int             x2_vari, tot_eq;{    int             i, j;    for (j = 1; j <= x2_vari; j++)	X[x2[j]] = agent[j];    for (j = 1; j <= tot_eq; j++)    {	X[x1[j]] = a1_b[j];	for (i = 1; i <= x2_vari; i++)	    X[x1[j]] = X[x1[j]] + agent[i] * final[j + x2_vari][i + 1];    }}

⌨️ 快捷键说明

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