/* ---------------------------------------------------------------------------- ex3.C mbwall 28jul94 Copyright (c) 1995-1996 Massachusetts Institute of Technology DESCRIPTION: Example program for the SimpleGA class and 2DBinaryStringGenome class. This program reads in a 2D pattern from a data file then tries to match the pattern in a 2D binary string genome. We use a simple GA (with linear scaled fitness selection and non-steady-state population generation) and 2D binary string genomes. This example also illustrates the use of the GAParameterList for setting default parameters on the genetic algorithm and for parsing the command line. ---------------------------------------------------------------------------- */ #include #include #include #include #include // the header for the GA we'll use #include // and the header for the genome we need float objective(GAGenome &); int main(int argc, char *argv[]) { cout << "Example 3\n\n"; cout << "This program reads in a data file then runs a simple GA whose\n"; cout << "objective function tries to match the pattern of bits that are\n"; cout << "in the data file.\n\n"; // See if we've been given a seed to use (for testing purposes). When you // specify a random seed, the evolution will be exactly the same each time // you use that seed number. for(int ii=1; ii= argc){ cerr << argv[0] << ": the file option needs a filename.\n"; exit(1); } else{ sprintf(filename, argv[i]); continue; } } else if(strcmp("seed", argv[i]) == 0){ if(++i < argc) continue; continue; } else { cerr << argv[0] << ": unrecognized arguement: " << argv[i] << "\n\n"; cerr << "valid arguments include standard GAlib arguments plus:\n"; cerr << " f\tfilename from which to read (" << filename << ")\n"; cerr << "\n"; exit(1); } } // Read in the pattern from the specified file. File format is pretty simple: // two integers that give the height then width of the matrix, then the matrix // of 1's and 0's (with whitespace inbetween). ifstream inStream(filename, ios :: in); if(!inStream){ cerr << "Cannot open " << filename << " for input.\n"; exit(1); } int height, width; inStream >> height >> width; short **target = new short*[width]; for(i=0; i> target[i][j]; inStream.close(); // Print out the pattern to be sure we got the right one. cout << "input pattern:\n"; for(j=0; j