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

00 前言

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

01 总体概述

前排高能预警,在下面的讲解中,会涉及很多C++语言的知识,特别是类与派生这一块的内容,如果C++基础比较薄弱的同学则需要回去(洗洗睡)再好好补一补啦,在这里小编就不再过多科普基础知识了。默认大家都是C++大佬,能一口说出虚函数表是什么的内种……

描述整一个ALNS算法逻辑过程的是一个叫ALNS的C++类。下面对其成员变量和成员函数讲解一下。

1.1 成员变量

//!   当前解。
ISolution* currentSolution;

//! 判断接受准则。
IAcceptanceModule* acceptanceCriterion;

//! ALNS算法运行的相关参数。
ALNS_Parameters* param;

//! destroy和repair方法的管理者。
AOperatorManager* opManager;

//! 最优解的管理者。
IBestSolutionManager* bestSolManager;

//! 局部搜索的管理者。
ILocalSearchManager* lsManager;

//! 自上次重新计算重新的权重以来的迭代次数。
size_t nbIterationsWC;

//! 当前迭代次数。
size_t nbIterations;

//! The current number of iterations without improvement.
size_t nbIterationsWithoutImprovement;

//! The number of iterations without improvement of the current solution.
size_t nbIterationsWithoutImprovementCurrent;

//! The number of iterations without acceptation of a transition.
size_t nbIterationsWithoutTransition;

//! The number of iterations since the last call to a local search
//! operator.
size_t nbIterationsWithoutLocalSearch;

//! 求解的总时间。
clock_t startingTime;

//! 最优解的下界。
double lowerBound;

//! A set containing the hash keys of the encountred solutions.
std::set<long long> knownKeys;

//! 用于计算求解过程的一些状态量。
Statistics stats;

//! 最近一次迭代的状态。
ALNS_Iteration_Status status;

//! 每次迭代完成后需要更新的对象。
std::vector<IUpdatable*> updatableStructures;

//! ALNS实例的名字。
std::string name;

上面的成员变量类型用的都是抽象类的指针,因为在实际写代码的过程中,coder们肯定还要对solution、localsearch等类进行继承和派生,接口重写等。用抽象类的指针好处就是在于当它指向子类对象时也能正确调用。再说明一点,上面的ISolution啊IAcceptanceModule等都是一些抽象类的类型,以后会进行介绍和讲解的,在这里大家知道它代表什么就行了。

1.2 成员函数

//! Constructor.
//! \param name the name of the instance.
//! \param initialSolution the starting solution that is going to be optimized.
//! \param acceptanceCrit the module that determine whether or not a new solution
//! is accepted as the current solution.
//! \param parameters the set of parameters to be use by the ALNS.
//! \param opMan an operator manager.
ALNS(std::string instanceName,
	ISolution& initialSolution,
	IAcceptanceModule& acceptanceCrit,
	 ALNS_Parameters& parameters,
	 AOperatorManager& opMan,
	 IBestSolutionManager& solMan,
	 ILocalSearchManager& lsMan);

//! Destructor.
virtual ~ALNS();

//! This method launch the solving process.
//! \return true if a feasible solution is found,
//! false otherwise.
bool solve();

//! This method seeks if a solution is already known,
//! if not it is added to the set of known solutions.
//! \param sol the so
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值