⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 psosolverconfig.cpp

📁 遗传算法vc++语言版源程序,台湾大学编写。
💻 CPP
字号:
#include "PSOSolver.h"

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>

using namespace std;

bool PSOSolver::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;
	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, "Vc")==0)
			{
				token = strtok(NULL, seps);
				if(token==NULL)
				{
					cout << "Wrong format for Vc\n";
					return false;
				}
				double Vc = atof(token); 
				if(Vc<0.0)
				{
					cout << "Vc is modified to 0.0";
					Vc = 0.0;
				}
				else if(Vc>1.0)
				{
					cout << "Vc is modified to 1.0";
					Vc = 1.0;
				}
				m_Vc = Vc;
			}
			else if(_stricmp(token, "K1")==0)
			{
				token = strtok(NULL, seps);
				if(token==NULL)
				{
					cout << "Wrong format for K1\n";
					return false;
				}
				double K1 = atof(token); 
				if(K1<0.0)
				{
					cout << "K1 is modified to 0.0";
					K1 = 0.0;
				}
				else if(K1>1.0)
				{
					cout << "K1 is modified to 1.0";
					K1 = 1.0;
				}
				m_K1 = K1;
			}
			else if(_stricmp(token, "K2")==0)
			{
				token = strtok(NULL, seps);
				if(token==NULL)
				{
					cout << "Wrong format for K2\n";
					return false;
				}
				double K2 = atof(token); 
				if(K2<0.0)
				{
					cout << "K2 is modified to 0.0";
					K2 = 0.0;
				}
				else if(K2>1.0)
				{
					cout << "K2 is modified to 1.0";
					K2 = 1.0;
				}
				m_K2 = K2;
			}
		}
	}
	in.close();
	return true;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -