📄 gabasesolverconfig.cpp
字号:
#include "GABaseSolver.h"
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
bool GABaseSolver::Configuration(const char *filename)
{
ifstream in(filename);
if(in.fail())
{
cout << "Can't open file " << filename << endl;
in.close();
return false;
}
char buffer[4096], *token, *token2;
char seps[] = " ,\t\n:";
while(!in.eof())
{
GETLINE(in, buffer);
token = strtok(buffer, seps);
if(token==NULL || token[0]=='`')
continue;
while(token!=NULL)
{
if(_stricmp(token, "Time")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for Time\n";
return false;
}
m_pTerm->m_MaxTime = 0;
while(token!=NULL)
{
if(strstr(token, "D")!=NULL || strstr(token, "d")!=NULL)
{
token[strlen(token)-1] = '\0';
m_pTerm->m_MaxTime += atoi(token) * 3600 * 24;
}
else if(strstr(token, "H")!=NULL || strstr(token, "h")!=NULL)
{
token[strlen(token)-1] = '\0';
m_pTerm->m_MaxTime += atoi(token) * 3600;
}
else if(strstr(token, "M")!=NULL || strstr(token, "m")!=NULL)
{
token[strlen(token)-1] = '\0';
m_pTerm->m_MaxTime += atoi(token) * 60;
}
else if(strstr(token, "S")!=NULL || strstr(token, "s")!=NULL)
{
token[strlen(token)-1] = '\0';
m_pTerm->m_MaxTime += atoi(token);
}
token = strtok(NULL, seps);
}
}
else if(_stricmp(token, "score")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for score\n";
return false;
}
m_pTerm->m_TolScore = atof(token);
}
else if(_stricmp(token, "Iteration")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for Iteration\n";
return false;
}
m_pTerm->m_Iter = atoi(token);
}
else if(_stricmp(token, "MutationRate")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for MutationRate\n";
return false;
}
double mr = atof(token);
if(mr<0.0)
{
cout << "MutationRate is modified to 0.0";
mr = 0.0;
}
else if(mr>1.0)
{
cout << "MutationRate is modified to 1.0";
mr = 1.0;
}
m_MutationRate = mr;
}
else if(_stricmp(token, "CrossoverRate")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for CrossoverRate\n";
return false;
}
double cr = atof(token);
if(cr<0.0)
{
cout << "Crossover Rate is modified to 0.0";
cr = 0.0;
}
else if(cr>1.0)
{
cout << "Crossover Rate is modified to 1.0";
cr = 1.0;
}
m_CrossoverRate = cr;
}
else if(_stricmp(token, "SelectionRate")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for SelectionRate\n";
return false;
}
double sr = atof(token);
if(sr<0.0)
{
cout << "Selection Rate is modified to 0.0";
sr = 0.0;
}
else if(sr>1.0)
{
cout << "Selection Rate is modified to 1.0";
sr = 1.0;
}
m_SelectionRate = sr;
}
else if(_stricmp(token, "NewSpringRate")==0)
{
token = strtok(NULL, seps);
if(token==NULL)
{
cout << "Wrong format for NewSpringRate\n";
return false;
}
double nsr = atof(token);
if(nsr<0.0)
{
cout << "New Spring Rate is modified to 0.0";
nsr = 0.0;
}
else if(nsr>1.0)
{
cout << "New Spring Rate is modified to 1.0";
nsr = 1.0;
}
m_NewSpringRate = nsr;
}
token = strtok(NULL, seps);
}
}
in.close();
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -