📄 evaluate.c
字号:
new_genera = population; population = new; /* Evaluate each of the agents in the new population */ for (i = 1; i <= pop_size; i++) { /* * if (population[i][x2_vari+1] != 0) do not reevaluate if no * changes were made *//*IP*/{ if (tot_eq != 0) { for (j = 1; j <= x2_vari + tot_eq; j++) X[j] = 0.0; find_X(fin_mat, population[i], X, x2_vari, tot_eq, x1, x2, a1_b); } else for (j = 1; j <= x2_vari; j++) X[j] = population[i][j]; population[i][0] = evaluate(X); /* * keep moving from X to Y till we satisfy NL&L feasibility * condition */ if (prob_dist_mode == 0) rindex = (int)frange_ran(1.0, (float)(pop_size)); else { temp_prob = frange_ran(0.0, 0.1); for (j=1; j<=pop_size; j++) { if (ref_probab[j] < temp_prob) { rindex=j; break; } } } amove = 0.0; amovecount = 0; nonlinear_check = FALSE; do { amove = frange_ran(0.0, 1.0); for (j = 1; j < x2_vari + 1; j++) temp_vec[j] = amove * ref_population[rindex][j] + (1 - amove) * population[i][j]; nonlinear_check = nonlinear(temp_vec, NI_NUMBER, NE_NUMBER, EPSILON); amovecount++; } while ((nonlinear_check == FALSE) && (amovecount < 100)); if (amovecount == 100) { /*printf("Z = Y\n");*/ for (j = 1; j < x2_vari + 1; j++) temp_vec[j] = ref_population[rindex][j]; } Teval = evaluate(temp_vec); R_Teval = evaluate(ref_population[rindex]); temp_vec[0] = Teval; population[i][0] = Teval; /* if eval(Z) > eval (Y), then replace Y by Z */ switch (MinMax) { case 0: if (Teval < R_Teval) { for (j = 0; j <= x2_vari + 1; j++) ref_population[rindex][j] = temp_vec[j]; /* printf("Z better than Y : Eval.\n"); */ } break; case 1: if (Teval > R_Teval) { for (j = 0; j <= x2_vari + 1; j++) ref_population[rindex][j] = temp_vec[j]; /* printf("Z better than Y : Eval.\n"); */ } break; } /* end of switch */ /* * replace population[i] with temp_vec as well, depending on * probab */ p_x_z = frange_ran(0.0, 1.0); if (p_x_z < pr_x_z_replace) for (j = 0; j <= x2_vari + 1; j++) population[i][j] = temp_vec[j]; } } /* Sort the new population based on their evaluation function */ sort(MinMax, population, pop_size); for (j = 1; j <= x2_vari; j++) X[j] = ref_population[1][j]; ref_population[1][0] = evaluate(X); sort(MinMax, ref_population, pop_size); switch (MinMax) { case 0: if (R_Teval > ref_population[1][0]) R_Teval = ref_population[1][0]; break; case 1: if (R_Teval < ref_population[1][0]) R_Teval = ref_population[1][0]; break; } switch (MinMax) { case 0: /* * printf("\nMin : Teval = %f, pop[1][0] = %f\n", Teval, * population[1][0]); */ if (Teval > population[1][0]) Teval = population[1][0]; if (R_Teval > ref_population[1][0]) R_Teval = ref_population[1][0]; peak_cnt = count_gener; if (peak_val > R_Teval) { peak_val = R_Teval; /* population[1][0]; */ fprintf(output, "%7lu \t%18.8f\n", count_gener, R_Teval); } break; case 1: /* * printf("\nMax : Teval = %f, pop[1][0] = %f\n", Teval, * population[1][0]); */ if (Teval < population[1][0]) Teval = population[1][0]; if (R_Teval > ref_population[1][0]) R_Teval = ref_population[1][0]; peak_cnt = count_gener; if (peak_val < R_Teval) { peak_val = R_Teval; /* population[1][0]; */ fprintf(output, "%7lu \t%18.8f\n", count_gener, R_Teval); } break; } /* printf("Generation# %7lu %18.8f ",count_gener, population[1][0]); */ /*printf("peak_cnt = %d\n", peak_cnt); */ /* Increment the number of iteration */ count_gener++; } while (count_gener <= generations); for (i=0; i<=x2_vari+1; i++) population[1][i] = ref_population[1][i]; fprintf(output, "\nBest solution was found at generation %lu (solution value = %.8f)\n", peak_cnt, peak_val); fprintf(output, "\n\nBest solution found:\n\n"); /* print best solution */ find_X(fin_mat, population[1], X, x2_vari, tot_eq, x1, x2, a1_b); for (j = 1; j <= x2_vari + tot_eq; j++) fprintf(output, " X[%2d] :\t%18.8f\n", j, X[j]); /* print final population *//* for(i=1; i<=pop_size; i++) { if (tot_eq != 0) { for(j=1; j<=x2_vari + tot_eq; j++) X[j] = 0.0; find_X(fin_mat,population[i],X,x2_vari,tot_eq,x1,x2,a1_b); for(j=1; j<=x2_vari + tot_eq; j++) print_pop[i][j] = X[j]; } else for(j=1; j<=x2_vari; j++) print_pop[i][j] = X[j] = population[i][j]; print_pop[i][0] = evaluate(X); print_pop[i][x2_vari+tot_eq+1] = population[i][x2_vari+1]; } fprintf(output,"\n\nThe Final Population:\n\n"); fprintf(output," Solution Value\t Agent variables: X[1] X[2] ...\n\n"); print_population(output, pop_size,x2_vari+tot_eq+1,print_pop);*/ free_matrix(population, 1, pop_size, 0); free_matrix(print_pop, 1, pop_size, 0); free_matrix(new_genera, 1, pop_size, 0); free_matrix(temp, 1, 2, 1); free_vector(probab, 1); free_vector(t_vec, 1); free_vector(cum_probab, 1); free_ivector(live, 1); free_ivector(die, 1);}/********************************************************************************//* *//* FUNCTION NAME : sort() *//* *//* SYNOPSIS : void sort(MinMax, population,pop_size) *//* *//* DESCRIPTION : This function sorts the population, in the *//* ascending or the descending order of the *//* evaluation function, depending on whether *//* it is a maximization or a minimization *//* function, respectively. *//* *//* As an alternative, the sortq function below *//* can be used, That sorting function uses *//* the quicksort algorithm. *//* *//* *//* FUNCTIONS CALLED : swap() *//* *//* CALLING FUNCITONS : optimization() *//* *//* AUTHOR : Swarnalatha Swaminathan *//* *//* DATE : 1/17/92 *//* *//* *//* REV DATE BY DESCRIPTION *//* --- ---- -- ----------- *//* A 9/11/92 Tom Logan Rewrote *//* *//********************************************************************************/voidsort(MinMax, population, pop_size) int MinMax, /* Tells whether it is a * maximizaiton or a minimization * function */ pop_size; /* Population size */ MATRIX population; /* Array of population */{ int i, j, k; /* If MinMax is 0 sorts in the descending order, and */ /* if it is 1 sorts in the ascending order */ /* Sorted in ascending or descending order, based on */ /* the evaluated values of each of the agents */ switch (MinMax) { case 0: for (i = 1; i <= pop_size; i++) for (j = i + 1; j <= pop_size; j++) if (population[i][0] > population[j][0]) swap(&population[i], &population[j]); break; case 1: for (i = 1; i <= pop_size; i++) for (j = i + 1; j <= pop_size; j++) if (population[i][0] < population[j][0]) swap(&population[i], &population[j]); break; default: fprintf(output, "Incorrect data: Must be a 0 or 1"); exit(1); }}/********************************************************************************//* *//* FUNCTION NAME : swap() *//* *//* SYNOPSIS : void swap(x,y) *//* *//* DESCRIPTION : This function interchanges the values of *//* x and y. *//* *//* FUNCTIONS CALLED : None *//* *//* CALLING FUNCITONS : sort() *//* *//* AUTHOR : Swarnalatha Swaminathan *//* *//* DATE : 1/17/92 *//* *//* *//* REV DATE BY DESCRIPTION *//* --- ---- -- ----------- *//* *//* *//********************************************************************************/voidswap(x, y) float **x, **y;{ float *temp; temp = *x; *x = *y; *y = temp;}/********************************************************************************//* *//* FUNCTION NAME : find_parent() *//* *//* SYNOPSIS : int find_parent(live,pop_size) *//* *//* DESCRIPTION : This function returns the index of the *//* agent in the population, which is to be *//* chosen for reproduction. *//* *//* FUNCTIONS CALLED : irange_ran() *//* *//* CALLING FUNCITONS : optimization() *//* *//* AUTHOR : Swarnalatha Swaminathan *//* *//* DATE : 1/17/92 *//* *//* *//* REV DATE BY DESCRIPTION *//* --- ---- -- ----------- *//* *//* *//********************************************************************************/intfind_parent(live, pop_size) int pop_size; /* Population size */ IVECTOR live; /* Vector containing the number * of times each agent */ /* is going to reproduce */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -