前言

上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去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. drbd+nfs+keepalived

    写的很详细 理论知识: https://www.cnblogs.com/kevingrace/p/5740940.html 写的很详细 负载: https://www.cnblogs.com/kevi ...

  2. 【转载】 C#中使用Count方法获取List集合中符合条件的个数

    很多时候操作List集合的过程中,我们需要根据特定的查询条件,获取List集合中有多少个实体对象符合查询条件,例如一批产品的对象List集合,如果这批产品的不合格数量大于10则重点备注.在C#中可以自 ...

  3. vue-resource发送请求

    <!DOCTYPE html> <html> <head> <title>vue-resource</title> <meta cha ...

  4. JavaScript内置一些方法的实现原理--new关键字,call/apply/bind方法--前戏

    new关键字,call/apply/bind方法都和this的绑定有关,在学习之前,首先要理解this. 一起来学习一下this吧 首先.this是一个对象. 对象很好理解,引用类型值,可以实现如th ...

  5. Java 之 打印流

    打印流 1.概述 平常在控制台打印输出,是调用 print 方法和 println 方法完成的,这两个方法都来自于 java.io.PrintStream 类,该类能够方便地打印各种数据类型的值,是一 ...

  6. 2019年6月车型数据Access数据库+缩略图 更新于2019年6月5日.

    工作需要才来采集的, 数据来源某卡汽车网, 分享出来给需要的人吧, 本着分享的精神, 我就不猥琐的放到csdn下载了 本来是sql server的, 我导出到access了, 也方便大家查看. 顺手抓 ...

  7. REST,以及RESTful的讲解

    详见:https://blog.csdn.net/qq_21383435/article/details/80032375 1.传统下的API接口对比规则概念REST 系统的特征演化优点&缺点 ...

  8. LNMP环境搭建之编译安装指南(php-5.3.27.tar.gz)

    测试环境:CentOS release 6.5 (Final) 软件安装:nginx   mysql-5.5.32-linux2.6-x86_64.tar.gz   php-5.3.27.tar.gz ...

  9. Python 一些内置函数的总结~~~~

    1. type() 两种用法 a. 当传入参数为一个时,返回值为参数的类型 b. 当传入参数为三个时,type(name, bases, dict) name: 类名 bases: 继承父类的元组,可 ...

  10. L ==> E · L · K

    三剑客:Elastic Stack 在学习ELK前,先对 Lucene作基本了解. 今天才知道关系型数据库的索引是 B-Tree,罪过... 减少磁盘寻道次数 ---> 提高查询性能 Lucen ...