📄 gabasega.c
字号:
const GAParameterList&GAGeneticAlgorithm::parameters(int& argc, char **argv, GABoolean flag){ params.parse(argc, argv, flag); // get the args we understand for(int i=0; i<params.size(); i++) setptr(params[i].fullname(), params[i].value()); return params;}#ifdef GALIB_USE_STREAMSconst GAParameterList&GAGeneticAlgorithm::parameters(const char* filename, GABoolean flag){ params.read(filename, flag); for(int i=0; i<params.size(); i++) setptr(params[i].fullname(), params[i].value()); return params;}const GAParameterList&GAGeneticAlgorithm::parameters(STD_ISTREAM& is, GABoolean flag){ params.read(is, flag); for(int i=0; i<params.size(); i++) setptr(params[i].fullname(), params[i].value()); return params;}#endif// Return 0 if everything is OK, non-zero if error. If we did not set anything// then we return non-zero (this is not an error, but we indicate that we did// not do anything).// The set method must set both the GA's parameter and the value in the// parameter list (kind of stupid to maintain two copies of the same data, but// oh well). The call to set on params is redundant for the times when this// method is called *after* the parameter list has been updated, but it is// necessary when this method is called directly by the user.intGAGeneticAlgorithm::setptr(const char* name, const void* value){ int status=1; params.set(name, value); // redundant for some cases, but not others if(strcmp(name, gaNnBestGenomes) == 0 || strcmp(name, gaSNnBestGenomes) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif nBestGenomes(*((int*)value)); status = 0; } else if(strcmp(name, gaNpopulationSize) == 0 || strcmp(name, gaSNpopulationSize) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif populationSize(*((int*)value)); status = 0; } else if(strcmp(name, gaNminimaxi) == 0 || strcmp(name, gaSNminimaxi) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif minimaxi(*((int*)value)); status = 0; } else if(strcmp(name, gaNnGenerations) == 0 || strcmp(name, gaSNnGenerations) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif nGenerations(*((int*)value)); status = 0; } else if(strcmp(name, gaNpConvergence) == 0 || strcmp(name, gaSNpConvergence) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((float*)value) << "'\n";#endif pConvergence(*((float*)value)); status = 0; } else if(strcmp(name, gaNnConvergence) == 0 || strcmp(name, gaSNnConvergence) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif nConvergence(*((int*)value)); status = 0; } else if(strcmp(name, gaNpCrossover) == 0 || strcmp(name, gaSNpCrossover) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((float*)value) << "'\n";#endif pCrossover(*((float*)value)); status = 0; } else if(strcmp(name, gaNpMutation) == 0 || strcmp(name, gaSNpMutation) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((float*)value) << "'\n";#endif pMutation(*((float*)value)); status = 0; } else if(strcmp(name,gaNscoreFrequency) == 0 || strcmp(name,gaSNscoreFrequency) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif stats.scoreFrequency(*((int*)value)); status = 0; } else if(strcmp(name,gaNflushFrequency) == 0 || strcmp(name,gaSNflushFrequency) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif stats.flushFrequency(*((int*)value)); status = 0; } else if(strcmp(name,gaNrecordDiversity) == 0 || strcmp(name,gaSNrecordDiversity) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif stats.recordDiversity(*((int*)value) ? gaTrue : gaFalse); status = 0; } else if(strcmp(name,gaNselectScores) == 0 || strcmp(name,gaSNselectScores)==0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << *((int*)value) << "'\n";#endif stats.selectScores(*((int*)value)); status = 0; } else if(strcmp(name,gaNscoreFilename) == 0 || strcmp(name,gaSNscoreFilename) == 0){#ifdef GA_DEBUG cerr << "GAGeneticAlgorithm::setptr\n setting '" << name << "' to '" << ((char*)value) << "'\n";#endif char tmpname[64]; memcpy(tmpname, value, strlen((char*)value)+1); stats.scoreFilename(tmpname); status = 0; } return status;}// This is a pretty ugly little hack to make doubles/floats work transparently.intGAGeneticAlgorithm::set(const char* name, double v) { int status=1; for(int i=0; i<params.size(); i++){ if(strcmp(name, params[i].fullname()) == 0 || strcmp(name, params[i].shrtname()) == 0){ if(params[i].type() == GAParameter::FLOAT){ float fval = (float)v; status = setptr(name, (void*)&fval); } else status = setptr(name, (void*)&v); } } return status;}intGAGeneticAlgorithm::get(const char* name, void* value) const { int status=1; if(strcmp(name, gaNseed) == 0 || strcmp(name, gaSNseed) == 0){ *((int*)value) = d_seed; status = 0; } else if(strcmp(name, gaNnBestGenomes) == 0 || strcmp(name, gaSNnBestGenomes) == 0){ *((int*)value) = stats.nBestGenomes(); status = 0; } else if(strcmp(name, gaNpopulationSize) == 0 || strcmp(name, gaSNpopulationSize) == 0){ *((int*)value) = pop->size(); status = 0; } else if(strcmp(name, gaNminimaxi) == 0 || strcmp(name, gaSNminimaxi) == 0){ *((int*)value) = minmax; status = 0; } else if(strcmp(name, gaNnGenerations) == 0 || strcmp(name, gaSNnGenerations) == 0){ *((int*)value) = ngen; status = 0; } else if(strcmp(name, gaNpConvergence) == 0 || strcmp(name, gaSNpConvergence) == 0){ *((float*)value) = pconv; status = 0; } else if(strcmp(name, gaNnConvergence) == 0 || strcmp(name, gaSNnConvergence) == 0){ *((int*)value) = nconv; status = 0; } else if(strcmp(name, gaNpCrossover) == 0 || strcmp(name, gaSNpCrossover) == 0){ *((float*)value) = pcross; status = 0; } else if(strcmp(name, gaNpMutation) == 0 || strcmp(name, gaSNpMutation) == 0){ *((float*)value) = pmut; status = 0; } else if(strcmp(name,gaNscoreFrequency) == 0 || strcmp(name,gaSNscoreFrequency) == 0){ *((int*)value) = stats.scoreFrequency(); status = 0; } else if(strcmp(name,gaNflushFrequency) == 0 || strcmp(name,gaSNflushFrequency) == 0){ *((int*)value) = stats.flushFrequency(); status = 0; } else if(strcmp(name,gaNrecordDiversity) == 0 || strcmp(name,gaSNrecordDiversity) == 0){ *((int*)value) = stats.recordDiversity(); status = 0; } else if(strcmp(name,gaNselectScores) == 0 || strcmp(name,gaSNselectScores)==0){ *((int*)value) = stats.selectScores(); status = 0; } else if(strcmp(name,gaNscoreFilename) == 0 || strcmp(name,gaSNscoreFilename) == 0){ *((const char**)value) = stats.scoreFilename(); status = 0; } return status;}void GAGeneticAlgorithm::objectiveFunction(GAGenome::Evaluator f){ for(int i=0; i<pop->size(); i++) pop->individual(i).evaluator(f);}voidGAGeneticAlgorithm::objectiveData(const GAEvalData& v){ for(int i=0; i<pop->size(); i++) pop->individual(i).evalData(v);}const GAPopulation&GAGeneticAlgorithm::population(const GAPopulation& p) { if(p.size() < 1) { GAErr(GA_LOC, className(), "population", gaErrNoIndividuals); return *pop; } pop->copy(p); pop->geneticAlgorithm(*this); return *pop;}intGAGeneticAlgorithm::populationSize(unsigned int value){ unsigned int ps = value; params.set(gaNpopulationSize, value); return pop->size(ps);}int GAGeneticAlgorithm::minimaxi(int m) { if(m == MINIMIZE) pop->order(GAPopulation::LOW_IS_BEST); else pop->order(GAPopulation::HIGH_IS_BEST); params.set(gaNminimaxi, m); minmax = (m == MINIMIZE ? MINIMIZE : MAXIMIZE); return minmax;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -