/* ---------------------------------------------------------------------------- ex7.C mbwall 19jan95 Copyright 1995-1996 Massachusetts Institute of Technology DESCRIPTION: This example functions the same way as example 3, but this example shows how to use some of the other member functions in the GA library. We also do a few more fancy things with the genome (ie use the read/write methods). ---------------------------------------------------------------------------- */ #include #include #include #include #include float objective(GAGenome &); int main(int argc, char *argv[]) { cout << "Example 7\n\n"; cout << "This program reads in a data file then runs a steady-state GA \n"; cout << "whose objective function tries to match the pattern of bits that\n"; cout << "are 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 data file option needs a filename.\n"; exit(1); } else{ sprintf(datafile, argv[i]); continue; } } else if(strcmp("pfile", argv[i]) == 0){ if(++i >= argc){ cerr << argv[0] << ": the parameters file option needs a filename.\n"; exit(1); } else{ sprintf(parmfile, argv[i]); params.read(parmfile); 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 arguements include GAlib arguments plus:\n"; cerr << " dfile\tdata file from which to read (" << datafile << ")\n"; cerr << " pfile\tparameters file (" << parmfile << ")\n\n"; cerr << "default parameters are:\n" << params << "\n\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). // Here we use a binary string genome to store the desired pattern. This // shows how you can read in directly from a stream into a genome. (This can // be useful in a population initializer when you want to bias your population) ifstream inStream(datafile, ios :: in); if(!inStream){ cerr << "Cannot open " << datafile << " for input.\n"; exit(1); } int height, width; inStream >> height >> width; GA2DBinaryStringGenome target(width, height); inStream >> target; inStream.close(); // Print out the pattern to be sure we got the right one. cout << "input pattern:\n"; for(j=0; jgene(i,j)); return(value); }