HANDLE WINAPI OpenW(const OpenInfo* info) { if (!info || info->StructSize < sizeof(OpenInfo)) return nullptr; //Determine file name to analyze wstring file_name; if (info->OpenFrom == OPEN_COMMANDLINE && info->Data) { const OpenCommandLineInfo* ocli = reinterpret_cast<const OpenCommandLineInfo*>(info->Data); if (!ocli || ocli->StructSize < sizeof(OpenCommandLineInfo) || !ocli->CommandLine || !ocli->CommandLine[0]) return nullptr; //Get command line wstring cmd_line = ocli->CommandLine; size_t pos = 0; while ((pos = cmd_line.find(L'\"', pos)) != string::npos) cmd_line.erase(pos, 1); while (!cmd_line.empty() && iswspace(cmd_line[0])) cmd_line.erase(0, 1); while (!cmd_line.empty() && iswspace(cmd_line[cmd_line.length() - 1])) cmd_line.erase(cmd_line.length() - 1, 1); if (cmd_line.empty()) return nullptr; //Expand environment variables in path string wstring exp_path(2048, 0); if (ExpandEnvironmentStrings(cmd_line.c_str(), &exp_path.front(), static_cast<DWORD>(exp_path.size() - 1))) exp_path.resize(lstrlen(exp_path.c_str())); else exp_path = cmd_line; const size_t path_len = _FSF.ConvertPath(CPM_FULL, exp_path.c_str(), nullptr, 0); if (path_len) { file_name.resize(path_len); _FSF.ConvertPath(CPM_FULL, exp_path.c_str(), &file_name[0], path_len); } } else if (info->OpenFrom == OPEN_PLUGINSMENU) { PanelInfo pi; ZeroMemory(&pi, sizeof(pi)); pi.StructSize = sizeof(pi); if (!_PSI.PanelControl(PANEL_ACTIVE, FCTL_GETPANELINFO, 0, &pi)) return nullptr; const intptr_t ppi_len = _PSI.PanelControl(PANEL_ACTIVE, FCTL_GETPANELITEM, static_cast<int>(pi.CurrentItem), nullptr); if (ppi_len == 0) return nullptr; vector<unsigned char> buffer(ppi_len); PluginPanelItem* ppi = reinterpret_cast<PluginPanelItem*>(&buffer.front()); FarGetPluginPanelItem fgppi; ZeroMemory(&fgppi, sizeof(fgppi)); fgppi.StructSize = sizeof(fgppi); fgppi.Size = buffer.size(); fgppi.Item = ppi; if (!_PSI.PanelControl(PANEL_ACTIVE, FCTL_GETPANELITEM, static_cast<int>(pi.CurrentItem), &fgppi)) return nullptr; const size_t file_name_len = _FSF.ConvertPath(CPM_FULL, ppi->FileName, nullptr, 0); if (file_name_len) { file_name.resize(file_name_len); _FSF.ConvertPath(CPM_FULL, ppi->FileName, &file_name[0], file_name_len); } } if (!file_name.empty()) { unlocker u; u.process(file_name.c_str()); } return nullptr; }
int main(int argc, char **argv) { std::chrono::time_point<std::chrono::system_clock> program_start, program_end; program_start = std::chrono::system_clock::now(); // MPI init - this has to happen before getopt() so the argv can be properly trimmed MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &mpi_tasks); MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); char hostname[1024]; hostname[1023] = '\0'; gethostname(hostname, 1023); srand(time(NULL)); double mutation_rate = 0.01; double crossover_rate = 0.01; int population_size = 10; int n_generations = 10; char c='h'; // Handle command line arguments while ((c = getopt (argc, argv, "t:g:p:c:m:s:h:e:x:")) != -1) switch (c) { case 'x': experiment_path = optarg; break; case 't': n_trials = atoi(optarg); break; case 'g': n_generations = atoi(optarg); break; case 'p': population_size = atoi(optarg); break; case 'm': mutation_rate = strtod(optarg, NULL); break; case 'c': crossover_rate = strtod(optarg, NULL); break; case 's': mutation_stdev = strtod(optarg, NULL); break; case 'e': elitism = atoi(optarg); break; case 'h': printf("Usage: %s -p {population size} -g {number of generations} -t {number of trials} -c {crossover rate} -m {mutation rate} -s {mutation standard deviation} -e {elitism 0 or 1}", argv[0]); break; case '?': if (optopt == 'p') fprintf (stderr, "Option -%c requires an argument specifying the population size.\n", optopt); else if (optopt == 'g') fprintf (stderr, "Option -%c requires an argument specifying the number of generations.\n", optopt); else if (optopt == 't') fprintf (stderr, "Option -%c requires an argument specifying the number of trials.\n", optopt); else if (optopt == 'c') fprintf (stderr, "Option -%c requires an argument specifying the crossover rate.\n", optopt); else if (optopt == 'm') fprintf (stderr, "Option -%c requires an argument specifying the mutation rate.\n", optopt); else if (optopt == 's') fprintf (stderr, "Option -%c requires an argument specifying the mutation standard deviation.\n", optopt); else if (isprint (optopt)) fprintf (stderr, "Unknown option `-%c'.\n", optopt); else fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt); return 1; default: printf("Usage: %s -p {population size} -g {number of generations} -t {number of trials} -c {crossover rate} -m {mutation rate} -s {mutation standard deviation}", argv[0]); abort (); } if (experiment_path.empty()) { printf("Usage: %s -p {population size} -g {number of generations} -t {number of trials} -c {crossover rate} -m {mutation rate} -s {mutation standard deviation} -x {argos experiment file}\n", argv[0]); exit(1); } float max_float = std::numeric_limits<float>::max(); //printf("%s:\tworker %d ready.\n", hostname, mpi_rank); // 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 = 12345; for(int i=1 ; i<argc ; i++) if(strcmp(argv[i++],"seed") == 0) seed = atoi(argv[i]); // popsize / mpi_tasks must be an integer population_size = mpi_tasks * int((double)population_size/(double)mpi_tasks+0.999); if (mpi_rank==0) { printf("Population size: %d\nNumber of trials: %d\nNumber of generations: %d\nCrossover rate: %f\nMutation rate: %f\nMutation stdev: %f\nAllocated MPI workers: %d\n", population_size, n_trials, n_generations, crossover_rate, mutation_rate, mutation_stdev, mpi_tasks); printf("elitism: %d\n", elitism); } // Define the genome GARealAlleleSetArray allele_array; allele_array.add(0, 1.0); // Probability of switching to search allele_array.add(0, 1.0); // Probability of returning to nest allele_array.add(0, 4*M_PI); // Uninformed search variation allele_array.add(0, 20); // Rate of informed search decay allele_array.add(0, 20); // Rate of site fidelity allele_array.add(0, 20); // Rate of laying pheremone allele_array.add(0, 20); // Rate of pheremone decay // Create the template genome using the phenotype map we just made. GARealGenome genome(allele_array, objective); genome.crossover(GARealUniformCrossover); genome.mutator(GARealGaussianMutatorStdev); // Specify our version of the Gaussuan mutator genome.initializer(CPFAInitializer); // Now create the GA using the genome and run it. GASimpleGA ga(genome); GALinearScaling scaling; ga.maximize(); // Maximize the objective ga.populationSize(population_size); ga.nGenerations(n_generations); ga.pMutation(mutation_rate); ga.pCrossover(crossover_rate); ga.scaling(scaling); if (elitism == 1) ga.elitist(gaTrue); else ga.elitist(gaFalse); if(mpi_rank == 0) ga.scoreFilename("evolution.txt"); else ga.scoreFilename("/dev/null"); ga.recordDiversity(gaTrue); ga.scoreFrequency(1); ga.flushFrequency(1); ga.selectScores(GAStatistics::AllScores); // Pass MPI data to the GA class ga.mpi_rank(mpi_rank); ga.mpi_tasks(mpi_tasks); //ga.evolve(seed); // Manual generations // initialize the ga since we are not using the evolve function ga.initialize(seed); // This is essential for the mpi workers to be sychronized // Name the results file with the current time and date time_t t = time(0); // get time now struct tm * now = localtime( & t ); stringstream ss; boost::filesystem::path exp_path(experiment_path); ss << "results/CPFA-evolution-" << exp_path.stem().string() << '-' <<GIT_BRANCH<<"-"<<GIT_COMMIT_HASH<<"-" << (now->tm_year) << '-' << (now->tm_mon + 1) << '-' << now->tm_mday << '-' << now->tm_hour << '-' << now->tm_min << '-' << now->tm_sec << ".csv"; string results_file_name = ss.str(); if (mpi_rank == 0) { // Write output file header ofstream results_output_stream; results_output_stream.open(results_file_name, ios::app); results_output_stream << "Population size: " << population_size << "\nNumber of trials: " << n_trials << "\nNumber of generations: "<< n_generations<<"\nCrossover rate: "<< crossover_rate<<"\nMutation rate: " << mutation_rate << "\nMutation stdev: "<< mutation_stdev << "Algorithm: CPFA\n" << "Number of searchers: 6\n" << "Number of targets: 256\n" << "Target distribution: power law" << endl; results_output_stream << "Generation" << ", " << "Compute Time (s)" << ", " << "Convergence" << ", " << "Mean" << ", " << "Maximum" << ", " << "Minimum" << ", " << "Standard Deviation" << ", " << "Diversity" << ", " << "ProbabilityOfSwitchingToSearching" << ", " << "ProbabilityOfReturningToNest" << ", " << "UninformedSearchVariation" << ", " << "RateOfInformedSearchDecay" << ", " << "RateOfSiteFidelity" << ", " << "RateOfLayingPheromone" << ", " << "RateOfPheromoneDecay"; results_output_stream << endl; results_output_stream.close(); } while(!ga.done()) { std::chrono::time_point<std::chrono::system_clock>generation_start, generation_end; if (mpi_rank == 0) { generation_start = std::chrono::system_clock::now(); } // Calculate the generation ga.step(); if(mpi_rank == 0) { generation_end = std::chrono::system_clock::now(); std::chrono::duration<double> generation_elapsed_seconds = generation_end-generation_start; ofstream results_output_stream; results_output_stream.open(results_file_name, ios::app); results_output_stream << ga.statistics().generation() << ", " << generation_elapsed_seconds.count() << ", " << ga.statistics().convergence() << ", " << ga.statistics().current(GAStatistics::Mean) << ", " << ga.statistics().current(GAStatistics::Maximum) << ", " << ga.statistics().current(GAStatistics::Minimum) << ", " << ga.statistics().current(GAStatistics::Deviation) << ", " << ga.statistics().current(GAStatistics::Diversity); for (int i = 0; i < GENOME_SIZE; i++) results_output_stream << ", " << dynamic_cast<const GARealGenome&>(ga.population().best()).gene(i); const GARealGenome& best_genome = dynamic_cast<const GARealGenome&>(ga.statistics().bestIndividual()); results_output_stream << endl; results_output_stream << "The GA found an optimum at: "; results_output_stream << best_genome.gene(0); for (int i = 1; i < GENOME_SIZE; i++) results_output_stream << ", " << best_genome.gene(i); results_output_stream << " with score: " << best_genome.score(); results_output_stream << endl; results_output_stream.close(); } } // Display the GA's progress if(mpi_rank == 0) { cout << ga.statistics() << " " << ga.parameters() << endl; } MPI_Finalize(); program_end = std::chrono::system_clock::now(); std::chrono::duration<double> program_elapsed_seconds = program_end-program_start; if(mpi_rank == 0) printf("Run time was %f seconds\n", program_elapsed_seconds.count()); return 0; }