[1007] | 1 | /**********fitnessfunctions.c************************************************************************************ |
---|
| 2 | fitfunc.c is a program that contains modules for enabling the user to write his own fitness functions |
---|
| 3 | //TODO: Unresolved Issues so far: |
---|
| 4 | //1)Inclusion of extreme points on the curve |
---|
| 5 | //2)Inclusion of consecutive zero-gradient points in the list of maximas minimas |
---|
| 6 | //3)Regarding mathematically non differentiable functions, like triangular functions, etc. |
---|
| 7 | ********************************************************************************************************/ |
---|
| 8 | |
---|
| 9 | #include<stdio.h> |
---|
| 10 | #include<math.h> |
---|
| 11 | #include<stdlib.h> |
---|
| 12 | #include<time.h> |
---|
| 13 | |
---|
| 14 | #define EPSILON 0.005 |
---|
| 15 | #define TRUE 1 |
---|
| 16 | #define FALSE 0 |
---|
| 17 | #define INCREMENT 0.01 |
---|
| 18 | #define PI 3.14 |
---|
| 19 | #define NO_OF_POINTS 1500 |
---|
| 20 | #define INVALID_SLOPE 99999.9999 |
---|
| 21 | #define TOLERANCE 1e-5 |
---|
| 22 | |
---|
| 23 | typedef struct Curve{ |
---|
| 24 | int curveID; |
---|
| 25 | double *xvals; |
---|
| 26 | double *yvals; |
---|
| 27 | int number_of_points; |
---|
| 28 | }Curve; |
---|
| 29 | |
---|
| 30 | /* |
---|
| 31 | * Creating a struct for Maxima and Minima Lists |
---|
| 32 | * Can be used as an argument for functions that give both local and global max/min |
---|
| 33 | * The responsibility for initially allocating and finally freeing memory to *xval and *yval arrays |
---|
| 34 | * lies with the calling function. Reallocation of memory for these arrays and updating size_of_list |
---|
| 35 | * will happen inside called functions. |
---|
| 36 | */ |
---|
| 37 | |
---|
| 38 | typedef struct MaxOrMinList{ |
---|
| 39 | double *yvals; |
---|
| 40 | double *xvals; |
---|
| 41 | int no_of_actual_max_or_mins; |
---|
| 42 | }MaxOrMinList; |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | double X_Axis_Values[NO_OF_POINTS], Y_Axis_Values[NO_OF_POINTS]; |
---|
| 46 | double slope_calc(int index); |
---|
| 47 | void initdisplay(); |
---|
| 48 | |
---|
| 49 | void InitializeCurves(int ID){ |
---|
| 50 | int index; |
---|
| 51 | // double init; |
---|
| 52 | switch(ID){ |
---|
| 53 | case 1:// Y = SIN(X); |
---|
| 54 | for(index=0;index<NO_OF_POINTS;index++){ |
---|
| 55 | X_Axis_Values[index] =(double)(index*INCREMENT); |
---|
| 56 | Y_Axis_Values[index] = sin(X_Axis_Values[index]); |
---|
| 57 | } |
---|
| 58 | break; |
---|
| 59 | case 2://Y = EXP(-X); |
---|
| 60 | for(index=0;index<NO_OF_POINTS;index++){ |
---|
| 61 | X_Axis_Values[index] =(double)(index*INCREMENT); |
---|
| 62 | Y_Axis_Values[index] = exp(-X_Axis_Values[index]); |
---|
| 63 | } |
---|
| 64 | break; |
---|
| 65 | case 3://Y = EXP(-x)*SINC(X); |
---|
| 66 | // init = -5; |
---|
| 67 | for(index=0;index<NO_OF_POINTS;index++){ |
---|
| 68 | // init+=INCREMENT; |
---|
| 69 | // X_Axis_Values[index] = init; |
---|
| 70 | X_Axis_Values[index] =(double)(index*INCREMENT); |
---|
| 71 | Y_Axis_Values[index] = exp(-X_Axis_Values[index])*sin(10*X_Axis_Values[index]); |
---|
| 72 | } |
---|
| 73 | break; |
---|
| 74 | default: |
---|
| 75 | printf("\nError: Invalid Value Entered\n"); |
---|
| 76 | exit(0); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | double y_at_x(int curveID, float xval){ |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | double y_at_x_temp(double xval){ |
---|
| 86 | int i; |
---|
| 87 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 88 | if(fabs(xval-X_Axis_Values[i]<=EPSILON)){ |
---|
| 89 | return Y_Axis_Values[i]; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | double slope_at_x(int curveID, double xval){ |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | double slope_at_x_temp(double xval){ |
---|
| 100 | int i; |
---|
| 101 | double slope_val = INVALID_SLOPE; |
---|
| 102 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 103 | if(fabs(xval-X_Axis_Values[i]<=EPSILON)){ |
---|
| 104 | slope_val = slope_calc(i); |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | return slope_val; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | int max_at_x(int curveID, double xval){ |
---|
| 112 | return FALSE; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /* |
---|
| 116 | *have made an implicit assumption here |
---|
| 117 | *only the first point of a series of zero gradient points will be a minima/maxima point |
---|
| 118 | *need some clarifications on whether this is acceptable.This will be important w.r.t square wave like curves, etc. |
---|
| 119 | */ |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | int max_at_x_temp(double xval){ |
---|
| 123 | double slope_at_prev_point = INVALID_SLOPE, slope_at_point = INVALID_SLOPE; |
---|
| 124 | slope_at_prev_point = slope_at_x_temp(xval-INCREMENT); |
---|
| 125 | slope_at_point = slope_at_x_temp(xval); |
---|
| 126 | if(slope_at_prev_point!=INVALID_SLOPE && slope_at_point!=INVALID_SLOPE){ |
---|
| 127 | if(slope_at_prev_point > 0 && slope_at_point <= 0){ |
---|
| 128 | return TRUE; |
---|
| 129 | }else{ |
---|
| 130 | return FALSE; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | printf("Error: The point is either outside the range of values for the curve or is an extreme point"); |
---|
| 134 | return FALSE; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | int min_at_x(int curveID, double xval){ |
---|
| 138 | return FALSE; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | int min_at_x_temp(double xval){ |
---|
| 142 | double slope_at_prev_point = INVALID_SLOPE, slope_at_point = INVALID_SLOPE; |
---|
| 143 | slope_at_prev_point= slope_at_x_temp(xval-INCREMENT); |
---|
| 144 | slope_at_point = slope_at_x_temp(xval); |
---|
| 145 | if(slope_at_prev_point!=INVALID_SLOPE && slope_at_point!=INVALID_SLOPE){ |
---|
| 146 | if(slope_at_prev_point < 0 && slope_at_point >= 0){ |
---|
| 147 | return TRUE; |
---|
| 148 | }else{ |
---|
| 149 | return FALSE; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | printf("Error: The point is either outside the range of values for the curve or is an extreme point"); |
---|
| 153 | return FALSE; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | double** local_maxima_list(int curveID){ |
---|
| 157 | return NULL; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /* |
---|
| 161 | *inputs required regarding the inclusion of extreme points on the curve |
---|
| 162 | * |
---|
| 163 | */ |
---|
| 164 | |
---|
| 165 | double slope_calc(int i){ |
---|
| 166 | double slope = INVALID_SLOPE; |
---|
| 167 | if(i>=0 && i<NO_OF_POINTS){ |
---|
| 168 | if(i!=NO_OF_POINTS-1){ |
---|
| 169 | slope = ((Y_Axis_Values[i+1]-Y_Axis_Values[i])/(X_Axis_Values[i+1]-X_Axis_Values[i])); |
---|
| 170 | }else{ |
---|
| 171 | slope = ((Y_Axis_Values[i]-Y_Axis_Values[i-1])/(X_Axis_Values[i]-X_Axis_Values[i-1])); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | return slope; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* |
---|
| 178 | *Let Allocation be handled by calling function |
---|
| 179 | *This function only reallocates if necessary |
---|
| 180 | */ |
---|
| 181 | MaxOrMinList* get_local_max_list(MaxOrMinList* buffer){ |
---|
| 182 | int no_of_actual_max = 0,i; |
---|
| 183 | double slope_at_prev_point = INVALID_SLOPE,slope_at_point = INVALID_SLOPE; |
---|
| 184 | if(buffer!=NULL){ |
---|
| 185 | if(buffer->xvals!=NULL && buffer->yvals!=NULL){ |
---|
| 186 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 187 | slope_at_prev_point = slope_calc(i-1); |
---|
| 188 | slope_at_point = slope_calc(i); |
---|
| 189 | if(slope_at_point != INVALID_SLOPE && slope_at_prev_point != INVALID_SLOPE){ |
---|
| 190 | if(slope_at_prev_point>0 && slope_at_point<=0){ |
---|
| 191 | ++no_of_actual_max; |
---|
| 192 | if(no_of_actual_max>1){ |
---|
| 193 | buffer->xvals = realloc(buffer->xvals,(sizeof(double))*(no_of_actual_max)); |
---|
| 194 | buffer->yvals = realloc(buffer->yvals,(sizeof(double))*(no_of_actual_max)); |
---|
| 195 | } |
---|
| 196 | buffer->yvals[no_of_actual_max-1]=Y_Axis_Values[i]; |
---|
| 197 | buffer->xvals[no_of_actual_max-1]=X_Axis_Values[i]; |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | buffer->no_of_actual_max_or_mins = no_of_actual_max; |
---|
| 202 | }else{ |
---|
| 203 | printf("\nError: XValue And Yvalue Array Not Initialized"); |
---|
| 204 | return NULL; |
---|
| 205 | } |
---|
| 206 | } |
---|
| 207 | return buffer; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /* |
---|
| 211 | *Let Allocation be handled by calling function |
---|
| 212 | *This function only reallocates if necessary |
---|
| 213 | */ |
---|
| 214 | MaxOrMinList* get_local_min_list(MaxOrMinList* buffer){ |
---|
| 215 | int no_of_actual_min = 0,i; |
---|
| 216 | double slope_at_prev_point = INVALID_SLOPE,slope_at_point = INVALID_SLOPE; |
---|
| 217 | if(buffer!=NULL){ |
---|
| 218 | if(buffer->xvals!=NULL && buffer->yvals!=NULL){ |
---|
| 219 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 220 | slope_at_prev_point = slope_calc(i-1); |
---|
| 221 | slope_at_point = slope_calc(i); |
---|
| 222 | if(slope_at_point != INVALID_SLOPE && slope_at_prev_point != INVALID_SLOPE){ |
---|
| 223 | if(slope_at_prev_point<0 && slope_at_point>=0){ |
---|
| 224 | ++no_of_actual_min; |
---|
| 225 | if(no_of_actual_min>1){ |
---|
| 226 | buffer->xvals = realloc(buffer->xvals,(sizeof(double))*(no_of_actual_min)); |
---|
| 227 | buffer->yvals = realloc(buffer->yvals,(sizeof(double))*(no_of_actual_min)); |
---|
| 228 | } |
---|
| 229 | buffer->yvals[no_of_actual_min-1]=Y_Axis_Values[i]; |
---|
| 230 | buffer->xvals[no_of_actual_min-1]=X_Axis_Values[i]; |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | buffer->no_of_actual_max_or_mins = no_of_actual_min; |
---|
| 235 | }else{ |
---|
| 236 | printf("\nError: XValue And Yvalue Array Not Initialized"); |
---|
| 237 | return NULL; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | return buffer; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | |
---|
| 244 | MaxOrMinList* get_global_max_list(MaxOrMinList* buffer){ |
---|
| 245 | MaxOrMinList *local_max_list,local_max_list_buffer; |
---|
| 246 | int i,no_of_actual_global_max; |
---|
| 247 | double temp_glob_max; |
---|
| 248 | if(buffer!=NULL){ |
---|
| 249 | local_max_list_buffer.xvals = malloc(sizeof(double)); |
---|
| 250 | local_max_list_buffer.yvals = malloc(sizeof(double)); |
---|
| 251 | if(local_max_list_buffer.xvals!=NULL && local_max_list_buffer.yvals!=NULL){ |
---|
| 252 | local_max_list = get_local_max_list(&local_max_list_buffer); |
---|
| 253 | if(local_max_list != NULL){ |
---|
| 254 | if(local_max_list->no_of_actual_max_or_mins > 0){ |
---|
| 255 | temp_glob_max = local_max_list->yvals[0]; |
---|
| 256 | buffer->yvals[0] = temp_glob_max; |
---|
| 257 | buffer->xvals[0] = local_max_list->xvals[0]; |
---|
| 258 | buffer->no_of_actual_max_or_mins = 1; |
---|
| 259 | no_of_actual_global_max=1; |
---|
| 260 | if(local_max_list->no_of_actual_max_or_mins == 1){ |
---|
| 261 | return buffer; |
---|
| 262 | } |
---|
| 263 | for(i=1;i<local_max_list->no_of_actual_max_or_mins;i++){ |
---|
| 264 | if(fabs(temp_glob_max-local_max_list->yvals[i])<TOLERANCE){ |
---|
| 265 | ++no_of_actual_global_max; |
---|
| 266 | }else{ |
---|
| 267 | if(temp_glob_max < local_max_list->yvals[i]){ |
---|
| 268 | no_of_actual_global_max =1; |
---|
| 269 | temp_glob_max = local_max_list->yvals[i]; |
---|
| 270 | }else{ |
---|
| 271 | continue; |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | if(no_of_actual_global_max>1){ |
---|
| 275 | //Reallocate space if necessary... |
---|
| 276 | buffer->yvals = realloc(buffer->yvals,no_of_actual_global_max*sizeof(double)); |
---|
| 277 | buffer->xvals = realloc(buffer->xvals,no_of_actual_global_max*sizeof(double)); |
---|
| 278 | if(buffer->xvals == NULL || buffer->yvals == NULL){ |
---|
| 279 | printf("\nError: Could not Reallocate space for the Global Maxima List.\n"); |
---|
| 280 | return NULL; |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | buffer->yvals[no_of_actual_global_max-1] = temp_glob_max; |
---|
| 284 | buffer->xvals[no_of_actual_global_max-1] = local_max_list->xvals[i]; |
---|
| 285 | buffer->no_of_actual_max_or_mins = no_of_actual_global_max; |
---|
| 286 | } |
---|
| 287 | }else{ |
---|
| 288 | printf("\nThere are no Local Maxima, consequently a Global Maxima List cannot be generated\n"); |
---|
| 289 | buffer->no_of_actual_max_or_mins = 0; |
---|
| 290 | } |
---|
| 291 | }else{ |
---|
| 292 | printf("\nError: Could not obtain Local Maxima List\n"); |
---|
| 293 | free(local_max_list_buffer.xvals); |
---|
| 294 | free(local_max_list_buffer.yvals); |
---|
| 295 | return NULL; |
---|
| 296 | } |
---|
| 297 | free(local_max_list_buffer.xvals); |
---|
| 298 | free(local_max_list_buffer.yvals); |
---|
| 299 | }else{ |
---|
| 300 | printf("\nError: Could not allocate space for the list\n"); |
---|
| 301 | return NULL; |
---|
| 302 | } |
---|
| 303 | }else{ |
---|
| 304 | printf("\nError: Uninitialized Buffer\n"); |
---|
| 305 | } |
---|
| 306 | return buffer; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | |
---|
| 310 | MaxOrMinList* get_global_min_list(MaxOrMinList* buffer){ |
---|
| 311 | MaxOrMinList *local_min_list,local_min_list_buffer; |
---|
| 312 | int i,no_of_actual_global_min; |
---|
| 313 | double temp_glob_min; |
---|
| 314 | if(buffer!=NULL){ |
---|
| 315 | local_min_list_buffer.xvals = malloc(sizeof(double)); |
---|
| 316 | local_min_list_buffer.yvals = malloc(sizeof(double)); |
---|
| 317 | if(local_min_list_buffer.xvals!=NULL && local_min_list_buffer.yvals!=NULL){ |
---|
| 318 | local_min_list = get_local_min_list(&local_min_list_buffer); |
---|
| 319 | if(local_min_list != NULL){ |
---|
| 320 | if(local_min_list->no_of_actual_max_or_mins > 0){ |
---|
| 321 | temp_glob_min = local_min_list->yvals[0]; |
---|
| 322 | buffer->yvals[0] = temp_glob_min; |
---|
| 323 | buffer->xvals[0] = local_min_list->xvals[0]; |
---|
| 324 | buffer->no_of_actual_max_or_mins = 1; |
---|
| 325 | no_of_actual_global_min=1; |
---|
| 326 | if(local_min_list->no_of_actual_max_or_mins == 1){ |
---|
| 327 | return buffer; |
---|
| 328 | } |
---|
| 329 | for(i=1;i<local_min_list->no_of_actual_max_or_mins;i++){ |
---|
| 330 | if(fabs(temp_glob_min-local_min_list->yvals[i])<TOLERANCE){ |
---|
| 331 | ++no_of_actual_global_min; |
---|
| 332 | }else{ |
---|
| 333 | if(temp_glob_min > local_min_list->yvals[i]){ |
---|
| 334 | no_of_actual_global_min =1; |
---|
| 335 | temp_glob_min = local_min_list->yvals[i]; |
---|
| 336 | }else{ |
---|
| 337 | continue; |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | if(no_of_actual_global_min>1){ |
---|
| 341 | //Reallocate space if necessary... |
---|
| 342 | buffer->yvals = realloc(buffer->yvals,no_of_actual_global_min*sizeof(double)); |
---|
| 343 | buffer->xvals = realloc(buffer->xvals,no_of_actual_global_min*sizeof(double)); |
---|
| 344 | if(buffer->xvals == NULL || buffer->yvals == NULL){ |
---|
| 345 | printf("\nError: Could not Reallocate space for the Global Minima List.\n"); |
---|
| 346 | return NULL; |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | buffer->yvals[no_of_actual_global_min-1] = temp_glob_min; |
---|
| 350 | buffer->xvals[no_of_actual_global_min-1] = local_min_list->xvals[i]; |
---|
| 351 | buffer->no_of_actual_max_or_mins = no_of_actual_global_min; |
---|
| 352 | } |
---|
| 353 | }else{ |
---|
| 354 | printf("\nThere are no Local Minima, consequently a Global Minima List cannot be generated\n"); |
---|
| 355 | buffer->no_of_actual_max_or_mins = 0; |
---|
| 356 | } |
---|
| 357 | }else{ |
---|
| 358 | printf("\nError: Could not obtain Local Minima List\n"); |
---|
| 359 | free(local_min_list_buffer.xvals); |
---|
| 360 | free(local_min_list_buffer.yvals); |
---|
| 361 | return NULL; |
---|
| 362 | } |
---|
| 363 | free(local_min_list_buffer.xvals); |
---|
| 364 | free(local_min_list_buffer.yvals); |
---|
| 365 | }else{ |
---|
| 366 | printf("\nError: Could not allocate space for the list\n"); |
---|
| 367 | return NULL; |
---|
| 368 | } |
---|
| 369 | }else{ |
---|
| 370 | printf("\nError: Uninitialized Buffer\n"); |
---|
| 371 | } |
---|
| 372 | return buffer; |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | MaxOrMinList* get_max_list_bet_xvals(MaxOrMinList* buffer, double xval1, double xval2){ |
---|
| 376 | int i,no_of_actual_max=0,start_index=0,end_index=0; |
---|
| 377 | double slope_at_prev_point = INVALID_SLOPE,slope_at_point = INVALID_SLOPE; |
---|
| 378 | if(fabs(xval1-X_Axis_Values[0])<EPSILON && fabs(xval2-X_Axis_Values[NO_OF_POINTS-1])<EPSILON){ |
---|
| 379 | return get_local_max_list(buffer); |
---|
| 380 | } |
---|
| 381 | if(buffer!=NULL){ |
---|
| 382 | if(buffer->xvals!=NULL && buffer->yvals!=NULL){ |
---|
| 383 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 384 | if (fabs(X_Axis_Values[i]-xval1)<EPSILON){ |
---|
| 385 | start_index = i; |
---|
| 386 | } |
---|
| 387 | if(fabs(X_Axis_Values[i]-xval2)<EPSILON){ |
---|
| 388 | end_index = i; |
---|
| 389 | } |
---|
| 390 | } |
---|
| 391 | if(start_index == 0 || end_index == 0){ |
---|
| 392 | printf("\nError: Invalid XValues Provided\n"); |
---|
| 393 | return NULL; |
---|
| 394 | } |
---|
| 395 | for(i=start_index;i<=end_index;i++){ |
---|
| 396 | slope_at_prev_point = slope_calc(i-1); |
---|
| 397 | slope_at_point = slope_calc(i); |
---|
| 398 | if(slope_at_point != INVALID_SLOPE && slope_at_prev_point != INVALID_SLOPE){ |
---|
| 399 | if(slope_at_prev_point>0 && slope_at_point<=0){ |
---|
| 400 | ++no_of_actual_max; |
---|
| 401 | if(no_of_actual_max>1){ |
---|
| 402 | buffer->xvals = realloc(buffer->xvals,(sizeof(double))*(no_of_actual_max)); |
---|
| 403 | buffer->yvals = realloc(buffer->yvals,(sizeof(double))*(no_of_actual_max)); |
---|
| 404 | } |
---|
| 405 | buffer->yvals[no_of_actual_max-1]=Y_Axis_Values[i]; |
---|
| 406 | buffer->xvals[no_of_actual_max-1]=X_Axis_Values[i]; |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | } |
---|
| 411 | buffer->no_of_actual_max_or_mins = no_of_actual_max; |
---|
| 412 | }else{ |
---|
| 413 | printf("\nError: XValue And Yvalue Array Not Initialized"); |
---|
| 414 | return NULL; |
---|
| 415 | } |
---|
| 416 | }else{ |
---|
| 417 | printf("\nError: Buffer passed to function get_max_list_bet_xvals() is NULL\n"); |
---|
| 418 | } |
---|
| 419 | return buffer; |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | MaxOrMinList* get_min_list_bet_xvals(MaxOrMinList* buffer, double xval1, double xval2){ |
---|
| 423 | int i,no_of_actual_min=0,start_index=0,end_index=0; |
---|
| 424 | double slope_at_prev_point = INVALID_SLOPE,slope_at_point = INVALID_SLOPE; |
---|
| 425 | if(fabs(xval1-X_Axis_Values[0])<EPSILON && fabs(xval2-X_Axis_Values[NO_OF_POINTS-1])<EPSILON){ |
---|
| 426 | return get_local_min_list(buffer); |
---|
| 427 | } |
---|
| 428 | if(buffer!=NULL){ |
---|
| 429 | if(buffer->xvals!=NULL && buffer->yvals!=NULL){ |
---|
| 430 | for(i=0;i<NO_OF_POINTS;i++){ |
---|
| 431 | if (fabs(X_Axis_Values[i]-xval1)<EPSILON){ |
---|
| 432 | start_index = i; |
---|
| 433 | } |
---|
| 434 | if(fabs(X_Axis_Values[i]-xval2)<EPSILON){ |
---|
| 435 | end_index = i; |
---|
| 436 | } |
---|
| 437 | } |
---|
| 438 | if(start_index == 0 || end_index == 0){ |
---|
| 439 | printf("\nError: Invalid XValues Provided\n"); |
---|
| 440 | return NULL; |
---|
| 441 | } |
---|
| 442 | for(i=start_index;i<=end_index;i++){ |
---|
| 443 | slope_at_prev_point = slope_calc(i-1); |
---|
| 444 | slope_at_point = slope_calc(i); |
---|
| 445 | if(slope_at_point != INVALID_SLOPE && slope_at_prev_point != INVALID_SLOPE){ |
---|
| 446 | if(slope_at_prev_point<0 && slope_at_point>=0){ |
---|
| 447 | ++no_of_actual_min; |
---|
| 448 | if(no_of_actual_min>1){ |
---|
| 449 | buffer->xvals = realloc(buffer->xvals,(sizeof(double))*(no_of_actual_min)); |
---|
| 450 | buffer->yvals = realloc(buffer->yvals,(sizeof(double))*(no_of_actual_min)); |
---|
| 451 | } |
---|
| 452 | buffer->yvals[no_of_actual_min-1]=Y_Axis_Values[i]; |
---|
| 453 | buffer->xvals[no_of_actual_min-1]=X_Axis_Values[i]; |
---|
| 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | } |
---|
| 458 | buffer->no_of_actual_max_or_mins = no_of_actual_min; |
---|
| 459 | }else{ |
---|
| 460 | printf("\nError: XValue And Yvalue Array Not Initialized"); |
---|
| 461 | return NULL; |
---|
| 462 | } |
---|
| 463 | }else{ |
---|
| 464 | printf("\nError: Buffer passed to function get_min_list_bet_xvals() is NULL\n"); |
---|
| 465 | } |
---|
| 466 | return buffer; |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | |
---|
| 470 | //TODO Myself: Delete This function, it has been written only for testing |
---|
| 471 | |
---|
| 472 | void display_max_min_lists(){ |
---|
| 473 | MaxOrMinList list,*buffer; |
---|
| 474 | int i; |
---|
| 475 | double xval1,xval2; |
---|
| 476 | list.xvals = malloc(sizeof(double)); |
---|
| 477 | list.yvals = malloc(sizeof(double)); |
---|
| 478 | if(list.xvals!=NULL && list.yvals!=NULL){ |
---|
| 479 | buffer = get_local_max_list(&list); |
---|
| 480 | if(buffer!=NULL){ |
---|
| 481 | printf("\nDisplaying Local Maxima List........\n"); |
---|
| 482 | sleep(1); |
---|
| 483 | if(list.no_of_actual_max_or_mins == 0){ |
---|
| 484 | printf("The function has ZERO local maximas"); |
---|
| 485 | } |
---|
| 486 | for(i=0;i<list.no_of_actual_max_or_mins;i++){ |
---|
| 487 | printf("\nLocal Maxima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 488 | } |
---|
| 489 | printf("\n\n\n\n"); |
---|
| 490 | }else{ |
---|
| 491 | printf("\nLocal Max List Returned Null\n"); |
---|
| 492 | } |
---|
| 493 | sleep(1); |
---|
| 494 | buffer = get_local_min_list(buffer); |
---|
| 495 | if(buffer!=NULL){ |
---|
| 496 | printf("\nDisplaying Local Minima List........\n"); |
---|
| 497 | if(buffer->no_of_actual_max_or_mins == 0){ |
---|
| 498 | printf("The function has ZERO local maximas"); |
---|
| 499 | } |
---|
| 500 | for(i=0;i<buffer->no_of_actual_max_or_mins;i++){ |
---|
| 501 | printf("\nLocal Minima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 502 | } |
---|
| 503 | printf("\n\n\n\n"); |
---|
| 504 | }else{ |
---|
| 505 | printf("\nLocal Minima List returned Null\n"); |
---|
| 506 | } |
---|
| 507 | sleep(1); |
---|
| 508 | buffer = get_global_max_list(buffer); |
---|
| 509 | if(buffer!=NULL){ |
---|
| 510 | printf("\nDisplaying Global Maxima List..."); |
---|
| 511 | if(buffer->no_of_actual_max_or_mins == 0){ |
---|
| 512 | printf("The function has ZERO global maximas"); |
---|
| 513 | } |
---|
| 514 | for(i=0;i<buffer->no_of_actual_max_or_mins;i++){ |
---|
| 515 | printf("\nGlobal Maxima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 516 | } |
---|
| 517 | printf("\n\n\n\n"); |
---|
| 518 | }else{ |
---|
| 519 | printf("\nGlobal Maxima List returned Null\n"); |
---|
| 520 | } |
---|
| 521 | sleep(1); |
---|
| 522 | buffer = get_global_min_list(buffer); |
---|
| 523 | if(buffer!=NULL){ |
---|
| 524 | printf("\nDisplaying Global Minima List...\n"); |
---|
| 525 | if(buffer->no_of_actual_max_or_mins == 0){ |
---|
| 526 | printf("The function has ZERO global minimas"); |
---|
| 527 | } |
---|
| 528 | for(i=0;i<buffer->no_of_actual_max_or_mins;i++){ |
---|
| 529 | printf("\nGlobal Minima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 530 | } |
---|
| 531 | printf("\n\n\n\n"); |
---|
| 532 | }else{ |
---|
| 533 | printf("\nGlobal Minima List returned Null\n"); |
---|
| 534 | } |
---|
| 535 | sleep(2); |
---|
| 536 | printf("\nTo find Maxima between two Xvalues....\n"); |
---|
| 537 | printf("Enter the first X Value\n"); |
---|
| 538 | scanf("%lf",&xval1); |
---|
| 539 | printf("\nEnter the Second X Value\n"); |
---|
| 540 | scanf("%lf",&xval2); |
---|
| 541 | buffer = get_max_list_bet_xvals(buffer,xval1,xval2); |
---|
| 542 | if(buffer!=NULL){ |
---|
| 543 | printf("\n\n\n\nDisplaying List of Maxima Between %lf and %lf...",xval1,xval2); |
---|
| 544 | if(buffer->no_of_actual_max_or_mins == 0){ |
---|
| 545 | printf("The function has ZERO maxima between the entered XValues"); |
---|
| 546 | } |
---|
| 547 | for(i=0;i<buffer->no_of_actual_max_or_mins;i++){ |
---|
| 548 | printf("\nMaxima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 549 | } |
---|
| 550 | printf("\n\n\n\n"); |
---|
| 551 | }else{ |
---|
| 552 | printf("\nMaxima List returned Null\n"); |
---|
| 553 | } |
---|
| 554 | sleep(2); |
---|
| 555 | printf("\nTo find Minima between two Xvalues....\n"); |
---|
| 556 | printf("Enter the first X Value\n"); |
---|
| 557 | scanf("%lf",&xval1); |
---|
| 558 | printf("\nEnter the Second X Value\n"); |
---|
| 559 | scanf("%lf",&xval2); |
---|
| 560 | buffer = get_min_list_bet_xvals(buffer,xval1,xval2); |
---|
| 561 | if(buffer!=NULL){ |
---|
| 562 | printf("\n\n\n\nDisplaying list of Minima between %lf and %lf...",xval1,xval2); |
---|
| 563 | if(buffer->no_of_actual_max_or_mins == 0){ |
---|
| 564 | printf("The function has ZERO minima between the entered XValues"); |
---|
| 565 | } |
---|
| 566 | for(i=0;i<buffer->no_of_actual_max_or_mins;i++){ |
---|
| 567 | printf("\nMinima Number %d = %lf Corresponding to Xval of %lf",i+1,buffer->yvals[i],buffer->xvals[i]); |
---|
| 568 | } |
---|
| 569 | printf("\n\n\n\n"); |
---|
| 570 | }else{ |
---|
| 571 | printf("\nMinima List returned Null\n"); |
---|
| 572 | } |
---|
| 573 | free(list.xvals); |
---|
| 574 | free(list.yvals); |
---|
| 575 | } |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | int main(){ |
---|
| 579 | initdisplay(); |
---|
| 580 | return 0; |
---|
| 581 | } |
---|
| 582 | |
---|
| 583 | //Display related Jazz, totally unnecessary once actual interfacing to API is done TODO: Delete |
---|
| 584 | void initdisplay(){ |
---|
| 585 | int choice; |
---|
| 586 | double xval; |
---|
| 587 | printf("\n\n\n\n\n\n\n\n\n\n"); |
---|
| 588 | printf("********************************************************"); |
---|
| 589 | printf("PROGRAM FOR FITNESS MODULES"); |
---|
| 590 | printf("********************************************************\n"); |
---|
| 591 | printf("\n\n\n\n\n\n"); |
---|
| 592 | sleep(1); |
---|
| 593 | printf("This PROGRAM illustrates some of the modules written to enable a user to write his own fitness function\n"); |
---|
| 594 | sleep(1); |
---|
| 595 | printf("\nThree types of functions are available for testing\n"); |
---|
| 596 | sleep(1); |
---|
| 597 | printf("(1) Sine Function (y = sinx)\n(2) Exponential Decay Function (y = exp(-x))\n(3) Rapidly Exponentially decaying Sine Function (y = exp(-x)*sin(10x))\n"); |
---|
| 598 | sleep(1); |
---|
| 599 | printf("\nEnter Your Choice\n"); |
---|
| 600 | scanf("%d",&choice); |
---|
| 601 | InitializeCurves(choice); |
---|
| 602 | sleep(1); |
---|
| 603 | printf("\n\nEnter a point BETWEEN %lf and %lf at which you would like slope to be found.....\n",X_Axis_Values[0],X_Axis_Values[NO_OF_POINTS-1]); |
---|
| 604 | scanf("%lf",&xval); |
---|
| 605 | sleep(1); |
---|
| 606 | printf("Slope at %lf is %lf\n", xval, slope_at_x_temp(xval)); |
---|
| 607 | sleep(1); |
---|
| 608 | if(max_at_x_temp(xval)){ |
---|
| 609 | printf("There is a maxima at given XVal\n"); |
---|
| 610 | }else if(min_at_x_temp(xval)){ |
---|
| 611 | printf("There is a minima at given Xval\n"); |
---|
| 612 | }else{ |
---|
| 613 | printf("There is neither a maxima nor a minima at given XVal\n"); |
---|
| 614 | } |
---|
| 615 | display_max_min_lists(); |
---|
| 616 | |
---|
| 617 | } |
---|