Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. //Default population size. Also the number of lines on the array
  6. int size = 500;
  7. //Declaring functions
  8. void popstart();
  9. int fitness(int ** vet , int j);
  10. int arraymult(int ** vet ,int * vet2, int j);
  11.  
  12. //starts filling the population array with size lines and 6 collumns. The last collumn will be the fitness value of the entire line
  13. void popstart() {
  14. //seeding
  15. srand(time(NULL));
  16. //line and collumn counter
  17. int i = 0;
  18. int j = 0;
  19. //population matrix
  20. int pop [size][6];
  21.  
  22. //starts filling pop with random values ranging from (0 to 177)+5
  23. for (j=0;j < size;j++) {
  24. for (i=0;i < 5;i++) {
  25. pop[j][i] = (((double)rand() / RAND_MAX) * 177.0)+5;
  26. printf("Generated:%d,%d: %d\n",j,i, pop[j][i]);
  27. }
  28. //I start calculating the fitness as soon as possible so I don't loop too many times around the array and take advantage of already being in the line, quickening the process.
  29. pop[j][6] = fitness(pop,j);
  30. }
  31. }
  32.  
  33. //function to calculate fitness. Receives pop and the current value of J to know what line it's being calculated on.
  34. int fitness(int ** vet , int j){
  35. // fitness will be returned. pmax is the size... I should be using size instead. Penalty is by how much it will hurt the total score.
  36. int fitness;
  37. int pmax = size;
  38. int penalty = 20;
  39.  
  40. //weigth array.
  41. int wg[5] = {2, 4, 5, 7, 12};
  42. //value array.
  43. int value[5] = {3, 6, 10, 18, 26};
  44.  
  45. //I multiply the values Eg:(vet1[j][1]*vet2[1]) and sum them all.
  46. int wgcromo = arraymult(vet,wg,j);
  47. int valuecromo = arraymult(vet,value,j);
  48. //I see if the current value is inside the range I want. Else I penalize the total score.
  49. if (wgcromo<pmax) fitness = valuecromo;
  50. else fitness = valuecromo - ( wgcromo - pmax) * penalty;
  51.  
  52. return fitness;
  53. }
  54.  
  55. //I multiply the values Eg:(vet1[j][1]*vet2[1]) and sum them all.
  56. int arraymult(int ** vet ,int * vet2, int j){
  57. int result=0;
  58. for (int i=0;i < 5;i++) {
  59. result = result +(vet[j][i]*vet2[i]);
  60. }
  61. return result;
  62. }
  63.  
  64. int main(){
  65.  
  66. popstart(size);
  67. return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement