代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析...

本文主要是介绍代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析

前言

上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去n的100次方心了。今天讲讲一些细枝末节,就是前面一直有提到的参数和一些状态的记录代码。这个简单啦,小编也不作过多解释了。大家直接看代码都能看懂,不过小编还是会把逻辑结构给大家梳理出来的。好了,开始干活。

01 ALNS_Iteration_Status

这个类,咳咳,不是抽象类了哈。主要用来记录ALNS迭代过程中的一些中间变量和状态等。主要是成员变量,成员函数都是简单的getter(获取成员变量的接口)或者setter(设置成员变量的接口)。所以这里就把成员变量贴出来好了,各个变量记录的内容注释也写得很详细,小编就不做多赘述以免扰乱了大家看代码的心。

private://! Id of the iteration corresponding to this status.size_t iterationId;//! Number of iteration since the last improvement of the BKSsize_t nbIterationWithoutImprovement;//! Number of iteration since the last improvement of the BKS//! or the last reload of the best known solution.size_t nbIterationWithoutImprovementSinceLastReload;//! Number of iterations since the last improvement of the current//! solution.size_t nbIterationWithoutImprovementCurrent;//! Number of iterations without transition.size_t nbIterationWithoutTransition;//! Indicate if a new best solution has been obtained.State newBestSolution;//! Indicate if the new solution has been accepted as the//! current solution.State acceptedAsCurrentSolution;//! Indicate if the new solution is already known.State alreadyKnownSolution;//! Indicate if the new solution improve the current solution.State improveCurrentSolution;//! Indicate if a local search operator has been used.State localSearchUsed;//! Indicate if solution has been improved by local search.State improveByLocalSearch;//! Indicate if the solution has already been repaired.State alreadyRepaired;//! Indicate if the new solution has already been destroyed.State alreadyDestroyed;};

02 ALNS_Parameters

该类是ALNS运行过程中的一些参数设置,和上面的ALNS_Iteration_Status差不多,主要功能集中在成员变量上,成员函数都是简单的getter(获取成员变量的接口)或者setter(设置成员变量的接口)。照例把成员变量贴出来吧~

public://! Enumeration representing the various kind of stopping criteria.//! MAX_IT: the maximum number of iterations.//! MAX_RT: the maximum run time.//! MAX_IT_NO_IMP: the maximum number of iterations without improvement.//! ALL: a mix of the MAX_IT, MAX_RT and MAX_IT_NO_IMP.enum StoppingCriteria {MAX_IT,MAX_RT,MAX_IT_NO_IMP,ALL};//! An enumeration listing a set of packaged AcceptanceModule Implementation.enum AcceptanceCriterioKind {SA};
protected://! Maximum number of iterations performed by the ALNS.size_t maxNbIterations;//! Maximum running time of the ALNS.double maxRunningTime;//! Maximum number of iterations without any improvement.size_t maxNbIterationsNoImp;//! Which stopping criterion should be used.StoppingCriteria stopCrit;//! Indicate if noise should be used.bool noise;//! Indicate after how many iterations should the scores of//! the operators be recomputed.size_t timeSegmentsIt;//! Indicate the number of iterations that should be performed//! before reinitialization of the scores of the operators.size_t nbItBeforeReinit;//! score adjustment parameter in case the last remove-insert//! operation resulted in a new global best solutionint sigma1;//! score adjustment parameter in case that the last remove-insert//! operation resulted in a solution that has not been accepted before and//! the objective value is better than the objective value of current solutionint sigma2;//! score adjustment parameter in case that the last remove-insert//! operation resulted in a solution that has not been accepted before and such//! that the score objective value is worse than the one of current solution but//! the solution was accepted.int sigma3;//! reaction factor 0 <= rho <= 1 for the update of the weights of the//! operators.double rho;//! The minimum possible weight for an operator.double minimumWeight;//! The maximum possible weight for an operator.double maximumWeight;//! Indicates the probability of using noised operators.double probabilityOfNoise;//! Kind of acceptance criterion used.AcceptanceCriterioKind acKind;//! patht to the configuration file of the acceptance criterion.std::string acPath;//! path to the file where the global stats have to be saved.std::string statsGlobPath;//! path to the file where the operators stats have to be saved.std::string statsOpPath;//! Indicate every each iteration logging is done. 不懂看后面。int logFrequency;//! A set of forbidden operators. 不懂看后面。std::vector<std::string> forbidenOperators;//! A set of forbidden local search operators. 不懂看后面。std::vector<std::string> forbidenLsOperators;//! The minimum percentage of the solution destroyed by the destroy operators.int minDestroyPerc;//! The maximum percentage of the solution destroyed by the destroy operators.int maxDestroyPerc;//! Indicate after how many iterations without improvement//! does the best known solution is reloaded.size_t reloadFrequency;//! Indicate if local search should be used.bool performLocalSearch;//! When the optimization process start, the parameters//! should not be modified. lock is set to true when the//! optimization begin. If the setter of the value//! of one parameter is called while lock is true, an//! error is raised.bool lock;};

不过有几个变量大家看了注释可能还不太明白是干嘛用的。在这里再解释一下。

logFrequency,隔多少次迭代输出一下当前的信息。直接给大家上两个图让大家心领神会一下:

  1. logFrequency = 1
    1240

  2. logFrequency = 100
    1240

懂了吧。

forbidenOperators是禁止的某些repair和destroy方法的集合,学过禁忌搜索的都知道这意味着什么,有些repair和destroy方法效果太差劲了,所以我们把它们给ban掉。

forbidenLsOperators和forbidenOperators差不多,不过它是禁止的某些LocalSearch方法的集合,效果太差嘛。。。

03 再论ALNS_Parameters

关于ALNS_Parameters它的大部分成员函数是简单的getter(获取成员变量的接口)或者setter(设置成员变量的接口)。但其CPP文件中,还有一个函数是从xml文件读取相应参数的。代码就不具体介绍了,主要是xml文件操作的一些api的使用,有现成的lib库,感兴趣的同学了解一下。

至于为什么用xml文件呢?其实直接把参数写死在程序里面也是可以的,不过读取xml文件获取相应的参数更符合标准,在实际生产中也更方便实用而已。
1240

04 小结

至此,整一个ALNS模块已经讲得差不多了,不知道大家都看懂了没有。看不懂的话可以多看几遍,很多地方也只是小编个人的理解,不一定正确,如果你觉得你有更好的想法,也可以联系小编一起讨论。

后面再出多几篇估计就差不多了。把判断接受准则讲讲,把局部搜索讲讲就差不多可以了。最后谢谢大家一路过来的支持哈。

代码及相关内容可关注公众号。更多精彩尽在微信公众号【程序猿声】
微信公众号

posted @ 2019-05-10 20:25 短短的路走走停停 阅读( ...) 评论( ...) 编辑 收藏

这篇关于代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/161495

相关文章

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

java解析jwt中的payload的用法

《java解析jwt中的payload的用法》:本文主要介绍java解析jwt中的payload的用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解析jwt中的payload1. 使用 jjwt 库步骤 1:添加依赖步骤 2:解析 JWT2. 使用 N

Python中__init__方法使用的深度解析

《Python中__init__方法使用的深度解析》在Python的面向对象编程(OOP)体系中,__init__方法如同建造房屋时的奠基仪式——它定义了对象诞生时的初始状态,下面我们就来深入了解下_... 目录一、__init__的基因图谱二、初始化过程的魔法时刻继承链中的初始化顺序self参数的奥秘默认

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思