fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <math.h>
  6. #include"mersenne.h"
  7.  
  8. /* Control Parameters of the search algorithm*/
  9. #define POP_SIZE 50 /* The number of candidate solutions*/
  10. #define MAX_ITER 300 /*The number of iterations*/
  11.  
  12. //Problem definitions
  13. #define DIM 20 //number of problem variables. Number of BITS. <<<<<<<MUDAR PARA CADA INSTANCIA!!!!!!!!!!!
  14. #define RUN 10 /*Algorithm can run many times in order to see its robustness*/
  15.  
  16. #define PM 0.1 //mutation factor
  17. #define CR .5 //PR parameter (Perturbation Rate)---crossing over factor---------------/
  18.  
  19. //Global variables
  20. int pop[POP_SIZE][DIM]; //population of candidate solutions.
  21. double fo[POP_SIZE]; //objective function value.
  22. int best[DIM]; //best solution found
  23. double bestfo[0]; //best fo value
  24. int best_index[0]; //index for the best solution
  25.  
  26. int U[DIM]; //solution to be evaluated
  27. int nfeval; /* number of function evaluations */
  28. double Best[RUN];//best fitness in each run.
  29.  
  30. //Functions declarations
  31. void AvgStdDev(double *Avg,double *StdDev,double Var[]);
  32. double randon( double inferior, double superior);
  33. void DE(int r);
  34.  
  35. double bde_fitness(int sol[DIM]);
  36.  
  37. void assignd(int D, int a[], int b[]);
  38.  
  39. double randon( double inferior, double superior)
  40. {
  41. // double aux = (float)inferior + ((superior - inferior)*rand()/(RAND_MAX+1.0));
  42. double aux = (float)inferior + ((superior - inferior)*MT_randInt(RAND_MAX)/(RAND_MAX+1.0));
  43.  
  44. return aux;
  45. }
  46.  
  47. /*Main program of the search algorithm*/
  48. int main()
  49. {
  50. int i, j, k, r;
  51. double avg,
  52. std;
  53.  
  54. srand(time(NULL));
  55. MT_seed();
  56.  
  57. nfeval = 0;
  58. for (r=0;r<RUN;r++)
  59. {
  60. printf("RUN %d\n", r);
  61.  
  62. //Init population
  63. for (j=0;j<POP_SIZE;j++)//each individual
  64. {
  65. fo[j] = 0.0;
  66. for (k=0; k<DIM;k++) //each dimension
  67. {
  68. best[k] = 0;
  69. pop[j][k] = (int)round(randon(0,1)); //binary population
  70. }
  71. }
  72. //Objective function calculation
  73. for (i = 0;i<POP_SIZE;i++)
  74. {
  75. for (j = 0;j<DIM;j++)
  76. U[j] = pop[i][j];
  77.  
  78. fo[i] = bde_fitness(U); //add your optimization problem
  79. }
  80.  
  81. //Best current solution identification.
  82. bestfo[0] = fo[0];
  83. best_index[0] = 0;
  84. for(i=0;i<POP_SIZE;i++)
  85. {
  86. if (fo[i]>=bestfo[0]) //>= for maximization //<= for minimization
  87. {
  88. Best[r] = fo[i];
  89. bestfo[0]=fo[i];
  90. for(j=0;j<DIM;j++)
  91. best[j]=pop[i][j];
  92.  
  93. best_index[0] = i;
  94. }
  95. }
  96.  
  97. DE(r); //evolutionary loop
  98.  
  99. //print best infividual and its fitness
  100. printf("Best individual. FO: %.2f\n", Best[r]);
  101. for (j = 0; j<DIM; j++)
  102. printf("%d ", best[j]);
  103. printf("\n");
  104.  
  105. }//end FOR RUN
  106.  
  107. AvgStdDev(&avg,&std,Best);
  108. printf("Average Best: %.2f +/- %.2f\n", avg, std);
  109. printf("Average number of function evaluations: %.f\n", (double)nfeval/RUN);
  110.  
  111. return 0;
  112. }//end MAIN
  113.  
  114. void AvgStdDev(double *Avg,double *StdDev,double Var[])
  115. {
  116. int i;
  117.  
  118. *Avg = 0;
  119. *StdDev = 0;
  120. for (i=0;i<RUN;i++)
  121. *Avg += Var[i];
  122. *Avg /= RUN;
  123.  
  124. for (i=0;i<RUN;i++)
  125. {
  126. *StdDev += pow((*Avg-Var[i]),2);
  127. }
  128. *StdDev /= RUN;
  129. *StdDev = sqrt(*StdDev);
  130. }
  131.  
  132. double bde_fitness(int sol[DIM])
  133. {
  134. int i, j, k;
  135. float FObj;
  136.  
  137. FObj = 0;
  138. // this simple problem concerns to find the individual with 1's and 0's interchanged. Maximization problem!
  139. for (i=0; i<(DIM-1); i++)
  140. {
  141. if (sol[i] != sol[i+1])
  142. FObj++;
  143. }
  144.  
  145. nfeval++; //update global variable
  146. return FObj;
  147. }
  148.  
  149. //Differential Evolution Code
  150. void DE(int r)
  151. {
  152. int i, j, L, n; /* counting variables */
  153. int r1; /* placeholders for random indexes */
  154.  
  155. int gen;
  156.  
  157. double trial_cost; /* buffer variable */
  158. int tmp[DIM];
  159.  
  160. /*-----Initialize random number generator-----------------------------*/
  161.  
  162. srand(time(NULL));
  163. MT_seed();
  164.  
  165. /*=======================================================================*/
  166. /*=========Iteration loop================================================*/
  167. /*=======================================================================*/
  168. gen = 0; /* generation counter reset */
  169. while (gen < MAX_ITER)
  170. { /* is accepted by compiler */
  171. gen++;
  172.  
  173. for (i=0; i<POP_SIZE; i++) /* Start of loop through ensemble */
  174. {
  175. /* Pick a random population member */
  176. r1=(int)( ((double)rand() / ((double)(RAND_MAX)+(double)(1)) ) * POP_SIZE);
  177. if (r1 == POP_SIZE)
  178. r1 = POP_SIZE-1;
  179.  
  180. /*-------BDE---------------------------------------------------------------------------------------*/
  181. /*-------Model 1: DE/rand/1/bin--------------------------------------------------------------------*/
  182. /*-------Model 2: DE/best/1/bin--------------------------------------------------------------------*/
  183. assignd(DIM,tmp,pop[i]);
  184. n = (int)( ((double)rand()/((double)(RAND_MAX)+(double)(1)) ) * DIM);
  185. for (L=0; L<DIM; L++) /* perform D binomial trials */
  186. {
  187. if (((double)rand()/((double)(RAND_MAX)+(double)(1)) < CR) || L == (DIM-1)) /* change at least one parameter */
  188. {
  189. if ( randon(0.0,1.0) < PM)//mutation occurs
  190. {
  191. if (tmp[n]==0)
  192. tmp[n] = 1;
  193. else tmp[n] = 0;
  194. }
  195. /*Two models for information exchange. Comment or uncomment the following lines.
  196.   Model 1: DE/rand/1/bin. Moves toward a randon solution
  197.   Model 2: DE/best/1/bin. moves toward best solution in current population */
  198. // else tmp[n] = pop[r1][n];//moves toward a randon solution. Model 1.
  199. else tmp[n] = best[n]; //moves toward the best solution in current population. Model 2.
  200. }
  201. n = (n+1)%DIM;
  202. }
  203.  
  204. /*=======Trial mutation now in tmp[]. Test how good this choice really was.==================*/
  205. for (L=0; L<DIM; L++) /* check bounds */
  206. {
  207. if (tmp[L] > 1)
  208. tmp[L] = 1;
  209. if (tmp[L] < 0)
  210. tmp[L] = 0;
  211. }
  212.  
  213. for (j = 0;j<DIM;j++)
  214. U[j] = tmp[j];
  215.  
  216. trial_cost = bde_fitness(U);
  217. /* Evaluate new vector in tmp[] */
  218. if (trial_cost >= fo[i]) /* >= for maximization. improved objective function value */
  219. {
  220. fo[i]=trial_cost;
  221.  
  222. for (j=0;j<DIM;j++)
  223. pop[i][j] = tmp[j];
  224.  
  225. if (trial_cost > bestfo[0]) //> for maximizatio
  226. { /* if so...*/
  227. bestfo[0]=trial_cost; /* reset cmin to new low...*/
  228. best_index[0]=i;
  229.  
  230. for (j=0;j<DIM;j++)
  231. best[j] = tmp[j];
  232.  
  233. Best[r] = bestfo[0];
  234. }
  235. }
  236. else
  237. {
  238. //DO NOTHING!!!
  239. }
  240.  
  241. }//END FOR End mutation loop through pop.
  242. }//end WHILE
  243. }
  244. /*-----------End of DE()------------------------------------------*/
  245.  
  246. void assignd(int D, int a[], int b[])
  247. /**C*F****************************************************************
  248. ** **
  249. ** Assigns D-dimensional vector b to vector a. **
  250. ** You might encounter problems with the macro ASSIGND on some **
  251. ** machines. If yes, better use this function although it's slower. **
  252. ** **
  253. ***C*F*E*************************************************************/
  254. {
  255. int j;
  256. for (j=0; j<D; j++)
  257. {
  258. a[j] = b[j];
  259. }
  260. }
  261.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:6:9: fatal error: 'mersenne.h' file not found
#include"mersenne.h"
        ^
1 error generated.
stdout
Standard output is empty