www.pudn.com > SimulatedAnnealing.rar > SimulatedAnnealing.cpp


// SimulatedAnnealing.cpp : Defines the entry point for the console application. 
#include "stdafx.h" 
#include  
#include  
#include 
#include  
#include  
using namespace std; 
 
struct City{ 
	string name; 
	double x; 
	double y; 
}; 
 
int city=0;                            //城市个数 
City* cities=NULL;				//存储城市信息 
double temperature= 0.0;   //当前温度 
int* solution_before = NULL;//前一个温度时的解 
int* solution_now    = NULL;//现在温度求得的解 
 
//fstream out("result10.txt");      //保存结果 
 
void readFile(){ 
	char fileName[20]; 
	cout<<"请输入文件名"; 
	cin>>fileName; 
	cout<<"需要停止时按CTRL+C,现在开始搜索………"<>city; 
	cities = new City[city]; 
	for(int i=0; i> cities[i].name; 
		inFile >> cities[i].x; 
		inFile >> cities[i].y; 
	} 
	inFile.close(); 
}    
 
void Start(){                                                                   //设置初始状态 
	temperature = 280; 
	if(solution_now!=NULL) 	delete []  solution_now; 
	solution_now = new int[city]; 
	for(int i=0; ij){ 
			int temp = i; 
			i = j; 
			j = temp; 
		} 
		break; 
	} 
 
	for(int k = 0; k(double)rand()/RAND_MAX){ 
						delete [] solution_now; 
						solution_now = solution_new; 
						change = true; 
					} 
				} 
				if(!change) 
					delete [] solution_new; 
			} 
			if( fabs(Length(solution_before) -Length(solution_now)) < 0.000001){ 
				Print(); 
				break; 
			} 
			temperature= 0.92 * temperature; 
		} 
	} 
} 
 
int _tmain(int argc, _TCHAR* argv[]) 
{ 
	readFile(); 
	srand((unsigned int)time(NULL)); 
	Simulate(); 
	return 0; 
}