代碼 | 自適應大鄰域搜索系列之(5) - ALNS_Iteration_Status和ALNS_P
上一篇推文說了,後面的代碼難度直線下降,各位小夥伴可以放去n的100次方心了。今天講講一些細枝末節,就是前面一直有提到的參數和一些狀態的記錄代碼。這個簡單啦,小編也不作過多解釋了。大家直接看代碼都能看懂,不過小編還是會把邏輯結構給大家梳理出來的。好了,開始幹活。
01 ALNS_Iteration_Status
這個類,咳咳,不是抽象類了哈。主要用來記錄ALNS叠代過程中的一些中間變量和狀態等。主要是成員變量,成員函數都是簡單的getter(獲取成員變量的接口)或者setter(設置成員變量的接口)。所以這裏就把成員變量貼出來好了,各個變量記錄的內容註釋也寫得很詳細,小編就不做多贅述以免擾亂了大家看代碼的心。
```C++
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(設置成員變量的接口)。照例把成員變量貼出來吧~ ```C++ 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,隔多少次叠代輸出一下當前的信息。直接給大家上兩個圖讓大家心領神會一下:
-
logFrequency = 1
- 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_P