




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、-作者xxxx-日期xxxx粒子群算法(C+版)【精品文檔】/#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>
2、;#include <map>#include <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;/最大迭代次數(shù)const int MAX_SCALE = 3000;/最
3、大種群規(guī)模const int MAX_CITY = 20 + 2;/最大城市數(shù)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
4、randomD() return (double)rand()/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;/當(dāng)前代數(shù) int maxGen;/迭代次數(shù) int bestNum; int bestGen;/最佳出現(xiàn)代數(shù) double distMAX_CITYMAX_CITY; int oPopMAX_
5、SCALEMAX_CITY;/粒子群 double fitnessMAX_SCALE;/種群適應(yīng)度,表示種群中各個個體的適應(yīng)度 vector<SO> listVMAX_SCALE;/ 每科粒子的初始交換序列 int PdMAX_SCALEMAX_CITY;/一顆粒子歷代中出現(xiàn)最好的解, double vPdMAX_SCALE;/解的評價值 int PgdMAX_CITY;/ 整個粒子群經(jīng)歷過的的最好的解,每個粒子都能記住自己搜索到的最好解 double vPgd;/ 最好的解的評價值 PSO() PSO(int s, int c, int mG, double ww, double
6、 dMAX_CITYMAX_CITY) scale = s; 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
7、 = bi; void init() nowGen = 0; 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 < scal
8、e; i+) / cout << i << " :" << 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 << "*" <<
9、 so.y << ' ' / cout <<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, oPopbestN
10、um, cityNum); double getVal(int 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;
11、for (int i = 0; i < scale; 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.
12、size(); for (int i = 0; i < 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
13、!= ai) s.x = i, s.y = cai; swap(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 (
14、int is = 0; is < scale; is+) 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.s
15、ize(); addTo(v, b, bn); listVis = 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(" &
16、quot;); printf("%dn", 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 <
17、 cn; j+) dij = dji = getDist(pvi, pvj); int main () srand(unsigned long)time(0); /設(shè)置時間種子 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.sol
18、ve(); cout << "*" << 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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公司轉(zhuǎn)讓股權(quán)合同
- 工地設(shè)備機械施工合同書
- 2025年寧波從業(yè)資格證應(yīng)用能力考些啥
- 《數(shù)據(jù)可視化技術(shù)應(yīng)用》2.3剖析用戶購買行為數(shù)據(jù)-教案
- 簡單版本的加工承攬合同6篇
- 工作室租房合同7篇
- 《愛心行動-圖形與拼組》作業(yè)設(shè)計方案
- 水力學(xué)模擬考試題與參考答案
- 電工崗位試題庫及參考答案
- 個人工作計劃周工作計劃
- 2025年第六屆(中小學(xué)組)國家版圖知識競賽測試題庫及答案
- GB/T 26436-2025禽白血病診斷技術(shù)
- 體育場館工程施工組織設(shè)計
- 春季校園常見傳染病及預(yù)防措施培訓(xùn)課件
- 國際標(biāo)準(zhǔn)下的AI技術(shù)應(yīng)用-深度研究
- 2025-2030年城市軌道交通運營行業(yè)深度調(diào)研及發(fā)展戰(zhàn)略咨詢報告
- 2025年江西生物科技職業(yè)學(xué)院高職單招職業(yè)技能測試近5年常考版參考題庫含答案解析
- 《信息技術(shù)(拓展模塊)》高職全套教學(xué)課件
- 2025天津市安全員《B證》考試題庫
- DB37T-住宅小區(qū)供配電設(shè)施建設(shè)標(biāo)準(zhǔn)編制說明
- DBJ61_T 179-2021 房屋建筑與市政基礎(chǔ)設(shè)施工程專業(yè)人員配備標(biāo)準(zhǔn)
評論
0/150
提交評論