【人工智能实验】蚁群算法解决TSP问题

该代码实现了一个基于蚁群算法的最短路径搜索程序,涉及到贪婪算法和轮盘赌法。程序首先通过贪婪算法初始化路径,然后使用蚁群算法不断更新路径并记录最短路径。在每轮更新中,蚂蚁会根据信息素浓度和距离选择下一个城市,并更新信息素。最终输出最短路径及其长度。
摘要由CSDN通过智能技术生成

定义了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;
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值