
人工蜂巢算法(ABHA):测试与结果
1. 概述
在上一篇文章中,我们通过深入研究其工作原理,探索了人工蜂巢算法(ABHA)的奇妙世界。我们描述了该算法的结构和类,并展示了算法伪代码以及移动(Moving)和修订(Revision)方法。这一介绍将为后续对该算法的进一步研究和理解奠定基础。
在本文中,我们将继续深入编码工作,并涵盖所有剩余的方法。与之前一样,我们将在各种测试函数上进行测试,以评估算法的效率和性能。最后,我们将通过评分表总结算法的工作结果。
让我们回顾一下基于蜜蜂个体状态和行为模型的ABHA算法的主要要点:
- 每只蜜蜂都被表示为一个独立的智能体,其行为由状态(新手、有经验、搜索和食物源)来调节。
- 在任何给定时刻,单只蜜蜂的行为由其可获得的内部和外部信息以及其动机状态决定,这符合一组特定的规则。
- 规则集对每只蜜蜂都是相同的,但由于不同空间位置的蜜蜂感知到的环境不同,它们的行为也会有所不同。
- 蜜蜂可能会根据它们的觅食经验和/或动机状态表现出不同的行为。
因此,ABHA模型将蜜蜂的行为表示为由一组适应个体特征和环境的规则所支配。
2. 继续算法实现
让我们继续使用上一篇文章中描述的伪代码来编写算法的方法。
StageActivityNovice方法控制新手蜜蜂如何根据随机搜索或跟随其他智能体的“舞蹈”来改变它们的位置。方法描述:
- 参数 - 该方法接受一个类型为S_ABHA_Agent 的agent对象的引用,它在算法中代表一个“新手”。
- 返回值 — 该方法不返回任何内容(void)。
方法逻辑:
1. 声明一个名为val的变量,用于存储智能体当前的坐标。
2. 通过for循环遍历智能体的所有坐标,其中coords是搜索空间中的总坐标数(或维度数)。
3. 在循环中,智能体当前坐标的值被存储在变量val中。
4. 依据概率决定执行哪种行为:
- 随机搜索h - 如果生成的随机数(通过u.RNDprobab ())小于randomSearchProbability,则执行随机搜索,并使用ActionRandomSearch (c)方法更新c坐标,该方法在这一维度中生成一个新的位置。
- 跟随“舞蹈” - 否则,智能体会跟随其他智能体的“舞蹈”。在这种情况下,c坐标通过ActionFollowingDance (c, val)方法更新,该方法使用val值,根据其他智能体的信息确定新位置。
StageActivityNovice方法控制ABHA算法中新手的行为,并最终执行以下关键操作:
1. 遍历每个智能体的坐标。
2. 根据随机概率决定智能体是执行随机搜索还是跟随其他智能体的“舞蹈”。
3. 根据选定的行为更新智能体在每个坐标上的位置。
该方法使新手能够通过随机策略以及基于与其他智能体互动的策略来适应环境。
//—————————————————————————————————————————————————————————————————————————————— //Actions 1 or 2 void C_AO_ABHA::StageActivityNovice (S_ABHA_Agent &agent) { double val; for (int c = 0; c < coords; c++) { val = agent.position [c]; if (u.RNDprobab () < randomSearchProbability) agent.position [c] = ActionRandomSearch (c); else agent.position [c] = ActionFollowingDance (c, val); } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的StageActivityExperienced方法负责 ABHA 算法中经验丰富的智能体的行为,并控制这些智能体如何根据随机概率和当前策略选择其行动。
1. 声明了变量rnd。它将用于存储生成的随机数,以决定智能体的行为。
2. 通过for循环遍历智能体的所有坐标,其中coords是搜索空间中的总坐标数(或维度数)。
3. 在循环中,使用RNDprobab ()方法为每个坐标生成一个随机数rnd,该方法返回介于0和1之间的值。
4. 如果rnd小于或等于agent.p_srs(随机搜索概率),智能体会执行随机搜索,并通过ActionRandomSearch (c)方法更新其在c坐标上的位置。
跟随“舞蹈”的概率:
- 如果rnd超过agent.p_srs且小于或等于agent.p_rul(跟随“舞蹈”的概率),智能体会跟随其他智能体的“舞蹈”,通过ActionFollowingDance (c, agent.position [c])方法更新其位置。
留在食物源附近的概率:
- 如果上述条件均不满足,智能体会留在食物源附近,并通过ActionHiveVicinity (c, agent.bestPosition [c])方法更新其位置,其中agent.bestPosition [c]表示智能体所知的最佳位置。
StageActivityExperienced方法控制 ABHA 算法中经验丰富的智能体的行为,并最终实现以下功能:
1. 遍历每个智能体的坐标。
2. 生成一个随机数以选择一个行为。
3. 根据生成的数字和概率,决定智能体是执行随机搜索、跟随其他智能体的“舞蹈”,还是留在食物源附近。
这种方法使经验丰富的智能体能够使用比新手更复杂的策略来适应环境,从而提高其资源搜索性能。
//—————————————————————————————————————————————————————————————————————————————— //actions 1 or 2 or 4 void C_AO_ABHA::StageActivityExperienced (S_ABHA_Agent &agent) { double rnd = 0; for (int c = 0; c < coords; c++) { rnd = u.RNDprobab (); // random search probability if (rnd <= agent.p_srs) { agent.position [c] = ActionRandomSearch (c); } else { // Probability of following the dance if (agent.p_srs < rnd && rnd <= agent.p_rul) { agent.position [c] = ActionFollowingDance (c, agent.position [c]); } // Probability of remaining near the source else { agent.position [c] = ActionHiveVicinity (c, agent.bestPosition [c]); } } } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的StageActivitySearch方法负责智能体在搜索阶段的行为,并控制智能体如何在搜索空间中移动,根据所选择的方向更新它们的位置。该方法执行以下操作:
1. 遍历每个智能体的坐标。
2. 对于每个坐标,调用ActionMovingDirection方法,该方法确定智能体移动的新方向。
3. 在相应的坐标上更新智能体的位置。
这种方法使智能体能够在搜索空间中积极移动,并根据所选择的方向调整它们的位置。
//—————————————————————————————————————————————————————————————————————————————— //Actions 3 void C_AO_ABHA::StageActivitySearch (S_ABHA_Agent &agent) { for (int c = 0; c < coords; c++) { agent.position [c] = ActionMovingDirection (agent, c); } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的StageActivitySource方法旨在执行与模型中智能体确定食物源相关的动作。它根据智能体靠近蜂巢的最佳位置来更新它们的位置。该方法执行以下步骤:
1. 初始化变量val。
2. 遍历每个智能体的坐标。
3. 对于每个坐标,通过调用ActionHiveVicinity方法更新智能体的位置,该方法根据智能体所知的最佳位置确定其新位置。
这种方法帮助“食物源”智能体将全部注意力集中在对已知食物源周围环境的详细探索上。
//—————————————————————————————————————————————————————————————————————————————— //Actions 4 void C_AO_ABHA::StageActivitySource (S_ABHA_Agent &agent) { double val = 0; for (int c = 0; c < coords; c++) { agent.position [c] = ActionHiveVicinity (c, agent.bestPosition [c]); } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ActionRandomSearch方法旨在在给定的坐标范围内执行随机搜索,并允许智能体在给定范围内随机选择一个值。该方法对搜索空间进行扩展探索,并执行以下操作:
1. 接受一个坐标索引,用于生成随机值。
2. 使用随机数生成方法在给定坐标的最小值和最大值范围内获取一个值。
3. 返回一个随机生成的double类型的值。
//—————————————————————————————————————————————————————————————————————————————— //1. Random search (random placement in a range of coordinates) double C_AO_ABHA::ActionRandomSearch (int coordInd) { return u.RNDfromCI (rangeMin [coordInd], rangeMax [coordInd]); } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ActionFollowingDance方法实现了跟随“舞蹈者”的逻辑,这意味着朝着已经达到一定经验水平的智能体的方向移动。该方法采用概率方法选择当前智能体将跟随的智能体,并在计算出的方向中引入随机噪声。
1. 计算总概率:
- 初始化变量totalProbability ,用于存储所有“经验丰富”的智能体的p_si概率之和。
- 通过循环检查所有智能体的状态。如果智能体经验丰富,其概率将被累加到totalProbability中。
2. 生成随机值并选择智能体:
- 生成随机值randomValue。将该值相对于totalProbability进行归一化。
- 在下一个循环中,累加经验丰富智能体的p_si概率。一旦累积概率超过randomValue,则保存选定智能体的ind索引并退出循环。
3. 检查选定的智能体并计算新值:
- 如果没有选择任何智能体(ind索引等于-1),则调用ActionRandomSearch方法进行随机搜索。
- 如果选择了智能体,则计算direction移动方向,即选定智能体的最佳位置与当前值val之间的差值。
- 在 -1到1的范围内生成随机noise。
- 返回一个新值,即当前val值根据方向和噪声进行调整后的结果。
ActionFollowingDance方法实现了跟随经验丰富智能体的策略(使用轮盘赌规则选择智能体,经验更丰富的“舞蹈”蜜蜂被选中的概率更高),通过概率方法选择智能体并在移动方向中添加随机噪声,从而使智能体的行为更加多样化和适应性强。
——————————————————————————————————————————————————————————————————————— //2. Follow the dance (move in the direction of the dancer) double C_AO_ABHA::ActionFollowingDance (int coordInd, double val) { //---------------------------------------------------------------------------- double totalProbability = 0; for (int i = 0; i < popSize; i++) { if (agents [i].state == S_ABHA_Agent::stateExperienced) { totalProbability += agents [i].p_si; } } //---------------------------------------------------------------------------- double randomValue = u.RNDprobab () * totalProbability; double cumulativeProbability = 0; int ind = -1; for (int i = 0; i < popSize; i++) { if (agents [i].state == S_ABHA_Agent::stateExperienced) { cumulativeProbability += agents [i].p_si; if (cumulativeProbability >= randomValue) { ind = i; break; } } } //---------------------------------------------------------------------------- if (ind == -1) { return ActionRandomSearch (coordInd); } double direction = agents [ind].bestPosition [coordInd] - val; double noise = u.RNDfromCI (-1.0, 1.0); return val + direction * noise; } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ActionMovingDirection方法负责在给定方向上以一定步长移动智能体。让我们逐步分析代码。
1. 智能体移动:
- 在指定的coordInd坐标上更新智能体的位置。
- agent.position [coordInd] - 智能体在给定坐标上的当前位置。
- agent.stepSize - 步长,智能体在这一方向上移动的距离。
- agent.direction [coordInd] - 智能体在指定坐标上的移动方向。
- 将stepSize乘以direction,得到加到智能体当前位置的移动量。
2. 步长衰减
- 移动完成后,stepSize通过stepSizeReductionFactor比例减小。
- 这是为了模拟运动衰减的效果,即智能体开始以更慢的速度移动,以便更精细地优化找到的解。
ActionMovingDirection方法实现了智能体在给定方向上移动的简单逻辑,考虑了步长,并在移动后减小步长。
//—————————————————————————————————————————————————————————————————————————————— //3. Move in a given direction with a step double C_AO_ABHA::ActionMovingDirection (S_ABHA_Agent &agent, int coordInd) { agent.position [coordInd] += agent.stepSize * agent.direction [coordInd]; agent.stepSize *= stepSizeReductionFactor; return agent.position [coordInd]; } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ActionHiveVicinity方法旨在定义代理在食物源附近的行为。ActionHiveVicinity方法负责在给定的坐标处生成一个新的位置。在当前位置附近生成新位置的概率较高。
//—————————————————————————————————————————————————————————————————————————————— //4. Move in the vicinity of a food source double C_AO_ABHA::ActionHiveVicinity (int coordInd, double val) { return u.PowerDistribution (val, rangeMin [coordInd], rangeMax [coordInd], 12); } //——————————————————————————————————————————————————————————————————————————————
让我们看看C_AO_ABHA类中的ChangingStateForNovice方法,该方法根据智能体当前的cost(适应度)和关于食物源的信息来改变智能体的状态。
1. 未使用该方法前的先前和最佳成本。
2. 检查状态:
- 如果当前食物源的成本agent.cost超过了平均值avgCost,智能体的状态将变为经验丰富(stateExperienced)。这意味着智能体已经获得了足够的关于高收益食物源的信息,并准备好进行更高效地行为。
3. 进入搜索状态:
- 如果当前值没有超过平均值,智能体将进入搜索状态(stateSearch)。这种状态假设智能体没有关于食物源的信息,应该开始随机搜索。
4. 随机搜索方向:
- 循环遍历所有coords坐标,并为每个坐标分配一个随机方向。RNDfromCI方法在指定范围内生成一个随机数,并用于确定在指定范围(rangeMin和rangeMax)内的移动量和方向。
5. 初始化搜索参数:
- 为智能体的移动设置stepSize,同时设置searchCounter,用于记录智能体在寻找食物时进行的迭代次数。
ChangingStateForNovice方法负责根据其食物源的价值改变“新手”蜜蜂的状态。如果价值高,智能体将变得经验丰富。如果价值低,智能体将进入搜索状态,开始随机搜索,并初始化其参数。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::ChangingStateForNovice (S_ABHA_Agent &agent) { //Current cost : Used to enter either the Experienced or Search state. //Previous cost: Not used. //Best cost : Not used. //Into Experienced. If a novice receives information about a highly profitable food source (for example, through dances of other bees), it may transition to the experienced state. //Into Search. If a novice does not receive information about food sources, it may begin a random search and enter a search state. if (agent.cost > avgCost) agent.state = S_ABHA_Agent::stateExperienced; else { agent.state = S_ABHA_Agent::stateSearch; for (int c = 0; c < coords; c++) { agent.direction [c] = u.RNDfromCI (-(rangeMax [c] - rangeMin [c]), (rangeMax [c] - rangeMin [c])); } agent.stepSize = initialStepSize; agent.searchCounter = 0; } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ChangingStateForExperienced方法用于根据智能体当前和之前的觅食成本值来控制经验丰富智能体的状态。让我们详细看一下这个方法:
1. 更改pab参数:
- 如果当前值小于之前的值:蜜蜂会降低其在食物源附近停留的概率(pab)。如果pab小于 0,则将其设置为 0。
- 如果当前值大于之前的值:蜜蜂会增加在食物源附近停留的概率。如果pab超过1,则将其设置为1。
- 如果当前值超过了最佳值,停留在食物源附近的概率被设置为最大值1。
2. 进入食物源或搜索状态:
- 如果当前值比群体平均值高出20%:蜜蜂进入食物源(stateSource)状态,这意味着它找到了一个好的食物源。在食物源停留的概率被设置为1。
- 如果当前值低于平均值:蜜蜂进入搜索(stateSearch)状态。这表明需要寻找新的食物源。
3. 随机搜索方向:
- 如果进入搜索状态:蜜蜂被赋予随机方向以寻找新的食物源。
4. 初始化搜索参数:
- 为智能体的移动设置了一个步长,同时设置了一个搜索计数器,用于跟踪智能体在寻找食物时所采取的步数。
ChangingStateForExperienced方法根据智能体当前的食物源价值和之前的值来控制经验丰富蜜蜂的状态。它通过比较当前值、先前值和最佳值来决定蜜蜂是继续探索当前食物源还是寻找新的食物源。pab参数(停留在食物源的概率)会根据成本的变化进行调整。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::ChangingStateForExperienced (S_ABHA_Agent &agent) { //Current cost : If the current value is high and the information is valid, it can pass this information on to other bees through a dance. //Previous cost: A bee compares the current value with the previous one to determine if the situation has improved. If the current value is better, it may increase the probability of passing the information. //Best cost : A bee can use the best value to assess whether to continue exploring a given food source or to look for a new one. //Into Search. If the information about the current food source is not good enough (e.g. the current fitness value is below the threshold), a bee may enter a search state to search for new sources. //Into Food Source. If information about a food source is confirmed (e.g. the current fitness value is high and stable), a bee may switch to a food source state to analyze the source in more depth. if (agent.cost < agent.prevCost) { agent.pab -= abandonmentRate; if (agent.pab < 0.0) agent.pab = 0.0; } if (agent.cost > agent.prevCost) { agent.pab += abandonmentRate; if (agent.pab > 1.0) agent.pab = 1.0; } if (agent.cost > agent.bestCost) agent.pab = 1.0; if (agent.cost > avgCost * 1.2) { agent.state = S_ABHA_Agent::stateSource; agent.pab = 1; } else if (agent.cost < avgCost) { agent.state = S_ABHA_Agent::stateSearch; for (int c = 0; c < coords; c++) { agent.direction [c] = u.RNDfromCI (-(rangeMax [c] - rangeMin [c]), (rangeMax [c] - rangeMin [c])); } agent.stepSize = initialStepSize; agent.searchCounter = 0; } } //——————————————————————————————————————————————————————————————————————————————
现在让我们更仔细地看看C_AO_ABHA类中的ChangingStateForSearch方法代码,该方法控制智能体在寻找食物源时的行为。该方法执行以下步骤:
1. 当前值与先前值的比较:
- 如果当前值 agent.cost小于先前值agent.prevCost,这意味着蜜蜂正在远离优质食物源。在这种情况下:
- 蜜蜂通过使用u.RNDfromCI函数为每个坐标方向生成随机值来改变其方向。
- 设置初始步长initialStepSize,蜜蜂准备好继续探索。
- 增加searchCounter搜索计数器,以便跟踪搜索尝试的次数。
2. 当前值与最佳值的比较:
- 如果当前值超过agent.bestCost,这意味着蜜蜂发现了一个更有利的食物源。在这种情况下:
- 步长通过指定的比例stepSizeReductionFactor减小,这表明需要更小的步长来精细化找到的解。
- 搜索计数器重置为0,因为蜜蜂发现了一个更有利的食物源。
3. 检查最大搜索尝试次数:
- 如果搜索计数器达到最大尝试次数maxSearchAttempts,这意味着蜜蜂在一定次数的尝试后没有找到有利的食物源。在这种情况下:
- 搜索计数器重置为0。
- 蜜蜂进入新手状态(stateNovice)。这意味着它应该重新开始搜索。
4. 检查优质食物源:
- 如果当前值比平均值avgCost高出20%,这表明蜜蜂发现了一个优质的食物源。在这种情况下:
- 蜜蜂进入食物源状态(stateSource),这意味着它将进一步评估该食物源的盈利能力。
- 停留在食物源附近的概率(pab)被设置为1。蜜蜂将更倾向于留在该食物源附近。
ChangingStateForSearch方法管理蜜蜂在搜索状态下的行为,基于当前值、先前值和最佳适应度值的比较做出决策。它允许蜜蜂适应环境,并进入食物源或新手状态,同时根据找到的食物源调整步长。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::ChangingStateForSearch (S_ABHA_Agent &agent) { //Current cost : A bee uses its current fitness value to assess its current position and to decide whether to continue searching or change direction //Previous value: A bee compares the current value with the previous one to determine if things have improved. If the current value is better, it can continue in the same direction. //Best value : The bee uses the best value to determine whether the current food source is more profitable than the previous ones. This helps it make decisions about whether to stay put or continue searching. //Into Food Source. If a searching bee finds a food source with good characteristics (e.g., the current fitness value is better than the threshold), it can switch to the food source state to evaluate the profitability of the source. //Into Novice. If a searching bee does not find any food sources or the information is impractical, it may revert to novice. if (agent.cost < agent.prevCost) { for (int c = 0; c < coords; c++) { agent.direction [c] = u.RNDfromCI (-(rangeMax [c] - rangeMin [c]), (rangeMax [c] - rangeMin [c])); } agent.stepSize = initialStepSize; agent.searchCounter++; } if (agent.cost > agent.bestCost) { agent.stepSize *= stepSizeReductionFactor; agent.searchCounter = 0; } if (agent.searchCounter >= maxSearchAttempts) { agent.searchCounter = 0; agent.state = S_ABHA_Agent::stateNovice; return; } if (agent.cost > avgCost * 1.2) { agent.state = S_ABHA_Agent::stateSource; agent.pab = 1; } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的ChangingStateForSource方法管理蜜蜂在利用食物源时的行为。其方法结构如下:
1. 进入搜索状态:
- 如果当前值agent.cost低avgCost,这表明当前食物源无利可图。
- 蜜蜂将通过abandonmentRate减少停留在当前食物源附近的概率(pab),这意味着它不太可能继续留在当前食物源附近。
- 如果从u.RNDprobab获取的随机值超过agent.pab,蜜蜂决定进入搜索状态(stateSearch):
- 概率重置为pab = 0。
- 蜜蜂通过u.RNDfromCI为每个坐标生成随机值来改变方向。
- 设置初始步长initialStepSize,并将搜索计数器searchCounter重置为0。
3. 进入经验丰富状态:
- 如果当前值agent.cost超过最佳值agent.bestCost,这表明当前食物源是有利可图的。
- 在这种情况下,蜜蜂进入经验丰富状态(stateExperienced),并将向其他蜜蜂传递有关优质食物源的信息。
- 停留在食物源附近的概率(pab)被设置为1。蜜蜂将尽可能留在该食物源附近。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::ChangingStateForSource (S_ABHA_Agent &agent) { //Current cost : If the current value is below the threshold, it may decide that the source is not good enough and start looking for a new one. //Previous value: The bee can use the previous value to compare and determine if the situation has improved. If the current value is worse, it may signal the need to change the strategy. //Best value : A bee uses the best value to decide whether to continue exploiting the current food source or to look for a new, more profitable one. //Into Search. If the current food source turns out to be impractical (e.g. the current fitness value is worse than the threshold), a bee may switch to a search state to search for new sources. //Into Experienced. If a food source bee finds a food source that proves beneficial, it may enter the experienced state to pass on the information to other bees. if (agent.cost < avgCost) { agent.pab -= abandonmentRate; if (u.RNDprobab () > agent.pab) { agent.state = S_ABHA_Agent::stateSearch; agent.pab = 0; for (int c = 0; c < coords; c++) { agent.direction [c] = u.RNDfromCI (-(rangeMax [c] - rangeMin [c]), (rangeMax [c] - rangeMin [c])); } agent.stepSize = initialStepSize; agent.searchCounter = 0; } } if (agent.cost > agent.bestCost) { agent.state = S_ABHA_Agent::stateExperienced; agent.pab = 1; } } //—————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的CalculateProbabilities方法负责根据每个智能体(蜜蜂)的当前值计算它们执行不同行为的概率。该方法执行以下步骤:
1. 变量初始化:
- maxCost初始化为最小可能值,以确保任何智能体的成本都会大于这个值。
- minCost初始化为最大值,以确保任何智能体的成本都可以小于这个值。
2. 寻找最大值和最小值:
- 循环遍历popSize中的所有智能体(蜜蜂)。
- 在循环中,将智能体的当前值与maxCost和minCost进行比较,并在必要时更新这些值。
3. 计算值的范围:
- costRange表示最大值和最小值之间的差值,这有助于对概率进行归一化。
4. 计算每个智能体的概率:
- p_si - 基于智能体成本的概率。成本越高,概率越高(通过范围归一化)。
- p_srs - 提前指定的随机搜索概率。
- p_rul - 跟随“舞蹈”的概率。这意味着停留在食物源的概率越高,跟随“舞蹈”的概率就越低。
- p_ab - 停留在食物源附近的概率,等于智能体的pab。
在计算完这三种概率后,计算它们的总和,并将每个概率归一化,使概率之和等于1。这是通过将每个概率除以总和来实现的。
CalculateProbabilities方法允许每只蜜蜂根据其当前成本评估执行各种行为(随机搜索、跟随舞蹈、停留在食物源附近)的可能性。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::CalculateProbabilities () { double maxCost = -DBL_MAX; double minCost = DBL_MAX; for (int i = 0; i < popSize; i++) { if (agents [i].cost > maxCost) maxCost = agents [i].cost; if (agents [i].cost < minCost) minCost = agents [i].cost; } double costRange = maxCost - minCost; for (int i = 0; i < popSize; i++) { agents [i].p_si = (maxCost - agents [i].cost) / costRange; agents [i].p_srs = randomSearchProbability; // random search probability agents [i].p_rul = 1.0 - agents [i].pab; // probability of following the dance agents [i].p_ab = agents [i].pab; // probability of staying near the source double sum = agents [i].p_srs + agents [i].p_rul + agents [i].p_ab; agents [i].p_srs /= sum; agents [i].p_rul /= sum; agents [i].p_ab /= sum; } } //——————————————————————————————————————————————————————————————————————————————
C_AO_ABHA类中的CalculateAverageCost方法用于计算群体中所有蜜蜂智能体的平均成本。这些信息对于分析群体的状态以及在算法内做出决策是必要的。平均成本作为智能体成功的指标,并且也用于进一步的计算。
//—————————————————————————————————————————————————————————————————————————————— void C_AO_ABHA::CalculateAverageCost () { double totalCost = 0; for (int i = 0; i < popSize; i++) { totalCost += agents [i].cost; } avgCost = totalCost / popSize; } //———————
3. 测试结果
ABHA结果
ABHA|Artificial Bee Hive Algorithm|10.0|10.0|0.1|0.1|0.99|0.5|
=============================
5 Hilly's; Func runs: 10000; result: 0.8413125195861497
25 Hilly's; Func runs: 10000; result: 0.5422730855489947
500 Hilly's; Func runs: 10000; result: 0.2630407626746883
=============================
5 Forest's; Func runs: 10000; result: 0.8785786358650522
25 Forest's; Func runs: 10000; result: 0.47779307049664316
500 Forest's; Func runs: 10000; result: 0.17181208858518054
=============================
5 Megacity's; Func runs: 10000; result: 0.5092307692307693
25 Megacity's; Func runs: 10000; result: 0.3387692307692307
500 Megacity's; Func runs: 10000; result: 0.1039692307692317
=============================
总分:4.12678 (45.85%)
我们已经完成了代码的编写,并对所有ABHA组件进行了详细的分析。现在,让我们直接进入在测试函数上测试算法,并评估其效率。在算法的可视化中,您可以看到测试结果存在相当大的差异。
ABHA在 Hilly 测试函数上
ABHA在 Forest 测试函数上
ABHA在 Megacity 测试函数上
研究结果表明,该算法在评分表中稳居中间位置。
# | AO | 说明 | Hilly值 | Hilly最终值 | Forest值 | Forest最终值 | Megacity (离散) | Megacity最终值 | 最终结果 | 最大百分比 | ||||||
10 p (5 F) | 50 p (25 F) | 1000 p (500 F) | 10 p (5 F) | 50 p (25 F) | 1000 p (500 F) | 10 p (5 F) | 50 p (25 F) | 1000 p (500 F) | ||||||||
1 | ANS | 跨邻域搜索 | 0.94948 | 0.84776 | 0.43857 | 2.23581 | 1.00000 | 0.92334 | 0.39988 | 2.32323 | 0.70923 | 0.63477 | 0.23091 | 1.57491 | 6.134 | 68.15 |
2 | CLA | 密码锁算法 | 0.95345 | 0.87107 | 0.37590 | 2.20042 | 0.98942 | 0.91709 | 0.31642 | 2.22294 | 0.79692 | 0.69385 | 0.19303 | 1.68380 | 6.107 | 67.86 |
3 | (P+O)ES | (P+O) 进化策略 | 0.92256 | 0.88101 | 0.40021 | 2.20379 | 0.97750 | 0.87490 | 0.31945 | 2.17185 | 0.67385 | 0.62985 | 0.18634 | 1.49003 | 5.866 | 65.17 |
4 | CTA | 彗星尾算法 | 0.95346 | 0.86319 | 0.27770 | 2.09435 | 0.99794 | 0.85740 | 0.33949 | 2.19484 | 0.88769 | 0.56431 | 0.10512 | 1.55712 | 5.846 | 64.96 |
5 | SDSm | 随机扩散搜索 M | 0.93066 | 0.85445 | 0.39476 | 2.17988 | 0.99983 | 0.89244 | 0.19619 | 2.08846 | 0.72333 | 0.61100 | 0.10670 | 1.44103 | 5.709 | 63.44 |
6 | ESG | 社会群体的进化 | 0.99906 | 0.79654 | 0.35056 | 2.14616 | 1.00000 | 0.82863 | 0.13102 | 1.95965 | 0.82333 | 0.55300 | 0.04725 | 1.42358 | 5.529 | 61.44 |
7 | SIA | 模拟各向同性退火 | 0.95784 | 0.84264 | 0.41465 | 2.21513 | 0.98239 | 0.79586 | 0.20507 | 1.98332 | 0.68667 | 0.49300 | 0.09053 | 1.27020 | 5.469 | 60.76 |
8 | ACS | 人工协同搜索 | 0.75547 | 0.74744 | 0.30407 | 1.80698 | 1.00000 | 0.88861 | 0.22413 | 2.11274 | 0.69077 | 0.48185 | 0.13322 | 1.30583 | 5.226 | 58.06 |
9 | TSEA | 龟壳演化算法 | 0.96798 | 0.64480 | 0.29672 | 1.90949 | 0.99449 | 0.61981 | 0.22708 | 1.84139 | 0.69077 | 0.42646 | 0.13598 | 1.25322 | 5.004 | 55.60 |
10 | DE | 差分进化 | 0.95044 | 0.61674 | 0.30308 | 1.87026 | 0.95317 | 0.78896 | 0.16652 | 1.90865 | 0.78667 | 0.36033 | 0.02953 | 1.17653 | 4.955 | 55.06 |
11 | CRO | 化学反应优化 | 0.94629 | 0.66112 | 0.29853 | 1.90593 | 0.87906 | 0.58422 | 0.21146 | 1.67473 | 0.75846 | 0.42646 | 0.12686 | 1.31178 | 4.892 | 54.36 |
12 | BSA | 鸟群算法 | 0.89306 | 0.64900 | 0.26250 | 1.80455 | 0.92420 | 0.71121 | 0.24939 | 1.88479 | 0.69385 | 0.32615 | 0.10012 | 1.12012 | 4.809 | 53.44 |
13 | HS | 和声搜索 | 0.86509 | 0.68782 | 0.32527 | 1.87818 | 0.99999 | 0.68002 | 0.09590 | 1.77592 | 0.62000 | 0.42267 | 0.05458 | 1.09725 | 4.751 | 52.79 |
14 | SSG | 树苗播种和生长 | 0.77839 | 0.64925 | 0.39543 | 1.82308 | 0.85973 | 0.62467 | 0.17429 | 1.65869 | 0.64667 | 0.44133 | 0.10598 | 1.19398 | 4.676 | 51.95 |
15 | (PO)ES | (PO) 进化策略 | 0.79025 | 0.62647 | 0.42935 | 1.84606 | 0.87616 | 0.60943 | 0.19591 | 1.68151 | 0.59000 | 0.37933 | 0.11322 | 1.08255 | 4.610 | 51.22 |
16 | BSO | 头脑风暴优化 | 0.93736 | 0.57616 | 0.29688 | 1.81041 | 0.93131 | 0.55866 | 0.23537 | 1.72534 | 0.55231 | 0.29077 | 0.11914 | 0.96222 | 4.498 | 49.98 |
17 | WOAm | 鲸鱼优化算法M | 0.84521 | 0.56298 | 0.26263 | 1.67081 | 0.93100 | 0.52278 | 0.16365 | 1.61743 | 0.66308 | 0.41138 | 0.11357 | 1.18803 | 4.476 | 49.74 |
18 | AEFA | 人工电场算法 | 0.87700 | 0.61753 | 0.25235 | 1.74688 | 0.92729 | 0.72698 | 0.18064 | 1.83490 | 0.66615 | 0.11631 | 0.09508 | 0.87754 | 4.459 | 49.55 |
19 | ACOm | 蚁群优化 M | 0.88190 | 0.66127 | 0.30377 | 1.84693 | 0.85873 | 0.58680 | 0.15051 | 1.59604 | 0.59667 | 0.37333 | 0.02472 | 0.99472 | 4.438 | 49.31 |
20 | BFO-GA | 细菌觅食优化 - ga | 0.89150 | 0.55111 | 0.31529 | 1.75790 | 0.96982 | 0.39612 | 0.06305 | 1.42899 | 0.72667 | 0.27500 | 0.03525 | 1.03692 | 4.224 | 46.93 |
21 | ABHA | 人工蜂巢算法 | 0.84131 | 0.54227 | 0.26304 | 1.64663 | 0.87858 | 0.47779 | 0.17181 | 1.52818 | 0.50923 | 0.33877 | 0.10397 | 0.95197 | 4.127 | 45.85 |
22 | ASBO | 适应性社会行为优化 | 0.76331 | 0.49253 | 0.32619 | 1.58202 | 0.79546 | 0.40035 | 0.26097 | 1.45677 | 0.26462 | 0.17169 | 0.18200 | 0.61831 | 3.657 | 40.63 |
23 | MEC | 思维进化计算 | 0.69533 | 0.53376 | 0.32661 | 1.55569 | 0.72464 | 0.33036 | 0.07198 | 1.12698 | 0.52500 | 0.22000 | 0.04198 | 0.78698 | 3.470 | 38.55 |
24 | IWO | 入侵杂草优化 | 0.72679 | 0.52256 | 0.33123 | 1.58058 | 0.70756 | 0.33955 | 0.07484 | 1.12196 | 0.42333 | 0.23067 | 0.04617 | 0.70017 | 3.403 | 37.81 |
25 | Micro-AIS | 微型人工免疫系统 | 0.79547 | 0.51922 | 0.30861 | 1.62330 | 0.72956 | 0.36879 | 0.09398 | 1.19233 | 0.37667 | 0.15867 | 0.02802 | 0.56335 | 3.379 | 37.54 |
26 | COAm | 布谷鸟优化算法 M | 0.75820 | 0.48652 | 0.31369 | 1.55841 | 0.74054 | 0.28051 | 0.05599 | 1.07704 | 0.50500 | 0.17467 | 0.03380 | 0.71347 | 3.349 | 37.21 |
27 | SDOm | 螺旋动力学优化 M | 0.74601 | 0.44623 | 0.29687 | 1.48912 | 0.70204 | 0.34678 | 0.10944 | 1.15826 | 0.42833 | 0.16767 | 0.03663 | 0.63263 | 3.280 | 36.44 |
28 | NMm | Nelder-Mead方法 M | 0.73807 | 0.50598 | 0.31342 | 1.55747 | 0.63674 | 0.28302 | 0.08221 | 1.00197 | 0.44667 | 0.18667 | 0.04028 | 0.67362 | 3.233 | 35.92 |
29 | FAm | 萤火虫算法 M | 0.58634 | 0.47228 | 0.32276 | 1.38138 | 0.68467 | 0.37439 | 0.10908 | 1.16814 | 0.28667 | 0.16467 | 0.04722 | 0.49855 | 3.048 | 33.87 |
30 | GSA | 引力搜索算法 | 0.64757 | 0.49197 | 0.30062 | 1.44016 | 0.53962 | 0.36353 | 0.09945 | 1.00260 | 0.32667 | 0.12200 | 0.01917 | 0.46783 | 2.911 | 32.34 |
31 | BFO | 细菌觅食优化 | 0.61171 | 0.43270 | 0.31318 | 1.35759 | 0.54410 | 0.21511 | 0.05676 | 0.81597 | 0.42167 | 0.13800 | 0.03195 | 0.59162 | 2.765 | 30.72 |
32 | ABC | 人工蜂群 | 0.63377 | 0.42402 | 0.30892 | 1.36671 | 0.55103 | 0.21874 | 0.05623 | 0.82600 | 0.34000 | 0.14200 | 0.03102 | 0.51302 | 2.706 | 30.06 |
33 | BA | 蝙蝠算法 | 0.59761 | 0.45911 | 0.35242 | 1.40915 | 0.40321 | 0.19313 | 0.07175 | 0.66810 | 0.21000 | 0.10100 | 0.03517 | 0.34617 | 2.423 | 26.93 |
34 | SA | 模拟退火 | 0.55787 | 0.42177 | 0.31549 | 1.29513 | 0.34998 | 0.15259 | 0.05023 | 0.55280 | 0.31167 | 0.10033 | 0.02883 | 0.44083 | 2.289 | 25.43 |
35 | IWDm | 智能水滴 M | 0.54501 | 0.37897 | 0.30124 | 1.22522 | 0.46104 | 0.14704 | 0.04369 | 0.65177 | 0.25833 | 0.09700 | 0.02308 | 0.37842 | 2.255 | 25.06 |
36 | PSO | 粒子群优化 | 0.59726 | 0.36923 | 0.29928 | 1.26577 | 0.37237 | 0.16324 | 0.07010 | 0.60572 | 0.25667 | 0.08000 | 0.02157 | 0.35823 | 2.230 | 24.77 |
37 | Boids算法 | 虚拟生物算法 | 0.43340 | 0.30581 | 0.25425 | 0.99346 | 0.35718 | 0.20160 | 0.15708 | 0.71586 | 0.27846 | 0.14277 | 0.09834 | 0.51957 | 2.229 | 24.77 |
38 | MA | 猴群算法 | 0.59107 | 0.42681 | 0.31816 | 1.33604 | 0.31138 | 0.14069 | 0.06612 | 0.51819 | 0.22833 | 0.08567 | 0.02790 | 0.34190 | 2.196 | 24.40 |
39 | SFL | 混合蛙跳算法 | 0.53925 | 0.35816 | 0.29809 | 1.19551 | 0.37141 | 0.11427 | 0.04051 | 0.52618 | 0.27167 | 0.08667 | 0.02402 | 0.38235 | 2.104 | 23.38 |
40 | FSS | 鱼群搜索 | 0.55669 | 0.39992 | 0.31172 | 1.26833 | 0.31009 | 0.11889 | 0.04569 | 0.47467 | 0.21167 | 0.07633 | 0.02488 | 0.31288 | 2.056 | 22.84 |
41 | RND | 随机 | 0.52033 | 0.36068 | 0.30133 | 1.18234 | 0.31335 | 0.11787 | 0.04354 | 0.47476 | 0.25333 | 0.07933 | 0.02382 | 0.35648 | 2.014 | 22.37 |
42 | GWO | 灰狼优化算法 | 0.59169 | 0.36561 | 0.29595 | 1.25326 | 0.24499 | 0.09047 | 0.03612 | 0.37158 | 0.27667 | 0.08567 | 0.02170 | 0.38403 | 2.009 | 22.32 |
43 | CSS | 人工电场算法 | 0.44252 | 0.35454 | 0.35201 | 1.14907 | 0.24140 | 0.11345 | 0.06814 | 0.42299 | 0.18333 | 0.06300 | 0.02322 | 0.26955 | 1.842 | 20.46 |
44 | EM | 类电磁算法 | 0.46250 | 0.34594 | 0.32285 | 1.13129 | 0.21245 | 0.09783 | 0.10057 | 0.41085 | 0.15667 | 0.06033 | 0.02712 | 0.24412 | 1.786 | 19.85 |
总结
ABHA算法已在各种测试函数上进行了广泛测试,结果表明它与其他基于群体智能的算法相比具有竞争力。在这些测试中,ABHA展示了其效率和可靠性。
这些研究强调了ABHA算法不仅限于解决传统优化问题,还有解决包括多目标优化和约束条件等更复杂问题方面的潜力。然而,我原本期望能够取得比这更令人印象深刻的结果。尽管如此,ABHA的结果在群体算法中确实是无与伦比的。
总体而言,该算法可以被视为一系列特定的方法和技术,而这些方法和技术适用于绝大多数其他优化算法。其结果的可复现性和对不同条件的适应性,使该算法成为计算优化领域的一个有前景的工具。因此,ABHA不仅拓展了优化算法的应用范围,还为人工智能领域的研究及其实际应用开辟了新的可能性。
图1. 根据相关测试,将算法的颜色等级大于或等于 0.99的结果以白色突出显示
图例2. 算法测试结果的直方图(标尺从 0 到 100,数字越大结果越好,
其中 100 是理论上的最大可能结果,将计算评级表格的脚本存档)
ABHA优缺点:
优势:
- 在低维问题上表现良好。
- 在离散函数上表现良好。
缺点:
- 复杂的逻辑和算法实现。
- 在平滑函数的高维问题上收敛速度较慢。
- 外部参数过多。
文章附有一个包含当前版本算法代码的归档文件。本文作者对标准算法描述的绝对准确性不承担责任。为提升搜索能力,已经对其中的许多算法进行了修改。文章中表述的结论和论断都是基于实验的结果。
本文由MetaQuotes Ltd译自俄文
原文地址: https://www.mql5.com/ru/articles/15486


