现在的位置: 首页 > 综合 > 正文

从TSPLib中提取出邻接矩阵

2013年09月17日 ⁄ 综合 ⁄ 共 3867字 ⁄ 字号 评论关闭

#include<cmath>
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

class City
{
	
private:
	string filename;
	int dimension;
	string comment;
	void cityGenerate(char* s);

public:
	City(char* s){	
		cityGenerate(s);
	}

	double **city;
	enum dt{COORD_DISPLAY,NODISPLAY,TWOD_DISPLAY}displayType;

	enum ewt{EUC_2D,GEO,EXPLICIT,CEIL_2D,others};
	enum ewf{LOWER_DIAG_ROW,UPPER_ROW,FULL_MATRIX,FUNCTION};
	enum ewt edgeWeightType;
	enum ewf edgeWeightFormat;
//	~City(){}
	string getFilename()
	{return filename;}
	void setFilename(string name)
	{filename=name;}

	int getDimension()
	{return dimension;}
	void setDimension(int d)
	{dimension=d;}

	enum ewt getewt()
	{return edgeWeightType;}
	void setewt(enum ewt ewtype)
	{edgeWeightType=ewtype;}

	enum ewf getewf()
	{return edgeWeightFormat;}
	void setewf(ewf ewformat)
	{edgeWeightFormat=ewformat;}

	string getComment()
	{return comment;}
	void setComment(string com)
	{comment=com;}

	void setDisplaytype(dt distype)
	{displayType=distype;}
	enum dt getDisplaytype()
	{return displayType;}
	template <class T>  //T为点坐标的数据结构,可以为int或double
		inline double distance(T x1,T y1,T x2,T y2)
	{
		T tempx=x1-x2,tempy=y1-y2;//cout<<endl<<"tempx"<<tempx<<endl;cout<<endl<<"tempy"<<tempy<<endl;
		return sqrt(tempx*tempx+tempy*tempy);
	}
	void showCityMatrix();
	void showUpperCityMatrix();
	void showLowerCityMatrix();

//	bool meetCriterion;
};
void City::showCityMatrix()
{
	cout<<"city matrix:"<<endl;
	for(int i=0;i<dimension;i++)
	{
		for(int j=0;j<dimension;j++)
			cout<<city[i][j]<<'\t';
		cout<<endl;
	}

}
void City::showUpperCityMatrix()
{
	cout<<"city upper matrix:"<<endl;
	for(int i=0;i<dimension;i++)
	{
		for(int j=i+1;j<dimension;j++)
			cout<<city[i][j]<<'\t';
		cout<<endl;
	}

}
void City::showLowerCityMatrix()
{
	cout<<"city Lower matrix:"<<endl;
	for(int i=0;i<dimension;i++)
	{
		for(int j=0;j<=i;j++)
			cout<<city[i][j]<<'\t';
	}

}

//void City::cityGenerate(char* s,City city);




void main()
{
	
	City c("bays29.tsp");
	return;
}


void City:: cityGenerate(char* s)
{
	cout<<"TSP路径文件名:"<<s<<endl;
	cout<<"--------------------------------------------"<<endl;
	ifstream in(s);
	for(string line;getline(in,line);)
	{
		string content;		
		int d=0;
		for(istringstream sin(line);sin>>content;)
		{
			if(content=="NAME:")
			{
				sin>>content;
				setFilename(content);
			}
			if(content=="COMMENT:")
			{
				string comment;
				while(sin>>content)
				{
					comment.append (content);
					comment.append(" ");
				}
				setComment(comment);
//				cout<<endl<<"评论:"<<city.getComment()<<endl;
			}
			if(content=="DIMENSION:")
			{
				sin>>d;
				setDimension(d);
			//城市矩阵初始化
				city=new double*[d];
				for(int i=0;i<d;i++)
				{
					city[i]=new double[d];
				}
			}
			if(content=="EDGE_WEIGHT_TYPE:")
			{
				sin>>content;
				if(content=="EUC_2D")
					setewt(EUC_2D);
				else
					if(content=="GEO")
						setewt(GEO);
					else
						if(content=="EXPLICIT")
							setewt(EXPLICIT);
						else
							if(content=="CEIL_2D")
								setewt( CEIL_2D);
							else
								setewt(others);
			}
			if(content=="EDGE_WEIGHT_FORMAT:")
			{
				sin>>content;
				if(content=="LOWER_DIAG_ROW")
					setewf(LOWER_DIAG_ROW);
				else
					if(content=="UPPER_ROW")
						setewf(UPPER_ROW);
					else
						if(content=="FUNCTION")
							setewf(FUNCTION);		
							else
							if(content=="FULL_MATRIX")
								setewf(City:: FULL_MATRIX);
			}			
			if(content=="DISPLAY_DATA_TYPE:")
			{
				sin>>content;
				if(content=="COORD_DISPLAY")
					setDisplaytype(COORD_DISPLAY);
				else
					if(content=="TWOD_DISPLAY")
						setDisplaytype(TWOD_DISPLAY);
					else
						setDisplaytype(NODISPLAY);		
			}

			//城市矩阵建立1
			d=getDimension();
			if(content=="NODE_COORD_SECTION")
			{				
				double* x=new double[d],*y=new double[d];

				for(int i=0;i<d;i++)
				{
					getline(in,line);
					istringstream inn(line);
					inn>>x[i]>>x[i];
					inn>>y[i];

				}
				for(i=0;i<d;i++)
				{
					for(int j=i;j<d;j++)
					{
						city[i][j]=distance(x[i],y[i],x[j],y[j]);
						city[j][i]=city[i][j];
					}
				}/**/
			}//城市矩阵建立1
		
			else if(content=="EDGE_WEIGHT_SECTION")
			{
				if(getewf()==UPPER_ROW)
				{
					
					for(int i=0;i<d;i++)
					{
						getline(in,line);
						istringstream inn(line);
						city[i][i]=0;
						for(int j=i+1;j<d;j++)
						{
							double b;
							inn>>b;
							city[i][j]=b;
							city[j][i]=city[i][j];
							
						}
					}//for
				}

				if(getewf()==LOWER_DIAG_ROW)
				{						
					int i=0,j=0;
					while(getline(in,line))
					{
						for(istringstream inn(line);inn>>city[i][j];)
						{
							city[j][i]=city[i][j];
							j++;
							if(j>i)
							{
								i++;
								j=0;
							}
						}

					}
				}
				if(getewf()==FULL_MATRIX)
				{						
					for(int i=0;i<d;i++)
					{	
						getline(in,line);
						istringstream inn(line);
						for(int j=0;j<d;j++)
						{
							double b;
							inn>>b;
							city[i][j]=b;					
						}
					}//for
				}
			}
		}//istringstream流分析
	}//每行内容分析
	in.close();
	return;
}/**/

抱歉!评论已关闭.