前言

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

01 总体概述

总所周知的是,每一个算法的最终目标都是求解出一个合理的满足心意的solution。因此对solution的定义和管理基本是每个算法都要涉及的。在本ALNS代码中呢,也对solution进行了一定的抽象和规范化,提供了一些标准化的接口,同样需要在具体使用中去重写这些接口。

关于solution的处理方式总得来说也由两个模块组成:

  • 关于solution的定义:ISolution抽象类
  • 关于bestSolution的管理:IBestSolutionManager(抽象类)、SimpleBestSolutionManager(派生类)

下面也对其一一进行讲解。

02 ISolution抽象类

该类只是对solution的进行一定的抽象定义,并没有具体实现各个接口,需要coder在后续的使用中重写编写这些接口。它应该具备的功能看代码就能理解了,注释也写得很详细。主要包括几个功能:获取目标值、获取目标惩罚值、解是否可行、获取每个solution独一无二的hash值等。

具体代码也很简单:

class ISolution
{
public:
virtual ~ISolution(){};
//! A getter for the value of the objective function.
//! \return the value of the objective function of this solution.
virtual double getObjectiveValue()=0;
//! \return a penalized version of the objective value if the solution
//! is infeasible.
virtual double getPenalizedObjectiveValue()=0;
//! A getter for the feasibility of the current solution.
//! \return true if the solution is feasible, false otherwise.
virtual bool isFeasible()=0;
//! A comparator.
//! \return true if this solution is "better" than the solution it is compared to.
virtual bool operator<(ISolution&)=0;
//! Compute the "distance" between solution.
//! This feature can be used as part of the ALNS to favor the
//! diversification process. If you do not plan to use this feature
//! just implement a method returning 0.
virtual int distance(ISolution&)=0;
//! This method create a copy of the solution.
virtual ISolution* getCopy()=0;
//! Compute a hash key of the solution.
virtual long long getHash()=0;
};

03 bestSolution的管理

关于bestSolution的管理有两个类搞定,它们的关系如下:

3.1 IBestSolutionManager

IBestSolutionManager其实也是一个抽象类,它也只是起到提供接口的作用。其中isNewBestSolution(ISolution& sol)是判断sol是不是新的最优解。reloadBestSolution(ISolution* currSol, ALNS_Iteration_Status& status)则根据需要判断是否要将已知的最优解作为当前解。

class IBestSolutionManager
{
public:
//! This method evaluate if a solution is a new best solution, and adds it to the
//! best solution pool in this case.
//! \param sol the solution to be tested.
//! \return true if the solution is a new best solution, false otherwise.
virtual bool isNewBestSolution(ISolution& sol)=0; //! Return a pointer to a best solution.
virtual std::list<ISolution*>::iterator begin()=0; //! Return a pointer to a best solution.
virtual std::list<ISolution*>::iterator end()=0; //! This function take care of reloading the best known
//! solution, as the current solution, if needed.
//! \param currSol a pointer to the current solution.
//! \param status the status of the current iteration.
//! \return a pointer to the current solution.
virtual ISolution* reloadBestSolution(ISolution* currSol, ALNS_Iteration_Status& status)=0;
};

3.2 SimpleBestSolutionManager

SimpleBestSolutionManager是继承于IBestSolutionManager的,值得注意的是,它管理的不止是一个BestSolution,而是多个BestSolution的集合(这些BestSolution目标值相同,但是结构不同)。下面是.h文件的代码:

class SimpleBestSolutionManager: public IBestSolutionManager {
public:
SimpleBestSolutionManager(ALNS_Parameters& param); virtual ~SimpleBestSolutionManager(); virtual bool isNewBestSolution(ISolution& sol); //! Return a pointer to a best solution.
std::list<ISolution*>::iterator begin(){return bestSols.begin();}; //! Return a pointer to a best solution.
std::list<ISolution*>::iterator end(){return bestSols.end();}; //! This function take care of reloading the best known
//! solution, as the current solution, if needed.
//! \param currSol a pointer to the current solution.
//! \param status the status of the current iteration.
//! \return a pointer to the current solution.
virtual ISolution* reloadBestSolution(ISolution* currSol, ALNS_Iteration_Status& status); //! Simple getter.
std::list<ISolution*>& getBestSols(){return bestSols;};
private:
std::list<ISolution*> bestSols; ALNS_Parameters* parameters; };

再回过头来看看.cpp文件的实现代码,也很简单,讲讲两个函数的实现方式就好了。isNewBestSolution(ISolution& sol)做的可不只是简单判断这么简单:

如果sol和最优解集合中的某个相同,那么返回false。

如果sol的目标值>最优解集合中的解的目标值,返回false。

如果sol的目标值<最优解集合中的某个解的目标值,那么将该最优解从集合中移除。

