




已閱讀5頁(yè),還剩8頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
/#pragma warning (disable: 4786)/#pragma comment (linker, /STACK:16777216)/HEAD#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const double MAX_VAL = (double)1e18;const int MAX_GEN = 30;/最大迭代次數(shù)const int MAX_SCALE = 3000;/最大種群規(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 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_SCALEMAX_CITY;/粒子群 double fitnessMAX_SCALE;/種群適應(yīng)度,表示種群中各個(gè)個(gè)體的適應(yīng)度 vector 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 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 = 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 scale; 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 * 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 fitnessi) vPgd = fitnessi; bestNum = i; copyArray(Pgd, oPopbestNum, 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; for (int i = 0; i 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 v)/ int vn = v.size(); for (int i = 0; i vn; i+) int x = vi.x, y = vi.y; swap(ax, ay); vector 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 v; SO s; for (int i = 0; i cityNum; i+) if (di != ai) s.x = i, s.y = cai; swap(ds.x, ds.y); v.push_back(s); return v;void addTo(vector &v, vector 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+) if (is = bestNum) continue; vector v; v.clear(); int lvn = w * listVis.size(); addTo(v, listVis, lvn); vector a = minus(Pdis, oPopis); int an = randomD() * a.size(); addTo(v, a, an); vector b = minus(Pgd, oPopis); int bn = randomD() * b.size(); 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( ); printf(%dn, Pgdi); cout bestGen bestNum endl; puts(); ;int cn;vector pv;double dMAX_CITYMAX_CITY;void pre(vector pv) int cn = pv.size(); for (int i = 0; i cn; i+) for (int j = i + 1; j 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 * 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
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2026學(xué)年安徽省明光市明光鎮(zhèn)映山中學(xué)三上數(shù)學(xué)期末聯(lián)考模擬試題含解析
- 2024年隴川縣三年級(jí)數(shù)學(xué)第一學(xué)期期末統(tǒng)考模擬試題含解析
- 2024年吉林省白城市鎮(zhèn)賚縣數(shù)學(xué)三年級(jí)第一學(xué)期期末考試試題含解析
- 公共空間設(shè)計(jì)思維與設(shè)計(jì)方法課件
- 2025年自考行政管理的知識(shí)框架及試題答案
- 2025年執(zhí)業(yè)醫(yī)師考試公共衛(wèi)生試題及答案
- 藥效學(xué)與藥代動(dòng)力學(xué)概述試題及答案
- 2025年執(zhí)業(yè)護(hù)士考試應(yīng)試技巧試題與答案
- 2025年執(zhí)業(yè)醫(yī)師考試模擬題分享試題及答案
- 用藥評(píng)估與病治結(jié)果相關(guān)性試題及答案
- 石油化工壓力管道安裝工藝及質(zhì)量控制重點(diǎn)
- 2025年上海市崇明區(qū)中考英語(yǔ)一模試卷
- 2024審計(jì)常用法規(guī)定性表述及適用
- 質(zhì)量管理科提高醫(yī)療質(zhì)量安全不良事件報(bào)告率PDCA
- 2025-2030年中國(guó)電力勘察設(shè)計(jì)行業(yè)現(xiàn)狀分析規(guī)劃研究報(bào)告新版
- DB31-T 1385-2022 科技成果分類(lèi)評(píng)價(jià)和價(jià)值潛力評(píng)價(jià)規(guī)范
- 骨科股骨頸骨折“一病一品”成果匯報(bào)
- 《大禹治水的故事》課本劇劇本:感受英雄力量領(lǐng)略堅(jiān)韌精神(6篇)
- 2018年高考英語(yǔ)全國(guó)一卷(精校+答案+聽(tīng)力原文)
- 管道直飲水項(xiàng)目可行性研究報(bào)告
- 工程決算書(shū)(結(jié)算書(shū))模板
評(píng)論
0/150
提交評(píng)論