代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析
前言
上一篇文章中我们具体解剖了ALNS类的具体代码实现过程,不过也留下了很多大坑。接下来的文章基本都是“填坑”了,把各个模块一一展现解析给大家。不过碍于文章篇幅等原因呢,也不会每一行代码都进行讲解,那些简单的代码就跳过了,相信大家也能一眼就看懂。好了,废话不多说,开始干活吧。
01 照旧总体概述
前面我们提到,ALNS中的重中之重就是Destroy和Repair方法了,在整一个ALNS框架中呢,涉及这俩货的有Destroy和Repair方法的具体实现、Destroy和Repair方法管理(包括各个Destroy和Repair方法权重分配、成绩打分、按权重选择哪个Destroy和Repair方法等操作)。所以在这次的ALNS代码中呢,这俩货的代码实现呢也分为两个模块:
- Destroy和Repair方法具体实现模块
- Destroy和Repair方法管理模块
下面我们将对其进行一一讲解,不知道大家小板凳准备好了没有。
02 Destroy和Repair方法具体实现
关于Destroy和Repair方法,由三个类组成,分别是AOperator、ADestroyOperator、ARepairOperator。它们之间的继承派生关系如下:

下面对其一一讲解。
2.1 AOperator
这是一个基础父类,它抽象了Destroy和Repair方法共有的一些方法和特征(成绩、权重、名称等等),然后Destroy和Repair方法再各自继承于它,实现自己的功能模块。成员变量已经注释清楚了,关于protected的一个成员noise噪声模式会在后面讲到。其他的方法也很简单就不做多解释了。
class AOperator
{
private:
//! Total number of calls during the process.
size_t totalNumberOfCalls;
//! Number of calls since the last evaluation.
size_t nbCallsSinceLastEval;
//! score of the operator.
double score;
//! weight of the operator.
double weight;
//! designation of the operator.
std::string operatorName;
protected:
//! Indicate if the operator is used in noise mode or not.
bool noise;
public:
//! Constructor.
AOperator(std::string name){
operatorName = name;
init();
}
//! Destructor.
virtual ~AOperator(){};
//! Initialize the values of the numbers of calls.
void init()
{
totalNumberOfCalls = 0;
nbCallsSinceLastEval = 0;
score = 0;
weight = 1;
}
//! reset the number of calls since last eval.
void resetNumberOfCalls()
{
nbCallsSinceLastEval = 0;
}
//! Simple getter.
//! \return the total number of calls to the operator since
//! the beginning of the optimization process.
size_t getTotalNumberOfCalls(){return totalNumberOfCalls;};
//! Simple getter.
//! \return the number of calls to this operator since the last
//! evaluation.
size_t getNumberOfCallsSinceLastEvaluation(){return nbCallsSinceLastEval;};
void increaseNumberOfCalls()
{
totalNumberOfCalls++;
nbCallsSinceLastEval++;
}
//! Simple getter.
double getScore() const
{
return score;
}
//! Simple getter.
double getWeight() const
{
return weight;
}
//! resetter.
void resetScore()
{
this->score = 0;
}
//! Simple setter.
void setScore(double s)
{
this->score = s;
}
//! Simple setter.
void setWeight(double weight)
{
this->weight = weight;
}
//! Simple getter.
std::string getName(){return operatorName;};
//! Set noise to true.
void setNoise(){noise=true;};
//! Set noise to false.
void unsetNoise(){noise=false;};
};
2.2 ADestroyOperator
该类主要是继承于上面的AOperator类,然后再此基础上加上Destroy操作的具体实现。它是一个抽象类,需要在后续的应用中重写Destroy操作的方法。
class ADestroyOperator : public AOperator {
protected:
//! The minimum destroy size used.
size_t minimunDestroy;
//! The maximum destroy size used.
size_t maximumDestroy;
public:
//! Constructor.
//! \param mini the minimum destroy size.
//! \param maxi the maximum destroy size.
//! \param s the name of the destroy operator.
ADestroyOperator(size_t mini, size_t maxi, std::string s) : AOperator(s)
{
minimunDestroy = mini;
maximumDestroy = maxi;
}
//! Destructor.
virtual ~ADestroyOperator(){};
//! This function is the one called to destroy a solution.
//! \param sol the solution to be destroyed.
virtual void destroySolution(ISolution& sol)=0;
};
2.3 ARepairOperator
同理,也是由AOperator类派生出来并加上Repair自己的实现方法的类。也是一个抽象类,需要在后续的使用中重写Repair方法。
class ARepairOperator : public AOperator {
public:
ARepairOperator(std::string s) : AOperator(s)
{
}
virtual ~ARepairOperator(){};
virtual void repairSolution(ISolution& sol)=0;
};
03 Destroy和Repair方法管理
对Destroy和Repair方法进行管理的由两个类来实现:AOperatorManager、OperatorManager。其中AOperatorManager是抽象类,只提供接口,OperatorManager继承于AOperatorManager。并对其接口进行实现。
3.1 AOperatorManager
该类抽象了OperatorManager的一些特征,只提供接口。因此成员函数都是纯虚函数。相关方法的说明已经注释在代码里面了。关于保护成员:stats用于保存算法迭代过程中的一些状态量,这个类后续也会讲解的。
class AOperatorManager
{
public:
//! This method selects a destroy operator.
//! \return a destroy operator.
virtual ADestroyOperator& selectDestroyOperator()=0;
//! This method selects a repair operator.
//! \return a repair operator.
virtual ARepairOperator& selectRepairOperator()=0;
virtual void recomputeWeights()=0;
//! Update the scores of the operators.
virtual void updateScores(ADestroyOperator& des, ARepairOperator& rep, ALNS_Iteration_Status& status)=0;
//! Indicate that the optimization process starts.
virtual void startSignal()=0;
//! Destroy the operators registered to this operator manager.
virtual void end()=0;
//! Simple setter.
void setStatistics(Statistics* statistics){stats = statistics;};
protected:
//! A pointer to the instance of the statistics module.
Statistics* stats;
};
3.2 OperatorManager
该类在AOperatorManager基础上也添加了一些自己额外的成员变量和函数方法。具体还是看代码理解吧,挺简单的,没有需要多解释的,我在这多少无益。
class OperatorManager: public AOperatorManager {
private:
//! The set of repair operators.
std::vector<AOperator*> repairOperators;
//! The set of destroy operators.
std::vector<AOperator*> destroyOperators;
//! The sum of the weights of the repair operators.
double sumWeightsRepair;
//! The sum of the weights of the destroy operators.
double sumWeightsDestroy;
//! The paramaters to be used by the ALNS.
ALNS_Parameters* parameters;
//! Indicate whether or not the next operators to be return
//! should be noised or not.
bool noise;
//! A counter that indicates the number of times repair operators with noise have been successfull
double performanceRepairOperatorsWithNoise;
//! A counter that indicates the number of times repair operators without noise have been successfull
double performanceRepairOperatorsWithoutNoise;
//! Use a roulette wheel to select an operator in a vector of operators.
//! \return the selected operator.
AOperator& selectOperator(std::vector<AOperator*>& vecOp, double sumW);
//! Recompute the weight of an operator.
void recomputeWeight(AOperator& op, double& sumW);
public:
//! Constructor
//! \param param the parameters to be used.
OperatorManager(ALNS_Parameters& param);
//! Destructor.
virtual ~OperatorManager();
//! This function recompute the weights of every operator managed by this
//! manager.
void recomputeWeights();
//! This method selects a destroy operator.
//! \return a destroy operator.
ADestroyOperator& selectDestroyOperator();
//! This method selects a repair operator.
//! \return a repair operator.
ARepairOperator& selectRepairOperator();
//! This method adds a repair operator to the list
//! of repair operator managed by this manager.
//! \param repairOperator the repair operator to be added.
void addRepairOperator(ARepairOperator& repairOperator);
//! This method adds a destroy operator to the list
//! of destroy operator managed by this manager.
//! \param destroyOperator the destroy operator to be added.
void addDestroyOperator(ADestroyOperator& destroyOperator);
//! This method run some sanity checks to ensure that it is possible
//! to "safely" use this manager within the ALNS.
void sanityChecks();
//! Update the scores of the operators.
virtual void updateScores(ADestroyOperator& des, ARepairOperator& rep, ALNS_Iteration_Status& status);
//! Indicate that the optimization process starts.
virtual void startSignal();
//! Destroy all the operators registered to this operator.
void end();
};
上面是该类的.h文件,关于其中某些函数方法的实现,小编下面挑一些来重点给大家讲讲,那些以小编的脑瓜子都能理解的代码就省略了,大家应该都能懂……
3.3 OperatorManager具体实现
又到了一大波代码时间,来吧来吧,小板凳准备好,要开始啦~
3.3.1 OperatorManager::recomputeWeight(...)
重新计算单个操作的权重。其有两个参数AOperator& op, double& sumW,其中 op是要重新计算权重的repair或者destroy方法,sumW是其对应集合的权重总和。
这里只讲一个新权重的计算方式就行:

