home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / epo-05.zip / simu.c < prev    next >
C/C++ Source or Header  |  1994-04-20  |  6KB  |  233 lines

  1. /****************************************************************************
  2. *                                                                           *
  3. *   simulaton program for the evolutionary optimizer:                       *
  4. *   target function is to be included in this source                        *
  5. *   Version 0.5                                                             *
  6. *   19.4.94            (C) Petra Roosen                                     *
  7. *                                                                           *
  8. ****************************************************************************/
  9.  
  10. #define UNIX            /* put as comment if preparing for OS/2! */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include "epo.h"
  16.  
  17.  
  18. /**** global variables: ****/
  19.  
  20. Paraset Indi;
  21. int     Parazahl, Runde;
  22. Iniset  Limits[5];
  23. FILE    *infile, *outfile, *flagfile;
  24. char    Transdir[250];
  25. int     sleeptime=1;
  26.  
  27.  
  28. /**** function prototypes: ****/
  29.  
  30. void    main(void);
  31. void    init(void);
  32. bool    empfange(void);
  33. void    rechne(void);
  34. bool    verschicke(void);
  35.  
  36.  
  37. /**************************************************************************/
  38. /**************************************************************************/
  39. /******************* Begin of target function determination ***************/
  40.  
  41. void rechne()  /* Example: Minimal fct. value of a hyper-paraboloid */
  42. {
  43.   double  Ergebnis;
  44.   int     i;
  45.  
  46.  
  47.   Ergebnis = 0;
  48.   for (i=0; i<Parazahl; i++)
  49.   {
  50.     Ergebnis += Indi.koeff[i] * Indi.koeff[i];
  51.   }
  52.  
  53.   Indi.Ergebnis = Ergebnis;
  54. }
  55.  
  56. /******************** End of target function determination *****************/
  57. /***************************************************************************/
  58. /***************************************************************************/
  59.  
  60. void main()
  61. {
  62.   bool klappt;
  63.  
  64.   init();               /* for transfer directory determination only */
  65.  
  66.   forever
  67.   {
  68.     Runde++;
  69.     klappt = empfange();
  70.     rechne();
  71.     printf("%d. simulator result: %G,          sleep time: %d\n",
  72.            Runde, Indi.Ergebnis, sleeptime);
  73.     klappt = verschicke();
  74.   }
  75. }
  76.  
  77.  
  78. void init()
  79. {
  80.   FILE    *fp;
  81.   char    dummy[250];
  82.  
  83.   printf("\n\n");
  84.   printf("  **************************************************\n");
  85.   printf("  * Simulator module for co-operation with the     *\n");
  86.   printf("  * evolutionary parameter optimizer               *\n");
  87.   printf("  * for general optimization problems              *\n");
  88.   printf("  *                                                *\n");
  89.   printf("  * Shareware version 0.5    (C)3/94  Petra Roosen *\n");
  90.   printf("  **************************************************\n\n");
  91.  
  92.  
  93.   /******* Einlesen des Transfer-Directories ******/
  94.   if ( (fp=fopen("strateg.dat","r")) )
  95.     {
  96.       fscanf(fp, "%s", dummy);
  97.       if ( strcmp(dummy, "EPO-0.5") )
  98.       {
  99.         printf("\007\007Wrong initialization file at the identification header!\n");
  100.         sleep(5);
  101.         exit(0);
  102.       }
  103.       fscanf(fp, "%*s");
  104.       fscanf(fp, "%*s %*s");
  105.       fscanf(fp, "%*s %*d");
  106.       fscanf(fp, "%*s %*d");
  107.       fscanf(fp, "%*s %*d");
  108.       fscanf(fp, "%*s %*d");
  109.       fscanf(fp, "%*s %*d");
  110.       fscanf(fp, "%*s %*s");
  111.       fscanf(fp, "%*s %*s");
  112.       fscanf(fp, "%s %s", dummy, Transdir);
  113.       if ( strcmp(dummy, "transfer_dir") )
  114.       {
  115.         printf("\007Faulty initialization file at \"transfer_dir\"!\n");
  116.         sleep(5);
  117.         exit(0);
  118.       }
  119.       fclose(fp);
  120.     }
  121. }
  122.  
  123.  
  124. bool empfange()
  125. {
  126.   int i, gelesen;
  127.   char aktFile[250];
  128.  
  129.  
  130.   sprintf(aktFile, "%so2s.msg", Transdir);
  131.  
  132.   do    /** auf die Existenz des Message-Files warten **/
  133.   {
  134. #ifdef UNIX
  135.     sleep(1);
  136. #else
  137.     _sleep2(400);
  138. #endif
  139.     flagfile = fopen(aktFile, "r");
  140.   }
  141.     while (flagfile == NULL);
  142.   fclose(flagfile);
  143.   remove(aktFile);
  144.  
  145.   sprintf(aktFile, "%so2s.dat", Transdir);
  146.  
  147. lies_Daten:
  148. #ifdef UNIX
  149.     sleep(sleeptime);
  150. #else
  151.     _sleep2(sleeptime);
  152. #endif
  153.   infile = fopen(aktFile, "r");
  154.  
  155.   if (infile == NULL)
  156.     {
  157.       printf("\007\007");
  158.       printf("There is no data file, although the message file was found.\n");
  159.       printf("Is there another program interfering with the file transfer\n");
  160.       printf("between optimizer and simulator??\n");
  161.       printf("Program dies because of error state!\n");
  162.       sleep(5);
  163.       exit(0);
  164.     }
  165.  
  166.   fscanf(infile, "%*s %*s %d %*s", &Parazahl);
  167.   if (Parazahl == 0)
  168.     {
  169.       printf("Stop message received from optimizer: simulator dies.\n");
  170.       fclose(infile);
  171.       sleep(5);
  172.       remove(aktFile);
  173.       exit(0);
  174.     }
  175.  
  176.   for (i=0; i<Parazahl; i++)
  177.   {
  178.     gelesen = fscanf(infile, "%s %lf %lf",
  179.                      Limits[i].Name, &(Indi.koeff[i]), &(Indi.dkoeff[i]) );
  180.     if (gelesen != 3)
  181.       {
  182.         fclose(infile);
  183.         sleeptime += 2;
  184.         goto lies_Daten;
  185.       }
  186.     sleeptime--;
  187.     sleeptime = max(sleeptime, 0);
  188.   }
  189.   fscanf(infile, "%*s %d %*s %d %*s %d %*s %d",
  190.          &(Indi.Kennzahl), &(Indi.Mama), &(Indi.Papa), &(Indi.Alter) );
  191.   fclose(infile);
  192.  
  193.   remove("o2s.dat");
  194.   remove("o2s.msg");
  195.  
  196.   return TRUE;
  197. }
  198.  
  199.  
  200. bool verschicke()
  201. {
  202.   int  i;
  203.   char aktFile[250];
  204.  
  205.  
  206.   sprintf(aktFile, "%ss2o.dat", Transdir);
  207.   outfile = fopen(aktFile, "w");
  208.  
  209.   if (outfile != NULL)
  210.     {
  211.       fprintf(outfile, "Ergebnis zu Kennzahl %d mit %d Parametern\n\n",
  212.               Indi.Kennzahl, Parazahl);
  213.       for (i=0; i<Parazahl; i++)
  214.       {
  215.         fprintf(outfile, "%s %G %G\n",
  216.                 Limits[i].Name, Indi.koeff[i], Indi.dkoeff[i]);
  217.       }
  218.       fprintf(outfile, "Mama     %d\n", Indi.Mama);
  219.       fprintf(outfile, "Papa     %d\n", Indi.Papa);
  220.       fprintf(outfile, "Alter    %d\n", Indi.Alter);
  221.       fprintf(outfile, "Ergebnis %G\n", Indi.Ergebnis);
  222.       fclose(outfile);
  223.       sprintf(aktFile, "%ss2o.msg", Transdir);
  224.       flagfile = fopen(aktFile, "w");
  225.       fclose(flagfile);
  226.  
  227.       return TRUE;
  228.         }
  229.         else
  230.             return FALSE;
  231. }
  232.  
  233.