




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、/#pragma warning (disable: 4786)/#pragma comment (linker, "/STACK:16777216")/HEAD#include <cstdio>#include <ctime>#include <cstdlib>#include <cstring>#include <queue>#include <string>#include <set>#include <stack>#include <map>#include
2、<cmath>#include <vector>#include <iostream>#include <algorithm>#include <time.h>#include <cstdlib>using namespace std;typedef long long LL;const double MAX_VAL = (double)1e18;const int MAX_GEN = 30;/最大迭代次数const int MAX_SCALE = 3000;/最大种群规模const int MAX_CITY = 20 +
3、 2;/最大城市数const double W_VAL = 0.729;/struct SO int x, y; SO() SO(int x, int y): x(x), y(y);struct Point double x, y; Point() Point(int x, int y):x(x), y(y); void read() scanf("%lf%lf", &x, &y); ;inline int randomI(int x) return rand()%x; inline double randomD() return (double)rand(
4、)/RAND_MAX; inline double getDist(Point a, Point b) return sqrt(a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);struct PSO double w; int scale; int cityNum; int nowGen;/当前代数 int maxGen;/迭代次数 int bestNum; int bestGen;/最佳出现代数 double distMAX_CITYMAX_CITY; int oPopMAX_SCALEMAX_CITY;/粒子群 double fitn
5、essMAX_SCALE;/种群适应度,表示种群中各个个体的适应度 vector<SO> listVMAX_SCALE;/ 每科粒子的初始交换序列 int PdMAX_SCALEMAX_CITY;/一颗粒子历代中出现最好的解, double vPdMAX_SCALE;/解的评价值 int PgdMAX_CITY;/ 整个粒子群经历过的的最好的解,每个粒子都能记住自己搜索到的最好解 double vPgd;/ 最好的解的评价值 PSO() PSO(int s, int c, int mG, double ww, double dMAX_CITYMAX_CITY) scale = s;
6、 cityNum = c; maxGen = mG; w = ww; for (int i = 0; i < cityNum; i+) for (int j = 0; j < cityNum; j+) distij = dij; void copyArray(double a, double b, int n) for (int i = 0; i < n; i+) ai = bi; void copyArray(int a, int b, int n) for (int i = 0; i < n; i+) ai = bi; void init() nowGen = 0;
7、 for (int i = 0; i < scale; i+) for (int j = 0; j < cityNum; ) int x = randomI(cityNum); int r; for (r = 0; r < j; r+) if (x = oPopir) break; if (r = j) oPopij = x;/ cout << oPopij << ' ' j+; / cout << endl; for (int i = 0; i < scale; i+) / cout << i <&
8、lt; " :" << endl; int vn = randomI(cityNum) + 1; for (int j = 0; j < vn; j+) int x = randomI(cityNum); int y = randomI(cityNum); while (x = y) y = randomI(cityNum); SO so(x, y); listVi.push_back(so);/ cout << so.x << "*" << so.y << ' ' / c
9、out <<endl; getFitness(); for (int i = 0; i < scale; i+) vPdi = fitnessi; copyArray(Pdi, oPopi, cityNum); bestNum = 0; vPgd = fitness0; bestGen = 0; for (int i = 0; i < scale; i+) if (vPgd > fitnessi) vPgd = fitnessi; bestNum = i; copyArray(Pgd, oPopbestNum, cityNum); double getVal(in
10、t x) double ret = 0; for (int i = 0; i < cityNum; i+) int xx = oPopxi % cityNum; int yy = oPopx(i + 1) % cityNum; ret += distxxyy; return ret; void getFitness() for (int i = 0; i < scale; i+) fitnessi = getVal(i); void UpdateVal() int j = 0; double vj = fitness0; for (int i = 0; i < scale;
11、i+) if (vPdi > fitnessi) vPdi = fitnessi; copyArray(Pdi, oPopi, cityNum);/? if (vj > fitnessi) vj = fitnessi; j = i; if (vj < vPgd) bestGen = nowGen;/ bestNum = j;/ vPgd = vj; copyArray(Pgd, oPopj, cityNum); void changeTo(int a, vector<SO> v)/ int vn = v.size(); for (int i = 0; i <
12、 vn; i+) int x = vi.x, y = vi.y; swap(ax, ay); vector<SO> minus(int a, int b)/ int cMAX_CITY, dMAX_CITY; for (int i = 0; i < cityNum; i+) di = bi; for (int i = 0; i < cityNum; i+) cai = i; vector<SO> v; SO s; for (int i = 0; i < cityNum; i+) if (di != ai) s.x = i, s.y = cai; swa
13、p(ds.x, ds.y); v.push_back(s); return v;void addTo(vector<SO> &v, vector<SO> a, int vn) for (int i = 0; i < vn; i+) v.push_back(ai); / Vii=wVi+ra(Pid-Xid)+rb(Pgd-Xid) void evolution() for (int ig = 0; ig < maxGen; ig+) nowGen = ig + 1;/nowGen for (int is = 0; is < scale; is+
14、) if (is = bestNum) continue; vector<SO> v; v.clear(); int lvn = w * listVis.size(); addTo(v, listVis, lvn); vector<SO> a = minus(Pdis, oPopis); int an = randomD() * a.size(); addTo(v, a, an); vector<SO> b = minus(Pgd, oPopis); int bn = randomD() * b.size(); addTo(v, b, bn); listVi
15、s = v; changeTo(oPopis, listVis);/ cout << listVis.size() << endl; getFitness(); UpdateVal(); void solve() init(); evolution(); printf("answer %lf:n", vPgd); printf("solution n"); for (int i = 0; i < cityNum; i+) if (i) printf(" "); printf("%dn"
16、, Pgdi); cout << bestGen << ' ' << bestNum << endl; puts(""); ;int cn;vector<Point> pv;double dMAX_CITYMAX_CITY;void pre(vector<Point> pv) int cn = pv.size(); for (int i = 0; i < cn; i+) for (int j = i + 1; j < cn; j+) dij = dji = getDist(p
17、vi, pvj); int main () srand(unsigned long)time(0); /设置时间种子 while (cin >> cn) pv.clear(); for (int i = 0; i < cn; i+) Point p; p.read(); pv.push_back(p); pre(pv); PSO solver(MAX_SCALE, cn, MAX_GEN, W_VAL, d); cout << "*" << endl; solver.solve(); cout << "*&qu
18、ot; << endl; return 0;/ cout << nowGen << " -:" << endl;/ for (int i = 0; i < scale; i+)/ / cout << vPdi << ' ' << fitnessi << endl;/ for (int j = 0; j < cityNum; j+)/ cout << Pdij << ' '/ cout << endl;/ / for (int i = 0; i < scale; i+)/ / for (int j = 0; j < listVi.size(); j+)/ cout << listVij.x << "*" << listVij.y << ' '/ cout << endl;/ / cout << vPgd << ' ' << bestGen << &
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程结算考试题及答案
- 家具设计标准与行业规范的理解试题及答案
- 会昌教招面试真题及答案
- 数量与质的对比理解题试题及答案
- 2025临床医学笔试题目及答案
- 植物上场测试题及答案
- 2025公务员考试试题及答案
- 2025飞行员面试试题及答案
- 区块链跨境支付系统稳定性与可靠性研究报告
- 教师教学改进方向的试题及答案
- 2025专利代理师笔试考试题库带答案
- 第3课《校园文化活动我参与》教案 海燕版综合实践活动 三年级下册
- 2025年保密教育线上培训考试试题及答案
- 大学生职业规划大赛《运动康复专业》生涯发展展示
- 高楼遮光补偿协议书范本
- 课题申报书:生成式人工智能赋能高职教学变革研究
- 2025-2030专用车产业规划及发展研究报告
- 《自由现金流折现法对东鹏特饮公司的财务估值实例分析》2000字
- 2024年四川绵阳科技城新区招聘社区工作者考试真题
- 2025-2030中国甘蔗收割机行业市场现状供需分析及投资评估规划分析研究报告
- 二零二五简短美发店劳动合同
评论
0/150
提交评论