日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

八皇后问题和八数码问题的最陡上升爬山法、首选爬山法、随机重启爬山法、模拟退火算法的分析和实现

發(fā)布時間:2025/3/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 八皇后问题和八数码问题的最陡上升爬山法、首选爬山法、随机重启爬山法、模拟退火算法的分析和实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對經(jīng)典算法的問題的回顧與感想

對八皇后問題和八數(shù)碼問題分別用最陡上升爬山法、首選爬山法、隨機重啟爬山法、模擬退火算法來實現(xiàn),并且分析他們的性能。


分析

要求實現(xiàn)的各個算法是有共同點的,比如,八皇后問題相關(guān)算法擁有相同的狀態(tài)空間,每個算法都有從當前狀態(tài)獲取下一狀態(tài)的需求,各個算法細節(jié)不同,共同點是,它們都是算法。
基于這樣的想法,我們可以將代碼劃分為3層:


運行示例

代碼比較長,附在最后面,讀者基于上述的思路是不難看懂的。
我們默認讀者已經(jīng)知道這幾個算法的思路,代碼直接給出實現(xiàn)。如果不懂,請翻閱人工智能課本,里面有講。

圖1,八數(shù)碼問題的交互界面

圖2,八皇后問題的交互界面

圖3 輸出界面示例


結(jié)果分析

為了便于研究,我們首先定義三個指標,分別是:
①因變1:算法找到解的成功率;
②因變2:算法找到的解的平均路徑長度
③因變3:算法找到解的平均耗散,用搜索的結(jié)點數(shù)衡量

對八皇后的結(jié)果分析


觀察表1,我們可以發(fā)現(xiàn)如下對八皇后問題的有趣結(jié)論:
(1)在八皇后問題中,當隨機測例數(shù)增加,最陡上升爬山法和首選爬山法的成功率都收斂到0.142左右,與書本結(jié)論吻合。
(2)最陡上升爬山法的解的平均路徑長度收斂到0.58左右,而首選爬山是0.83左右。這說明最陡爬山法的平均走了更短的路徑到達全局最優(yōu)解。
(3)雖然最陡上山法的平均路徑長度更短,但是搜索耗散卻比首選爬山法多,原因是,為了得到最陡的子結(jié)點,需要探查所有的相鄰子節(jié)點。反應(yīng)在數(shù)據(jù)上是,最陡上山法的解的平均耗散率收斂到32左右,而首選爬山法僅為19左右。
(4)隨機重啟爬山法比最陡上升法和首選爬山法的成功率兜大大提高,重啟次數(shù)為5,隨機測例10000時,成功率高達0.6,約為前二者算法的4倍。但是也付出了更大的代價,平均解長度是最陡上升法的18.4倍,是首選爬山法的12.7倍;解的平均耗散度是最陡爬山法的17倍,是首選爬山法的28.4倍。
(5)隨機重啟爬山法的成功率隨初始溫度的增加而上升。當重啟次數(shù)為0時,退化為首選爬山法,成功率、解長度、解耗散度都和首選爬山法接近。隨著重啟次數(shù)增加,成功率也大大上升,當重啟次數(shù)為7時,成功率為0.7092,是重啟次數(shù)為0的4.96倍,相應(yīng)地,解長度和解耗散也大大增加。
(6)模擬退火算法隨起始溫度上升,成功率也上升。理論上分析,當起始溫度足夠高,退火過程足夠長的時候,成功率可以接近1。但是其開銷也會變得極大,0.43成功率的退火算法的解長度是0.46成功率退火算法的3706倍,而解耗散是146.5倍。

對八數(shù)碼問題的結(jié)果分析


觀察表2,我們可以發(fā)現(xiàn)如下對八數(shù)碼問題的有趣結(jié)論:
(1)與八皇后問題類似,最陡上升和首選爬山法收斂到了接近的成功率,此處是0.4左右。但是解長度和解耗散并沒有八皇后問題大,可能的原因是,八數(shù)碼問題的相鄰子結(jié)點空間遠比八皇后問題大。
(2)這里沒有使用隨機重啟算法,因為八數(shù)碼問題關(guān)心解路徑,如果使用了隨機重啟算法,則違反了規(guī)則。
(3)初始狀態(tài)是通過目標隨機打亂得來的,先隨機取上限次數(shù)一下的打亂數(shù)x,然后隨機方向移動白塊x次。我們發(fā)現(xiàn),打亂步數(shù)上限的多少,對成功率的影響并不大,無論最陡爬山算法,首選爬山算法,模擬退火算法都如此。可能的原因是,在打亂次數(shù)上限很小的時候,成功率就已經(jīng)收斂了。
(4)模擬退火算法在八數(shù)碼問題和八皇后問題的表現(xiàn)類似。都隨著初始溫度的上升而上升,同時解長度和解耗散急劇增大。理論上,當初始溫度足夠高,成功率會逼近1。

代碼

由于實在太多,我就不逐行解釋。讀者把握分析時的思路,應(yīng)該不難讀懂。