其中:
Rho为设定的[0, 1]之间的参数,PrevWeight表示旧的权重,nbCalls表示在上一次自上一次更新完权重到现在该方法被调用的次数,timeSegmentsIt表示迭代多少次需要重新计算一下权重的迭代次数,currentScore表示旧的成绩。理解了这些就很easy了。
void OperatorManager::recomputeWeight(AOperator& op, double& sumW)
{
double prevWeight = op.getWeight();
sumW -= prevWeight;
double currentScore = op.getScore();
size_t nbCalls = op.getNumberOfCallsSinceLastEvaluation();
double newWeight = (1-parameters->getRho())*prevWeight + parameters->getRho()*(static_cast<double>(nbCalls)/static_cast<double>(parameters->getTimeSegmentsIt()))*currentScore;
// We ensure that the weight is within the bounds.
if(newWeight > parameters->getMaximumWeight())
{
newWeight = parameters->getMaximumWeight();
}
if(newWeight < parameters->getMinimumWeight())
{
newWeight = parameters->getMinimumWeight();
}
sumW += newWeight;
op.setWeight(newWeight);
op.resetScore();
op.resetNumberOfCalls();
}
值得注意的是还有一个OperatorManager::recomputeWeights()成员函数是用于重新计算repair或者destroy方法集合的,它的实现主要也还是调用OperatorManager::recomputeWeight(AOperator& op, double& sumW)方法来实现的。
3.3.2 OperatorManager::selectOperator(...)
相信了解过遗传算法轮盘赌实现过程的小伙伴对这里都不会陌生,当然,并不是说权重大的方法一定会被选中,只是被选中的可能性会大而已。具体过程是先生成一个在0到sumWeight之间的中间权重randomWeightPos ,然后从第一个方法开始用变量cumulSum进行权重累加,直到cumulSum>=randomWeightPos 为止,那么停止累加时最后这个方法就是幸运儿了。
AOperator& OperatorManager::selectOperator(std::vector<AOperator*>& vecOp, double sumW)
{
double randomVal = static_cast<double>(rand())/static_cast<double>(RAND_MAX);
double randomWeightPos = randomVal*sumW;
double cumulSum = 0;
for(size_t i = 0; i < vecOp.size(); i++)
{
cumulSum += vecOp[i]->getWeight();
if(cumulSum >= randomWeightPos)
{
if(noise)
{
vecOp[i]->setNoise();
}
else
{
vecOp[i]->unsetNoise();
}
vecOp[i]->increaseNumberOfCalls();
return *(vecOp[i]);
}
}
assert(false);
return *(vecOp.back());
}
3.3.3 OperatorManager::updateScores(...)
该成员函数用来更新各个Destroy和Repair方法的成绩。参数是Destroy和Repair方法的集合,以及ALNS迭代过程中的各种状态信息。便于说明下面用rScore和dScore分别代表Repair和Destroy方法的成绩。具体实现如下:
- 如果找到新的最优解,rScore+=Sigma1,dScore+=Sigma1。其中Sigma1是设定参数。
- 如果当前解得到改进,rScore+=Sigma2,dScore+=Sigma2。其中Sigma2是设定参数。
- 如果当前解没有得到改进 and 当前解是之前没有出现过的 and 当前解被接受作为新的解了,rScore+=Sigma3,dScore+=Sigma3。其中Sigma3是设定参数。
void OperatorManager::updateScores(ADestroyOperator& des, ARepairOperator& rep, ALNS_Iteration_Status& status)
{
if(status.getNewBestSolution() == ALNS_Iteration_Status::TRUE)
{
rep.setScore(rep.getScore()+parameters->getSigma1());
des.setScore(des.getScore()+parameters->getSigma1());
performanceRepairOperatorsWithNoise += 1;
performanceRepairOperatorsWithoutNoise += 1;
}
if(status.getImproveCurrentSolution() == ALNS_Iteration_Status::TRUE)
{
rep.setScore(rep.getScore()+parameters->getSigma2());
des.setScore(des.getScore()+parameters->getSigma2());
performanceRepairOperatorsWithNoise += 1;
performanceRepairOperatorsWithoutNoise += 1;
}
if(status.getImproveCurrentSolution() == ALNS_Iteration_Status::FALSE
&& status.getAcceptedAsCurrentSolution() == ALNS_Iteration_Status::TRUE
&& status.getAlreadyKnownSolution() == ALNS_Iteration_Status::FALSE)
{
rep.setScore(rep.getScore()+parameters->getSigma3());
des.setScore(des.getScore()+parameters->getSigma3());
performanceRepairOperatorsWithNoise += 1;
performanceRepairOperatorsWithoutNoise += 1;
}
/* OLD VERSION */
/*
if(parameters->getNoise())
{
double randNoise = static_cast<double>(rand())/RAND_MAX;
noise = (randNoise<parameters->getProbabilityOfNoise());
}
*/
/* NEW VERSION */
if(parameters->getNoise())
{
double performanceRepairOperatorsGlobal = 0;
performanceRepairOperatorsGlobal += performanceRepairOperatorsWithNoise;
performanceRepairOperatorsGlobal += performanceRepairOperatorsWithoutNoise;
double randomVal = static_cast<double>(rand())/RAND_MAX;
double randomWeightPos = randomVal*performanceRepairOperatorsGlobal;
noise = (randomWeightPos < performanceRepairOperatorsGlobal);
}
}
至此,差不过难点都讲完了,不知道你萌都understand了吗?
04 小结
好了,以上就是今天的代码内容,别急着走开哦,后面还有好多好多的内容没讲呢。
这是个天坑,希望大家和小编一起努力,共同填完它。哈哈,谢谢各位的至此。
代码 | 自适应大邻域搜索系列之(3) - Destroy和Repair方法代码实现解析的更多相关文章
- 代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析
前言 上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去n的100次方心了.今天讲讲一些细枝末节,就是前面一直有提到的参数和一些状态的记录代码.这个简单啦,小编也不作过多解释了.大家直接看代码 ...
- 代码 | 自适应大邻域搜索系列之(7) - 局部搜索LocalSearch的代码解析
前言 好了小伙伴们我们又见面了,咳咳没错还是我.不知道你萌接连被这么多篇代码文章刷屏是什么感受,不过,酸爽归酸爽.今天咱们依然讲代码哈~不过今天讲的依然很简单,关于局部搜索LocalSearch的代码 ...
- 代码 | 自适应大邻域搜索系列之(2) - ALNS算法主逻辑结构解析
00 前言 在上一篇推文中,教大家利用了ALNS的lib库求解了一个TSP问题作为实例.不知道你萌把代码跑起来了没有.那么,今天咱们再接再厉.跑完代码以后,小编再给大家深入讲解具体的代码内容.大家快去 ...
- 代码 | 自适应大邻域搜索系列之(4) - Solution定义和管理的代码实现解析
前言 上一篇讲解了destroy和repair方法的具体实现代码,好多读者都在喊酸爽和得劲儿--今天这篇就讲点简单的,关于solution的定义和管理的代码实现,让大家回回神吧--哈哈. 01 总体概 ...
- 代码 | 自适应大邻域搜索系列之(6) - 判断接受准则SimulatedAnnealing的代码解析
前言 前面三篇文章对大家来说应该很简单吧?不过轻松了这么久,今天再来看点刺激的.关于判断接受准则的代码.其实,判断接受准则有很多种,效果也因代码而异.今天介绍的是模拟退火的判断接受准则.那么,相关的原 ...
- 干货 | 自适应大邻域搜索(Adaptive Large Neighborhood Search)入门到精通超详细解析-概念篇
01 首先来区分几个概念 关于neighborhood serach,这里有好多种衍生和变种出来的胡里花俏的算法.大家在上网搜索的过程中可能看到什么Large Neighborhood Serach, ...
- 自适应大邻域搜索代码系列之(1) - 使用ALNS代码框架求解TSP问题
前言 上次出了邻域搜索的各种概念科普,尤其是LNS和ALNS的具体过程更是描述得一清二楚.不知道你萌都懂了吗?小编相信大家早就get到啦.不过有个别不愿意透露姓名的热心网友表示上次没有代码,遂不过瘾啊 ...
- C#基础拾遗系列之一:先看懂IL代码
一.前言 首先,想说说为什么要写这样系列的文章,有时候在和同事朋友聊天的时候,经常会听到这样的话题: (1)在这家公司没什么长进,代码太烂,学不到东西.(你有没有想想框架为什么这样写,代码还可以怎么去 ...
- 【优化算法】变邻域搜索算法(VNS)求解TSP(附C++详细代码及注释)
00 前言 上次变邻域搜索的推文发出来以后,看过的小伙伴纷纷叫好.小编大受鼓舞,连夜赶工,总算是完成了手头上的一份关于变邻域搜索算法解TSP问题的代码.今天,就在此给大家双手奉上啦,希望大家能ENJO ...
随机推荐
- MySQL学习3---事务
MySQL 事务 MySQL 事务主要用于处理操作量大,复杂度高的数据. 在 MySQL 中只有使用了 Innodb 数据库引擎的数据库或表才支持事务. 事务处理可以用来维护数据库的完整性,保证成批的 ...
- [leetcode]273. Integer to English Words 整数转英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- CentOS 6.5网络配置静态IP地址
打开VMvare,并进入虚拟机 2 输入用户名,按回车键,再输入密码,登录系统 3 进行网络配置前,需要确认几个事情: 1. 网络适配器模式是否为NAT模式 右键虚拟机,或者点击VMvare菜单栏中的 ...
- 数据库查询返回Resource id #9后的处理方式
如果在调用PHP查询数据库,在echo后返回的是Resource id #9,可能你的输出方式是: $sql="SELECT * FROM dbname WHERE id='1'" ...
- 关闭Found duplicated code
IDEA中的这个“发现重复代码 - Found duplicated code“的这个提示甚是烦躁. Settings —> Editor —> Inspections —> Gen ...
- apicloud 和 微信小程序,你会用哪 个?
微信 小程序开始火了,app跨平台的革命再次高涨,不得不说,不用再担心android和ios双版本开发成本,及h5的开发 和apicloud一样,不需要关注平台问题,只需要关注前端js.css就能大a ...
- java并发编程实战:第十五章----原子变量与非阻塞机制
非阻塞算法:使用底层的原子机器指令(例如比较并交换指令)代替锁来确保数据在并发访问中的一致性 应用于在操作系统和JVM中实现线程 / 进程调度机制.垃圾回收机制以及锁和其他并发数据结构 可伸缩性和活跃 ...
- oracle内部结构
数据库管理系统将数据存储在磁盘.磁带以及其他的裸设备上,虽然这些设备的访问速度相比内存慢很多,但其非易失性和大容量的特点使他们成为数据存储的不二之选. 本文主要讨论大型数据库产品的磁盘存储内部结构,这 ...
- 使用 log4j 打印日志
开发阶段:发现程序的问题,排错 产品阶段:记录程序运行的状况 Maven中配置依赖 通过配置文件输出日志的格式,输送的位置等 一.入门实例 1.新建一个JAva工程,导入包log4j-1.2.17.j ...
- 20145233计算机病毒实践九之IDA的使用
20145233计算机病毒实践之IDA的使用 PSLIST导出函数做了什么 这个函数是一个export函数,所以在view中选择export 查到后,双击打开这个函数的位置 仔细看这个函数可以发现这个 ...