Free2Code
The majority of forums are now only available as archives, which means posting/editing is disabled.

The Anything and Everything forum is still open.
 
Time: 2013-05-18, 04:38am
debug error using struct
Subject: debug error using struct  ·  Posted: 2006-03-08, 02:03am
Rank: ? (4)
Member #: 26866
Code:
  1. #include "def_struct.c"
  2. int main()
  3. {
  4. float Max_Eval;
  5. int level =0;
  6. result=PSO(level, Max_Eval);
  7. }
  8. where pso is a structure
  9. struct position PSO(int level, float Max_Eval) 
  10. {
  11. int add_option=1; 
  12. int already;
  13. .........
  14. }


and struct position is defined in def_struct.c
Code:
  1. struct position {struct vector p;struct f f;};
  2. struct position PSO(int level, float Max_Eval);


When I debug the code,the program throws an exception while trying to call pso.Can anyone help me in this regard.


» Post edited 2006-03-10, 03:19am by misterhaan.

 
  Reply to this ·  Post link ·  Top
Subject: Re: debug error using struct  ·  Posted: 2006-03-08, 03:57am
Rank: ? (4827)
Member #: 3416
the error is that PSO comes after main, and you didn't put a prototype in before main. that means PSO doesn't exist yet when you're running main.

try adding this before int main:

struct position PSO(int, float);

my mind is like a steel trap! it only hangs on to the big stuff. visit my forums at track7.org
 
  Reply to this ·  Post link ·  Top
Subject: Re: debug error using struct  ·  Posted: 2006-03-08, 09:26pm
Rank: ? (4)
Member #: 26866
I tried to place a prototype before main but the problem persists.

 
  Reply to this ·  Post link ·  Top
Subject: Re: debug error using struct  ·  Posted: 2006-03-09, 08:50am
Rank: ? (768)
Member #: 11085
well, your problem could be that you are trying to include a .c file, so you can try renaming that to def_struct.h. Beyond that, I can't help you without seeing the rest of your code.

- relpats_eht
 
  Reply to this ·  Post link ·  Top
