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

本文主要是介绍代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析...,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

前言

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

  2. logFrequency = 100
    1240

懂了吧。

forbidenOperators是禁止的某些repair和destroy方法的集合,学过禁忌搜索的都知道这意味着什么,有些repair和destroy方法效果太差劲了,所以我们把它们给ban掉。

forbidenLsOperators和forbidenOperators差不多,不过它是禁止的某些LocalSearch方法的集合,效果太差嘛。。。

03 再论ALNS_Parameters

关于ALNS_Parameters它的大部分成员函数是简单的getter(获取成员变量的接口)或者setter(设置成员变量的接口)。但其CPP文件中,还有一个函数是从xml文件读取相应参数的。代码就不具体介绍了,主要是xml文件操作的一些api的使用,有现成的lib库,感兴趣的同学了解一下。

至于为什么用xml文件呢?其实直接把参数写死在程序里面也是可以的,不过读取xml文件获取相应的参数更符合标准,在实际生产中也更方便实用而已。
1240

04 小结

至此,整一个ALNS模块已经讲得差不多了,不知道大家都看懂了没有。看不懂的话可以多看几遍,很多地方也只是小编个人的理解,不一定正确,如果你觉得你有更好的想法,也可以联系小编一起讨论。

后面再出多几篇估计就差不多了。把判断接受准则讲讲,把局部搜索讲讲就差不多可以了。最后谢谢大家一路过来的支持哈。

代码及相关内容可关注公众号。更多精彩尽在微信公众号【程序猿声】
微信公众号

posted @ 2019-05-10 20:25 短短的路走走停停 阅读( ...) 评论( ...) 编辑 收藏

这篇关于代码 | 自适应大邻域搜索系列之(5) - ALNS_Iteration_Status和ALNS_Parameters的代码解析...的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/161495

相关文章

网页解析 lxml 库--实战

lxml库使用流程 lxml 是 Python 的第三方解析库,完全使用 Python 语言编写,它对 XPath表达式提供了良好的支 持,因此能够了高效地解析 HTML/XML 文档。本节讲解如何通过 lxml 库解析 HTML 文档。 pip install lxml lxm| 库提供了一个 etree 模块,该模块专门用来解析 HTML/XML 文档,下面来介绍一下 lxml 库

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

活用c4d官方开发文档查询代码

当你问AI助手比如豆包,如何用python禁止掉xpresso标签时候,它会提示到 这时候要用到两个东西。https://developers.maxon.net/论坛搜索和开发文档 比如这里我就在官方找到正确的id描述 然后我就把参数标签换过来

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

【生成模型系列(初级)】嵌入(Embedding)方程——自然语言处理的数学灵魂【通俗理解】

【通俗理解】嵌入(Embedding)方程——自然语言处理的数学灵魂 关键词提炼 #嵌入方程 #自然语言处理 #词向量 #机器学习 #神经网络 #向量空间模型 #Siri #Google翻译 #AlexNet 第一节:嵌入方程的类比与核心概念【尽可能通俗】 嵌入方程可以被看作是自然语言处理中的“翻译机”,它将文本中的单词或短语转换成计算机能够理解的数学形式,即向量。 正如翻译机将一种语言

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能