前言

上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去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,隔多少次迭代输出一下当前的信息。直接给大家上两个图让大家心领神会一下:

  1. logFrequency = 1

  2. 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的代码解析的更多相关文章

  1. 代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析

    前言 上一篇文章中我们具体解剖了ALNS类的具体代码实现过程,不过也留下了很多大坑.接下来的文章基本都是"填坑"了,把各个模块一一展现解析给大家.不过碍于文章篇幅等原因呢,也不会每 ...

  2. 代码 | 自适应大邻域搜索系列之(7) - 局部搜索LocalSearch的代码解析

    前言 好了小伙伴们我们又见面了,咳咳没错还是我.不知道你萌接连被这么多篇代码文章刷屏是什么感受,不过,酸爽归酸爽.今天咱们依然讲代码哈~不过今天讲的依然很简单,关于局部搜索LocalSearch的代码 ...

  3. 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析

    00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...

  4. 代码 | 自适应大邻域搜索系列之(4) - Solution定义和管理的代码实现解析

    前言 上一篇讲解了destroy和repair方法的具体实现代码,好多读者都在喊酸爽和得劲儿--今天这篇就讲点简单的,关于solution的定义和管理的代码实现,让大家回回神吧--哈哈. 01 总体概 ...

  5. 代码 | 自适应大邻域搜索系列之(6) - 判断接受准则SimulatedAnnealing的代码解析

    前言 前面三篇文章对大家来说应该很简单吧?不过轻松了这么久,今天再来看点刺激的.关于判断接受准则的代码.其实,判断接受准则有很多种,效果也因代码而异.今天介绍的是模拟退火的判断接受准则.那么,相关的原 ...

  6. 自适应大邻域搜索代码系列之(1) - 使用ALNS代码框架求解TSP问题

    前言 上次出了邻域搜索的各种概念科普,尤其是LNS和ALNS的具体过程更是描述得一清二楚.不知道你萌都懂了吗?小编相信大家早就get到啦.不过有个别不愿意透露姓名的热心网友表示上次没有代码,遂不过瘾啊 ...

  7. 干货 | 自适应大邻域搜索(Adaptive Large Neighborhood Search)入门到精通超详细解析-概念篇

    01 首先来区分几个概念 关于neighborhood serach,这里有好多种衍生和变种出来的胡里花俏的算法.大家在上网搜索的过程中可能看到什么Large Neighborhood Serach, ...

  8. C#基础拾遗系列之一:先看懂IL代码

    一.前言 首先,想说说为什么要写这样系列的文章,有时候在和同事朋友聊天的时候,经常会听到这样的话题: (1)在这家公司没什么长进,代码太烂,学不到东西.(你有没有想想框架为什么这样写,代码还可以怎么去 ...

  9. 【优化算法】变邻域搜索算法(VNS)求解TSP(附C++详细代码及注释)

    00 前言 上次变邻域搜索的推文发出来以后,看过的小伙伴纷纷叫好.小编大受鼓舞,连夜赶工,总算是完成了手头上的一份关于变邻域搜索算法解TSP问题的代码.今天,就在此给大家双手奉上啦,希望大家能ENJO ...

随机推荐

  1. 17. Scala泛型、上下界、视图界定、上下文界定

    17.1 泛型的基本介绍 17.1.1 基本介绍 1) 如果我们要求函数的参数可以接受任意类型,可以使用泛型,这个类型可以代表任意的数据类型 2) 例如List,在创建List时,可以传入整型.字符串 ...

  2. 解决warning: Clock skew detected. Your build may be incomplete

    原因:机器系统时间与文件时间不一致 解决:更新所有文件的时间后重新编译 find . -type f | xargs -n 5 touch make clean make xargs  -n num ...

  3. C#使用管理员权限打开cmd执行命令行

    最近遇到个棘手的问题,服务器远程连不上,但是ftp可以,可能远程连接的服务挂了或者防火墙入站规则有点问题,想要重启,得找机房工作人员,还是挺麻烦的 想了想可以上传个执行cmd命令的东西,然后远程访问触 ...

  4. shell 学习笔记2-shell-test

    一.字符串测试表达式 前面一篇介绍:什么是shell,shell变量请参考: shell 学习笔记1-什么是shell,shell变量 1.字符串测试表达式参数 字符串需要用""引 ...

  5. pdm文件打开方式

    转自:https://blog.csdn.net/qq_36855191/article/details/79299216 pdm打开网站:http://www.dmanywhere.cn/

  6. iOS音频学习笔记二:iOS SDK中与音频有关的相关框架

      上层:       Media Player Framework: 包含MPMoviePlayerController.MPMoviePlayerViewController.MPMusicPla ...

  7. python之反射机制与callattr()、issubclass()、isinstance、type()相关

    一.反射机制 * 反射可以理解为 通过字符串的形式,动态导入模块: 利用字符串的形式,在对象(模块)中操作(查找/获取/删除/添加)成员,是一种基于字符串的事件驱动! 反射机制的内置函数 # hasa ...

  8. 并发编程.md

    操作系统基础 人机矛盾: CPU利用率低 磁带存储+批处理:降低数据的读取时间,提高CPU的利用率 多道操作系统------在一个任务遇到IO的时候主动让出CPU,给其他任务使用 由操作系统完成 切换 ...

  9. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  10. 第五周作业:markdown语法小总结

    第五周作业 markdown语法总结 早就想写这么一个文章了,关于markdown的语法,因为最近使用的比较多,所以特地总结一下 一,标题 首先要介绍的就是标题, 标题一共有六级 # h1 ## h2 ...