最后如果没有返回false,将sol加入最优解集合。

而reloadBestSolution(ISolution* currSol, ALNS_Iteration_Status& status)根据status的状态,返回最优解集合中最后一个解或者返回当前解。

bool SimpleBestSolutionManager::isNewBestSolution(ISolution& sol)
{
for(list<ISolution*>::iterator it = bestSols.begin(); it != bestSols.end(); it++)
{
ISolution& currentSol = *(*it);
if(currentSol<sol)
{
return false;
}
else if(sol<currentSol)
{
delete *it;
it = bestSols.erase(it);
if(it == bestSols.end())
{
break;
}
}
else if(currentSol.getHash() == sol.getHash())
{
return false;
}
}
ISolution* copy = sol.getCopy();
bestSols.push_back(copy);
return true;
} ISolution* SimpleBestSolutionManager::reloadBestSolution(ISolution* currSol, ALNS_Iteration_Status& status)
{
if(status.getNbIterationWithoutImprovementSinceLastReload() > 0 &&
((status.getNbIterationWithoutImprovementSinceLastReload() % parameters->getReloadFrequency()) == 0))
{
status.setNbIterationWithoutImprovementSinceLastReload(0);
delete currSol;
return bestSols.back()->getCopy();
}
else
{
return currSol;
}
}

04 小结

好了,以上就是今天的内容,是不是特别简单呢?相信在克服之前两篇文章的读者来说,后面都是一马平川、一帆风顺了。哈哈。

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

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

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

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

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

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

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

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

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

  5. 代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析

    前言 上一篇推文说了,后面的代码难度直线下降,各位小伙伴可以放去n的100次方心了.今天讲讲一些细枝末节,就是前面一直有提到的参数和一些状态的记录代码.这个简单啦,小编也不作过多解释了.大家直接看代码 ...

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

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

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

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

  8. 大数据学习系列之九---- Hive整合Spark和HBase以及相关测试

    前言 在之前的大数据学习系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive集群搭建 中介绍了集群的环境搭建,但是在使用hive进行数据查询的时候会非常的慢,因为h ...

  9. 大数据小白系列——HDFS(2)

    这里是大数据小白系列,这是本系列的第二篇,介绍一下HDFS中SecondaryNameNode.单点失败(SPOF).以及高可用(HA)等概念. 上一篇我们说到了大数据.分布式存储,以及HDFS中的一 ...

随机推荐

  1. PHP逻辑运算符中的and和&&以及or和||是有区别的

    下图是PHP的逻辑运算符: 看图中and和&&都是“与”,而or和||都是“或”,初开起来没有区别,但实际上这里面有一个优先级别的区别,即: &&和||的优先级别要高于 ...

  2. svn建立分支和svn代码合并的操作方法

    首先说下为什么我们需要用到分支-合并.比如项目demo下有两个小组,svn下有一个trunk版.由于客户需求突然变化,导致项目需要做较大改动,此时项目组决定由小组1继续完成原来正进行到一半的工作[某个 ...

  3. myeclipse 快捷键,从步骤开始的大括号定位到匹配方法结束的大括号

    myeclipse 快捷键,从方法开始的大括号定位到匹配方法结束的大括号转至匹配的括号 Ctrl+Shift+P ctr+shift+r   文件名搜索文件 ctr+h           搜索文件里 ...

  4. VueX-状态管理器

    一.VueX功能与解决的问题 1.中央状态管理器的功能: 1.1.可以管理共享状态1.2.提供一 个可修改状态的方法1.3.提供状态获取的方法1.4.状态更改后,有通知机制 2.中央状态管理器解决的问 ...

  5. CSS实现图片阴影效果

    <title>无标题文档</title> <style type="text/css"> /*方法一:使用一个GIF文件的方法*/ .gifsh ...

  6. Centos 7 搭建wordpress

    1.安装mysql 详情见:http://www.cnblogs.com/jw35/p/6044170.html 2.关闭firewalld与selinux systemctl stop firewa ...

  7. IKAnalyzer兼容Lucene 5.4.0版本抛出异常?

    ava.lang.AbstractMethodError: org.apache.lucene.analysis.Analyzer.createComponents(Ljava/lang/String ...

  8. centos 7.6 开机报错信息(一):welcome to emergency mode!

    welcome to emergency mode!after logging in ,type "journalctl -xb" to view system logs,&quo ...

  9. [LeetCode 题解]: Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  10. Solr相似度算法一:DefaultSimilarity(基于TF-IDF的默认相似度算法)

    默认的similarity是基于TF/IDF 模块. 该 similarity有以下配置选项: discount_overlaps –确定是否重叠的标识(标记位置增量为0)都将被忽略在正常计算的时候. ...