/* ---------------------------------------------------------------------------- ex18.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. The type of GA can be specified at the command line. ---------------------------------------------------------------------------- */ #include #include #include #include #include float objective(GAGenome &); int cntr=0; int main(int argc, char *argv[]) { cout << "Example 18\n\n"; cout << "This program is designed to compare the GA types. You can\n"; cout << "specify steady-state, incremental, or simple GA and tweak any\n"; cout << "of the parameters for each of these GA types. The objective\n"; cout << "function tries to match a pattern read in from a file.\n\n"; cout.flush(); // 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. unsigned int seed = 0; for(int ii=1; ii= argc){ cerr << argv[0] << ": which GA do you want? (simple, ss, or inc)\n"; exit(1); } else{ if(strcmp(argv[i],"simple") == 0) whichGA = SIMPLE; else if(strcmp(argv[i],"ss") == 0) whichGA = STEADY_STATE; else if(strcmp(argv[i],"inc") == 0) whichGA = INCREMENTAL; else cerr << argv[0] << ": ga must be one of: simple, ss, or inc.\n"; continue; } } else if(strcmp("file", argv[i]) == 0 || strcmp("f", argv[i]) == 0){ if(++i >= 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 arguements include standard GAlib arguments plus:\n"; cerr << " f\tfilename from which to read (" << filename << ")\n"; cerr << "\n"; cerr << " ga simple|ss|inc\twhich GA to use (simple)\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