前言

上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去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. 【优先队列】Function

    Function 题目描述 wls有n个二次函数Fi(x)=aix2+bix+ci(1≤i≤n).现在他想在且x为正整数的条件下求的最小值.请求出这个最小值. 输入 第一行两个正整数n,m.下面n行, ...

  2. Scratch(一)为什么你要学Scratch儿童编程

    因为人工智能和机器人学科的崛起,似乎一夜之间未来就变成了程序员的天下,尤其是在知乎上,不会编程都没办法和这群程序员好好说话了.我已经搬了一辈子砖了,难道我的孩子也还要接着搬?这就是现在大部分家长的焦虑 ...

  3. SQLSERVER查询存储过程内容

    --使用语句查看一个存储过程的定义 EXEC sp_helptext 'Auth_BankCardAuthorize' --查询所有存储过程的名称以及定义 SELECT name, definitio ...

  4. Java 之 字节输入流 [InputStream]

    一.字节输入流 java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中. 它定义了字节输入流的基本共性功能方法. 共性方法: public void c ...

  5. JavaScript CryptoJS库 加密与解密

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. cocos creator按钮点击按钮弹起效果设置方法

    如图所示: 只要设置下button的Transition的属性为Scale即可,参数自己调整下.

  7. Linux/Aix日常报错整理

    [root@localhost ~]# umount /mnt umount.nfs: /mnt: device is busy umount.nfs: /mnt: device is busy 问题 ...

  8. python之第一对象,函数名的应用,闭包

    一.第一对象 在 Python 中万物皆为对象,函数也不例外,函数作为对象可以赋值给一个变量.可以作为元素添加到集合对象中.可作为参数值传递给其它函数, 还可以当做函数的返回值,这些特性就是第一类对象 ...

  9. Socket的一些疑惑整理

    关于listen的问题请看steven<tcp/ip详解1>18章18.11.4 呼入连接请求队列一节,说的很清楚

  10. CQRS项目

    CQRS+ES项目解析-Diary.CQRS   在<当我们在讨论CQRS时,我们在讨论些神马>中,我们讨论了当使用CQRS的过程中,需要关心的一些问题.其中与CQRS关联最为紧密的模式莫 ...