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

  1.  
  2.  
  3. /********************************************************************
  4. (c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.
  5. ********************************************************************/
  6.  
  7. /*
  8.  
  9. This is a test program for the population class, which is an
  10. implementation of a genetic algorithm that uses an evolutionary
  11. approach to solving problems. The population class has little
  12. information about the problem but is passed a pointer to a
  13. scoring function which places a numerical score on any given
  14. solution. Each solution is represented by a string of bytes. The
  15. population class starts with n random solutions to the problem.
  16. They are scored with the scoring function and sorted by their
  17. scores or their "fitness". The least fit are dropped from the
  18. population. They are replaced by the offspring of the more fit
  19. which are generated by combining bytes of successful parents.
  20. Mutation or randomization is then applied to some elements of
  21. each generation.
  22.  
  23.  
  24.  
  25. Each new generation is then treated in the same way. To
  26. summarize:
  27.  
  28.  
  29.  
  30. 1) The worst solutions are dropped from the population.
  31.  
  32.  
  33.  
  34. 2) The best are combined with other survivors to create new
  35. offspring which have some of the properties of each of their
  36. parents. This introduces new combinations of properties.
  37.  
  38.  
  39.  
  40. 3) Mutation is applied randomly to the population,
  41. introducing new properties that were not previously found in the
  42. population.
  43.  
  44.  
  45.  
  46. 4) Each element of the population is scored. ( Better
  47. solutions are scored higher.) The population is sorted by score.
  48.  
  49.  
  50.  
  51. This process is repeated many times, possibly thousands of
  52. generations or even more may be required before a desired
  53. solution evolves.
  54.  
  55.  
  56. */
  57.  
  58.  
  59.  
  60.  
  61. //-------------------------<   GMAINCI.CPP    >-------------------------
  62.  
  63.  
  64. #include"genetic.h"
  65. #include"genfuncs.h"
  66. #include<string.h>
  67. #include<fstream.h>
  68.  
  69.  
  70. ofstream outfile;
  71.  
  72.  
  73.  
  74. #define POP_SIZE           150
  75.  
  76.  
  77. extern unsigned _stklen=40000U;
  78.  
  79.  
  80. // This is the main program which sets up the population and starts the
  81. // evolution of a "solution"
  82.  
  83. main(int argc,char **argv)
  84. {
  85.    
  86.    double result;
  87.    time_t elapsed_time;
  88.    
  89.    // open the output file
  90.    outfile.open("genout.txt");
  91.    
  92.    // Set up the population class for traveling sales man.
  93.    population pop(POP_SIZE,               // number of ellements in population
  94.    sizeof(long),           // size of each gene
  95.    60,                     // number of genes
  96.    score_func,             // pointer to scoreing function
  97.    print_solution,         // pointer to display function
  98.    init_round,             // pointer to the initializing round function
  99.    pre_init);              // pointer to the pre initializing function
  100.    
  101.    
  102.    /*
  103.    // Set up the population class for match string.
  104.    population pop(POP_SIZE,               // number of ellements in population
  105.    sizeof(char),           // size of each gene
  106.    26,                     // number of genes
  107.    score_func,             // pointer to scoreing function
  108.    print_solution,         // pointer to display function
  109.    init_round,             // pointer to the initializing round function
  110.    pre_init);              // pointer to the pre initializing function
  111.    
  112.    */
  113.    /*
  114.    // Set up the population class for financial buy sell signals.
  115.    population pop(POP_SIZE,               // number of ellements in population
  116.    sizeof(char),           // size of each gene
  117.    10,                     // number of genes
  118.    score_func,             // pointer to scoreing function
  119.    print_solution,         // pointer to display function
  120.    init_round,             // pointer to the initializing round function
  121.    pre_init);              // pointer to the pre initializing function
  122.    */
  123.    //load part of a previous population if spesified in the command line
  124.    if(argc==3)
  125.    {
  126.       pop.load_from_file(argv[1],0,atoi(argv[2]));
  127.    }
  128.    
  129.    // Set up the parameters for the evolution
  130.    evolve_parms ev_parms;
  131.    ev_parms.read_file("genparms.txt");
  132.    
  133.    
  134.    result      =pop.evolve               (1,               // display results on screen ?
  135.    0,               // display population status ?
  136.    ev_parms);       // use these parameters.
  137.    
  138.    
  139.    // Display final score.
  140.    outfile<<"\nfinal score="<<result<<"\n\n";
  141.    cout   <<"\nfinal score="<<result<<"\n\n";
  142.    
  143.    // print the final population
  144.    pop.print_top(1);
  145.    
  146.    //save the final population in genpop.bin
  147.    pop.save_to_file("genpop.bin",0,POP_SIZE-1);
  148.    
  149.    
  150.    return 0;
  151. }
  152.