对蚁群算法中轮盘赌选择城市的理解

蚁群算法中为了保证蚂蚁选择路径的随机性,在选择路径时概率大的路径被选择的概率大,但同时概率小的路径也有可能被选中,而不是直接选择概率大的路径,这样就不会所有的蚂蚁到这里都做出同样的选择,导致算法失去随机性。

为了避免算法失去随机性,在选择路径时使用轮盘赌的方法来选择。将每条路径的概率看作是轮盘的一个扇面,旋转轮盘,指针停在哪一个扇面上就选择对应概率的路径,通过使用一个[0,1]之间的随机数 rand 来模拟指针停止时指向的扇面。

假设路径A、B、C、D、E对应的概率分别为0.1、0.2、0.1、0.5、0.1,则0<rand<=0.1对应路径A扇面,0.1<rand<=0.3对应路径B扇面,0.3<rand<=0.4对应路径C扇面,0.4<rand<=0.9对应路径D扇面,0.9<rand<=1对应路径E扇面。

这样就可以保证概率大的路径被选择的概率大同时概率小的路径也有可能被选中。

分析Matlab程序

Pcum=cumsum(P);
Select=find(Pcum>=rand);
to_visit=J(Select(1));

P是选择各条路径的概率

Pcum中是路径概率的累加和,来模仿各条路径对应轮盘扇面大小

rand 是[0,1]之间的随机数,假设为0.7,位于 [0.4,0.7] 之间

find(Pcum>=rand) 返回所有大于 rand 的扇面,路径D和路径E都被选中

J(Select(1)) 选择第一个大于 rand 的扇面,选中路径D

 

参考博客:

1. https://blog.csdn.net/pymqq/article/details/51375522

2. http://blog.sina.com.cn/s/blog_9a43f8da01013jwv.html

 

  • 13
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
蚁群算法是一种模拟蚂蚁觅食行为的优化算法,常用于解决组合优化问题。而轮盘蚁群算法的一种选择策略,用于确定下一步的行动。 以下是一个使用轮盘选择策略的蚁群算法的Python代码示例: ```python import random # 初始化蚂蚁数量、城市数量和信息素矩阵 ant_num = 10 city_num = 5 pheromone = [[1.0 for _ in range(city_num)] for _ in range(city_num)] # 初始化城市距离矩阵 distance = [ [0, 2, 3, 4, 5], [2, 0, 6, 7, 8], [3, 6, 0, 9, 10], [4, 7, 9, 0, 11], [5, 8, 10, 11, 0] ] # 轮盘选择下一个城市 def roulette_selection(probabilities): total = sum(probabilities) r = random.uniform(0, total) accumulate = 0 for i in range(len(probabilities)): accumulate += probabilities[i] if accumulate >= r: return i # 蚁群算法主循环 for _ in range(100): # 初始化每只蚂蚁的当前城市和已访问城市列表 ants = [[random.randint(0, city_num-1)] for _ in range(ant_num)] visited = [[False for _ in range(city_num)] for _ in range(ant_num)] # 每只蚂蚁按照轮盘选择下一个城市 for _ in range(city_num-1): for ant in ants: current_city = ant[-1] unvisited_cities = [i for i in range(city_num) if not visited[ant_num][i]] probabilities = [pheromone[current_city][city] / distance[current_city][city] for city in unvisited_cities] next_city = roulette_selection(probabilities) ant.append(next_city) visited[ant_num][next_city] = True # 更新信息素矩阵 delta_pheromone = [[0.0 for _ in range(city_num)] for _ in range(city_num)] for ant in ants: for i in range(city_num-1): current_city = ant[i] next_city = ant[i+1] delta_pheromone[current_city][next_city] += 1 / distance[current_city][next_city] for i in range(city_num): for j in range(city_num): pheromone[i][j] = (1 - 0.5) * pheromone[i][j] + delta_pheromone[i][j] # 输出最优路径 best_ant = max(ants, key=lambda x: sum(distance[x[i]][x[i+1]] for i in range(city_num-1))) best_path = [str(city) for city in best_ant] print("Best path:", "->".join(best_path)) ``` 这段代码实现了一个简单的蚁群算法,其使用了轮盘选择策略来确定下一个城市。代码城市数量为5,距离矩阵和信息素矩阵都是手动设置的示例数据。你可以根据实际问题进行相应的修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值