home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / genetk / gmainst.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. /*
  10.  
  11. This is a test program for the population class, which is an
  12. implementation of a genetic algorithm that uses an evolutionary
  13. approach to solving problems. The population class has little
  14. information about the problem but is passed a pointer to a
  15. scoring function which places a numerical score on any given
  16. solution. Each solution is represented by a string of bytes. The
  17. population class starts with n random solutions to the problem.
  18. They are scored with the scoring function and sorted by their
  19. scores or their "fitness". The least fit are dropped from the
  20. population. They are replaced by the offspring of the more fit
  21. which are generated by combining bytes of successful parents.
  22. Mutation or randomization is then applied to some elements of
  23. each generation.
  24.  
  25.  
  26.  
  27. Each new generation is then treated in the same way. To
  28. summarize:
  29.  
  30.  
  31.  
  32. 1) The worst solutions are dropped from the population.
  33.  
  34.  
  35.  
  36. 2) The best are combined with other survivors to create new
  37. offspring which have some of the properties of each of their
  38. parents. This introduces new combinations of properties.
  39.  
  40.  
  41.  
  42. 3) Mutation is applied randomly to the population,
  43. introducing new properties that were not previously found in the
  44. population.
  45.  
  46.  
  47.  
  48. 4) Each element of the population is scored. ( Better
  49. solutions are scored higher.) The population is sorted by score.
  50.  
  51.  
  52.  
  53. This process is repeated many times, possibly thousands of
  54. generations or even more may be required before a desired
  55. solution evolves.
  56.  
  57.  
  58. */
  59.  
  60.  
  61. //-------------------------<   GMAINST.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           500
  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 travelling sales man.
  93.    population pop(POP_SIZE,               // number of ellements in population
  94.    sizeof(long),           // size of each gene
  95.    40,                     // 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.