//***************************************************** // eightQueue.hpp // 包括八皇后問題的eightQueueNode類和eightQueueNodeFactory的實現(xiàn) //***************************************************** #include <iostream> #include <stdlib.h> #include <time.h> class eightQueueNode { public:int arr[8];eightQueueNode(int a,int b, int c, int d, int e, int f, int g, int h){arr[0] = a;arr[1] = b;arr[2] = c;arr[3] = d;arr[4] = e;arr[5] = f;arr[6] = g;arr[7] = h;}eightQueueNode(const eightQueueNode& node) {arr[0] = node.arr[0];arr[1] = node.arr[1];arr[2] = node.arr[2];arr[3] = node.arr[3];arr[4] = node.arr[4];arr[5] = node.arr[5];arr[6] = node.arr[6];arr[7] = node.arr[7];}~eightQueueNode(){}bool operator==(const eightQueueNode& node) {return (this->arr[0] == node.arr[0]) && (this->arr[1] == node.arr[1]) && (this->arr[2] == node.arr[2])&& (this->arr[3] == node.arr[3]) && (this->arr[4] == node.arr[4]) && (this->arr[5] == node.arr[5])&& (this->arr[6] == node.arr[6]) && (this->arr[7] == node.arr[7]);} };class eightQueueNodeFactory{private:int ranNum(){return rand() % 8;}bool isTheArrayAllTrue(bool isAllCheck[64]) {for(int i = 0; i < 64; i++) {if(isAllCheck[i] == false) {return false;}}return true;}public:eightQueueNodeFactory(){srand((unsigned)time(NULL));}eightQueueNode getARandomNode() {return eightQueueNode(ranNum(),ranNum(),ranNum(),ranNum(),ranNum(),ranNum(),ranNum(),ranNum());}int evaluate(const eightQueueNode& node) {int numOfAttack = 0;for(int i = 0; i < 7; i++) {for(int j = i + 1; j < 8; j++) {if (node.arr[i] == node.arr[j] || (node.arr[i]-node.arr[j]) == (i-j) || (node.arr[i]-node.arr[j]) == (j-i)) {numOfAttack++;}}}return numOfAttack;}int getBestNextNode(eightQueueNode& node) {eightQueueNode ans = node;eightQueueNode tmp = node;int costOfSearch = 0;for(int i = 0; i < 64; i++) {tmp = node;tmp.arr[i/8] = i % 8;if(evaluate(tmp) < evaluate(ans)) {ans = tmp;} else if(evaluate(tmp) == evaluate(ans)) {if(rand() / double(RAND_MAX) > 0.5) {ans = tmp;}}}node = ans;return 56;}// the input node is confirmed to be not the bestint getNextBetterNode(eightQueueNode& node) {bool isAllCheck[64];for(int i = 0; i < 64; i++) isAllCheck[i] = false;eightQueueNode tmp = node;int costOfSearch = 1;while(evaluate(tmp) >= evaluate(node)) {// 子節(jié)點全部搜索過,都比當前差if(isTheArrayAllTrue(isAllCheck)) return costOfSearch;// 初始化,找下一鄰居tmp = node;int a = rand() % 64;isAllCheck[a] = true;tmp.arr[a/8] = a % 8;costOfSearch++;if(tmp == node) {continue;}}node = tmp;return costOfSearch;}int getARandomNeighbour(eightQueueNode& node) {eightQueueNode tmp = node;int cost = 0;while(node == tmp) {cost++;int a = rand() % 64;tmp.arr[a/8] = a % 8;}node = tmp;return cost;} };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
//*************************************** // eightQueueRunner.cpp // 包括八皇后問題的各個算法和簡單交互界面實現(xiàn) //*************************************** #include "eightQueue.hpp" #include <iostream> #include <math.h>using namespace std; eightQueueNodeFactory factory;#define NUM_OF_LOOP 10000 #define NUM_OF_REBOOT 0 #define BEGIN_TEMP 10000 #define STOP_TEMP 1void mostSteepClimbing(); void firstSeclectionClimbing(); void randomRebootClimbing(); void simulatedAnnealing();int main() {int choice = 0;do{cout << endl;cout << "The Eight Queue Problem:" << endl;cout << " 0 -- most steep climbing" << endl;cout << " 1 -- first selection climbing" << endl;cout << " 2 -- random reboot climbing" << endl;cout << " 3 -- stimulated annealing" << endl;cout << "please input your choice: ";cin >> choice;if (choice == -1) break;switch(choice) {case 0:mostSteepClimbing();break;case 1:firstSeclectionClimbing();break;case 2:randomRebootClimbing();break;case 3:simulatedAnnealing();break;default:break;}}while(true);return 0; }void mostSteepClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;eightQueueNode curState = factory.getARandomNode();eightQueueNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getBestNextNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {break;} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; }void firstSeclectionClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;eightQueueNode curState = factory.getARandomNode();eightQueueNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getNextBetterNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {break;} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; }void randomRebootClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0, numOfReboot = NUM_OF_REBOOT;eightQueueNode curState = factory.getARandomNode();eightQueueNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getNextBetterNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {if(numOfReboot > 0) {numOfReboot--;curState = factory.getARandomNode();tmp = curState;} else {break;}} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; }void simulatedAnnealing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;double curTemp = BEGIN_TEMP;eightQueueNode curState = factory.getARandomNode();eightQueueNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;} else if(curTemp < STOP_TEMP){// 溫度過低,則停止break;}// 走一步,則溫度下降lenOfRoute++;curTemp -= 1;cost += factory.getARandomNeighbour(tmp);int deltaOfEvalutaion = factory.evaluate(tmp) - factory.evaluate(curState);if(deltaOfEvalutaion >= 0) {// 根據(jù)溫度差來決定是否接受double probility = exp(curTemp/(deltaOfEvalutaion));if(rand() / double(RAND_MAX) < deltaOfEvalutaion) {curState = tmp;}} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
//*************************************************************** // eightDigit.hpp // 包括八數(shù)碼問題的eightDigitNode類和eightDigitFactory類的實現(xiàn) // ************************************************************** #include <iostream> #include <stdlib.h> #include <time.h> #include <math.h>#define LOOP_OF_SHUFFLE 50class eightDigitNode { public:int arr[9];int blank_x = 2, blank_y = 2;eightDigitNode(){for(int i = 0; i < 8; i++) {arr[i] = i+1;}arr[8] = 0;}~eightDigitNode(){}eightDigitNode(const eightDigitNode& node) {arr[0] = node.arr[0];arr[1] = node.arr[1];arr[2] = node.arr[2];arr[3] = node.arr[3];arr[4] = node.arr[4];arr[5] = node.arr[5];arr[6] = node.arr[6];arr[7] = node.arr[7];arr[8] = node.arr[8];blank_x = node.blank_x;blank_y = node.blank_y;}bool operator==(const eightDigitNode& node) {for(int i = 0; i < 8; i++) {if(arr[i] != node.arr[i]) return false;}return true;}bool goLeft() {if(blank_y == 0) {return false;} else {arr[blank_x*3+blank_y] = arr[blank_x*3+blank_y-1];arr[blank_x*3+blank_y-1] = 0;blank_y--;return true;}}bool goRight() {if(blank_y == 2) {return false;} else {arr[blank_x*3+blank_y] = arr[blank_x*3+blank_y+1];arr[blank_x*3+blank_y+1] = 0;blank_y++;return true;}}bool goUp() {if(blank_x == 0) {return false;} else {arr[blank_x*3+blank_y] = arr[(blank_x-1)*3+blank_y];arr[(blank_x-1)*3+blank_y] = 0;blank_x--;return true;}}bool goDown() {if(blank_x == 2) {return false;} else {arr[blank_x*3+blank_y] = arr[(blank_x+1)*3+blank_y];arr[(blank_x+1)*3+blank_y] = 0;blank_x++; return true;}} };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
class eightDigitNodeFactory{ private:int getManhattanDistian(const int& src, const int& target) {return abs(src%3 - target%3) + abs(src/3 - target/3); }int getNumOfWrongNumber(const eightDigitNode& node) {int res = 0;for(int i = 0; i < 8; i++) {if (node.arr[i] != i+1) res++;}if(node.arr[8] != 0) res++;return res; }bool getANeighbourNode(int direction, eightDigitNode& node) {switch(direction) {case 0: return node.goLeft();case 1: return node.goUp();case 2: return node.goRight();default:return node.goDown();}}bool isTheArrayAllTrue(bool isAllCheck[4]) {for(int i = 0; i < 4; i++) {if(isAllCheck[i] == false) {return false;}}return true;}public:eightDigitNodeFactory() {srand((unsigned)time(NULL));}eightDigitNode getARandomNode() {eightDigitNode output;int timesOfShuffle = rand() % LOOP_OF_SHUFFLE;while(timesOfShuffle--) {while(!getANeighbourNode(rand()%4, output));}return output;}int evaluate(const eightDigitNode& node) {eightDigitNode tmp;int distanceToTargetState = 0;for(int i = 0; i < 9; i++) {int j = 0;while(tmp.arr[j] != node.arr[i]) {j++;}distanceToTargetState += getManhattanDistian(i, j);}return distanceToTargetState + 3*getNumOfWrongNumber(node);}int getBestNextNode(eightDigitNode& node) {eightDigitNode tmp = node, ans = node;for(int i = 0; i < 4; i++) {tmp = node;if(getANeighbourNode(i, tmp) && evaluate(tmp) < evaluate(ans)) {ans = tmp;} else if(evaluate(tmp) == evaluate(ans)) {if(rand() / double(RAND_MAX) > 0.5) {ans = tmp;}}}node = ans;return 4;}int getNextBetterNode(eightDigitNode& node) {bool isAllCheck[4];for(int i = 0; i < 4; i++) {isAllCheck[i] = false;}eightDigitNode tmp = node;int costOfSearch = 1;while(evaluate(tmp) >= evaluate(node)) {// 子節(jié)點全部搜索過,都比當前差if(isTheArrayAllTrue(isAllCheck)) return costOfSearch;// 初始化,找下一鄰居tmp = node;int a = rand() % 4;isAllCheck[a] = true;getANeighbourNode(a, tmp);costOfSearch++;if(tmp == node) {continue;}}node = tmp;return costOfSearch;}int getARandomNeighbour(eightDigitNode& node) {int costOfSearch = 0;eightDigitNode tmp = node;while(!getANeighbourNode(rand()%4, tmp)) {costOfSearch++;tmp = node;}node = tmp;return costOfSearch;} };
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
//********************************************* // eightDigitRunner.cpp // 包括八數(shù)碼問題各算法的和簡單交互界面實現(xiàn) //******************************************** #include "eightDigit.hpp" #include <iostream> #include <math.h>using namespace std; eightDigitNodeFactory factory;#define NUM_OF_LOOP 10000 #define NUM_OF_REBOOT 7 #define BEGIN_TEMP 100000 #define STOP_TEMP 1void mostSteepClimbing(); void firstSeclectionClimbing(); void randomRebootClimbing(); void simulatedAnnealing();int main() {int choice = 0;do{cout << endl;cout << "The Eight Digit Problem:" << endl;cout << " 0 -- most steep climbing" << endl;cout << " 1 -- first selection climbing" << endl;cout << " 2 -- random reboot climbing" << endl;cout << " 3 -- stimulated annealing" << endl;cout << "please input your choice: ";cin >> choice;if (choice == -1) break;switch(choice) {case 0:mostSteepClimbing();break;case 1:firstSeclectionClimbing();break;case 2:randomRebootClimbing();break;case 3:simulatedAnnealing();break;default:break;}}while(true);return 0; }void mostSteepClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;eightDigitNode curState = factory.getARandomNode();eightDigitNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getBestNextNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {break;} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl;cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; }void firstSeclectionClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;eightDigitNode curState = factory.getARandomNode();eightDigitNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getNextBetterNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {break;} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; } void randomRebootClimbing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;eightDigitNode curState = factory.getARandomNode();eightDigitNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;}lenOfRoute++;cost += factory.getNextBetterNode(tmp);if(factory.evaluate(tmp) >= factory.evaluate(curState)) {break;} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return; } void simulatedAnnealing() {int sumOfsuccess = 0, sumOfFailure = 0;double theSumOfCostOfSearchOfSuccess = 0, theSumLengthOfSolutionOfSuccess = 0;for(int i = 0; i < NUM_OF_LOOP; i++) {bool success = false;int cost = 0, lenOfRoute = 0;double curTemp = BEGIN_TEMP;eightDigitNode curState = factory.getARandomNode();eightDigitNode tmp = curState;while(true) {// if satisfied, then breakif (factory.evaluate(curState) == 0) {success = true;break;} else if(curTemp < STOP_TEMP){// 溫度過低,則停止break;}// 走一步,則溫度下降lenOfRoute++;curTemp -= 1;cost += factory.getARandomNeighbour(tmp);int deltaOfEvalutaion = factory.evaluate(tmp) - factory.evaluate(curState);if(deltaOfEvalutaion >= 0) {// 根據(jù)溫度差來決定是否接受double probility = exp(curTemp/(deltaOfEvalutaion));if(rand() / double(RAND_MAX) < deltaOfEvalutaion) {curState = tmp;}} else {curState = tmp;}}if(success) {sumOfsuccess++;theSumOfCostOfSearchOfSuccess += cost;theSumLengthOfSolutionOfSuccess += lenOfRoute;} else {sumOfFailure++;}// outputcout << "case " << i << " cost: " << cost << " " << "lengthOfSolution: " << lenOfRoute << " ";if(success){cout << "success ";} else {cout << "failure ";}cout << endl;}cout << "Sum of success: " << sumOfsuccess << endl;cout << "Sum of failure " << sumOfFailure << endl;cout << "Rate of success " << sumOfsuccess/((double)(sumOfFailure + sumOfsuccess)) << endl; cout << "The average solution length of success:" << theSumLengthOfSolutionOfSuccess/NUM_OF_LOOP << endl;cout << "The average cost of search of success:" << theSumOfCostOfSearchOfSuccess/NUM_OF_LOOP << endl; return;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet"></div>

總結(jié)

以上是生活随笔為你收集整理的八皇后问题和八数码问题的最陡上升爬山法、首选爬山法、随机重启爬山法、模拟退火算法的分析和实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。