Subject: Re: debug error using struct  ·  Posted: 2006-03-09, 11:10pm
Rank: ? (4)
Member #: 26866
here is the detailed code
Code:
  1. #include "C:\Dokumente und Einstellungen\pappala\Desktop\Tribes_research\def_struct.H"
  2.  int main()
  3. {
  4. int           bidon;
  5. int                i,j;
  6. int            level;
  7. float   Max_Eval;
  8. int                nb_pb;
  9. struct position result;
  10. pi=(double)2*acos(0);
  11. two_pi=2*pi;
  12. // Initialize function names. Just for display
  13. f_functs=fopen("Z:/functions.txt","r");
  14. i=1;
  15. next_funct:
  16.     fscanf(f_functs,"%i %s\n",&bidon,&functions[i]);
  17.   if (bidon!=-1)
  18.   {
  19.       printf("\n %i %s",bidon,functions[i]);
  20.     i=i+1;
  21.     goto next_funct;
  22.   }
  23. printf("\n-----------------------------------\n");
  24. fclose (f_functs);
  25.  
  26. //===================  Other files  (keep them open during the whole process)
  27. f_problem=fopen("Z:/problems.txt","r");
  28. f_run=fopen("run.txt","w");
  29. f_run_c=fopen("run_c.txt","w");
  30. f_swarm=fopen("swarm.txt","w");
  31. f_synth=fopen("synth.txt","w");
  32. f_trace=fopen("trace.txt","w");
  33. f_energy=fopen("energy.txt","w");
  34. f_discrete=fopen("discrete.txt","r");
  35. f_trace_run=fopen("trace_run.txt","w");
  36. //-----------------
  37. // Read strategies  (for tests. Will be hard coded later) . See move_particle()
  38. for (i=0;i<2;i++)  
  39. {
  40.       for (j=0;j<Max_status;j++)
  41.          fscanf(f_problem, "%f",&strategies[i][j]);
  42. }
  43. fscanf(f_problem,"%i",&confin_interv); // If <0, no interval (boundary) confinement
  44. fscanf(f_problem,"%i",&circular_hood); // Option. Usually equal to 0.
  45. // If >0,  indicates the size of the circular neighbourhood
  46. //    This option is just to rapidly compare with parametric PSO
  47. if (circular_hood<0) 
  48. {
  49.     if (circular_hood<-1)
  50.     {
  51.         circular_hood=-circular_hood;
  52.         rand_hood=circular_hood;
  53.     }
  54.     else
  55.     {                  
  56.         //rand_hood=K_optim(problem[level].init_size,problem[level].eps); 
  57.         // Just for info. : init_size and eps are not yet known
  58.     }
  59. }
  60. else 
  61. {
  62.     rand_hood=0;  
  63. }
  64. // Check whether positions have to be memorized or not
  65. MEMO=0;
  66. for (j=0;j<Max_status;j++)
  67. {
  68.    if (strategies[0][j]==18 || strategies[0][j] ==19) MEMO=1;
  69. }
  70. fscanf(f_problem, "%i",&no_best);
  71. fscanf(f_problem,"%i",&adapt); // Usually 0. If >0, it is a constant swarm size
  72. fscanf(f_problem,"%i",&linkreorg);
  73. fscanf(f_problem,"%i",&nb_pb_max); // Number of different problems
  74. nb_pb=0;
  75. problem:
  76. printf("\n Problem number %i",nb_pb);
  77. // Set level to the main one (normal case)
  78.  level=0;
  79.  
  80.   // Description of the problem(s)
  81.   problem[level]=read_problem(f_problem,f_data,level); // READ THE PROBLEM DESCRIPTION
  82.    if (TSP==1 || QAP==1) // For TSP or QAP problem, the graph has been read in read_problem
  83.                                        // Check it if is consistent with the data
  84.    {
  85.           if(problem[level].DD!=problem[level].P.size)
  86.           {
  87.             printf("\n ERROR. Graph size %i not consistent with problem data  %i",problem[level].P.size, problem[level].DD);
  88.            return EXIT_SUCCESS;
  89.           }
  90.    }
  91.   display_problem(level);
  92.    times=0; // you can run several times the same problem (problemeter problem[level].times)
  93.    // Print titles on the f_run file
  94.   fprintf(f_run,"Function Run Target"  ;
  95.   for (i=0;i<problem[level].nb_f;i++) fprintf (f_run," f%i",i+1);
  96.   fprintf(f_run," Eval_nb Duration Added Removed"  ;    
  97.    for (i=0;i<problem[level].DD;i++) fprintf( f_run," x%i",i+1);
  98.   if (problem[level].funct==11) // Apples trees (more generally, use of homogeneous coordinates
  99.   {
  100.     fprintf(f_run," Real position");
  101.     for (i=1;i<problem[level].DD-1;i++) fprintf( f_run," _");
  102.    }
  103.    
  104.   // Print titles on the f_synth file
  105.   fprintf(f_synth,"Function Dim. Target Wanted_error Nb_of_runs Mean_eval._nb Std_dev") ;
  106.   fprintf(f_synth," Success_rate");
  107.   fprintf(f_synth," Min_eval Max_eval Mean_swarm Mean_error Std_dev Mean_error_cont Std_dev Min_error Max_error Duration") ;
  108.   problem[1]=problem[0];
  109.   problem[1].DD=1;
  110.   // Compute coefficients for some mouve options
  111.       coeff_S_C=coeff_SC(problem[level].DD);
  112.     phi=2/0.97725;
  113.     khi=1/(phi-1+sqrt(phi*phi-2*phi));
  114.     cmax=khi*phi;
  115.   // Display function and dimension
  116.   printf("\n%s %iD",functions[problem[level].funct],problem[level].DD);
  117.   seed_rand_kiss(1); // Initialize pseudo random number generator
  118. nprogr=1; // For progressive problems like Neural Network Training
  119. //core:
  120. for (Max_Eval=problem[level].Max_Eval;fabs(Max_Eval)<=fabs(problem[level].Max_Eval_2);Max_Eval=Max_Eval+problem[level].Max_Eval_delta)
  121. {
  122.    // Arbitrary very high values for best of the best, to begin
  123.   for (i=0;i<problem[level].nb_f;i++)  BEST.f.f[i]=infinite;
  124.   printf("\n**********\n Run PSO with Max_Eval= %.0f",Max_Eval);
  125.   times=0;
  126.   result=PSO(level, Max_Eval); // ******* CORE OF THE PROGRAM *******
  127. }
  128. //nprogr=nprogr+1; if (nprogr<=PROGR) goto core;
  129. nb_pb=nb_pb+1; if(nb_pb<nb_pb_max) goto problem;
  130.   return EXIT_SUCCESS;
  131. }
  132. /*============================ S U B R O U T I N E S =====================================*/
  133. /* Except the first one, they are in alphabetical order                                   */
  134. struct position PSO(int level, float Max_Eval)  // Main routine.
  135.  {
  136. double         eps_run[Max_run][2];
  137. double         eval_run[Max_run];
  138. double      strateg[nb_strateg]; // Just for information (see move_particle)
  139. struct i_group    ig,igt;
  140. struct    particle    part_best;
  141. struct f     previous_best;
  142. struct particle    partg;
  143. struct    particle    partt;
  144. struct position   post;
  145. struct f         f0,f1;
  146. int        already;
  147. int             bad;
  148. //struct position best_result;
  149. int         better;
  150. int            chance;
  151. int                cycle,cycle_size;
  152. int                d;
  153. //int                dim;
  154. double         duration;
  155. double         duration_tot=0;
  156. double         Ek; // Kinetic energy
  157. double            Ep; // Potential energy
  158. double         error,error_cont;
  159. int                failure_tot;
  160. int             good;
  161. int                i,i0;
  162. int             Iter;
  163. double            improv;
  164. int                  k;
  165. int                j;
  166. int               l;
  167. int         label_best,label_worst;
  168. int         labelt;
  169. int         m;
  170. int               max_add;
  171. double            max_eps;
  172. double               max_f;
  173. double            mean_eps,mean_eps_cont;
  174. double          mean_eval;
  175. double            mean_swarm_size;
  176. double            min_eps;
  177. int                n_add;
  178. int             n_connect;
  179. int             n_local_remove;
  180. int                n_remove;
  181. double            nb_eval_max;
  182. double            nb_eval_min;
  183. double            nb_no_progress;
  184. int                     new_label;
  185. double               pr;
  186. int                     rank;
  187. int                     rank_best,rank_worst;
  188. double               sigma_eps,sigma_eps_cont;
  189. double               sigma_eval;
  190. int                     size;
  191. int            swarm_size;
  192. clock_t         ticks0,ticks1, ticks2;
  193. int                     TRsize0;
  194. //double            volume; // Just for info
  195. //double            x_max,x_min;
  196. //double      x1,x2;
  197. //int                worst[Max_swarmsize]; // Label of worst particle in each tribe
  198. double            z,zzz;
  199. int        add_option=1; // 0 => generate just random particles //  1 => generate also a hopefully good one 
  200.  zzz=0;
  201. nb_eval_max=0;
  202. nb_eval_min=infinite;
  203. failure_tot=0; // Number of failures
  204. min_eps=infinite;   // This is a _minimization_ problem
  205. mean_eps=0;
  206. max_eps=0;
  207. for (i=0;i<nb_strateg;i++) strateg[i]=0; // Just for information about the strategies used
  208. for (i=0;i<Max_status;i++) status_count[i]=0; // Just for information about particle status
  209. //======================================== You may solve several times the same problem
  210.  
  211. times_loop:
  212.  memo[level].size=0;
  213.  /* Useful only when using pivot method 
  214.      Note: for implementation of a future ReHope method (Restart)
  215.      it would be interesting to put this instruction BEFORE the restart loop
  216.      in order to keep memory of best results. In such a case, you also have to put the
  217.       other "... =0" before the loop 
  218.  */
  219. eval_f_tot[level] =0; // Number of objective function evaluations
  220. eval_f[level]=0; // Number of evaluations counter
  221. offline_error=0; // Useful only for dynamic optimisation 
  222. offline_error_cont=0;
  223.  ticks1=clock(); // Just for information. To evaluate processor time needed
  224. chance=0; // Equal to 1 if a solution is found
  225. nb_no_progress=0;// If this number becomes > parapet, then => Failure
  226. Iter=1;        // Number of iterations. Just for information
  227. n_add=0;
  228. n_change=1;
  229. n_remove=0;
  230. mean_swarm_size=0;
  231. label[level]=0; // Label of the first particle. Just for later visualisation
  232.  ticks0=clock();
  233. Xmin.size=problem[level].DD;
  234. Xmax.size=Xmin.size;
  235.  for(d=0;d<Xmin.size;d++) // Initialize Xmin and Xmax
  236.  { 
  237.      Xmin.x[d]=problem[level].H.min[d];
  238.      Xmax.x[d]=problem[level].H.max[d];
  239.  }
  240. //srand((unsigned)time(NULL)); // Re initialize random number generation, according to the time
  241. //srand(1);  // Re initialize random number generation with the same seed
  242.     // Check improvement every cycle_size time steps
  243.     cycle_size=problem[level].init_size; // Initial value
  244.                             // ******** SWARM INITIALIZATION *********
  245. if (problem[level].printlevel>=0)
  246.    printf("\n------------------------------------------------------------------------------------------------------------");
  247. if (problem[level].printlevel>0)
  248.     printf(" \n Run %i/%i",times+1,problem[level].times);
  249.    
  250. // Initialize the first tribe
  251. best_result.p.size= problem[level].DD;
  252. best_result.f.size= problem[level].nb_f;
  253. previous_best.size=problem[level].nb_f;                                                                 
  254. TR[level].size=1; // Number of tribes
  255. TR[level].tr[0].size=problem[level].N; // Tribe size
  256.  max_f=0;
  257.  // Generate particles and keep the best result found so far
  258.  //printf("\n Run %i",times+1);
  259.  if (TSP==1)  // Special initialization for TSP
  260.  {
  261.     TR[level].tr[0]=init_swarm_tsp(problem[level].N,problem[level].target,problem[level]. printlevel,level);
  262.     goto end_init;
  263.  }
  264.   if (QAP==1)  // Special initialization for QAP
  265.  {
  266.     TR[level].tr[0]=init_swarm_qap(problem[level].N,problem[level].target,problem[level].printlevel,level);
  267.     goto end_init;
  268.  }
  269.  // Normal initialization (random)
  270.    for (i=0;i<TR[level].tr[0].size;i++)
  271.    {
  272.     TR[level].tr[0].part[i]=init_particle(0,level,dummy_part);
  273.    }
  274.    
  275.  end_init:    // End of initialisation
  276. //   display_swarm(1,level);
  277. best_result=TR[level].tr[0].part[0].x;
  278. previous_best=best_result.f;
  279. for (i=1;i<TR[level].tr[0].size;i++)
  280. {
  281. //display_position( TR[level].tr[0].part[i].x);
  282.    // Look for the best result
  283.     better=better_than(TR[level].tr[0].part[i].x.f, best_result.f,level);
  284.      if (better==1)
  285.     {
  286.       previous_best= best_result.f;
  287.     best_result=TR[level].tr[0].part[i].x;
  288.     }
  289.     offline_error_cont+=total_error(best_result.f);
  290. }  // next i
  291. if (BIN==1) 
  292.     for (d=0;d<best_result.p.size;d++) 
  293.         best_result.p.x[d]=floor(best_result.p.x[d]+0.5);
  294.     // Memorize the best value found so far
  295.         error=total_error(best_result.f); 
  296.         offline_error=error;
  297.         offline_error_cont=error;
  298.     if (error<min_eps) min_eps=error;
  299. // In case of progressive approach, keep the best of the best
  300. if (nprogr>1)    TR[level].tr[0].part[0].x=BEST;
  301.  // Display best result
  302. //if (problem[level].printlevel>0)
  303.  {
  304.     printf("\n Best result after initialization");
  305.     display_position(best_result);
  306.     printf("\n");
  307.  }
  308.  // Check if solution found by chance
  309. if (DYN==0)  if (error<=problem[level].eps) goto end_by_chance;
  310.  // Memorize positions   ("Black board" optional method)
  311.  if (MEMO==1)
  312.  {
  313.     for (i=0;i<TR[level].tr[0].size;i++)
  314.     {
  315.        add_memo(TR[level].tr[0].part[i].x,level);
  316.     }
  317.  }
  318.  
  319. // Initialize i-groups
  320. if (circular_hood>0) goto circular; // Option to simulate classical PSO
  321. if (rand_hood>0) goto rand_i_group;
  322.     // First particle
  323. TR[level].tr[0].part[0].ig.size=TR[level].tr[0].size; // i-group = all particles
  324. for (j=0;j<TR[level].tr[0].part[0].ig.size;j++)
  325. {
  326.     TR[level].tr[0].part[0].ig.label[j]=TR[level].tr[0].part[j].label;
  327. }
  328.     // Same i-group for all the others
  329. for (i=1;i<TR[level].tr[0].size;i++)
  330.    TR[level].tr[0].part[i].ig=TR[level].tr[0].part[0].ig;
  331.  goto before_cycles;
  332.  //------------------------------
  333.  circular: // W A R N I N G
  334.  // Valid only for constant swarm size option (and, then, just one tribe, rank 0)
  335.   size= TR[level].tr[0].size;
  336.  for (i=0;i<size;i++)
  337.  {
  338.     TR[level].tr[0].part[i].ig.size=circular_hood;
  339.     for (j=0;j<TR[level].tr[0].part[i].ig.size;j++)
  340.     {
  341.         rank=i+j-(int)(circular_hood/2.0);
  342.         if (rank<0) rank=rank+size;
  343.         else
  344.            if (rank>=TR[level].tr[0].size)  rank=rank-size;
  345.         
  346.        TR[level].tr[0].part[i].ig.label[j]=TR[level].tr[0].part[rank].label;
  347.     }
  348. }
  349.  goto before_cycles;
  350. //--------------------------  Option random i-groups
  351. rand_i_group:
  352. swarm_size=problem[level].init_size;
  353. for(k=0;k<TR[level].size;k++)
  354. {
  355.      size= TR[level].tr[k].size;
  356.      for (i=0;i<size;i++)
  357.      {
  358.         TR[level].tr[k].part[i].ig.size=rand_hood;
  359.         for (j=0;j<TR[level].tr[k].part[i].ig.size;j++)
  360.         {
  361.            rank=alea(0,swarm_size);
  362.            TR[level].tr[k].part[i].ig.label[j]=TR[level].tr[k].part[rank].label;
  363.         }
  364.      }
  365. }
  366. //------------------------
  367. before_cycles:
  368.    if (problem[level].save==-1)  // Save swarm size and energies
  369.                                         // Just for information
  370.         {
  371.            Ek=energy_k(level); Ep=energy_p(level);
  372.            swarm_size=problem[level].init_size+n_add-n_remove; 
  373.            fprintf(f_energy,"\n%i %i %i %i %f %f",(int)eval_f_tot[level],swarm_size,n_add,n_remove,Ek,Ep);
  374.         }
  375.     if (problem[level].save==-2) // Save info for convergence curve plotting
  376.     {
  377.         fprintf(f_trace_run,"Eval error error_cont n_add");
  378.         fprintf(f_trace_run,"\n%.0f %f %f 0",eval_f_tot[level],error,error);
  379.     }
  380. recent_change=0;  // For Moving Peaks
  381. // cycles:
  382. cycle=0;  // Number of iterations since last adaptive update
  383.                                                                                                                 // ******** SWARM MOVES *********
  384. do_Iter: //termination criterion is (currenteps<=eps or eval_f_tot>stop)
  385.     if (problem[level].printlevel>0)
  386.     {
  387.         printf("\n  Best result after %.0f evaluations:  ",eval_f_tot[level]);
  388.          for (i=0;i<best_result.f.size;i++) printf (" %f",best_result.f.f[i]); 
  389.          if (DYN==1) printf("\n offline error %f",offline_error/n_change);
  390.       
  391.        }
  392.      if (problem[level].printlevel>1)
  393.      {
  394.         display_position (best_result);
  395.         printf("\n------ MOVING the %i tribe(s)",TR[level].size);
  396.     }
  397.     if (problem[level].printlevel>1) 
  398.     {
  399.         printf("\n %i tribe(s)",TR[level].size);
  400.     }
  401.     if (problem[level].save>=2) fprintf(f_trace,"\n %i tribe(s)",TR[level].size);
  402.     
  403.     for (i=0;i<TR[level].size;i++) // For each tribe ...
  404.     {
  405.         if (problem[level].printlevel>1)
  406.         {
  407.             display_tribe(f_trace,TR[level].tr[i],0,level);
  408.         }
  409.         if (problem[level].printlevel>2)
  410.         {
  411.             for (j=0;j<TR[level].tr[i].size;j++)
  412.                 display_i_group(f_trace,TR[level].tr[i].part[j],0);
  413.         }
  414.         if (problem[level].save>0)
  415.         {
  416.             fprintf(f_trace,"\n  tribe %i",i);
  417.             display_tribe(f_trace,TR[level].tr[i],1,level);
  418.         }
  419.         for (j=0;j<TR[level].tr[i].size;j++) // ... for each particle ...
  420.         {
  421.          // Find the "best informer" (the true one, or at random)
  422.             partg=best_informer(TR[level].tr[i].part[j],no_best,level);
  423.          // ******************* MOVE ************************;
  424.             TR[level].tr[i].part[j]=move_particle(TR[level].tr[i].part[j],partg,level,0); 
  425.             
  426.             if(MEMO==1)  add_memo(TR[level].tr[i].part[j].x,level);
  427.              
  428.             // Eventually update the best result
  429.                better=better_than(TR[level].tr[i].part[j].p_i.f, best_result.f,level);
  430.   
  431.                offline_error_cont+=total_error(best_result.f);
  432.             if (better==1)
  433.             {
  434.               // For dynamic optimisation, it is better
  435.               // to permanently evaluate the error
  436.                 offline_error+=-total_error(best_result.f);
  437.                 previous_best=best_result.f; 
  438.                 best_result=TR[level].tr[i].part[j].p_i;
  439.                 if (BIN==1) 
  440.     for (d=0;d<best_result.p.size;d++) 
  441.         best_result.p.x[d]=floor(best_result.p.x[d]+0.5);
  442.         
  443.                 error=total_error(best_result.f);
  444.                 offline_error+=error;
  445. //   printf(" %f",  offline_error);
  446.        
  447.                 if (DYN==1)
  448.                     error=offline_error/n_change; // For non recursive dynamic optim
  449.                       if (problem[level].printlevel>0)
  450.                       {
  451.                          printf("\n");
  452.                          printf(" Eval: %.0f",eval_f_tot[level]);
  453.                          printf(" Result: %.8f",error);
  454.                       }
  455.                     if (problem[level].save==-2) // Save info for convergence curve plotting
  456.                     {
  457.                         fprintf(f_trace_run,"\n%.0f %f %f %i",eval_f_tot[level],error,offline_error_cont/(eval_f_tot[level]-n_add),n_add);
  458.                     }
  459.                       if (problem[level].save>0)
  460.                       {
  461.                          fprintf(f_trace,"\n %f",best_result.f.f[0]);
  462.                       }
  463.                      if(DYN==0 && error<=problem[level].eps) chance=1;  else chance=0;
  464.                       if(chance==1) goto end_by_chance; // A solution has been found
  465.                       
  466.             }
  467.    }  // next j
  468.  
  469.             // Arbitrary stop criterion
  470.             ticks2=clock();
  471.             clock_tick=ticks2-ticks1;
  472.             if((Max_Eval<0 && eval_f_tot[level]>-Max_Eval) || Max_Eval>clock_tick)
  473.             {
  474.                 failure_tot=failure_tot+1; // Just for information
  475.                  // if (problem[level].printlevel>0)
  476.                 printf("\n FAILURE eval_tot  %.0f > max= %.0f",eval_f_tot[level],fabs(Max_Eval));
  477.                 goto the_end;
  478.             }      
  479.         } // next i
  480.      if (problem[level].printlevel>1) display_swarm(1,level);
  481.    // Special local search for TSP 
  482.    if (TSP==1)
  483.    {
  484.       for (i=0;i<TR[level].size;i++) // For each tribe ...
  485.         {
  486.          for (j=0;j<TR[level].tr[i].size;j++) // ... for each particle ...
  487.             {              
  488.               TR[level].tr[i].part[j].x=local_search_tsp(TR[level].tr[i].part[j].x,problem[level].target,level);
  489.           
  490.                // If improvement, memorize
  491.                better=better_than(TR[level].tr[i].part[j].x.f, TR[level].tr[i].part[j].p_i.f,level);
  492.                if (better==1)
  493.                {
  494.                  TR[level].tr[i].part[j].p_i= TR[level].tr[i].part[j].x;
  495.                }
  496.             }
  497.       }
  498.    } // end if(TSP==1)
  499.       // Special local search for QAP
  500.    if (QAP==1)
  501.    {
  502.       for (i=0;i<TR[level].size;i++) // For each tribe ...
  503.         {
  504.             for (j=0;j<TR[level].tr[i].size;j++) // ... for each particle ...
  505.             {
  506.                TR[level].tr[i].part[j].x=local_search_qap(TR[level].tr[i].part[j].x,problem[level].target,level);
  507.                // If improvement, memorize
  508.                better=better_than(TR[level].tr[i].part[j].x.f, TR[level].tr[i].part[j].p_i.f,level);
  509.                if (better==1)
  510.                {
  511.                  TR[level].tr[i].part[j].p_i= TR[level].tr[i].part[j].x;
  512.                }
  513.             }
  514.       }
  515.    } // end if(QAP==1)
  516.     // Total particle number as criterion
  517.     cycle_size=problem[level].init_size+n_add-n_remove;
  518.     swarm_size=cycle_size;
  519.    if (cycle_size>Max_swarmsize)
  520.    {
  521.      printf("\nWARNING. You should increase Max_swarmsize (%i) in def_struct.c",Max_swarmsize);
  522.     cycle=Max_swarmsize;
  523.    }
  524.     mean_swarm_size=mean_swarm_size+cycle_size;
  525.       Iter=Iter+1; // number of time steps. Just for information
  526.     if (adapt>0) goto end_adapt; // Non adaptive option (constant swarm size)
  527.     //                                ***********  ADAPTATION(S)  (begin) *************
  528.   // Number of connections
  529.   n_connect=0;
  530.  for (i=0;i<TR[level].size;i++) // For each tribe
  531. {
  532.     for (j=0;j<TR[level].tr[i].size;j++)  // for each particle
  533.     {
  534.       n_connect=n_connect+TR[level].tr[i].part[j].ig.size;
  535.     }
  536. }
  537.     cycle=cycle+1;
  538. if (cycle<n_connect)
  539.         goto end_adapt; // Adaptation is not made at each iteration
  540.                                          // just from time to time
  541.         TRsize0=TR[level].size; // Memorize the current number of tribes
  542.         // Look for Xmin and Xmax. Useful only if you use init 3 (see init_particle)
  543.         if (swarm_size>1) // Needs several particles
  544.         {
  545.             for (d=0;d<problem[level].DD;d++) // loop on dimensions
  546.             {        
  547.                 Xmin.x[d]=problem[level].H.max[d];
  548.                 Xmax.x[d]=problem[level].H.min[d];
  549.                 for (i=0;i<TRsize0;i++) // Loop on tribes
  550.                 {
  551.                     for (j=0;j<TR[level].tr[i].size;j++) // loop on particles
  552.                     {
  553.                         z=TR[level].tr[i].part[j].p_i.p.x[d];
  554.                         if(z<Xmin.x[d]) Xmin.x[d]=z;
  555.                         if(z>Xmax.x[d]) Xmax.x[d]=z;
  556.                     }
  557.                 } 
  558.             }
  559.         }
  560.         // If improvement, "bad" particles are not useful => remove them
  561.         //  If deterioration, it is worthwhile to generate a very different particle
  562.         for (i=0;i<TRsize0;i++) // Loop on tribes, to check them
  563.         {
  564.             // Find the best particle in the current tribe
  565.             label_best=TR[level].tr[i].part[0].label; // Label of the best
  566.             f0=TR[level].tr[i].part[0].p_i.f;
  567.             rank_best=0;
  568.             if (TR[level].tr[i].size>1)
  569.             {
  570.                 for (j=1;j<TR[level].tr[i].size;j++)
  571.                 {
  572.                     f1=TR[level].tr[i].part[j].p_i.f;
  573.                     if (better_than(f1,f0,level)==1)
  574.                     {
  575.                         label_best=TR[level].tr[i].part[j].label;
  576.                         f0=f1;
  577.                         rank_best=j;
  578.                     }
  579.                 }
  580.             }
  581.                  // Find the worst particle in the current tribe
  582.            // (maybe for future version)
  583.                 label_worst=TR[level].tr[i].part[0].label; // Label of the worst
  584.                 f0=TR[level].tr[i].part[0].p_i.f;
  585.                 rank_worst=0;
  586.                 for (j=1;j<TR[level].tr[i].size;j++)
  587.                 {
  588.                         f1=TR[level].tr[i].part[j].p_i.f;
  589.                         if (better_than(f1,f0,level)!=1)
  590.                         {
  591.                            label_worst=TR[level].tr[i].part[j].label;
  592.                            f0=f1;
  593.                            rank_worst=j;
  594.                         }
  595.                 }
  596.                 //worst[i]=TR[level].tr[i].part[rank_worst].label;
  597.             // Count how many particles of the current tribe have improved their position
  598.             improv=local_improv(TR[level].tr[i],level);
  599.       if (problem[level].fuzzy==0)   // Crisp decision rules. Note a tribe may be both bad an good
  600.       {
  601.            bad=improv<TR[level].tr[i].size; // At least one particle has NOT improved its position             
  602.            good=improv>0; // At least one particle has improved  its position
  603.       }
  604.       else   // Use fuzzy rules to decide whether the tribe is bad or good
  605.       {
  606.          z= TR[level].tr[i].size;
  607.          pr=alea_float(0,z);
  608.          bad=(improv<=pr);
  609.          good=1-bad;
  610.       }
  611. if (bad==0) bad=alea(0,1); // To be sure there are on the whole 
  612.                             // more generations than deletions
  613.       if (bad==1)
  614.         {
  615.                     // If it is the first bad tribe, generate a new empty one
  616.                     if (TR[level].size==TRsize0)
  617.                     {
  618.                         TR[level].size=TR[level].size+1;
  619.                         TR[level].tr[TRsize0].size=0;
  620.                     }
  621.               // max_add= TR[level].tr[i].size+1;
  622.              // max_add=alea(1,TR[level].tr[i].size); // TEST. May generate several particles
  623.                 max_add=1;
  624.                 if (add_option==1) max_add=2;
  625.               if (good && (TR[level].tr[i].size>1 || TR[level].tr[i].part[0].status>0)) //The worst particle will be removed
  626.                  max_add=max_add+1;
  627.                for (m=0;m<max_add;m++)  // May generate several particles
  628.                {
  629.                    //Generate a particle in the new tribe (rank of the tribe=TRsize0)
  630.                   if (TSP==1)
  631.                   {
  632.                     TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size]=init_particle_tsp(problem[level].DD,problem[level].target,level);
  633.                     goto new_label;
  634.                   }
  635.                   if (QAP==1)
  636.                   {
  637.                     TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size]=init_particle_qap(problem[level].DD,problem[level].target,level);
  638.                     goto new_label;
  639.                   }
  640.                 
  641.                   if (add_option==0 || (add_option==1 && m!=1)) // Random init
  642.                     TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size]=init_particle(0,level,dummy_part);
  643.                     if (add_option==1 && m==1) // Generate a hopefully good particle
  644.                     {
  645.                         // Find the best informer of the tribe
  646.                         part_best=TR[level].tr[i].part[0];
  647.                         for (j=0;j<TR[level].tr[i].size;j++) // ... for each particle ...
  648.                         {
  649.                         partg=best_informer(TR[level].tr[i].part[j],0,level);
  650.                     //    partg=best_informer(TR[level].tr[i].part[j],no_best,level);
  651.                         better=better_than(partg.p_i.f,part_best.p_i.f,level);
  652.                         if (better==1) part_best=partg;                
  653.                         }
  654.                         // Generate a particle around the best position known by the best informer
  655.                         partg=TR[level].tr[i].part[label_best]; // Best particle of the tribe
  656.                         partt=move_particle(partg,part_best,level,1); 
  657.                         partt=complete_part(partt,0,level);
  658.                         partt.ig.size=0;
  659.                         TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size]=partt;
  660.                     }
  661. // ---
  662. new_label:
  663.                     new_label=TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size].label;            
  664.                     if(rand_hood>0) goto end_i_groups;
  665.                     TR[level].tr[TRsize0].size=TR[level].tr[TRsize0].size+1; // Increase the (new) tribe size
  666.                     n_add=n_add+1;
  667.                     // Begin to build its i-group:
  668.                     // with the label of the best particle that has generated the new particle
  669.                     // Add it to the i-group of the new particle
  670.                     rank=TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size].ig.size;
  671.                     TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size].ig.label[rank]=label_best;                
  672.                     TR[level].tr[TRsize0].part[TR[level].tr[TRsize0].size].ig.size=rank+1; // Increase the i-group size
  673.                     // Update the i_group of the "generating" best particle ...
  674.                     rank=TR[level].tr[i].part[rank_best].ig.size;
  675.                     TR[level].tr[i].part[rank_best].ig.label[rank]=new_label;
  676.                     TR[level].tr[i].part[rank_best].ig.size=rank+1;
  677. /*
  678.                     // ... or ... Update the i_groups of all particles of the "generating" tribe
  679.                     // (if you use this option, do not use the previous one)
  680.                     for (j=0;j<TR[level].tr[i].size;j++)
  681.                     {
  682.                         rank=TR[level].tr[i].part[j].ig.size;
  683.                         TR[level].tr[i].part[j].ig.label[rank]=new_label;
  684.                         TR[level].tr[i].part[j].ig.size=rank+1;
  685.                     }
  686. */
  687.                   end_i_groups:;
  688.                } // m<max_add
  689.             }   // End "if (bad)"
  690.             // If  enough improvement
  691.         if (good==1)
  692.             {
  693.                 n_local_remove=0;
  694.                 // If there is just one particle, check whether an informer is better
  695.                 if (TR[level].tr[i].size==1)
  696.                 {
  697.                     // Find the best informer
  698.                     partg=best_informer(TR[level].tr[i].part[0],no_best,level);
  699.                     
  700.                     if (better_than(partg.p_i.f,TR[level].tr[i].part[0].p_i.f,level)==1)
  701.           { label_best=partg.label;
  702.             goto remove;
  703.           }
  704.                     goto exit_good;
  705.                 }
  706. remove:
  707.                     // Remove the worst particle of the current tribe
  708.                     n_local_remove=n_local_remove+1;
  709.                     // Temporarily keep its i-group
  710.                     ig=TR[level].tr[i].part[rank_worst].ig;
  711.           
  712.                     // Remove it from the tribe (compact the tribe)
  713.                     if (rank_worst<TR[level].tr[i].size-1)
  714.                     {
  715.                         for (j=rank_worst;j<TR[level].tr[i].size-1;j++)
  716.                         {
  717.                             TR[level].tr[i].part[j]=TR[level].tr[i].part[j+1];
  718.                         }
  719.                     }
  720.                     TR[level].tr[i].size=TR[level].tr[i].size-1;
  721.                     n_remove=n_remove+1;
  722.                     // Replace it by the best in all i-groups
  723.                     for (j=0;j<TR[level].size;j++) // For each i_group...
  724.                     {
  725.                         for (k=0;k<TR[level].tr[j].size;k++) // ... for each particle ...
  726.                         {
  727.                             // Check if the best is already in the i-group
  728.                             already=-1;
  729.                             for (l=0;l<TR[level].tr[j].part[k].ig.size;l++)
  730.                                 if (TR[level].tr[j].part[k].ig.label[l]==label_best) 
  731.                                     {already=l;}
  732. //check_inf:
  733.                             
  734.                             igt.size=0;
  735.                             for (l=0;l<TR[level].tr[j].part[k].ig.size;l++) //... for each informer...
  736.                             {
  737.                                 labelt=TR[level].tr[j].part[k].ig.label[l];
  738.                                 if (labelt!=label_worst)
  739.                                 {igt.label[igt.size]=labelt; igt.size=igt.size+1;}
  740.                             }
  741.              if(already==-1 && label_worst!=label_best)
  742.                {igt.label[igt.size]=label_best; igt.size=igt.size+1;}
  743.                             TR[level].tr[j].part[k].ig=igt;
  744. //next_part:;
  745.                         }
  746.                     }  // End "for each i-group"
  747.                          if(rand_hood>0) goto end_i_groups_2;
  748.                     // Merge its i-group (except itself) into the one of the best
  749.                     for (j=0;j<ig.size;j++) // Loop on informers
  750.                     {
  751.                         if (ig.label[j]==label_worst) continue;
  752.                         size=TR[level].tr[i].part[rank_best].ig.size;
  753.                         for (k=0;k<size;k++)
  754.                         {
  755.                             if (ig.label[j]==TR[level].tr[i].part[rank_best].ig.label[k]) goto next_informer;
  756.                         }
  757.                         TR[level].tr[i].part[rank_best].ig.label[size]=ig.label[j];
  758.                         TR[level].tr[i].part[rank_best].ig.size=size+1;
  759.                     next_informer:;
  760.                     }
  761.                end_i_groups_2:;
  762.                 } // End "if (good)"
  763. exit_good:;
  764.         } // End "for each tribe"
  765.  if (rand_hood>0) goto empty_tribe;
  766.                // Complete the i-groups of each particle of the new tribe (if there is one)
  767.         // with all particles of the tribe
  768.       if (TR[level].size>TRsize0)
  769.         {
  770.             for (i=0;i<TR[level].tr[TRsize0].size;i++) // For each particle ...
  771.             {
  772.                 for (j=0;j<TR[level].tr[TRsize0].size;j++) // ... add the others as informers
  773.                 {
  774.                     rank=TR[level].tr[TRsize0].part[i].ig.size;
  775.                     labelt=TR[level].tr[TRsize0].part[j].label;
  776.                     TR[level].tr[TRsize0].part[i].ig.label[rank]=labelt;
  777.                     TR[level].tr[TRsize0].part[i].ig.size=rank+1;
  778.                 }
  779.             }
  780.         }
  781. empty_tribe:
  782.         // Eventually remove empty tribes
  783.         for (i=0;i<TR[level].size;i++)
  784.         {
  785.             if (TR[level].tr[i].size>0) continue; // Not empty
  786.                      i0=i;
  787.                 if (i<TR[level].size-1) // Compact the tribe list
  788.                 {
  789.                     for (j=i;j<TR[level].size-1;j++)
  790.                     {
  791.                         TR[level].tr[j]=TR[level].tr[j+1];
  792.                     }
  793.                     i=i-1;
  794.                 }
  795.                 TR[level].size=TR[level].size-1;
  796.                 if (problem[level].printlevel>0)
  797.                 {
  798.                     printf("\n %i tribes. No particle anymore in tribe %i!",TR[level].size+1,i0);
  799.                     printf(" => New tribe number %i",TR[level].size);
  800.                 }
  801.                 if (TR[level].size==0)
  802.                 {
  803.                     printf("\n NO PARTICLE ANYMORE!");
  804.                     goto the_end;
  805.                 }
  806.                 goto empty_tribe;
  807.         }
  808. if(linkreorg==1 && TR[level].size>1) // NEW 6.2 Optionnally reorganize links
  809. {
  810.     link_reorg(level);
  811. }
  812.         cycle=0;
  813.         if(problem[level].save==1) save_swarm(f_swarm,level);
  814.     end_adapt:;       //   ---------------------------  ADAPTATION(S)  (end)
  815.  if(rand_hood>0)
  816.  {
  817.       swarm_size=problem[level].init_size+n_add-n_remove;
  818.       if(circular_hood==1) rand_hood=K_optim(swarm_size,problem[level].eps);
  819.       for(k=0;k<TR[level].size;k++)
  820.       {
  821.          size= TR[level].tr[k].size;
  822.          for (i=0;i<size;i++)
  823.          {
  824.             TR[level].tr[k].part[i].ig.size=rand_hood;
  825.             for (j=0;j<TR[level].tr[k].part[i].ig.size;j++)
  826.             {
  827.                rank=alea(0,swarm_size);
  828.                TR[level].tr[k].part[i].ig.label[j]=TR[level].tr[k].part[rank].label;
  829.             }
  830.          }
  831.       }
  832.  }
  833.          if (problem[level].save==-1)  // Save swarm size and energies
  834.                                         // Just for information
  835.         {
  836.            Ek=energy_k(level); Ep=energy_p(level);
  837.            swarm_size=problem[level].init_size+n_add-n_remove; 
  838.            fprintf(f_energy,"\n%i %i %i %i %f %f",(int)eval_f_tot[level],swarm_size,n_add,n_remove,Ek,Ep);
  839.         }
  840.         
  841.         goto do_Iter; // Iterates till a criterion is met
  842. end_by_chance:
  843.         printf("\n CHANCE !");
  844. the_end: // ------------------------------------------------------------------------------------------------------------------- END
  845.     error_cont=offline_error_cont/(eval_f_tot[level]-n_add);
  846.     if (DYN==0)
  847.     error=total_error(best_result.f);
  848.     else error=offline_error/n_change;// Mean error for dynamic optimisation
  849.     if (problem[level].save==-2) // Save info for convergence curve plotting
  850.     {
  851.         fprintf(f_trace_run,"\n%.0f %f %f %i",eval_f_tot[level],error,error_cont,n_add);
  852.     }
  853. ticks2=clock();
  854. clock_tick=ticks2-ticks1; // Processor time needed
  855. duration = (double)(clock_tick/CLOCKS_PER_SEC);
  856. duration_tot=duration_tot+duration;
  857. eval_run[times]=eval_f_tot[level];
  858. eps_run[times][0]=error;
  859. eps_run[times][1]=error_cont;
  860.             // Keep the min. and max. eps. Just for information
  861.             if (error<min_eps) min_eps=error;
  862.             if (error>max_eps) max_eps=error;
  863. if (level>0)  goto end_PSO;
  864. mean_swarm_size=mean_swarm_size/Iter;
  865. Mean_swarm=Mean_swarm+mean_swarm_size;
  866. if (problem[level].printlevel>=0)
  867. {
  868.    if(error<=problem[level].eps)
  869.    {
  870.       printf("\n SUCCESS");
  871.       }
  872.       else printf("\n FAILURE");
  873.  
  874.   printf ("\n Run %i => eps %f, eps_cont %f,eval: %.0f, duration: %.2f",times+1,error,error_cont,eval_f_tot[level],duration);
  875.   printf(" + %i - %i",n_add,n_remove);
  876.   printf(" (%.0f)",mean_swarm_size);
  877.    display_position(best_result);
  878. //fprintf(f_trace,"%f %f\n",error,eval_f_tot[level]);
  879. // Save run result
  880. // This print should be consistent with the titles written at the very beginning
  881. fprintf (f_run,"\n%i %i %f",problem[level].funct,times+1,problem[level].target);
  882. if(DYN==0)
  883. for (i=0;i<best_result.f.size;i++) fprintf (f_run," %f",best_result.f.f[i]); // f value(s)
  884. else fprintf(f_run," %.12f",error);// For non recursive dynamic optimisation
  885. fprintf(f_run," %.0f %f %i %i",eval_f_tot[level],duration,n_add,n_remove);
  886. for (i=0;i<best_result.p.size;i++) fprintf(f_run," %.12f",best_result.p.x[i]); // position
  887.   if (problem[level].funct==11) // Apple trees
  888.   {
  889.     post= homogen_to_carte(best_result);
  890.     printf("\n   Cartesian coordinates: ");
  891.     for (i=0;i<post.p.size;i++)  printf(" %f",post.p.x[i]);
  892.       fprintf(f_run,"   ");
  893.   for (i=0;i<post.p.size;i++)  fprintf(f_run," %f",post.p.x[i]);
  894.   }
  895. }
  896. if (eval_f_tot[level]>nb_eval_max) nb_eval_max=eval_f_tot[level];
  897. if (eval_f_tot[level]<nb_eval_min) nb_eval_min=eval_f_tot[level];
  898. if(problem[level].printlevel>0) print_N_run(level,best_result);
  899. if(error<total_error(BEST.f)) BEST=best_result; // Best of the best  amongst runs
  900.     times=times+1;
  901.     if (times<problem[level].times) goto times_loop;
  902.   //-----------------------------------------------------------------------------------------
  903.   if (problem[level].printlevel<0) goto end_PSO;
  904.         ticks2=clock();
  905.         mean_eval=zzz/problem[level].times;
  906.         mean_eps=mean_eps/problem[level].times;
  907.         Mean_swarm=Mean_swarm/problem[level].times;
  908.         duration=duration_tot/problem[level].times;
  909.         mean_eval=0;
  910.         mean_eps=0; mean_eps_cont=0;
  911. //        times=problem[level].times;
  912.         for (i=0;i<times;i++)
  913.         {
  914.             mean_eval=mean_eval+eval_run[i];
  915.             mean_eps=mean_eps+eps_run[i][0];
  916.             mean_eps_cont=mean_eps_cont+eps_run[i][1];
  917.         }
  918.         mean_eval=mean_eval/times;
  919.         mean_eps=mean_eps/times;
  920.         mean_eps_cont=mean_eps_cont/times;
  921.         sigma_eval=0;
  922.         for (i=0;i<problem[level].times;i++)
  923.         {
  924.             sigma_eval=sigma_eval+(eval_run[i]-mean_eval)*(eval_run[i]-mean_eval);
  925.         }
  926.         sigma_eval=sqrt (sigma_eval/problem[level].times);
  927.         sigma_eps=0; sigma_eps_cont=0;
  928.         for (i=0;i<problem[level].times;i++)
  929.         {
  930.             sigma_eps=sigma_eps+(eps_run[i][0]-mean_eps)*(eps_run[i][0]-mean_eps);
  931.             sigma_eps_cont+=(eps_run[i][1]-mean_eps_cont)*(eps_run[i][1]-mean_eps_cont);
  932.         }
  933.         sigma_eps=sqrt (sigma_eps/problem[level].times);
  934.         sigma_eps_cont=sqrt (sigma_eps_cont/problem[level].times);
  935.         fprintf(f_synth,"\n");
  936.         fprintf(f_synth,"%2i",problem[level].funct);
  937.         fprintf(f_synth," %2i",problem[level].DD);
  938.         fprintf(f_synth," %4.3f",problem[level].target);
  939.         fprintf(f_synth," %4.10f",problem[level].eps);
  940.         fprintf(f_synth," %i",problem[level].times);
  941.         fprintf(f_synth," %.0f",mean_eval); // Mean number of evaluations
  942.         fprintf(f_synth," %.0f",sigma_eval); // Standard deviation for eval
  943.       z= 1- (double)failure_tot/(double)problem[level].times ;
  944.         fprintf(f_synth," %.3f",z);  // Success rate
  945.         fprintf(f_synth, " %.0f %.0f",nb_eval_min,nb_eval_max);
  946.         fprintf(f_synth, " %4.0f ",Mean_swarm);
  947.         fprintf(f_synth, " %4.10f ",mean_eps);
  948.         fprintf(f_synth," %4.10f",sigma_eps); // Standard deviation for eps
  949.         fprintf(f_synth, " %4.10f ",mean_eps_cont);
  950.         fprintf(f_synth," %4.10f",sigma_eps_cont);
  951.         fprintf(f_synth, " %4.10f ",min_eps);
  952.         fprintf(f_synth, " %4.10f ",max_eps);
  953.         fprintf(f_synth," / Time %4.4f",duration);
  954.         printf("\nMEAN NUMBER OF EVALUATIONS: %.0f",mean_eval); // Mean number of evaluations
  955.         printf(" sigma %.0f",sigma_eval); // Standard deviation
  956.         printf(" [%.0f %.0f ]",nb_eval_min,nb_eval_max);
  957.         printf("\nMean eps: %4.10f",mean_eps);
  958.         printf(" sigma %4.10f",sigma_eps);
  959.         printf(" [%4.10f, %4.10f]",min_eps,max_eps);
  960.         printf("\nMean eps cont.: %4.10f",mean_eps_cont);
  961.         printf(" sigma %4.10f",sigma_eps_cont);
  962.         printf("\nMean swarm:%4.0f ",Mean_swarm);
  963.         printf("\nTOTAL NUMBER OF FAILURES: %i/%i", failure_tot,problem[level].times);
  964. //        if (eval_f_sup>0) printf(" (after %.0f evaluations)",eval_f_sup);
  965.   printf("\n Success rate %.3f", z ;
  966.         duration = duration_tot/(float)problem[level].times;
  967.         printf(" / MEAN TIME %4.4f  ",duration);
  968. /*
  969.    if (level==0)    // Display strategies used
  970.    {
  971.       printf("\n strategies used: ");
  972.       for (i=0;i<nb_strateg;i++)
  973.       if (strateg[i]>0)
  974.          printf("\n %i: %.0f times  ",i,strateg[i]) ;
  975.      }
  976.  */
  977.  if (level==0) // Display particle status
  978.  {
  979.    printf("\n\n Status  Count    Rate ");
  980.     z=0; for (i=0;i<Max_status;i++) z=z+status_count[i];
  981.    for (i=0;i<Max_status;i++) printf("\n %i      %.0f    %.0f",i,status_count[i],100.0*status_count[i]/z);
  982.  }
  983.  if (problem[level].printlevel>1) display_swarm(1,level);
  984.          
  985.  save_swarm(f_swarm,level);
  986. fprintf(f_swarm,"\n%i\n",-1); // End of save swarm file
  987.       fclose(f_swarm);
  988.          fprintf(f_run,"\n%i\n",-1); // End of save run file
  989.          fclose(f_run);
  990.   if (problem[level].nb_f==1) goto end_PSO;
  991.   // Multiobjective problem. Clean up the result file
  992. printf("\n Multiobjective cleaning");
  993.   f_run=fopen("run.txt","r");
  994.   f_run_clean=fopen("run_cleaned.txt","w");
  995.   clean_run(f_run,f_run_clean,problem[level].nb_f,problem[level].DD,level);
  996. end_PSO:
  997. if (problem[level].printlevel>0)
  998. {
  999.    if(MEMO==1) display_memo(level);
  1000. }
  1001. return best_result;
  1002. };
  1003.  // ----------------------------------------------------------------------------- ADD_MEMO
  1004. void add_memo(struct position x, int level)
  1005. {
  1006.  /*
  1007.    Put a new position in memo, so that memo is sorted by increasing error.
  1008.    Note: memo[2] is a global variable
  1009.    Useful only if MEMO option is used.
  1010.  */
  1011.  double error,error1;
  1012.   int  i;
  1013.   int rank_p;
  1014.  
  1015.  error=total_error(x.f);
  1016.  if (memo[level].size==0)  // The very first time
  1017.  {
  1018.        memo[level].x[memo[level].size]=x;
  1019.        memo[level].error[memo[level].size]=error;
  1020.        memo[level].size=memo[level].size+1;
  1021.        rank_p=0;
  1022.        goto end;
  1023.  }
  1024.  // Find the first worst already stored
  1025.  for (i=0;i<memo[level].size;i++)
  1026.  {
  1027.    error1=memo[level].error[i];
  1028.    if (error1>=error) {rank_p=i;goto shift;}
  1029.  }
  1030.  rank_p=memo[level].size;
  1031.  goto insert;
  1032.  
  1033.  shift:  // Make place for the new position to store
  1034.   for (i=memo[level].size;i>rank_p;i=i-1)
  1035.   {
  1036.      memo[level].x[i]=  memo[level].x[i-1] ;
  1037.      memo[level].error[i]=memo[level].error[i-1];
  1038.   }
  1039.  insert:  // Put the new position
  1040.    memo[level].size=memo[level].size+1;
  1041.   if (memo[level].size>=Max_Memo-1) memo[level].size=Max_Memo-1;
  1042.    
  1043.    memo[level].x[rank_p]=x;
  1044.    memo[level].error[rank_p]=error;
  1045.    end: 
  1046.    printf(" ");
  1047.  //printf("\n add_memo (size %i) rank %i, error %f",memo[level].size,rank_p,error);
  1048.  //display_memo(level);
  1049. }
  1050. // ----------------------------------------------------------------------------- BEST_INFORMER
  1051. struct particle    best_informer(struct particle part,int no_best,int level)
  1052. {
  1053. /*
  1054.    Look for the best informer of the particle.
  1055.    If no_best=1, choose it at random.
  1056.     2 => pseudo gradient
  1057.     0 (default) => really the best
  1058.     3 => 0 or 2 at random
  1059. */
  1060. int               better;
  1061. char            bid;
  1062. double            delta1,delta2;
  1063. double            dist;
  1064. double            error;
  1065. double            grad;
  1066. int                i;
  1067. int                j;
  1068. int                j_best;
  1069. int                k;
  1070. int                k_best;
  1071. struct f            f0;
  1072. int                labelt;
  1073. switch (no_best)
  1074. {
  1075. case 1:// Choose it at random
  1076.     i=alea(0,part.ig.size-1);
  1077.     labelt=part.ig.label[i];
  1078.     // Find the particle with this label
  1079.     for (j=0;j<TR[level].size;j++) // For each tribe ...
  1080.         for (k=0;k<TR[level].tr[j].size;k++) // ... for each particle in the tribe
  1081.         {
  1082.             if (TR[level].tr[j].part[k].label==labelt)
  1083.                {
  1084.                 j_best=j;
  1085.                 k_best=k;
  1086.                 break;
  1087.                }
  1088.         }
  1089.     printf("\nERROR. best_informer 1. Can't find particle with label %i",labelt);
  1090.     display_swarm(1,level);
  1091.     scanf("%c",&bid);
  1092.     return TR[level].tr[j].part[k];
  1093. case 2: // Pseudo gradient
  1094. case2:
  1095.     j_best=-1;
  1096.     k_best=0;
  1097.     grad=0;
  1098.     for (i=0;i<part.ig.size;i++) // For each informer
  1099.     {
  1100.         labelt=part.ig.label[i];
  1101.         // Find the particle with this label
  1102.         for (j=0;j<TR[level].size;j++) // For each tribe ...
  1103.         {
  1104.             for (k=0;k<TR[level].tr[j].size;k++) // ... for each particle in the tribe
  1105.             {
  1106.                 if (TR[level].tr[j].part[k].label==labelt) goto compare2;
  1107.             }
  1108.         }
  1109.         printf("\nERROR. best_informer (2). Can't find particle with label %i",labelt);
  1110.         printf("\n Particle label %i. ig list:",part.label);
  1111.         for (k=0;k<part.ig.size;k++) printf(" %i",part.ig.label[k]);
  1112.         display_swarm(1,level);
  1113.         scanf("%c",&bid);
  1114.         break;
  1115.     compare2:
  1116.         error=total_error(part.p_i.f);
  1117.         delta1=error-total_error(TR[level].tr[j].part[k].p_i.f);
  1118.         dist=distance(part.p_i,TR[level].tr[j].part[k].p_i);
  1119.         if (dist<=0) continue;
  1120.         delta2=delta1/dist;
  1121.         if (delta2>grad)
  1122.         {
  1123.             j_best=j;k_best=k; grad=delta2;
  1124.         }
  1125.     }
  1126.   if(j_best>=0) break;
  1127.   // If no better than the particle itself
  1128.    //if(j_best<0) return part;  // return the part
  1129.    if(j_best<0) goto case0; // Use the classical method
  1130.    if(j_best<0) {j_best=0;k_best=0;break;} // Choose particle 0
  1131. case 3: // Random 0 or 2
  1132.     if (alea(0,1)<0.5) goto case0; else goto case2;
  1133. default: // Find really the best
  1134. case0:
  1135.     j_best=0;
  1136.     k_best=0;
  1137.     //f0=TR[level].tr[j_best].part[k_best].p_i.f;
  1138.     f0.size=TR[level].tr[j_best].part[k_best].p_i.f.size;
  1139.     for (i=0;i<f0.size;i++) f0.f[i]=infinite;
  1140.  
  1141. /* 
  1142. printf("\n best_informer. Particle %i. Informers: ",part.label);
  1143. for (i=0;i<part.ig.size;i++) 
  1144. {
  1145.     labelt=part.ig.label[i];
  1146.     printf(" %i",labelt);
  1147. }
  1148. */
  1149.     for (i=0;i<part.ig.size;i++) // For each informer
  1150.     {
  1151.         labelt=part.ig.label[i];
  1152.         // Find the particle (tribe j, particle k) with this label
  1153.         for (j=0;j<TR[level].size;j++) // For each tribe ...
  1154.         {
  1155.             for (k=0;k<TR[level].tr[j].size;k++) // ... for each particle in the tribe
  1156.             {
  1157.                 if (TR[level].tr[j].part[k].label==labelt) goto compare;
  1158.             }
  1159.         }
  1160.         printf("\nERROR. best_informer (default). Can't find particle with label %i",labelt);
  1161.         printf("\n Particle label %i. ig list:",part.label);
  1162.         for (k=0;k<part.ig.size;k++) printf(" %i",part.ig.label[k]);
  1163.         display_swarm(1,level);
  1164.         scanf("%c",&bid);
  1165.     compare:
  1166.         better=better_than( TR[level].tr[j].part[k].p_i.f,f0,level);
  1167.         if (better!=1) continue;
  1168.             j_best=j;
  1169.             k_best=k;
  1170.             f0=TR[level].tr[j_best].part[k_best].p_i.f;
  1171.     }
  1172.     break;
  1173. } // end switch no_best
  1174. // end:
  1175.  //printf("\n label %i, k_best %i",part.label,k_best);
  1176. return TR[level].tr[j_best].part[k_best];
  1177. }
  1178. // ----------------------------------------------------------------------------- BETTER_THAN
  1179. int    better_than(struct f f1,struct f f2, int level)
  1180. {
  1181. /* return 1 if f1 is better than f2
  1182.           0 if f2 is worse than f2
  1183.           2 if they are equivalent
  1184. Useful mainly for multiobjective problem
  1185. f1 and f2 are supposed to be the same size
  1186. */
  1187.  int d;
  1188.  double eps;
  1189.  
  1190.   if(lexico>0) goto lexico;
  1191.   
  1192.  for( d=0;d<f1.size;d++)
  1193.  {
  1194.     if (f1.f[d]>f2.f[d]) return 0;
  1195.  }
  1196.  // All f1's f_values are <=
  1197.   for( d=0;d<f1.size;d++)
  1198.  {
  1199.       if (f1.f[d]<f2.f[d]) return 1;  // At least one is <
  1200.  }
  1201. return 2;    // They are all equal
  1202.  lexico: // Lexicographic sort
  1203.  eps=problem[level].eps/ f1.size;
  1204.    for( d=0;d<f1.size;d++)
  1205.  {
  1206.       if (fabs(f1.f[d]-f2.f[d])+eps<MIN(f1.f[d],f2.f[d])) continue;
  1207.       if (f1.f[d]<eps && f2.f[d]<eps) continue;
  1208.      if (f1.f[d]<f2.f[d]-eps)   return 1;
  1209.      if (f1.f[d]>=f2.f[d]-eps)   return 0;
  1210.  }
  1211.  return 2;
  1212. }
  1213. //------------------------------------------------------------------------------ CLEAN_RUN
  1214. void clean_run(FILE *f_run,FILE *f_run_clean, int nb_f,int DD,int level)
  1215. {
  1216.  /*
  1217.    For multiobjective problem, keep only the non dominated points
  1218.   f_run: 3 numbers, nb_f values, 4 numbers, position (DD values)
  1219.   EOF = -1
  1220.  */
  1221.  int     better;
  1222.  float bid;
  1223.  int     d;
  1224.   int eof;
  1225.   int    i,j,k;
  1226.   int    ibid;
  1227.   char line[400];
  1228.   int    n_clean;
  1229.   int    n_pos;
  1230.   struct position pos[Max_run];
  1231. int      clean[Max_run];
  1232.   printf("\nMultiobjective. A moment, please, I clean up the result file");
  1233. if (nb_pb_max>1)
  1234. {
  1235.     printf("\n WARNING. I can clean up the run file for you are running");
  1236.     printf("\n           the program on several problems");
  1237. }
  1238. // Shunt the title line
  1239. fgets( line,400,f_run);
  1240.          
  1241. // Read the positions
  1242.    n_pos=0;
  1243. read_pos:
  1244.         fscanf(f_run,"%i",&eof);
  1245.         if(eof==-1) goto clean_up;
  1246.         
  1247.         fscanf(f_run,"%i %f",&ibid,&bid);
  1248.         for (d=0;d<nb_f;d++)
  1249.         {
  1250.            fscanf(f_run," %f",&bid);
  1251.            pos[n_pos].f.f[d]=bid;
  1252.         }
  1253.         fscanf(f_run,"%f %f %f %f",&bid,&bid, &bid,&bid);
  1254.         for (d=0;d<DD;d++)
  1255.         {
  1256.            fscanf(f_run," %f",&bid);
  1257.             pos[n_pos].p.x[d]=bid;
  1258.         }
  1259.         pos[n_pos].f.size=nb_f;
  1260.         pos[n_pos].p.size=DD;
  1261.          n_pos=n_pos+1;
  1262.          goto read_pos;
  1263. clean_up:
  1264. // Write the titles
  1265. for(d=0;d<nb_f;d++)    fprintf(f_run_clean," f%i", d+1);
  1266. for(d=0;d<DD;d++)    fprintf(f_run_clean," x%i", d+1);
  1267.  n_clean=0;
  1268.  // For each result, look if it is dominated by another one
  1269.  // If not, write it in the f_run_clean file
  1270.  for (i=0;i<n_pos;i++)
  1271.  {
  1272.        if (i==n_pos-1) goto already1;
  1273.        
  1274.        // Compare to not already checked positions
  1275.        for (j=i+1;j<n_pos;j++)
  1276.        {
  1277.          better=better_than(pos[j].f,pos[i].f,level);
  1278.          if (better==1) goto next_i; // pos(j) is better. Don't keep pos(i)
  1279.        }
  1280.  already1:        
  1281.          // Compare to already saved positions
  1282.         for (j=0;j<n_clean;j++)
  1283.         {
  1284.            k=clean[j];
  1285.            better=better_than(pos[k].f,pos[i].f,level);
  1286.            if (better!=0) goto next_i;  // pos(k) is better or equal. Don't keep pos(i)
  1287.          }
  1288.   
  1289.          clean[n_clean]=i;
  1290.          n_clean=n_clean+1;
  1291.  next_i:;
  1292.  }
  1293.  // Write the cleaned result
  1294.  for (i=0;i<n_clean;i++)
  1295.  {
  1296.     j=clean[i];
  1297.     fprintf(f_run_clean,"\n");
  1298.       for(d=0;d<nb_f;d++)    fprintf(f_run_clean," %f", pos[j].f.f[d]);
  1299.       for(d=0;d<DD;d++)    fprintf(f_run_clean," %.12f", pos[j].p.x[d]);
  1300.  }
  1301.  printf("\n OK, finished. I keep %i non dominated positions. See file run_cleaned.txt",n_clean);
  1302. }
  1303. // ----------------------------------------------------------------------------- COMPLETE_PART
  1304. struct particle complete_part(struct particle par, int option,int level)
  1305.  // WARNING: position must be already initialized. Complete the particle
  1306. {
  1307. struct particle    part;
  1308. part=par;
  1309. if (option==0) // Completely new particle
  1310. {
  1311.     part.label=label[level];
  1312.     label[level]=label[level]+1;
  1313. }
  1314. //part.ig.size=1;
  1315. //part.ig.label[0]=part.label;
  1316. part.x.p.size=problem[level].DD;
  1317. part=constrain(part,level); // Keep in the search space
  1318. part.x.f.size=problem[level].nb_f;
  1319. part.x.f=MyFunction(part.x,problem[level].funct,problem[level].target,level); //evaluation of the position
  1320. part.p_i=part.x; // previous best = current position
  1321. part.status=0; // no improvement
  1322. part.prev_x=part.x;
  1323. return part;
  1324. }
  1325. // ----------------------------------------------------------------------------- HOMOGEN_TO_CARTE
  1326. struct position    homogen_to_carte(struct position pos)
  1327. {
  1328. /* Convert a position given by its homogeneous coordinates into
  1329. the same position given by its cartesian coordinates. The homogeneous coordinates
  1330. are supposed to be given relatively to the unit points:
  1331. (0, ... 0), (1,0,...0) (0,1,0...,0) ... (0,...,0,1)
  1332. See, for example, Apple Trees problem
  1333. */
  1334. int                d;
  1335. struct position post;
  1336. double            s;
  1337. post.p.size=pos.p.size-1;
  1338. s=0;
  1339. for (d=0;d<pos.p.size;d++)
  1340. {
  1341.         s=s+pos.p.x[d];
  1342.     }
  1343. if (fabs (s)<almostzero)
  1344. {
  1345.     printf("\n WARNING. Incorrect homogeneous coordinates. Return null point");
  1346.     for (d=0;d<post.p.size;d++)
  1347.         post.p.x[d]=0;
  1348.     return post;
  1349. }
  1350. for (d=0;d<post.p.size;d++)
  1351.     post.p.x[d]=pos.p.x[d+1]/s;
  1352. return post;
  1353. }
  1354. // ----------------------------------------------------------------------------- INIT_PARTICLE
  1355. struct particle    init_particle(int option,int level,struct particle part0)
  1356. { // Initialisation of a particle
  1357. int                d;
  1358. double            dist_min;
  1359. int                loop;
  1360. int                loop_max;
  1361. int                random; // See below the different kinds of random generation
  1362. int                place;
  1363. int                rank;
  1364. struct particle part={0};
  1365. //random=alea(0,3);  // 0,1,2,3
  1366. random=alea(0,2);
  1367. //if (problem[level].nb_f>1)
  1368. //   random=2*alea(0,1); // 0 or 2    (mainly useful for multiobjective problem)
  1369. //else
  1370.  //  random=0;
  1371. //random=alea(0,1); if(random==1) random=2;
  1372. part.x.p.size=problem[level].DD;
  1373. if(option>0) part=part0; // Re-init position, but no the links
  1374. if (option==2) goto complete; // Just recompute the position
  1375. dist_min=0;
  1376. loop=0;
  1377. loop_max=problem[level].DD;
  1378. switch (random)
  1379. {
  1380. case 0:    // Random generation anywhere in the search space
  1381. for( d = 0;d<problem[level].DD;d++)  //for each dimension
  1382. {
  1383.     part.x.p.x[d] = alea_float(problem[level].H.min[d],problem[level].H.max[d]);
  1384. }
  1385. break;
  1386. case 1: // Random generation on an edge of the search space
  1387. for( d = 0;d<problem[level].DD;d++)
  1388. {
  1389.     place=alea(0,2);
  1390.     switch (place)
  1391.     {
  1392.     case 0:
  1393.         part.x.p.x[d] = problem[level].H.min[d];
  1394.         break;
  1395.     case 1:
  1396.         part.x.p.x[d] = problem[level].H.max[d];
  1397.         break;
  1398.     case 2:
  1399.         part.x.p.x[d] = (problem[level].H.min[d]+problem[level].H.max[d])/2;
  1400.         break;
  1401.     }
  1402. }
  1403. break;
  1404. case 2: // On the frontier
  1405.     for( d = 0;d<problem[level].DD;d++)
  1406.     {
  1407.         part.x.p.x[d] = alea_float(problem[level].H.min[d],problem[level].H.max[d]);
  1408.     }
  1409.     rank=alea(0,part.x.p.size-1);
  1410.     place=alea(0,1);
  1411.     if (place==0)     part.x.p.x[rank]=problem[level].H.min[d];
  1412.     else part.x.p.x[rank]=problem[level].H.max[d];
  1413.     break;
  1414. case 3: // Inside the memory swarm
  1415. //printf("\n init_particle %f %f",Xmin.x[0],Xmax.x[0]);
  1416. for( d = 0;d<problem[level].DD;d++)  //for each dimension
  1417. {
  1418.     part.x.p.x[d] = alea_float(Xmin.x[d],Xmax.x[d]);
  1419. }
  1420. }
  1421. complete:
  1422. part=complete_part(part,option,level);
  1423. //display_position(part.x);
  1424. if (problem[level].printlevel>1)
  1425.    printf("\n init => %f",total_error(part.x.f));
  1426. // For some strategies, more features are needed
  1427. for( d = 0;d<problem[level].DD;d++)  //for each dimension
  1428. {
  1429.      part.mod[d]=0; // Has not been modified (for Taboo option)
  1430.      part.v[d]=0; // Or random, if you want
  1431.   }
  1432. return part;
  1433. }
  1434. // ----------------------------------------------------------------------------- LINK_REORG
  1435. void link_reorg(int level)
  1436. {
  1437. /* Reorganise information links between tribes
  1438. The tribe list TR[level] is a global variable 
  1439. Note 1: symmetry of the information graph is not garanteed
  1440. Note 2: connectivity of the information graph is not garanteed
  1441. Note 3:  this is just a "research" option. Does not seem to be very efficient
  1442. */
  1443. int                    better;
  1444. struct    particle    best;
  1445. double                dist;
  1446. double                dist_far;
  1447. double                dist_near;
  1448. struct    particle    farest;
  1449. int                    far_ig_rank;
  1450. int                    i,i2,i3,i4;
  1451. int                    i_best;
  1452. int                    i_near;
  1453. int                    inform;
  1454. int                    label;
  1455. struct    particle    nearest;
  1456. int                    not_inform;
  1457. struct    particle    part;
  1458. int                    t,t2,t3;
  1459. int                    t_near;
  1460. for(t=0;t<TR[level].size;t++) // For each tribe
  1461. {
  1462. // ... find the best particle B
  1463.     best=TR[level].tr[t].part[0]; i_best=0;
  1464.     for (i=1;i<TR[level].tr[t].size;i++) 
  1465.     {
  1466.         better=    better_than(TR[level].tr[t].part[i].p_i.f,best.p_i.f,level);
  1467.         if(better==1) {best=TR[level].tr[t].part[i]; i_best=i;}
  1468.     }
  1469.     // Find a particle that is not an informer
  1470.     for(t2=0;t2<TR[level].size;t2++)
  1471.     {
  1472.         if (t2==t) continue; // All particles of the tribe t are informers for best
  1473.         label=TR[level].tr[t].part[i].label;
  1474.         for (i=0;i<best.ig.size;i++)
  1475.         {
  1476.             if (best.ig.label[i]!=label) 
  1477.             {nearest=TR[level].tr[t].part[i];goto loop_swarm;}
  1478.         }
  1479.         printf("\nWARNING.link_reorg. Can't find a non informer for particle label %i",best.label);
  1480.         goto end;
  1481.     }
  1482. loop_swarm:
  1483. // ... loop on all particles of the swarm
  1484.     farest=best; far_ig_rank=-1;
  1485.     dist_far=0; // Distance farest informer<->best
  1486.     dist_near=infinite; // Distance nearest non informer <-> best
  1487.     not_inform=0;
  1488.     for(t2=0;t2<TR[level].size;t2++)
  1489.     {
  1490.         for (i=0;i<TR[level].tr[t2].size;i++)
  1491.         {
  1492.             label=TR[level].tr[t2].part[i].label;
  1493.             if(label==best.label) continue;
  1494.             // Find the particle whose label is "label"
  1495.             for(t3=0;t3<TR[level].size;t3++)
  1496.             {
  1497.                 for (i3=0;i3<TR[level].tr[t3].size;i3++)
  1498.                 {
  1499.                     if(label!=TR[level].tr[t3].part[i3].label) continue;    
  1500.                         part=TR[level].tr[t3].part[i3];
  1501.                         goto links;
  1502.                 }
  1503.             }
  1504.             printf("\nERROR. link_reorg. Can't find a particle with label %i",label);
  1505.             display_swarm(1,level);
  1506.             goto end;
  1507. links:
  1508.             // Compute the distance
  1509.             dist=distance(best.p_i,part.p_i);
  1510.             inform=0;
  1511. //   if it is an informer, look for the farest one F
  1512.             for (i2=0;i2<best.ig.size;i2++)
  1513.             {
  1514.                 if (best.ig.label[i2]==label)
  1515.                 {
  1516.                     inform=1;
  1517. //printf("\n dist,dist_far %f %f",dist,dist_far);
  1518. //printf("\n  label %i",best.label);display_position(best.p_i);
  1519. //printf("\n   label %i",part.label); display_position(part.p_i);
  1520.                     if(dist>=dist_far)
  1521.                     {farest=part;dist_far=dist; far_ig_rank=i2;}
  1522.                 }
  1523.             }
  1524.             if(inform==1) continue;
  1525.             //   if it is not an informer, look for the nearest one N
  1526.             not_inform=1;
  1527. //printf("\n %f %f",dist,dist_near);
  1528.             if(dist<dist_near)
  1529.             {nearest=part;dist_near=dist;t_near=t3;i_near=i3;
  1530. //printf("\n t3,i3 %i %i", t3,i3);            
  1531.             }
  1532.         } // end loop i on particles
  1533.     } // end loop t2 on tribes
  1534. //printf("\n not inform %i",not_inform);
  1535. if(far_ig_rank<0)
  1536. {
  1537.     printf("\nWARNING. link_reorg. No farer informer for particle %i \n",best.label);
  1538.     printf("\n ig size %i/ List:",best.ig.size);
  1539.     for (i3=0;i3<best.ig.size;i3++) printf(" %i",best.ig.label[i3]);
  1540.     goto end;
  1541. }
  1542. if(not_inform==1) // If there exist a non informer particle for "best"
  1543. {
  1544.     //   remove link B<-F and replace it by the link B<-N
  1545.     
  1546. //printf("\n best %i",best.label);
  1547. //printf(" ig %i",far_ig_rank);
  1548. //printf("  %i => %i",best.ig.label[far_ig_rank],nearest.label);    
  1549.     TR[level].tr[t].part[i_best].ig.label[far_ig_rank]=nearest.label;
  1550. // Add symmetrical link
  1551. //printf("\n t_near i_near %i %i, ig size %i",t_near,i_near, nearest.ig.size);
  1552.  
  1553.     for (i4=0;i4<nearest.ig.size;i4++) 
  1554.         if (nearest.ig.label[i4]==best.label) goto end_not_inform; 
  1555.     // best.label not already on the informer list of nearest
  1556.         nearest.ig.label[nearest.ig.size]=best.label;
  1557.         nearest.ig.size=nearest.ig.size+1;
  1558.         TR[level].tr[t_near].part[i_near]=nearest;
  1559. end_not_inform:;
  1560. }
  1561. } // end loop t on tribes
  1562. //printf("\n");
  1563. end:;
  1564. }
  1565. // ----------------------------------------------------------------------------- LOCAL_IMPROVE
  1566. int    local_improv(struct tribe tr,int level)
  1567. {
  1568. // Count how many particles in the tribe have improved their position
  1569. int better;
  1570. int    i;
  1571. int    n;
  1572. n=0;
  1573. for (i=0;i<tr.size;i++)
  1574. {
  1575. better=better_than(tr.part[i].p_i.f, tr.part[i].prev_x.f,level);
  1576.    if (better==1) n=n+1;
  1577. }
  1578. return n;
  1579. }
  1580. // ----------------------------------------------------------------------------- MOVE_PARTICLE
  1581. struct particle    move_particle(struct particle part,struct particle partg,int level, int gener)
  1582. { //  ********* CORE OF THE ALGORITHM ************************************************
  1583.  // * after a move option means "seems interesting"
  1584. // ?  means "questionable"
  1585. int                     better;
  1586. double                c0,c1,c2,c3;
  1587. struct    position    center_i,center_g;
  1588. int                    choice;
  1589. double            coeff_rho;
  1590. int                    d;
  1591. int                    DD;
  1592. double               delta;
  1593. double               df;
  1594. int         dist;
  1595. double               error,error_g,error_i;
  1596. int           k;
  1597. double         lambda;
  1598. double         mu;
  1599. double          nonunif;
  1600. struct particle        partt;
  1601. double                pgd;
  1602. double                pid;
  1603. struct   position       pivot;
  1604. double                pr;
  1605. double        r;
  1606. struct    vector        rand_x;
  1607. struct    vector        rand_i,rand_g;
  1608. double                rho,rho1,rho2,rho3,rho_x,rho_i,rho_g;
  1609. double         sigma, sigma_d;
  1610. int               sign=1;
  1611. int                  status;
  1612. int                    strategy;
  1613. int               sub_case;
  1614. double               xd;
  1615. double        vd;
  1616. double         z,zz;
  1617.  if(problem[level].printlevel>1)
  1618.  {
  1619.     if (gener==0)
  1620.      printf("\n move_particle %i, status %i",part.label,part.status);
  1621.     else
  1622.         printf("\n move_particle. Generate a new particle");
  1623.     }
  1624. DD=part.x.p.size;
  1625. partt=part;
  1626. if (gener==1) {choice=1;goto strategy0;} // Not really a move but a generation
  1627. partt.prev_x=partt.x;
  1628. error=total_error(part.x.f);
  1629. error_i=total_error(part.p_i.f);
  1630. error_g=total_error(partg.p_i.f);
  1631. if(error_g<=0) return partt; // Solution has already been found!
  1632. if (part.status<=11) {status=0;   goto switch_s;}
  1633. if (part.status<35) {status=1;   goto switch_s;}
  1634. if (part.status<53) {status=2;   goto switch_s;}
  1635. status=3;
  1636. switch_s:
  1637. switch (status)
  1638. {
  1639. case 0:   // Bad particle
  1640.     strategy=(int)strategies[0][0];
  1641.       nonunif=strategies[1][0];
  1642.     break;
  1643. case 1:   // Neither bad nor good
  1644.     strategy=(int)strategies[0][1];
  1645.       nonunif=strategies[1][1];
  1646.     break;
  1647. case 2:     // Good particle
  1648.     strategy=(int)strategies[0][2];
  1649.       nonunif=strategies[1][2];
  1650.     break;
  1651. case 3:       // Very good particle
  1652.     strategy=(int)strategies[0][3];
  1653.       nonunif=  strategies[1][3];
  1654.     break;
  1655. }
  1656. status_count[status]= status_count[status]+1; // Just for information
  1657. switch (strategy)
  1658. {
  1659. case -1: // Pure random
  1660. for( d = 0;d<DD;d++)  //for each dimension
  1661. {
  1662.     partt.x.p.x[d] = alea_float(problem[level].H.min[d],problem[level].H.max[d]);
  1663. }
  1664. break;
  1665.  //------------------------------------------------------------------------------------------------- CASE 0
  1666. case 0: // ? Around p_i OR around p_g
  1667. // Method 0. Very good for some functions, very bad for some others
  1668. c1=1/error_i;
  1669. c2=1/error_g;
  1670. c3=c1+c2;
  1671. c1=c1/c3;
  1672. c2=c2/c3;
  1673. pr=alea_float(0,1);
  1674. if (pr<=c1) choice=0; else choice=1;    // More probably around p_g
  1675. //if (pr<=0.5) choice=0; else choice=1; // Same probability
  1676. //choice=1; // TEST only around p_g
  1677. strategy0:
  1678. rho1=distance(part.x,part.p_i);
  1679. rho2=distance(part.x,partg.p_i);
  1680. rho3=distance(part.p_i,partg.p_i);
  1681. if (choice==0)  // A point around p_i
  1682. {
  1683.       if (rho3>rho1) rho=rho3; else rho=rho1;
  1684.       rand_x=rand_in_hypersphere(DD, rho,nonunif);
  1685.       for (d = 0;d<DD;d++) 
  1686.     {
  1687.         pid=part.p_i.p.x[d];
  1688.         partt.x.p.x[d]  = rand_x.x[d]+pid;
  1689.     }
  1690. }
  1691. else  // A point around p_g
  1692. {
  1693.    if (rho3>rho2) rho=rho3; else rho=rho2;
  1694.       rand_x=rand_in_hypersphere(DD, rho,nonunif);
  1695.    for (d = 0;d<DD;d++) 
  1696.     {
  1697.         pgd=partg.p_i.p.x[d];
  1698.         partt.x.p.x[d]  = rand_x.x[d]+pgd;
  1699.     }
  1700. }
  1701. if (gener==1) return partt;
  1702. break;
  1703.   //------------------------------------------------------------------------------------------------- CASE 1
  1704. case 1: // * Pivot. Mixture "around p_i" "around p_g". The best compromise?
  1705. //  case1:
  1706.     lambda=1;
  1707.   case1a:
  1708. rho=distance(part.p_i,partg.p_i);
  1709. rho_i=rho; rho_g=rho;
  1710. //z=d_min(part.p_i,level); if (z<rho) rho_i=z; 
  1711. //z=d_min(partg.p_i,level); if (z<rho) rho_g=z;
  1712. rand_i=rand_in_hypersphere(DD, rho_i,nonunif); // Prepare random points in hypersphere
  1713. rand_g=rand_in_hypersphere(DD, rho_g,nonunif);
  1714.     c1=1/error_i;
  1715.     c2=1/error_g;
  1716.     c3=c1+c2;
  1717.     c1=c1/c3;
  1718.     c2=c2/c3;
  1719.     for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  1720.     {
  1721.         pid=part.p_i.p.x[d];
  1722.         pgd=partg.p_i.p.x[d];
  1723.         partt.x.p.x[d]  = c1*(rand_i.x[d]+pid) + c2*(rand_g.x[d]+pgd);
  1724.         partt.x.p.x[d]=partt.x.p.x[d]*lambda;
  1725.     }
  1726.     break;
  1727.  //------------------------------------------------------------------------------------------------- CASE 2
  1728. case 2: // ? Mixture "around x" "around p_i" "around p_g"
  1729. //Method 2
  1730. c0=1/error;
  1731. c1=1/error_i;
  1732. c2=1/error_g;
  1733. c3=c0+c1+c2;
  1734. c0=c0/c3;
  1735. c1=c1/c3;
  1736. c2=c2/c3;
  1737. rho1=distance(part.x,part.p_i);
  1738. rho2=distance(part.x,partg.p_i);
  1739. rho3=distance(part.p_i,partg.p_i);
  1740. if (rho2<rho1) rho_x=rho2; else rho_x=rho1;
  1741. if (rho1<rho3) rho_i=rho1; else rho_i=rho3;
  1742. if (rho3<rho2) rho_g=rho3; else rho_g=rho2;
  1743. rand_x=rand_in_hypersphere(DD, rho_x,nonunif);
  1744. //printf("\n %f",rho);
  1745. rand_i=rand_in_hypersphere(DD, rho_i,nonunif);
  1746. //printf("\n %f",rho);
  1747. rand_g=rand_in_hypersphere(DD, rho_g,nonunif);
  1748. //printf("\n %f\n",rho);
  1749. for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  1750. {
  1751.     partt.x.p.x[d]  = c0*(part.x.p.x[d]+rand_x.x[d])+c1*(part.p_i.p.x[d]+rand_i.x[d])+c2*(partg.p_i.p.x[d]+rand_g.x[d]);
  1752. }
  1753. break;
  1754.   //------------------------------------------------------------------------------------------------- CASE 3
  1755. case 3: // ? Try to find promising areas (center and radius) and choose one at random
  1756. // Method 3.
  1757. center_i.p.size=DD;
  1758. center_g.p.size=DD;
  1759. c0=error-error_i;
  1760. if (c0<almostzero)
  1761. {
  1762.     center_i=part.p_i;
  1763.     rho_i=distance(part.p_i,partg.p_i);
  1764. }
  1765. else
  1766. {
  1767.     c0=error/c0;
  1768.     for (d=0;d<DD;d++)
  1769.         center_i.p.x[d]=part.x.p.x[d]+c0*(part.x.p.x[d]-part.p_i.p.x[d]);
  1770.     rho_i=distance(part.p_i,center_i);
  1771. }
  1772. c0=error_i-error_g;
  1773. if (c0<almostzero)
  1774. {
  1775.     center_g=partg.p_i;
  1776.     rho_g=distance(part.p_i,partg.p_i);
  1777. }
  1778. else
  1779. {
  1780.     c0=error_i/c0;
  1781.     for (d=0;d<DD;d++)
  1782.         center_g.p.x[d]=part.p_i.p.x[d]+c0*(part.p_i.p.x[d]-partg.p_i.p.x[d]);
  1783.         rho_g=distance(partg.p_i,center_g);
  1784. }
  1785. c1=1/error_i;
  1786. c2=1/error_g;
  1787. c3=c1+c2;
  1788. c1=c1/c3;
  1789. c2=c2/c3;
  1790. pr=alea_float(0,1);
  1791. if (pr<=c1) choice=0; else choice=1;
  1792. if (choice==0)
  1793. {
  1794.     rand_i=rand_in_hypersphere(DD, rho_i,nonunif);
  1795.     for (d = 0;d<DD;d++) // A point around center_i
  1796.     {
  1797.         pid=center_i.p.x[d];
  1798.         partt.x.p.x[d]  = rand_i.x[d]+pid;
  1799.     }
  1800. }
  1801. else
  1802. {
  1803.     rand_g=rand_in_hypersphere(DD, rho_g,nonunif);
  1804.     for (d = 0;d<DD;d++) // A point around center_g
  1805.     {
  1806.         pgd=center_g.p.x[d];
  1807.         partt.x.p.x[d]  = rand_g.x[d]+pgd;
  1808.     }
  1809. }
  1810. break;
  1811.   //------------------------------------------------------------------------------------------------- CASE 4
  1812. case 4: // ? Mixture "around p_i" "around p_g" and "particle previous move"
  1813. rho=distance(part.p_i,partg.p_i);   // Same radius for the two hyperspheres
  1814. rho_i=rho;
  1815. rho_g=rho;
  1816. c1=error_i;
  1817. c2=error_g;
  1818. c0=(c1-c2)/(c1+c2);  // Should, on the whole, decrease during the process
  1819. case4a:
  1820.         c1=1/error_i;
  1821.     c2=1/error_g;
  1822.     c3=c1+c2;
  1823.     c1=c1/c3;
  1824.     c2=c2/c3;
  1825. rand_i=rand_in_hypersphere(DD, rho_i,nonunif); // Prepare random points in hypersphere
  1826. rand_g=rand_in_hypersphere(DD, rho_g,nonunif);
  1827.     for (d = 0;d<DD;d++) // Prepare a point "between" p_i and p_g
  1828.     {
  1829.         pid=part.p_i.p.x[d];
  1830.         pgd=partg.p_i.p.x[d];
  1831.         partt.x.p.x[d]  = c1*(rand_i.x[d]+pid) + c2*(rand_g.x[d]+pgd);
  1832.     }
  1833.  //case4b:
  1834.      for (d = 0;d<DD;d++)  // Add a contribution of the "velocity"
  1835.     {
  1836.         partt.x.p.x[d]  =  partt.x.p.x[d] +sign*c0*( part.x.p.x[d]-part.prev_x.p.x[d]);
  1837.     }
  1838.     break;
  1839.   //------------------------------------------------------------------------------------------------- CASE 5
  1840.    case 5: // ? Mixture "around p_i" "around p_g" and "particle previous move"
  1841.  // Like case 4, but radiuses may be diffferent, and bigger
  1842. // case5:
  1843. rho1=distance(part.x,part.p_i);
  1844. rho2=distance(part.x,partg.p_i);
  1845. rho3=distance(part.p_i,partg.p_i);
  1846. //if (rho1>rho3) rho_i=rho1; else rho_i=rho3;
  1847. //if (rho3>rho2) rho_g=rho3; else rho_g=rho2;
  1848. rho_i=MAX(rho1,rho3);
  1849. rho_g=MAX(rho2,rho3);
  1850. c1=error_i;
  1851. c2=error_g;
  1852. c0=(c1-c2)/(c1+c2);  // Should, on the whole, decrease during the process
  1853. goto case4a;
  1854.    //------------------------------------------------------------------------------------------------- CASE 9  (HYPERPARALLELEPIDS)
  1855.    case 9: //* "Classical PSO", for comparison. Mixture "around p_i" "around p_g" and "particle previous move"
  1856.             // with hyperparallelepids
  1857.     for (d = 0;d<DD;d++)
  1858.     {
  1859.         pid=part.p_i.p.x[d];
  1860.         pgd=partg.p_i.p.x[d];
  1861.     xd=part.x.p.x[d];
  1862.     partt.x.p.x[d]=xd+khi*(xd-part.prev_x.p.x[d]) + alea_float(0,cmax/2)*(pid-xd) + alea_float(0,cmax/2)*(pgd-xd);
  1863.    }
  1864.    
  1865.     break;
  1866.     //------------------------------------------------------------------------------------------------- CASE 10
  1867.    case 10:  //?? Keep moving. Neighbours are not taken into account.
  1868.        sign=1;
  1869. case10:
  1870.  c1=error_i;
  1871. c2=error_g;
  1872. c0=(c1-c2)/(c1+c2);  // Should, on the whole, decrease during the process
  1873.     for (d = 0;d<DD;d++)
  1874.     {
  1875.             xd=part.x.p.x[d];
  1876.             partt.x.p.x[d]  =xd+sign*c0*(xd-part.prev_x.p.x[d]) ;
  1877.             //partt.x.p.x[d]  =xd+alea_float(0,1)*(xd-part.prev_x.p.x[d]) ;
  1878.     }
  1879.    break;
  1880.      //------------------------------------------------------------------------------------------------- CASE 11
  1881.     case 11:   //?? Go back.   Neighbours are not taken into account.
  1882.     sign=-1;
  1883.     goto case10;
  1884.     break;
  1885.    //------------------------------------------------------------------------------------------------- CASE 12
  1886. case 12: //* Pivot+noise. Mixture "around p_i" "around p_g" + noise on position
  1887.     //6.1 Add decreasing noise (the same on each element)
  1888.         z=error_i;
  1889.         zz=error_g;
  1890.         c0=(z-zz)/(z+zz);
  1891.         lambda=1+alea_normal(0,c0);
  1892.         goto case1a;
  1893.     break;
  1894.  // ------------------------------------------------------------------------------------------------ CASE 13
  1895.  
  1896.  //------------------------------------------------------------------------------------------------- CASE 14
  1897. case 14: // Mixture "around p_i" "around p_g" , using Direct Gaussian distribution
  1898. rho=distance(part.p_i,partg.p_i);
  1899. rand_i=rand_in_hypersphere(DD, rho,-fabs(nonunif)); // Prepare random points in hypersphere
  1900.     c1=1/error_i;
  1901.     c2=1/error_g;
  1902.     c3=c1+c2;
  1903.     c1=c1/c3;
  1904.     c2=c2/c3;
  1905.     for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  1906.     {
  1907.         pid=part.p_i.p.x[d];
  1908.         pgd=partg.p_i.p.x[d];
  1909.         partt.x.p.x[d]  = c1*pid + c2*pgd+rand_i.x[d];
  1910.     }
  1911.     break;
  1912.   //------------------------------------------------------------------------------------------------- CASE 15
  1913. case 15: // Mixture "around p_i" "around p_g" , "around x",
  1914.             //using Direct Gaussian distribution (pivot method)
  1915.         mu=0.91;
  1916.      // Compute "gradients"
  1917.    rho=distance(part.x,part.p_i);
  1918.    rho_i=  distance(part.x,part.p_i);
  1919.    rho_g=distance(part.p_i,partg.p_i);
  1920.     if (rho>0) c1=(error-error_i)/rho;   else c1=0;   // "gradient" x-p
  1921.     if (rho_i>0) c2=(error-error_g)/rho_i;     else c2=0;        // "gradient" x-g
  1922.     if (rho_g>0) c3=(error_i-error_g)/rho_g;   else c3=0;     // "gradient p-g
  1923.    // Choose the pivot   (p or g)
  1924.       if (c3>=c1 && c3>=c2)
  1925.       {
  1926.        pivot=partg.p_i;
  1927.        rho=rho_g;
  1928.         //delta=1+2*(part.p_i.f.f[0] -partg.p_i.f.f[0]);
  1929.          sub_case=3;
  1930.        goto next_case15;
  1931.       }
  1932.         if (c2>=c1 && c2>=c3)
  1933.         {
  1934.          pivot=partg.p_i;
  1935.          rho=rho_i;
  1936.         // delta=1+2*(part.x.f.f[0] -partg.p_i.f.f[0]);
  1937.           sub_case=2;
  1938.          goto next_case15;
  1939.         }
  1940.          pivot=part.p_i;
  1941.          //delta=1+2*(part.x.f.f[0] -part.p_i.f.f[0]);
  1942.           sub_case=1;
  1943. next_case15:
  1944. //printf("\n rho %f",rho);
  1945.    if(rho<=0) {partt.x=part.x; break; } // No move
  1946.              lambda=1;
  1947.             for (d = 0;d<DD;d++)
  1948.             {
  1949.               switch (sub_case)
  1950.               {
  1951.               case 1:
  1952.               delta=fabs(part.x.p.x[d]-part.p_i.p.x[d]);
  1953.               break;
  1954.               case 2:
  1955.               delta=fabs(part.x.p.x[d]-partg.p_i.p.x[d]);
  1956.               break;
  1957.               case 3:
  1958.               delta=fabs(part.p_i.p.x[d]-partg.p_i.p.x[d]);
  1959.               break;
  1960.               }
  1961.               if (delta>0)
  1962.               lambda=lambda*delta;
  1963.             }
  1964.              sigma=mu*pow(rho/lambda,(double)(1/DD));
  1965.       for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  1966.     {
  1967.             switch (sub_case)
  1968.               {
  1969.               case 1:
  1970.                sigma_d=sigma*fabs(part.x.p.x[d]-part.p_i.p.x[d]);
  1971.               break ;
  1972.               case 2:
  1973.                 sigma_d=sigma*fabs(part.x.p.x[d]-partg.p_i.p.x[d]);
  1974.               break;
  1975.               case 3:
  1976.                  sigma_d=sigma*fabs(part.p_i.p.x[d]-partg.p_i.p.x[d]);
  1977.               break;
  1978.               }
  1979.          //   delta=fabs(alea_normal(0,sigma_d));
  1980. // printf(" sigma_d %f  delta %f " ,sigma_d,delta);
  1981.         //partt.x.p.x[d]  = pivot.p.x[d]+delta;
  1982.          delta=fabs(alea_normal(0, fabs(part.p_i.p.x[d]-partg.p_i.p.x[d])));  // Test
  1983.            partt.x.p.x[d]  = (part.p_i.p.x[d]+part.p_i.p.x[d])/2+delta;      // Test
  1984.     }
  1985.                break;
  1986.   //------------------------------------------------------------------------------------------------- CASE 16
  1987. case 16: // Mixture "around p_i" "around p_g" , "around x",
  1988.             //pivot method (using Direct Gaussian distribution if nonunif<0)
  1989. c1=1/error;
  1990. c2=1/error_i;
  1991. c3=1/error_g;
  1992. c0=c1+c2+c3;
  1993. c1=c1/c0;
  1994. c2=c2/c0;
  1995. c3=c3/c0;
  1996.    for (d = 0;d<DD;d++) pivot.p.x[d]=c1*part.x.p.x[d]+c2*part.p_i.p.x[d]+c3*partg.p_i.p.x[d];
  1997.  pivot.p.size=DD;
  1998.   rho=distance(pivot,partg.p_i);
  1999.    //rho_i=  distance(part.x,part.p_i);
  2000.    //rho_g=distance(part.p_i,partg.p_i);
  2001.    rand_g=rand_in_hypersphere(DD, rho,nonunif);
  2002.     for (d = 0;d<DD;d++)
  2003.     {
  2004.        partt.x.p.x[d]  =pivot.p.x[d]+rand_g.x[d];
  2005.     }
  2006.             break;
  2007.    //------------------------------------------------------------------------------------------------- CASE 17
  2008.  case 17:  // Simplified mixture (x,p_i,p_g)
  2009. rho1=distance(part.x,part.p_i);
  2010. rho2=distance(part.x,partg.p_i);
  2011. if (rho2<rho1) rho2=rho1;
  2012. rand_x=rand_in_hypersphere(DD, rho2,nonunif);
  2013. for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  2014. {
  2015.     partt.x.p.x[d]  = part.x.p.x[d]+rand_x.x[d];
  2016. }
  2017.  break;
  2018.  //------------------------------------------------------------------------------------------------- CASE 18
  2019.  case 18:  // Pivot method, with hyperspheres
  2020.   pivot=pivot_choice(level); 
  2021.   rho=distance(part.p_i,pivot);    // 
  2022.   rand_x=rand_in_hypersphere(DD, rho,nonunif);
  2023. for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  2024. {
  2025.    partt.x.p.x[d]  = pivot.p.x[d]+rand_x.x[d];
  2026. }
  2027.   break;
  2028.   //------------------------------------------------------------------------------------------------- CASE 19
  2029. case 19: // Pivot method with mixture "around p_i" "around pivot"
  2030.   pivot=pivot_choice(level);
  2031. rho=distance(part.p_i,pivot);
  2032. rand_i=rand_in_hypersphere(DD, rho,nonunif); // Prepare random points in hypersphere
  2033. rand_g=rand_in_hypersphere(DD, rho,nonunif);
  2034.  error_g=total_error(pivot.f);
  2035.  
  2036.          c1=1/error_i;
  2037.     c2=1/error_g;
  2038.     c3=c1+c2;
  2039.     c1=c1/c3;
  2040.     c2=c2/c3;
  2041.     for (d = 0;d<DD;d++) // for each dimension // HERE THE PARTICLE MOVES *****
  2042.     {
  2043.         pid=part.p_i.p.x[d];
  2044.         pgd=pivot.p.x[d];
  2045.         partt.x.p.x[d]  = c1*(rand_i.x[d]+pid) + c2*(rand_g.x[d]+pgd);
  2046.     }
  2047.     break;
  2048.  //----------------------------------------------------------------------------------- CASE 20
  2049.  case 20: // Semi-hypersphere in direction x(t-1)->x(t), centered on x
  2050. rho=distance(part.prev_x,part.x);
  2051. rand_x=rand_in_hypersphere(DD, rho,nonunif);
  2052.     for (d = 0;d<DD;d++)
  2053.     {
  2054.             xd=part.x.p.x[d];
  2055.             if (rand_x.x[d]*(xd-part.prev_x.p.x[d])<0)  rand_x.x[d]=0;
  2056.             partt.x.p.x[d]  =xd+rand_x.x[d];
  2057.     }
  2058.     break;
  2059.   //----------------------------------------------------------------------- CASE 21  (HYPERPARALLELEPIDS)
  2060.    case 21: // * For difficult problems (combinatorial, for example)
  2061.             // Gaussian on each dimension
  2062.     c1=2/0.97725; // With Gaussian distibrution,  0.97725 is the
  2063.                   //probability to have the result in [mean +- 2*standard_dev]
  2064.     c0=1/(c1-1+sqrt(c1*c1-2*c1));  // Constriction
  2065.     for (d = 0;d<DD;d++)
  2066.     {
  2067.         pid=part.p_i.p.x[d];
  2068.         pgd=partg.p_i.p.x[d];
  2069.         xd=part.x.p.x[d];
  2070.   
  2071.     rho1=fabs(pid-xd);
  2072.     rho2=fabs(pgd-xd);
  2073.     partt.x.p.x[d]  =xd+c0*((xd-part.prev_x.p.x[d]) + alea_normal(pid-xd,rho1/2)+ alea_normal(pgd-xd,rho2/2));
  2074.   
  2075.    }
  2076.     break;
  2077.   //---------------------------------------------------------------------- CASE 22  (HYPERPARALLELEPIDS)
  2078.    case 22: // For difficult problems (combinatorial?)
  2079.             // Intervals
  2080.       c1=1/error_i;
  2081.     c2=1/error_g;
  2082.     c3=c1+c2;
  2083.     c1=c1/c3;
  2084.     c2=c2/c3;
  2085.     
  2086.     for (d = 0;d<DD;d++)
  2087.     {
  2088.         pid=part.p_i.p.x[d];
  2089.         pgd=partg.p_i.p.x[d];
  2090.         rho=fabs(pid-pgd);
  2091.         partt.x.p.x[d]  = c1*(rand_i.x[d]+pid) + c2*(rand_g.x[d]+pgd);
  2092.         partt.x.p.x[d]  = c1*alea_float(pid-rho,pid+rho) + c2*alea_float(pgd-rho,pgd+rho);
  2093.    }
  2094.     break;
  2095. case 23: // * Ellipso&Atilde;ƒ&Acirc;&macr;dal positive sectors 
  2096.     // TO DO
  2097.     // replace by the one of OEP5, which doesn't need the hard coded c1
  2098. //case_23:
  2099.     rho=coeff_S_C*cmax;
  2100.     rand_i=rand_in_hypersphere(DD, rho,nonunif);
  2101.     rand_g=rand_in_hypersphere(DD, rho,nonunif);
  2102.      for (d=0;d<DD;d++) 
  2103.      {             
  2104.         pid=part.p_i.p.x[d];
  2105.         pgd=partg.p_i.p.x[d];
  2106.         xd=part.x.p.x[d];
  2107.          partt.x.p.x[d]=xd+khi*(xd-part.prev_x.p.x[d])+fabs(rand_i.x[d])*(pid-xd);
  2108.         partt.x.p.x[d]=partt.x.p.x[d]+fabs(rand_g.x[d])*(pgd-xd);
  2109.      }
  2110. break;
  2111. //-------------------------------------------------------------------- CASE 21  (HYPERPARALLELEPIDS)
  2112.    case 24: // Gaussian on each dimension
  2113.     coeff_rho=1;
  2114.     c0=1;
  2115.     c1=1;
  2116.     c2=1;
  2117.     for (d = 0;d<DD;d++)
  2118.     {
  2119.         pid=part.p_i.p.x[d];
  2120.         pgd=partg.p_i.p.x[d];
  2121.         xd=part.x.p.x[d];
  2122.   
  2123.     rho1=fabs(pid-xd);
  2124.     rho2=fabs(pgd-xd);
  2125.     partt.x.p.x[d]  =c0*xd+ alea_normal(pid-xd,c1*rho1)+ alea_normal(pgd-xd,c2*rho2); 
  2126.    }
  2127.     break;
  2128.    case 25: // Gaussian on each dimension
  2129.            // Just "local" search around the best position
  2130. //    choice=alea(0,1);
  2131.     choice=0; 
  2132.     if (choice==0)
  2133.         for (d = 0;d<DD;d++)
  2134.         {
  2135.             pgd=partg.p_i.p.x[d];
  2136.             xd=part.x.p.x[d];
  2137.             rho2=fabs(pgd-xd);
  2138.             partt.x.p.x[d]  =pgd+ alea_normal(pgd-xd,rho2);  // a
  2139.         }
  2140.     else
  2141.         for (d = 0;d<DD;d++)
  2142.         {
  2143.             pgd=partg.p_i.p.x[d];
  2144.             xd=part.x.p.x[d];
  2145.             rho2=fabs(pgd-xd);
  2146.             partt.x.p.x[d]  =pgd+ alea_normal(0,rho2); // b
  2147.         }
  2148.     break;
  2149.     case 26: // Uniform on each dimension
  2150.            // Just "local" search around the best position
  2151.     for (d = 0;d<DD;d++)
  2152.     {
  2153.         pgd=partg.p_i.p.x[d];
  2154.         xd=part.x.p.x[d];
  2155.     rho2=fabs(pgd-xd);
  2156.     partt.x.p.x[d]  =pgd+ (2*alea(0,1)-1)*alea_float(0,rho2); 
  2157.    }
  2158.     break;
  2159.   case 100: //  KE-C Method (Jim Kennedy and Russ Eberhart method, improved by Maurice Clerc)
  2160.     c0=1;
  2161.     c1=2.0;
  2162.     c2=c1;
  2163.     for (d=0;d<DD;d++)
  2164.     {
  2165.         xd=part.x.p.x[d];
  2166.         pid=part.p_i.p.x[d];
  2167.         pgd=partg.p_i.p.x[d];
  2168.         vd=part.v[d]; // "velocity"
  2169.         c3=c0*vd + alea_float(0,c1)*(pid-xd);  // c0*v + rand(0,c1)*(p-x)
  2170.         c3=c3+alea_float(0,c2)*(pgd-xd); //  + rand(0,c2)*(g-x)
  2171.                                       //Note that g is either the local best or the global best
  2172.                                       // depending on the neighbourhood size
  2173.         partt.v[d]=(int)c3; // New integer velocity
  2174.         c3=  xd+ partt.v[d]; // Add velocity
  2175.         c3=1/(1+exp(-c3)); // Keep position in ]0,1[
  2176.         pr=alea_float(0,1); // Probabilistic binary choice
  2177.         if (pr<c3)     partt.x.p.x[d]=1; else     partt.x.p.x[d]=0;
  2178.     }
  2179.     break;
  2180.     
  2181.    case 101: //  KE-C with taboo
  2182.     c0=1;
  2183.     c1=2.0;
  2184.     c2=c1;
  2185.     for (d=0;d<DD;d++)
  2186.     {
  2187.         if(part.mod[d]>0) continue; // "Taboo". Modify only if it has not just been modified
  2188.         xd=part.x.p.x[d];
  2189.         pid=part.p_i.p.x[d];
  2190.         pgd=partg.p_i.p.x[d];
  2191.         vd=part.v[d]; // "velocity"
  2192.         c3=c0*vd + alea_float(0,c1)*(pid-xd);  // c0*v + rand(0,c1)*(p-x)
  2193.         c3=c3+alea_float(0,c2)*(pgd-xd); //  + rand(0,c2)*(g-x)
  2194.                                       //Note that g is either the local best or the global best
  2195.                                       // depending on the neighbourhood size
  2196.         partt.v[d]=(int)c3; // New integer velocity
  2197.         c3=  xd+ partt.v[d]; // Add velocity
  2198.         c3=1/(1+exp(-c3)); // Keep position in ]0,1[
  2199.         pr=alea_float(0,1); // Probabilistic binary choice
  2200.         if (pr<c3)     partt.x.p.x[d]=1; else     partt.x.p.x[d]=0;
  2201.     }
  2202.     for (d=0;d<DD;d++)
  2203.     {
  2204.       if (partt.x.p.x[d]!=part.x.p.x[d])  partt.mod[d]=1; // Has been modified
  2205.           else partt.mod[d]=0; // Has not been modified
  2206.       }
  2207.     break;
  2208.   case 111: // Binary. Pivot method
  2209.    //from an idea of P. Serra, 1997
  2210.    // (seriously) modified and adapted to binary problems by M. Clerc, 2004
  2211.     // Works pretty well on some problems .. and pretty bad on some others
  2212.   // Note that there is no velocity, and the current position is not used
  2213.   // It works better when information links are modified at random
  2214.   // if there has been no improvement after the previous iteration
  2215.  partt.x=partg.p_i; // Best previous position g of the best informer
  2216.  // (for D>=2). Simplified formula.  Note that "dist" is an integer
  2217.  dist=(int)log(DD);
  2218.  if (dist<3) dist=3; if (dist>DD) dist=DD;  // 111a.
  2219.   r=alea_float(1,dist); //111. Note that r is never equal to dist
  2220.   //r=1+alea(1,dist-1);  //111b. Theoretically equivalent
  2221.  // r=1+alea(1,dist); //111c. A bit larger
  2222.  // r=alea(1,dist-1);  // 111d.   More uniform , on dist k values
  2223.                               // Sometimes far better but also sometimes far worse
  2224.  for (k=0;k<r;k++) // Define a position around g
  2225.  //    Note that even if there at least two k values
  2226.  //  there is a small probability (1/D^k)
  2227.  // that just one bit is modified
  2228.   {
  2229.     d=alea(0,DD-1);
  2230.     partt.x.p.x[d]=1- partt.x.p.x[d];
  2231.   }
  2232.  break;
  2233.  case 116: // Binary. Pivot method with "taboo"
  2234.  partt.x=partg.p_i; // Best previous position g of the best informer
  2235.  dist=(int)log(DD);
  2236.  if (dist<3) dist=3; if (dist>DD) dist=DD;  // 111a.
  2237.   r=alea_float(1,dist); //11. Note that r is never equal to dist
  2238.  for (k=0;k<r;k++)
  2239.   {
  2240.     d=alea(0,DD-1);
  2241.     if(part.mod[d]<1) // "Taboo". Modify only if it has not just been modified
  2242.       {
  2243.         partt.x.p.x[d]=1- partt.x.p.x[d];
  2244.       }
  2245.   }
  2246.     for (d=0;d<DD;d++)
  2247.     {
  2248.       if (partt.x.p.x[d]!=part.x.p.x[d])  partt.mod[d]=1; // Has been modified
  2249.           else partt.mod[d]=0; // Has not been modified
  2250.       }
  2251.  break;
  2252.  
  2253.     
  2254.  //------------------------------------------------------------------------------------------------- default
  2255. default:
  2256.    printf("\n ERROR. move_particle. Non defined strategy value %i",strategy);
  2257.      break;
  2258. }   // End switch(strategy)
  2259. // Adjustements -------
  2260. if (problem[level].printlevel>2)
  2261. {
  2262.     printf("\n  move_particle. Adjustements");
  2263.     display_position(partt.x);
  2264. }
  2265. partt=constrain(partt,level); // Keep it in the search space
  2266. // End od adjustements ------
  2267.  // ********** evaluation of the position     
  2268. partt.x.f=MyFunction(partt.x,problem[level].funct,problem[level].target,level); 
  2269.  
  2270. // Save best position
  2271. better=better_than(partt.x.f,partt.p_i.f,level);
  2272. if( better==1)
  2273. {
  2274.     partt.p_i = partt.x;
  2275. //    nb_improv=nb_improv+1;
  2276. }
  2277. //display_position(partt.x);
  2278.  //printf("\n %f %f",  total_error(partt.x.f),best_result.f.f[0]);
  2279.  
  2280. // Recompute the status
  2281. df=total_error(partt.x.f)-error;
  2282. if (df<0) partt.status=50;   // Improvement +
  2283. if (df==0) partt.status=30;   // No change  =
  2284. if (df >0) partt.status=10;     // Deterioration  -
  2285. df=error-total_error(part.prev_x.f);    // For the previous move...
  2286. if (df<0) partt.status=partt.status+5;   // Improvement   +
  2287. if (df==0) partt.status=partt.status+3;   // No change  =
  2288. if (df >0) partt.status=partt.status+1;     // Deterioration  -
  2289.  
  2290. /* Finally, we have the following table
  2291. 55  <=> + +
  2292. 53 <=> = +
  2293. 51 <=> - +
  2294. 35 <=> + =
  2295. 33  <=>  = =
  2296. 31 <=>  - =
  2297. 15 <=> + -
  2298. 13 <=> = -
  2299. 11 <=> - -
  2300. */
  2301.   
  2302. if (problem[level].printlevel>2) display_position(partt.x);
  2303. return partt;
  2304. }
  2305.  //---------------------------------------------------------------------------- PIVOT_CHOICE
  2306. struct position pivot_choice(int level)
  2307. {
  2308. /*
  2309.  Choose the pivot according to the memorized positions
  2310.  The better a position , the higher the probability it will be chosen as a pivot
  2311. Note: memo is a global variable
  2312. */
  2313. int                     i;
  2314. double               r;
  2315. int                     rank_p;
  2316. int                     size;
  2317. double               sum,sum1;
  2318. size=memo[level].size;
  2319. rank_p=0;
  2320. if (size==1) goto end;
  2321. sum=0;
  2322. for (i=0;i<size;i++)
  2323. {
  2324.      sum=sum+1/memo[level].error[i];
  2325. }
  2326.  r=alea_float(0,sum);
  2327.   sum1=1/memo[level].error[0];
  2328.  
  2329.  check:
  2330.   if (r<=sum1) goto end;
  2331.      rank_p=  rank_p+1;
  2332.      sum1=sum1+1/memo[level].error[rank_p];
  2333.      
  2334.      goto check;
  2335.      
  2336.  end:
  2337.  //printf("\npivot_choice rank %i    size %i",rank_p,size);
  2338.   rank[level]=rank_p; // Global variable. Useful for some strategies
  2339.  return memo[level].x[rank_p];
  2340. }
  2341. // ----------------------------------------------------------------------------- REINIT_SWARM
  2342. void reinit_swarm(int level,int option)
  2343. {
  2344.   /*
  2345.   Warning. No special initialisation like TSP or QAP, for the moment
  2346.   TO DO later
  2347.     */
  2348.     
  2349.     int better;
  2350.       int    d;
  2351.     int    i;
  2352.     int    i_best;
  2353.     int    init_option;
  2354.     int    j;
  2355.     int    j_best;
  2356.     struct position best;
  2357. // TR is a global variable
  2358. if(problem[level].printlevel>1) printf("\n reinit_swarm");
  2359. if(problem[level].printlevel>2) display_swarm(1,level);
  2360. best=best_result;
  2361. best_result.f.f[0]=infinite;
  2362. // Modify positions
  2363. init_option=1; // will re-init the position in init_particle, but not the links
  2364. //init_option=2; // Just recompute the position
  2365. for (i=0;i<TR[level].size;i++)
  2366. {
  2367.     for (j=0;j<TR[level].tr[i].size;j++)
  2368.     {
  2369.         TR[level].tr[i].part[j]=init_particle(init_option,level,TR[level].tr[i].part[j]);
  2370.          better=better_than(TR[level].tr[i].part[j].x.f, best_result.f,level);
  2371.        if (better==1)
  2372.         {
  2373.             i_best=i;
  2374.             j_best=j;
  2375.             best_result=TR[level].tr[i].part[j].x;
  2376.         }
  2377.     }
  2378. }
  2379. if (BIN==1) 
  2380.     for (d=0;d<best_result.p.size;d++) 
  2381.         best_result.p.x[d]=floor(best_result.p.x[d]+0.5);
  2382. if(option==0) // Eventually keep the best position found so far
  2383. {
  2384.     if (DYN==1) // The f value for the "best" may have changed
  2385.     {
  2386.         best.f=MyFunction(best,problem[level].funct,problem[level].target,level);
  2387.         eval_f_tot[level]+=-1; // We don't count this evaluation
  2388.     }
  2389.     
  2390.     better=better_than(best.f, best_result.f,level);
  2391.     if (better==1)
  2392.     {
  2393.         best_result=best;
  2394.         TR[level].tr[i_best].part[j_best].x=best;
  2395.         TR[level].tr[i_best].part[j_best].p_i=best;
  2396.     }
  2397. }
  2398.     if(problem[level].printlevel>1) display_swarm(1,level);
  2399. }
  2400. //================================================================  TOTAL_ERROR
  2401.  double    total_error(struct f err)
  2402.   {
  2403.    /*
  2404.      Compute the total error of a multiobjective result
  2405.    */
  2406.     double error;
  2407.     int  i;
  2408.     if (err.size==1) return err.f[0];
  2409.     
  2410.     error=0;
  2411.     for (i=0;i<err.size;i++) error=error+err.f[i]*err.f[i];
  2412.     error=sqrt(error);
  2413.     return error;
  2414.   }
  2415. and the header file def_struct.H looks like this 
  2416. #ifdef HAVE_CONFIG_H
  2417. #include <config.h>
  2418. #endif
  2419. #include <math.h>
  2420. #include <stdio.h>
  2421. #include <string.h>
  2422. #include <stdlib.h>
  2423. #include <time.h>
  2424. #define ulong unsigned long // For pseudo random numbers generation
  2425. #define RAND_MAX_KISS ((unsigned long) 4294967295)
  2426. #define E                2.718281828459045
  2427. //#define pi                3.141592653589793238462
  2428. #define    Max_C            70   // Max line number for a matrix
  2429. #define Max_DD             100 // Max number of dimensions
  2430.                               // Warning. For TSP problems, be sure Max_C=Max_DD>number of nodes
  2431. #define    Max_discrete    50 // Max number of discrete possible values for a discrete variable
  2432. #define Max_f        10 // Maximum number of objective functions (for multiobjective problems)
  2433. #define Max_nodes        50 // Maximum number of nodes in a level (for Neural Network Training)
  2434. #define    Max_M            10 // Max vector size for RtoR+ problem
  2435. #define  Max_Memo    10 // Max number of memorized positions
  2436. #define  Max_run     1000 // Max number of times you can run the same problem
  2437. #define Max_status 4
  2438. #define Max_swarmsize     201 // Theoretically infinite, but my computer does not like infinite...
  2439. #define nb_strateg   150 // Max number of defined strategies (see move_particle())
  2440. struct search_space    {int dim;float min[Max_DD];float max[Max_DD];float dx[Max_DD];float volume;};
  2441. struct vector        {double x[Max_DD];int size;};
  2442. struct f            {double f[Max_f];int size;};
  2443. struct position        {struct vector p;struct f f;};
  2444. struct matrix        {int size;double val[Max_C][Max_DD];int nb_cont;double max_cont;};
  2445. struct problem         {int constrain;int nb_f;struct matrix P;int funct;int DD;struct search_space H;float target;float
  2446. eps;int printlevel;float Max_Eval; float Max_Eval_2;float Max_Eval_delta;int save;int N;int times;int mine;int all_diff;char data[20];float used_constr;int
  2447. init_file;char init_swarm_r[20];char init_swarm_w[20];float max_fr;int init_size;int fuzzy;int peaks;};
  2448. struct    i_group        {int label[Max_swarmsize];int size;}; // List of particle labels                                
  2449. // A given particle may belong to several i-groups
  2450. struct particle        {int mod[Max_DD];double v[Max_DD];int label;struct position x;struct position p_i;struct i_group ig;int status;struct position prev_x;};
  2451. struct    tribe        {int size;struct particle part[Max_swarmsize];}; // Set of particles                                        
  2452. // A given particle belongs to just _one_ tribe
  2453. struct    tribe_list    {int size;struct tribe tr[Max_swarmsize];};
  2454. struct    vector_c    {int size;double x[Max_C];};
  2455. struct    vector_i    {int size; int x[Max_DD];}; // For special integer problem (binary, in particular)
  2456. struct memo       {int size;struct position x[Max_Memo];double error[Max_Memo];};
  2457. struct    discrete    {int d; int size; double v[Max_discrete];}; // d= rank of the discrete variable
  2458.                                                 // size= nb of values, v = value list
  2459. struct roots {double z[3]; int status;};// Roots of a polynom a*x^3+...=0
  2460. // Subroutines (declarations)
  2461. #include "read_display_save.h"
  2462. #include "tools.h"
  2463. #include "extra_tools.h"
  2464. #include "myconstrain.h"
  2465.  #include "TSP.h"
  2466.  #include "QAP.h"
  2467. #include "movpeaks_mc.h"
  2468.  
  2469. void              add_memo(struct position x, int level);
  2470. double            ANNCOLORCUBE(struct vector weights);
  2471. double          ANNParity4(struct vector weights);
  2472. double            ANNPIMA(struct vector weights);
  2473. double            ANNSERVO(struct vector weights);
  2474. double          ANNSINSIMP(struct vector weights);
  2475. double          ANNXor(struct vector weights);
  2476. double            apple_trees(struct position pos); // For "apple trees" example
  2477. struct particle    best_informer(struct particle part,int no_best,int level);
  2478. int                better_than(struct f f1,struct f f2,int level);
  2479. void clean_run(FILE *f_run,FILE *f_run_clean, int nb_f,int DD,int level);
  2480. double coeff_SC(int D);
  2481. struct particle complete_part(struct particle par,int option,int level);
  2482. struct position    homogen_to_carte(struct position pos) ;
  2483. struct particle    init_particle(int option,int level, struct particle part0);
  2484. void            link_reorg(int level);
  2485. int                local_improv(struct tribe tr,int level);
  2486. double             max_comp(struct position pos);
  2487. double          MINLP(struct position pos,int option);
  2488. double             min_comp(struct position pos);
  2489. struct particle    move_particle(struct particle part,struct particle partg,int level, int gener);
  2490. struct f            MyFunction(struct position pos,int funct,double target,int level);
  2491. struct position pivot_choice(int level);
  2492. struct position    PSO(int level, float Max_Eval);
  2493. ulong    rand_kiss();
  2494. void            reinit_swarm(int level,int option);
  2495. void    seed_rand_kiss(ulong seed);
  2496. double            tot_fifty_fifty(struct position pos);
  2497. double               total_error(struct f err);
  2498. // Global variables
  2499. float            a[Max_M]; // Vector for RtoR+ problem
  2500. int                adapt;
  2501. double        almostzero=0.000000001;  //to avoid overflow by dividing by too small value;
  2502. int                  circular_hood; // Flag for option. Read as data. Useful just to have the "classical
  2503.                                              // constricted PSO" for comparison. Usually equal to 0. If not, the value
  2504.                                              // is the size of the circular neighbourhood.
  2505. int                AS=-3; // Arbitrary value. Means "Assigned"
  2506. struct position BEST;
  2507. struct position best_result;
  2508. int                BIN; // Flag for binary problem
  2509. clock_t            clock_tick;
  2510. double            cmax;
  2511. struct position coeff;
  2512. double            coeff_S_C;
  2513. int       confin_interv;
  2514. struct    discrete discrete[Max_DD];
  2515. int                discrete_nb; // Number of special discrete variables
  2516. struct particle    dummy_part; // Just as empty parameter
  2517. int     DYN; // Flag for dynamic optimisation
  2518. float             e[Max_DD][Max_M]; // Matrix for RtoR+ problem
  2519. double         eval_f[2];
  2520. double         eval_f_tot[2];
  2521. char            functions[100][100]={" "}; // Function names
  2522. int                geno_size; // for Moving Peaks. = search space dimension
  2523. int                H; // Flag for Coloring problem. Indicates what kind of projection to do
  2524. int       HIDDEN; // For Neural Network Training
  2525. double            infinite=99999999999999999.0;
  2526. int       INPUT; // For Neural Network Training
  2527. double            khi;
  2528. int                label[2]; // To labellize particles
  2529. int       landscape[100][Max_DD]; // For binary multimodal problem
  2530. int             lexico; // Flag for using or not lexicographical order for multiobjective optim.
  2531. int                linkreorg; // Flag for using (1) or not (0) link reorganization
  2532. struct memo   memo[2]; // To memorize positions (mainly for pivot method)
  2533.  int                NA=-32000;// Arbitrary value. Means "non assigned"
  2534. int                NO=-2; // Arbitrary value. Means "no change"
  2535. int                max_rand;
  2536. //int                Max_swarm; // Just for info
  2537. double            Mean_swarm; // Just for info
  2538. int               MEMO; // Flag. 1 if positions have to be memorized (pivot methods)
  2539. int                MM;
  2540. int                nb_pb_max;
  2541. double            n_change; // For dynamic optimisation. Number of change
  2542. int                no_best;
  2543. int       nprogr; // For progressive approach
  2544. double            offline_error; // Error for dynamic optimisation
  2545.                                 // modified before each change
  2546. double            offline_error_cont; // Error for dynamic optimisation, 
  2547.                                     // but continuously computed
  2548. int       OUTPUT; // For Neural Network Training
  2549. double            phi;
  2550. double            pi;
  2551. struct problem problem[2];
  2552. int   PROGR; // Flag for progressive approach
  2553. int            QAP; // Flag for QAP. Use it only for level 0
  2554. int               rand_hood; // Flag for using random i-groups or not (>0 =yes, and it gives the size, 0=no)
  2555. int               rank[2];
  2556. int                recent_change; // Move Peaks.  1 indicates that a change has just ocurred 
  2557. int                recurs; // Flag for recursive call (particularly for reinit_swarm)
  2558. double                retp[Max_nodes];
  2559. double   status_count[Max_status]; // Just for information about particle status
  2560. float strategies[2][Max_status]; //See  Adaptations and move_particle(). See also explanation in problem.txt
  2561. int                times;
  2562. struct tribe_list    TR[2];
  2563. int               TSP; // Flag. 1 if TSP. Use it only for level 0
  2564. double            two_pi;
  2565. struct vector    Xmax,Xmin;
  2566. //--------
  2567. FILE *f_discrete; // Possibles values for discrete variables
  2568.                 // (if they can't be computed just by giving min, max and granularity)
  2569. FILE *f_energy;
  2570. FILE *f_init_r; // (optional) to Read an initial swarm
  2571. FILE *f_init_w; // to Write an initial swarm for future use
  2572. FILE *f_data; // For additional data, defining the problem (see Coloring/Frequency Assignment, RtoR+)
  2573. FILE *f_functs; // Function names
  2574. FILE *f_matrix;
  2575. FILE *f_problem; // Problem file
  2576. FILE *f_run; // To save the run
  2577. FILE *f_run_c; // To temporarily save the run for multiobjective problem
  2578. FILE *f_run_clean; // "Cleaned runs" for a multiobjective problem
  2579. FILE *f_vector;
  2580. FILE *f_swarm;  // To save swarm
  2581. FILE *f_synth; // Summary (mean values) if a given problem is ran several times
  2582. FILE *f_trace; // Some additional information (see parameter "save" in problems.txt)
  2583. FILE *f_trace_run; // save the number of evaluations and the best result after each iteration
  2584.                   //(in order to plot the convergence curve)

please help me in this regard.
pappala





» Post edited 2006-03-10, 03:22am by misterhaan.

 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

Penguino AVR

Want to learn about robotics or microcontrollers?
Check out the Penguino AVR from our friends at
Icy Labs