home *** CD-ROM | disk | FTP | other *** search
-
-
- /********************************************************************
- (c) Copyright 1993 Toms Computer Solutions,Inc. All rights reserved.
- ********************************************************************/
-
- /*
-
- This is a test program for the population class, which is an
- implementation of a genetic algorithm that uses an evolutionary
- approach to solving problems. The population class has little
- information about the problem but is passed a pointer to a
- scoring function which places a numerical score on any given
- solution. Each solution is represented by a string of bytes. The
- population class starts with n random solutions to the problem.
- They are scored with the scoring function and sorted by their
- scores or their "fitness". The least fit are dropped from the
- population. They are replaced by the offspring of the more fit
- which are generated by combining bytes of successful parents.
- Mutation or randomization is then applied to some elements of
- each generation.
-
-
-
- Each new generation is then treated in the same way. To
- summarize:
-
-
-
- 1) The worst solutions are dropped from the population.
-
-
-
- 2) The best are combined with other survivors to create new
- offspring which have some of the properties of each of their
- parents. This introduces new combinations of properties.
-
-
-
- 3) Mutation is applied randomly to the population,
- introducing new properties that were not previously found in the
- population.
-
-
-
- 4) Each element of the population is scored. ( Better
- solutions are scored higher.) The population is sorted by score.
-
-
-
- This process is repeated many times, possibly thousands of
- generations or even more may be required before a desired
- solution evolves.
-
-
- */
-
-
-
-
- //-------------------------< GMAINCI.CPP >-------------------------
-
-
- #include"genetic.h"
- #include"genfuncs.h"
- #include<string.h>
- #include<fstream.h>
-
-
- ofstream outfile;
-
-
-
- #define POP_SIZE 150
-
-
- extern unsigned _stklen=40000U;
-
-
- // This is the main program which sets up the population and starts the
- // evolution of a "solution"
-
- main(int argc,char **argv)
- {
-
- double result;
- time_t elapsed_time;
-
- // open the output file
- outfile.open("genout.txt");
-
- // Set up the population class for traveling sales man.
- population pop(POP_SIZE, // number of ellements in population
- sizeof(long), // size of each gene
- 60, // number of genes
- score_func, // pointer to scoreing function
- print_solution, // pointer to display function
- init_round, // pointer to the initializing round function
- pre_init); // pointer to the pre initializing function
-
-
- /*
- // Set up the population class for match string.
- population pop(POP_SIZE, // number of ellements in population
- sizeof(char), // size of each gene
- 26, // number of genes
- score_func, // pointer to scoreing function
- print_solution, // pointer to display function
- init_round, // pointer to the initializing round function
- pre_init); // pointer to the pre initializing function
-
- */
- /*
- // Set up the population class for financial buy sell signals.
- population pop(POP_SIZE, // number of ellements in population
- sizeof(char), // size of each gene
- 10, // number of genes
- score_func, // pointer to scoreing function
- print_solution, // pointer to display function
- init_round, // pointer to the initializing round function
- pre_init); // pointer to the pre initializing function
- */
- //load part of a previous population if spesified in the command line
- if(argc==3)
- {
- pop.load_from_file(argv[1],0,atoi(argv[2]));
- }
-
- // Set up the parameters for the evolution
- evolve_parms ev_parms;
- ev_parms.read_file("genparms.txt");
-
-
- result =pop.evolve (1, // display results on screen ?
- 0, // display population status ?
- ev_parms); // use these parameters.
-
-
- // Display final score.
- outfile<<"\nfinal score="<<result<<"\n\n";
- cout <<"\nfinal score="<<result<<"\n\n";
-
- // print the final population
- pop.print_top(1);
-
- //save the final population in genpop.bin
- pop.save_to_file("genpop.bin",0,POP_SIZE-1);
-
-
- return 0;
- }
-