蚁群算法源代码1精编版.docx
- 文档编号:9012908
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:17
- 大小:21.28KB
蚁群算法源代码1精编版.docx
《蚁群算法源代码1精编版.docx》由会员分享,可在线阅读,更多相关《蚁群算法源代码1精编版.docx(17页珍藏版)》请在冰豆网上搜索。
蚁群算法源代码1精编版
1.#include
2.#include
3.#include
4.using namespace std;
5.
6.//该程序是以蚁群系统为模型写的蚁群算法程序(强调:
非蚂蚁周模型),以三个著名的TSP问题为测试对象
7.//通过微调参数,都可以获得较好的解
8.
9./*
10.//----------
(1)问题一:
Oliver 30 城市 TSP 问题 best_length = 423.7406; ------------------------
11.//该程序最好的结果是423.741,可运行多次获得
12.//城市节点数目
13.#define N 30
14.//城市坐标
15.double C[N][2]={
16. {2,99},{4,50},{7,64},{13,40},{18,54},{18,40},{22,60},{24,42},{25,62},{25,38},
17. {37,84},{41,94},{41,26},{44,35},{45,21},{54,67},{54,62},{58,35},{58,69},{62,32},
18. {64,60},{68,58},{71,44},{71,71},{74,78},{82,7},{83,46},{83,69},{87,76},{91,38}
19.};
20.//----------上面参数是固定的,下面的参数是可变的-----------
21.//蚂蚁数量
22.#define M 30
23.//最大循环次数NcMax
24.int NcMax = 500;
25.//信息启发因子,期望启发式因子,全局信息素挥发参数,局部信息素挥发参数, 状态转移公式中的q0
26.double alpha = 2, beta = 3, rou = 0.1, alpha1 = 0.1, qzero = 0.01;
27.//-----------问题一结束------------------------------------------------------------------------
28.*/
29.
30./*
31.//----------
(2)问题二:
Elion50 城市 TSP 问题 best_length = 427.96; ----------------------------
32.//该程序最好的结果是428.468,可运行多次获得
33.//城市节点数目
34.#define N 50
35.//城市坐标
36.double C[N][2]={
37. {5,64}, {5,25}, {5,6}, {7,38}, {8,52}, {10,17},
38. {12,42}, {13,13}, {16,57}, {17,33}, {17,63},
39. {20,26}, {21,47}, {21,10}, {25,32}, {25,55},
40. {27,68}, {27,23}, {30,48}, {30,15}, {31,62},
41. {31,32}, {32,22}, {32,39}, {36,16}, {37,69},
42. {37,52}, {38,46}, {39,10}, {40,30}, {42,57},
43. {42,41}, {43,67}, {45,35}, {46,10}, {48,28},
44. {49,49}, {51,21}, {52,33}, {52,41}, {52,64},
45. {56,37}, {57,58}, {58,27}, {58,48}, {59,15},
46. {61,33}, {62,42}, {62,63}, {63,69}
47.};
48.//----------上面参数是固定的,下面的参数是可变的-----------
49.//蚂蚁数量
50.#define M 50
51.//最大循环次数NcMax
52.int NcMax = 1000;
53.//信息启发因子,期望启发式因子,全局信息素挥发参数,局部信息素挥发参数, 状态转移公式中的q0
54.double alpha = 2, beta = 4, rou = 0.1, alpha1 = 0.1, qzero = 0.01;
55.//-----------问题二结束------------------------------------------------------------------------
56.*/
57.
58.//----------(3)问题三:
Elion75 城市 TSP 问题 best_length = 542.31;
59.//该程序最好的结果是542.309,可运行多次获得
60.//城市节点数目
61.#define N 75
62.//城市坐标
63.double C[N][2]={
64.{6,25}, {7,43}, {9,56}, {10,70}, {11,28},
65.{12,17}, {12,38}, {15,5}, {15,14}, {15,56},
66.{16,19}, {17,64}, {20,30}, {21,48}, {21,45},
67.{21,36}, {22,53}, {22,22}, {26,29}, {26,13},
68.{26,59}, {27,24}, {29,39}, {30,50}, {30,20},
69.{30,60}, {31,76}, {33,34}, {33,44}, {35,51},
70.{35,16}, {35,60}, {36,6}, {36,26}, {38,33},
71.{40,37}, {40,66}, {40,60}, {40,20}, {41,46},
72.{43,26}, {44,13}, {45,42}, {45,35}, {47,66},
73.{48,21}, {50,30}, {50,40}, {50,50}, {50,70},
74.{50,4}, {50,15}, {51,42}, {52,26}, {54,38},
75.{54,10}, {55,34}, {55,45}, {55,50}, {55,65},
76.{55,57}, {55,20}, {57,72}, {59,5}, {60,15},
77.{62,57}, {62,48}, {62,35}, {62,24}, {64,4},
78.{65,27}, {66,14}, {66,8}, {67,41}, {70,64}
79.};
80.//----------上面参数是固定的,下面的参数是可变的-----------
81.//蚂蚁数量
82.#define M 75
83.//最大循环次数NcMax
84.int NcMax =1000;
85.//信息启发因子,期望启发式因子,全局信息素挥发参数,局部信息素挥发参数, 状态转移公式中的q0
86.double alpha = 2, beta = 5, rou = 0.1, alpha1 = 0.1, qzero = 0.1;
87.//-----------问题三结束------------------------------------------------------------------------
88.
89.
90.//===========================================================================================================
91.//局部更新时候使用的的常量,它是由最近邻方法得到的一个长度
92.//什么是最近邻方法?
:
)就是从源节点出发,每次选择一个距离最短的点来遍历所有的节点得到的路径
93.//每个节点都可能作为源节点来遍历
94.double Lnn;
95.//矩阵表示两两城市之间的距离
96.double allDistance[N][N];
97.
98.//计算两个城市之间的距离
99.double calculateDistance(int i, int j)
100.{
101. return sqrt(pow((C[i][0]-C[j][0]),2.0) + pow((C[i][1]-C[j][1]),2.0));
102.}
103.
104.//由矩阵表示两两城市之间的距离
105.void calculateAllDistance()
106.{
107. for(int i = 0; i < N; i++)
108. {
109. for(int j = 0; j < N; j++)
110. {
111. if (i !
= j)
112. {
113. allDistance[i][j] = calculateDistance(i, j);
114. allDistance[j][i] = allDistance[i][j];
115. }
116. }
117. }
118.}
119.
120.//获得经过n个城市的路径长度
121.double calculateSumOfDistance(int* tour)
122.{
123. double sum = 0;
124. for(int i = 0; i< N ;i++)
125. {
126. int row = *(tour + 2 * i);
127. int col = *(tour + 2* i + 1);
128. sum += allDistance[row][col];
129. }
130. return sum;
131.}
132.
133.class ACSAnt;
134.
135.class AntColonySystem
136.{
137.private:
138. double info[N][N], visible[N][N];//节点之间的信息素强度,节点之间的能见度
139.public:
140. AntColonySystem()
141. {
142. }
143. //计算当前节点到下一节点转移的概率
144. double Transition(int i, int j);
145. //局部更新规则
146. void UpdateLocalPathRule(int i, int j);
147. //初始化
148. void InitParameter(double value);
149. //全局信息素更新
150. void UpdateGlobalPathRule(int* bestTour, int globalBestLength);
151.};
152.
153.//计算当前节点到下一节点转移的概率
154.double AntColonySystem:
:
Transition(int i, int j)
155.{
156. if (i !
= j)
157. {
158. return (pow(info[i][j],alpha) * pow(visible[i][j], beta));
159. }
160. else
161. {
162. return 0.0;
163. }
164.}
165.//局部更新规则
166.void AntColonySystem:
:
UpdateLocalPathRule(int i, int j)
167.{
168. info[i][j] = (1.0 - alpha1) * info[i][j] + alpha1 * (1.0 / (N * Lnn));
169. info[j][i] = info[i][j];
170.}
171.//初始化
172.void AntColonySystem:
:
InitParameter(double value)
173.{
174. //初始化路径上的信息素强度tao0
175. for(int i = 0; i < N; i++)
176. {
177. for(int j = 0; j < N; j++)
178. {
179. info[i][j] = value;
180. info[j][i] = value;
181. if (i !
= j)
182. {
183. visible[i][j] = 1.0 / allDistance[i][j];
184. visible[j][i] = visible[i][j];
185. }
186. }
187. }
188.}
189.
190.//全局信息素更新
191.void AntColonySystem:
:
UpdateGlobalPathRule(int* bestTour, int globalBestLength)
192.{
193. for(int i = 0; i < N; i++)
194. {
195. int row = *(bestTour + 2 * i);
196. int col = *(bestTour + 2* i + 1);
197. info[row][col] = (1.0 - rou) * info[row][col] + rou * (1.0 / globalBestLength);
198. info[col][row] =info[row][col];
199. }
200.}
201.
202.class ACSAnt
203.{
204.private:
205. AntColonySystem* antColony;
206.protected:
207. int startCity, cururentCity;//初始城市编号,当前城市编号
208. int allowed[N];//禁忌表
209. int Tour[N][2];//当前路径
210. int currentTourIndex;//当前路径索引,从0开始,存储蚂蚁经过城市的编号
211.public:
212. ACSAnt(AntColonySystem* acs, int start)
213. {
214. antColony = acs;
215. startCity = start;
216. }
217. //开始搜索
218. int* Search();
219. //选择下一节点
220. int Choose();
221. //移动到下一节点
222. void MoveToNextCity(int nextCity);
223.
224.};
225.
226.//开始搜索
227.int* ACSAnt:
:
Search()
228.{
229. cururentCity = startCity;
230. int toCity;
231. currentTourIndex = 0;
232. for(int i = 0; i < N; i++)
233. {
234. allowed[i] = 1;
235. }
236. allowed[cururentCity] = 0;
237. int endCity;
238. int count = 0;
239. do
240. {
241. count++;
242. endCity = cururentCity;
243. toCity = Choose();
244. if (toCity >= 0)
245. {
246. MoveToNextCity(toCity);
247. antColony->UpdateLocalPathRule(endCity, toCity);
248. cururentCity = toCity;
249. }
250. }while(toCity >= 0);
251. MoveToNextCity(startCity);
252. antColony->UpdateLocalPathRule(endCity, startCity);
253.
254. return *Tour;
255.}
256.
257.//选择下一节点
258.int ACSAnt:
:
Choose()
259.{
260. int nextCity = -1;
261. double q = rand()/(double)RAND_MAX;
262. //如果 q <= q0,按先验知识,否则则按概率转移,
263. if (q <= qzero)
264. {
265. double probability = -1.0;//转移到下一节点的概率
266. for(int i = 0; i < N; i++)
267. {
268. //去掉禁忌表中已走过的节点,从剩下节点中选择最大概率的可行节点
269. if (1 == allowed[i])
270. {
271. double prob = antColony->Transition(cururentCity, i);
272. if (prob > probability)
273. {
274. nextCity = i;
275. probability = prob;
276. }
277. }
278. }
279. }
280. else
281. {
282. //按概率转移
283. double p = rand()/(double)RAND_MAX;//生成一个随机数,用来判断落在哪个区间段
284. double sum = 0.0;
285. double probability = 0.0;//概率的区间点,p 落在哪个区间段,则该点是转移的方向
286. //计算概率公式的分母的值
287. for(int i = 0; i < N; i++)
288. {
289. if (1 == allowed[i])
290. {
291. sum += antColony->Transition(cururentCity, i);
292. }
293. }
294. for(int j = 0; j < N; j++)
295. {
296. if (1 == allowed[j] && sum > 0)
297. {
298. probability += antColony->Transition(cururentCity, j)/sum;
299
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 算法 源代码 精编