home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / genetk / stgenfun.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-28  |  2.3 KB  |  101 lines

  1.  
  2.  
  3. /********************************************************************
  4. (c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.
  5. ********************************************************************/
  6.  
  7.  
  8.  
  9. #include<string.h>
  10. #include<fstream.h>
  11. #include<iostream.h>
  12. #include<iomanip.h>
  13. #include<stdlib.h>
  14. #include<conio.h>
  15. #include<math.h>
  16. #include<stdio.h>
  17.  
  18. //                              1         2         3         4         5         6         7
  19. //                     1234567890123456789012345678901234567890123456789012345678901234567890
  20. char   *TARGET_STRING="Genetic algorithm at work!";
  21. #define TARGET_STRING_LENGTH   27
  22.  
  23.  
  24.  
  25. #include"genfuncs.h"
  26.  
  27.  
  28.  
  29. extern ofstream outfile;
  30.  
  31.  
  32. void pre_init()
  33. {
  34.    clrscr();
  35.    printf("/********************************************************************\n");
  36.    printf("(c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.\n");
  37.    printf("********************************************************************/\n");
  38.    
  39.    
  40.    printf("\n\nDemo of a genetic algorithm trying to guess a target string.\n");
  41.    printf("\nTarget String = Genetic algorithm at work!\n");
  42.    printf("\nPress Q to quit.\n");
  43.    
  44.    
  45.    
  46.    printf("\n\n\nPress any key to continue\n");
  47.    
  48.    getch();
  49.    
  50.    randomize();
  51.    clrscr();
  52. }
  53.  
  54. void init_round()
  55. {
  56. }
  57.  
  58.  
  59. double score_func(unsigned char *gene_ptr)
  60. {
  61.    char s[TARGET_STRING_LENGTH];
  62.    strncpy(s,gene_ptr,TARGET_STRING_LENGTH-1);
  63.    s[TARGET_STRING_LENGTH-1]='\0';
  64.    long double score=0,temp;
  65.    for(int i=0;i<TARGET_STRING_LENGTH;i++)
  66.    {
  67.       score-=((double)abs(s[i]-TARGET_STRING[i]));
  68.    }
  69.    return (double) score;
  70. }
  71.  
  72. // This is a function passed to the population class to display a given
  73. // solution. In this case the solution is displayed graphically as a map
  74. // of the cities and the route taken by the salesman.
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. void print_solution(unsigned char *gene_ptr,double score,ofstream *ofs)
  82. {
  83.    unsigned char s[TARGET_STRING_LENGTH];
  84.    clrscr();
  85.    cout<<"Score = "<<score<<"\n\n";
  86.    strncpy(s,gene_ptr,TARGET_STRING_LENGTH-1);
  87.    s[TARGET_STRING_LENGTH-1]='\0';
  88.    for(int i=0;i<TARGET_STRING_LENGTH-1;i++)
  89.    {
  90.       if((s[i]>=' ')&&(s[i]<=128))
  91.       {
  92.          cout<<s[i];
  93.       }
  94.       else
  95.       {
  96.          cout<<'*';
  97.       }
  98.    }
  99. }
  100.  
  101.