定义了Point类忘了用了结果代码变成了粪山😭
其中涉及贪婪算法,轮盘赌法,以及看不懂自己写的什么了
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <time.h>
#include<fstream>
#include<sstream>
using namespace std;
//created by jzyghsh
#define MAXPATH 9999999
struct Point { //city class
string name;
double x, y;
int num;
};
void init();
double distance(Point a, Point b);
double greedyPath(vector<Point> v);
bool isIn(string str, vector<string> list);
void nextCity();
int findNumOfCity(string name);
string RWS(vector<string> visitList);
void update();
int n;//city number
int m;//ant number
int alpha = 1, beta = 2;
double rou = 0.5;
vector<Point> cities;//city group (unchaged)
vector<vector<double>> dist;//distance list (unchaged)
vector<vector<double>> message;//message level on every road
vector<vector<string>> visitedCities;//abandon list
vector<double> p;
vector<double> C;
double minPath = FLT_MAX;
vector<string> bestPath;
int nextCityNum;
int main() {
#ifndef ONLINE_JUDGE
freopen("E:\\txts\\wi29.txt", "r", stdin);
#endif
int times = 0;
int i;
double difference = 0.0;
vector<int> count;
init();
while (times<=100) {
nextCity();
update();
times++;
}
nextCity();
cout << "共生成" << times * m << "只蚂蚁" << endl;
cout << "最终路径为:";
for (i = 0; i < bestPath.size(); i++) {
cout << bestPath[i] << "->";
}
cout << "最终路径长度为:" << minPath << endl;
return 0;
}
void nextCity() {
int i, j, k, begin, first;
string nextVisit;
int notVisit,nowVisit;
double up, pi, totalUp = 0, ci = 0;
C.clear();
for (i = 0; i < m; i++) {
notVisit = n - visitedCities[i].size();
ci = 0;
C.push_back(ci);
vector<string> visitList;
first = 0;
while (notVisit > 0) {
visitList.clear();//available visit list
totalUp = 0;
p.clear();
for (j = 0; j < n; j++) {
if (visitedCities[i].back() == cities[j].name) {
nowVisit = j;
if (first == 0) {
begin = j;
first = 1;
}
break;
}
}
for (j = 0; j < n; j++) {
if (!isIn(cities[j].name, visitedCities[i])) {
//cout << cities[j].name << "not in" << endl;
up = pow(message[nowVisit][j], alpha) * pow((1 / dist[nowVisit][j]), beta);//i有问题
totalUp += up;
p.push_back(up);
visitList.push_back(cities[j].name);
//cout << visitList.back() << " ";
}
}
//cout << endl;
//created by jzyghsh
//cout << p.size() << endl;
for (j = 0; j < p.size(); j++) {
p[j] /= totalUp;
}
nextVisit = RWS(visitList);
visitedCities[i].push_back(nextVisit);
//cout << "add" << visitList[nextVisit] << endl;
for (k = 0; k < n; k++) {
if (nextVisit == cities[k].name) {
nextCityNum = k;
}
}
C[i] += dist[nowVisit][nextCityNum];
//cout << cities[nowVisit].name << " " << cities[nextCityNum].name << endl;
notVisit--;
}
//cout << endl;
C[i] += dist[nextCityNum][begin];
//cout << cities[nextCityNum].name << " " << cities[begin].name << endl;
}
for (i = 0; i < m; i++) {
cout << "蚂蚁" << i << "爬过的路径为:";
visitedCities[i].push_back(visitedCities[i][0]);
for (j = 0; j < visitedCities[i].size(); j++) {
cout << visitedCities[i][j] << "->";
}
cout << "路径长度为:" << C[i] << endl;
if (C[i] < minPath) {
minPath = C[i];
bestPath = visitedCities[i];
}
}
}
string RWS(vector<string> visitList) {//select a city
double q, area = 0, temp;
string t;
int i, j;
q = rand()/double(RAND_MAX);//0~1 double number
//cout << "q=" << q;
for (i = 0; i < visitList.size()-1; i++) {
for (j = i; j < visitList.size() - 1; j++) {
if (p[i] > p[i + 1]) {
temp = p[i];
p[i] = p[i + 1];
p[i + 1] = temp;
t = visitList[i];
visitList[i] = visitList[i + 1];
visitList[i + 1] = t;
}
}
}
for (i = 0; i < p.size(); i++) {
area += p[i];
if (q < area) {
//cout << "choose" << i << endl;
nextCityNum = i;
return visitList[i];
}
}
return visitList[0];
}
void update() {
int i, j;
int cityA = 0, cityB = 0;
for (i = 0; i < n; i++) {// message fade
for (j = 0; j < n; j++) {
message[i][j] *= (1 - rou);
}
}
for (i = 0; i < m; i++) {//message adding
for (j = 0; j < visitedCities[i].size()-1; j++) {
cityA = findNumOfCity(visitedCities[i][j]);
cityB = findNumOfCity(visitedCities[i][j + 1]);
message[cityA][cityB] += 1 / C[i];
//cout << cities[cityA].name << "to" << cities[cityB].name << endl;
}
//cout << endl;
}
for (i = 0; i < m; i++) {//randomly put ants again
int ran;
visitedCities[i].clear();
ran = rand() % n;
visitedCities[i].push_back(cities[ran].name);
cout << "蚂蚁" << i << "初始在城市" << visitedCities[i].back() << endl;
}
}
int findNumOfCity(string name) {
int i;
for (i = 0; i < cities.size(); i++) {
if (name == cities[i].name) {
return i;
}
}
return NULL;
}
void init() {
int i = 0, j = 0;
double Cnn,t0;
cin >> n >> m;
for (i = 0; i < n; i++) {
Point p;
cin >> p.name >> p.x >> p.y;
p.num = i;
cities.push_back(p);
}
for (i = 0; i < n; i++) {//init distance list
vector<double> v;
dist.push_back(v);
for (j = 0; j < n; j++) {
dist[i].push_back(distance(cities[i], cities[j]));
}
}
Cnn = greedyPath(cities);
t0 = m / Cnn;
cout << "用贪婪算法得到的初始路径长度为:" << Cnn << endl;
for (i = 0; i < n; i++) {//init message list
vector<double> v;
message.push_back(v);
for (j = 0; j < n; j++) {
if (i != j) {
message[i].push_back(t0);
}
else {
message[i].push_back(0);
}
}
}
for (i = 0; i < m; i++) {//randomly put ants
int ran;
vector<string> str;
ran = rand() % n;
str.push_back(cities[ran].name);
visitedCities.push_back(str);
str.clear();
cout << "蚂蚁" << i << "初始在城市" << visitedCities[i].back() << endl;
}
}
double distance(Point a,Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
bool isIn(string str,vector<string> list) {
int i = 0, length = list.size();
for (i = 0; i < length; i++) {
if (str == list[i]) {
return true;
}
}
return false;
}
double greedyPath(vector<Point> v) {//greedy method get Cnn
double res = 0, nowPath, minPath;
int count = 0, begin, end;
int i = 0, j = 0;
vector<string> checked;
string choseCity;
int choseCityNum;
srand((unsigned)time(NULL));
begin = rand() % n;
end = begin;
checked.push_back(v[begin].name);
choseCityNum = begin;
while (count < v.size()-1) {
minPath = MAXPATH;
for (i = 0; i < n; i++) {
if (i != begin) {
nowPath = distance(v[begin], v[i]);
if (nowPath < minPath && !isIn(v[i].name,checked)) {
minPath = nowPath;
choseCity = v[i].name;
choseCityNum = i;
}
}
}
checked.push_back(choseCity);
begin = choseCityNum;
res += minPath;
count++;
}
checked.push_back(checked[0]);
cout << "贪婪算法路径为:";
for (i = 0; i < checked.size(); i++) {
cout << checked[i] << "->";
}
cout << endl;
res += dist[begin][end];
//cout << begin << " " << end;
return res;
}