




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
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)度,表示種群中各個(gè)個(gè)體的適應(yīng)度 vector<SO> listVMAX_SCALE;/ 每科粒子的初始交換序列 int PdMAX_SCALEMAX_CITY;/一顆粒子歷代中出現(xiàn)最好的解, double vPdMAX_SCALE;/解的評(píng)價(jià)值 int PgdMAX_CITY;/ 整個(gè)粒子群經(jīng)歷過(guò)的的最好的解,每個(gè)粒子都能記住自己搜索到的最好解 double vPgd;/ 最好的解的評(píng)價(jià)值 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è)置時(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 出租充氣皮艇合同范本
- 幾人共同購(gòu)房合同范本
- 電纜外貿(mào)合同范本
- 包裝合同范本8篇
- 公司合同范本梳理審核
- 倉(cāng)庫(kù)流轉(zhuǎn)合同范本
- 單位集資建房轉(zhuǎn)讓合同范本
- 勞防用品采購(gòu)合同范本
- 出售立軸制砂機(jī)合同范本
- 出售玻璃蓋板合同范本
- 《信息技術(shù)(拓展模塊)》高職全套教學(xué)課件
- 環(huán)保行業(yè)環(huán)保管理制度環(huán)保責(zé)任落實(shí)制度
- 2025年山東菏投建設(shè)集團(tuán)招聘筆試參考題庫(kù)含答案解析
- 市政質(zhì)量員繼續(xù)教育考試題庫(kù)集(含答案)
- 售后工程師述職報(bào)告
- 《公司法完整版》課件2024
- 2024年下半年信息系統(tǒng)項(xiàng)目管理師真題及答案
- ??低曤娏π袠I(yè)系統(tǒng)解決方案
- 2024-2030年中國(guó)街舞培訓(xùn)行業(yè)發(fā)展趨勢(shì)及競(jìng)爭(zhēng)格局分析報(bào)告
- 油田勘測(cè)設(shè)計(jì)行業(yè)發(fā)展概況及未來(lái)三年行業(yè)發(fā)展趨勢(shì)預(yù)測(cè)
- 期末練習(xí)卷(模擬試題)-2024-2025學(xué)年 一年級(jí)上冊(cè)數(shù)學(xué)人教版
評(píng)論
0/150
提交評(píng)論