📄 frange_ran.c
字号:
#include "genocop.h"/********************************************************************************//* *//* FUNCTION NAME : frange_ran() *//* *//* SYNOPSIS : float frange_ran(llim,ulim) *//* *//* DESCRIPTION : This function returns a random float number *//* between the llim and ulim. *//* *//* FUNCTIONS CALLED : None *//* *//* CALLING FUNCTIONS : find_live_die(), *//* get_F(), *//* initialize_x2(), *//* oper1(). *//* *//* AUTHOR : Tom Logan *//* *//* DATE : 2/23/93 *//* *//* *//* REV DATE BY DESCRIPTION *//* --- ---- -- ----------- *//* *//* *//********************************************************************************/float frange_ran(llim, ulim) float ulim, llim; /* The upper and lower limits * between which the random */ /* number is to be generated */{ float diff, num1; diff = ulim - llim; if (diff == 0) return (llim); else if (diff < 0.0001) return ((flip() == TAIL) ? llim : ulim); do num1 = llim + (float) ((newrand() * (ulim - llim)) / 65535); while ((num1 < llim) || (num1 > ulim)); return (num1);}/********************************************************************************//* *//* FUNCTION NAME : initialize_x2() *//* *//* SYNOPSIS : FLAG initialize_x2(final,rc,x1,x2,x1_vari, *//* X, a1_b) *//* *//* DESCRIPTION : This function generates random values for *//* each of the x2 variables, in such a way that*//* none of the constriants are violated; if no *//* such values could be found after a *//* predetermined number of times, it returns *//* FALSE. *//* *//* FUNCTIONS CALLED : frange_ran(), *//* ivector(), *//* matrix(), *//* vector(). *//* *//* CALLING FUNCTIONS : main() *//* *//* AUTHOR : Swarnalatha Swaminathan *//* *//* DATE : 1/17/92 *//* *//* *//* REV DATE BY DESCRIPTION *//* --- ---- -- ----------- *//* *//* *//********************************************************************************/FLAG initialize_x2(final, rc, x1, x2, x1_vari, X, a1_b,bothflag,ninumber,nenumber, epsilon) MATRIX final; /* The final matrix */ VECTOR X, a1_b; /* Vector to hold the values of * all the variables to be * generated */ IVECTOR x1, x2; /* The p-variables eliminated and * the remaining variables */ INDEX rc; /* Row and column of the final * matrix */ int x1_vari; /* Number of variables eliminated */ int bothflag; /* if bothflag= TRUE, both NL and * L constraints are checked */ int ninumber; /* no. of NI constraints */ int nenumber; /* no. of NE constraints */ float epsilon; /* precision variable */{ int i, j, /* counter variables */ x2_vari = rc.c - 2, /* Total number of existing * variables */ try = 0; /* Number of tries before getting * a value for a variable */ FLAG _LOW, /* Flags to check if the lower * bounds are satisfied */ _HIGH; /* Flags to check if the pper * bounds are satisfied */ VECTOR temp; /* Temporary vector to hold the * random values of the variables */ MATRIX trymat; /* Total after substitution of * the generated variable value */ double sum = 0.0; FLAG _CHECK = TRUE; FLAG LOCAL_CHECK = TRUE; FLAG NL_CHECK = TRUE; /* for NL constraints */ temp = vector(1, x2_vari); trymat = matrix(1, rc.r - x2_vari, 0, x2_vari); LOCAL_CHECK = TRUE; _CHECK = TRUE; NL_CHECK = TRUE; do { if (try < TRIES) /* Increment the number of tries */ ++try; /* * For each constraints, generate a value for a variable between * the bounds */ for (i = 1; i <= x2_vari; i++) temp[i] = frange_ran(final[i][1], final[i][rc.c]); if (x2_vari != rc.r) { for (j = x2_vari + 1; j <= rc.r; j++) { sum = 0.0; /* * Evaluate the equation after a value is generated for a * variable */ for (i = 1; i <= x2_vari; i++) sum = sum + temp[i] * final[j][i + 1]; /* Check if the lower and upper bounds are satisfied */ _LOW = (sum >= final[j][1]) ? TRUE : FALSE; _HIGH = (sum <= final[j][rc.c]) ? TRUE : FALSE; /* if NL specified, check for NL as well */ if (bothflag == TRUE) NL_CHECK = nonlinear(X, ninumber, nenumber, epsilon); if ((!_LOW) || (!_HIGH)) break; if (bothflag == TRUE) if (!NL_CHECK) break; } } else { _LOW = _HIGH = TRUE; if (bothflag == TRUE) NL_CHECK = nonlinear(X, ninumber, nenumber, epsilon); } } while ((try < TRIES) && ((!_LOW) || (!_HIGH) || (!NL_CHECK))); if (try >= TRIES) { _CHECK = FALSE; printf("No feasible random solution found after %d tries\n", TRIES); /*printf("NL_CHECK = %d, bothflag = %d\n", NL_CHECK, bothflag);*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -