代码 | 自适应大邻域搜索系列之(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 BKS
size_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 solution
int 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 solution
int 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,隔多少次迭代输出一下当前的信息。直接给大家上两个图让大家心领神会一下:
logFrequency = 1

logFrequency = 100

懂了吧。
forbidenOperators是禁止的某些repair和destroy方法的集合,学过禁忌搜索的都知道这意味着什么,有些repair和destroy方法效果太差劲了,所以我们把它们给ban掉。
forbidenLsOperators和forbidenOperators差不多,不过它是禁止的某些LocalSearch方法的集合,效果太差嘛。。。
03 再论ALNS_Parameters
关于ALNS_Parameters它的大部分成员函数是简单的getter(获取成员变量的接口)或者setter(设置成员变量的接口)。但其CPP文件中,还有一个函数是从xml文件读取相应参数的。代码就不具体介绍了,主要是xml文件操作的一些api的使用,有现成的lib库,感兴趣的同学了解一下。
至于为什么用xml文件呢?其实直接把参数写死在程序里面也是可以的,不过读取xml文件获取相应的参数更符合标准,在实际生产中也更方便实用而已。

04 小结
至此,整一个ALNS模块已经讲得差不多了,不知道大家都看懂了没有。看不懂的话可以多看几遍,很多地方也只是小编个人的理解,不一定正确,如果你觉得你有更好的想法,也可以联系小编一起讨论。
后面再出多几篇估计就差不多了。把判断接受准则讲讲,把局部搜索讲讲就差不多可以了。最后谢谢大家一路过来的支持哈。
代码及相关内容可关注公众号。更多精彩尽在微信公众号【程序猿声】

代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析的更多相关文章
- 代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析
前言 上一篇文章中我们具体解剖了ALNS类的具体代码实现过程,不过也留下了很多大坑.接下来的文章基本都是"填坑"了,把各个模块一一展现解析给大家.不过碍于文章篇幅等原因呢,也不会每 ...
- 代码 | 自适应大邻域搜索系列之(7) - 局部搜索LocalSearch的代码解析
前言 好了小伙伴们我们又见面了,咳咳没错还是我.不知道你萌接连被这么多篇代码文章刷屏是什么感受,不过,酸爽归酸爽.今天咱们依然讲代码哈~不过今天讲的依然很简单,关于局部搜索LocalSearch的代码 ...
- 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析
00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...
- 代码 | 自适应大邻域搜索系列之(4) - Solution定义和管理的代码实现解析
前言 上一篇讲解了destroy和repair方法的具体实现代码,好多读者都在喊酸爽和得劲儿--今天这篇就讲点简单的,关于solution的定义和管理的代码实现,让大家回回神吧--哈哈. 01 总体概 ...
- 代码 | 自适应大邻域搜索系列之(6) - 判断接受准则SimulatedAnnealing的代码解析
前言 前面三篇文章对大家来说应该很简单吧?不过轻松了这么久,今天再来看点刺激的.关于判断接受准则的代码.其实,判断接受准则有很多种,效果也因代码而异.今天介绍的是模拟退火的判断接受准则.那么,相关的原 ...
- 自适应大邻域搜索代码系列之(1) - 使用ALNS代码框架求解TSP问题
前言 上次出了邻域搜索的各种概念科普,尤其是LNS和ALNS的具体过程更是描述得一清二楚.不知道你萌都懂了吗?小编相信大家早就get到啦.不过有个别不愿意透露姓名的热心网友表示上次没有代码,遂不过瘾啊 ...
- 干货 | 自适应大邻域搜索(Adaptive Large Neighborhood Search)入门到精通超详细解析-概念篇
01 首先来区分几个概念 关于neighborhood serach,这里有好多种衍生和变种出来的胡里花俏的算法.大家在上网搜索的过程中可能看到什么Large Neighborhood Serach, ...
- C#基础拾遗系列之一:先看懂IL代码
一.前言 首先,想说说为什么要写这样系列的文章,有时候在和同事朋友聊天的时候,经常会听到这样的话题: (1)在这家公司没什么长进,代码太烂,学不到东西.(你有没有想想框架为什么这样写,代码还可以怎么去 ...
- 【优化算法】变邻域搜索算法(VNS)求解TSP(附C++详细代码及注释)
00 前言 上次变邻域搜索的推文发出来以后,看过的小伙伴纷纷叫好.小编大受鼓舞,连夜赶工,总算是完成了手头上的一份关于变邻域搜索算法解TSP问题的代码.今天,就在此给大家双手奉上啦,希望大家能ENJO ...
随机推荐
- PHP Math常量
常量名 常量名 常量值 PHP M_E e 2.7182818284590452354 4 M_EULER Euler 常量 0.57721566490153286061 5.2.0 M_LNPI l ...
- 在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几)
原文:在论坛中出现的比较难的sql问题:18(字符合并 整数解析星期几) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得 ...
- VC 中的ATL ActiveX 和 MFC ActiveX 有什么区别
原文转自 https://www.cnblogs.com/zhwl/archive/2012/11/29/2794509.html ATL是ActiveXTemplateLibrary的缩写,它是一套 ...
- SVN_04建库
示范加入一个代码库[Repository] [1]点击Repository右键,创建一个新库 (常规FSFS存储库) [2]在下面所看到的文本框中输入库名称 只创建空的库 创建完库后,没有任何内容在里 ...
- Go 编译 && 工具
编译和工具链 Go 的工具链非常丰富,从获取源码.编译.文档.测试.性能分析,到源码格式化.源码提示.重构工具等应有尽有 在 Go 中可以使用测试框架编写单元测试,使用统一的命令行即可测试及输出测试报 ...
- Map和TreeMap的特点
Map的特点: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 TreeMap的特点: 无序,不允许重复(无序指元素顺序与添加顺序不一致) TreeMap集合默认会对键进行排 ...
- oracle:sql添加合计语句(union)
转自:https://blog.csdn.net/qq_43563538/article/details/90370925 原表见下图: 查询全部的语句: SELECT * from BD_MARK_ ...
- [LeetCode] 76. 最小覆盖子串 ☆☆☆☆☆(滑动窗口)
https://leetcode-cn.com/problems/minimum-window-substring/solution/hua-dong-chuang-kou-suan-fa-tong- ...
- 【SpringMVC】参数绑定
一.概述 1.3 参数绑定过程 1.2 @RequestParam 二.自定义绑定使用属性编辑器 2.1 使用WebDataBinder(了解) 2.2 使用WebBindingInitializer ...
- CentOS7.X版本系统的下载和安装
一.下载CentOS镜像 1.打开浏览器输入centos.org 2.选择版本下载 3.进入下载页面选择下载版本的种子链接,在迅雷下载即可. 二安装CentOS系统 1.服务器开机,根据界面提示进入磁 ...