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