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

对经典算法的问题的回顾与感想

对八皇后问题和八数码问题分别用最陡上升爬山法、首选爬山法、随机重启爬山法、模拟退火算法来实现,并且分析他们的性能。


分析

要求实现的各个算法是有共同点的,比如,八皇后问题相关算法拥有相同的状态空间,每个算法都有从当前状态获取下一状态的需求,各个算法细节不同,共同点是,它们都是算法。
基于这样的想法,我们可以将代码划分为3层:
这里写图片描述


运行示例

代码比较长,附在最后面,读者基于上述的思路是不难看懂的。
我们默认读者已经知道这几个算法的思路,代码直接给出实现。如果不懂,请翻阅人工智能课本,里面有讲。

图1,八数码问题的交互界面
这里写图片描述
图2,八皇后问题的交互界面
这里写图片描述
图3 输出界面示例
这里写图片描述


结果分析

为了便于研究,我们首先定义三个指标,分别是:
①因变1:算法找到解的成功率;
②因变2:算法找到的解的平均路径长度
③因变3:算法找到解的平均耗散,用搜索的结点数衡量

对八皇后的结果分析

这里写图片描述
观察表1,我们可以发现如下对八皇后问题的有趣结论:
(1)在八皇后问题中,当随机测例数增加,最陡上升爬山法和首选爬山法的成功率都收敛到0.142左右,与书本结论吻合。
(2)最陡上升爬山法的解的平均路径长度收敛到0.58左右,而首选爬山是0.83左右。这说明最陡爬山法的平均走了更短的路径到达全局最优解。
(3)虽然最陡上山法的平均路径长度更短,但是搜索耗散却比首选爬山法多,原因是,为了得到最陡的子结点,需要探查所有的相邻子节点。反应在数据上是,最陡上山法的解的平均耗散率收敛到32左右,而首选爬山法仅为19左右。
(4)随机重启爬山法比最陡上升法和首选爬山法的成功率兜大大提高,重启次数为5,随机测例10000时,成功率高达0.6,约为前二者算法的4倍。但是也付出了更大的代价,平均解长度是最陡上升法的18.4倍,是首选爬山法的12.7倍;解的平均耗散度是最陡爬山法的17倍,是首选爬山法的28.4倍。
(5)随机重启爬山法的成功率随初始温度的增加而上升。当重启次数为0时,退化为首选爬山法,成功率、解长度、解耗散度都和首选爬山法接近。随着重启次数增加,成功率也大大上升,当重启次数为7时,成功率为0.7092,是重启次数为0的4.96倍,相应地,解长度和解耗散也大大增加。
(6)模拟退火算法随起始温度上升,成功率也上升。理论上分析,当起始温度足够高,退火过程足够长的时候,成功率可以接近1。但是其开销也会变得极大,0.43成功率的退火算法的解长度是0.46成功率退火算法的3706倍,而解耗散是146.5倍。

对八数码问题的结果分析

这里写图片描述
观察表2,我们可以发现如下对八数码问题的有趣结论:
(1)与八皇后问题类似,最陡上升和首选爬山法收敛到了接近的成功率,此处是0.4左右。但是解长度和解耗散并没有八皇后问题大,可能的原因是,八数码问题的相邻子结点空间远比八皇后问题大。
(2)这里没有使用随机重启算法,因为八数码问题关心解路径,如果使用了随机重启算法,则违反了规则。
(3)初始状态是通过目标随机打乱得来的,先随机取上限次数一下的打乱数x,然后随机方向移动白块x次。我们发现,打乱步数上限的多少,对成功率的影响并不大,无论最陡爬山算法,首选爬山算法,模拟退火算法都如此。可能的原因是,在打乱次数上限很小的时候,成功率就已经收敛了。
(4)模拟退火算法在八数码问题和八皇后问题的表现类似。都随着初始温度的上升而上升,同时解长度和解耗散急剧增大。理论上,当初始温度足够高,成功率会逼近1。

代码

由于实在太多,我就不逐行解释。读者把握分析时的思路,应该不难读懂。

//*****************************************************
// eightQueue.hpp
// 包括八皇后问题的eightQueueNode类和eightQueueNodeFactory的实现
//*****************************************************
#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)) {// 子节点全部搜索过,都比当前差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
// 包括八皇后问题的各个算法和简单交互界面实现
//***************************************
#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) {// 根据温度差来决定是否接受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
// 包括八数码问题的eightDigitNode类和eightDigitFactory类的实现
// **************************************************************
#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)) {// 子节点全部搜索过,都比当前差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
// 包括八数码问题各算法的和简单交互界面实现
//********************************************
#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) {// 根据温度差来决定是否接受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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/549396.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

配置BGP

配置BGP<?xml:namespace prefix o ns "urn:schemas-microsoft-com:office:office" />一、 实验目的:配置BGP协议,实现两个isp之间的通信二、 实验拓扑结构图三、实验步骤1. 基本接口配置(略) 2. 配置bgp协议isp<?xml:namespace prefix …

MyXls初级教程

这些天使用MyXls导出Excel报表&#xff08;因为Apose.Cells要收费&#xff09;。感觉MyXls虽然功能远没有Cells强大&#xff0c;但是胜在开源、免费而且性能稳定可靠。用作出一般情况下的报表。足矣&#xff01; 记下几个初级使用方法&#xff0c;希望能够给初入门的人一点帮助…

蓝桥杯 - 历届试题 - 日期问题

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_33531813/article/details/79516258 </div><div id"content_views" class"markdown_views"><!-- flowchart 箭头图标 勿删 --…

分享WCF文件传输---WCFFileTransfer

前几天分享了分享了WCF聊天程序--WCFChat &#xff0c;本文和大家一起分享利用WCF实现文件的传输。程序运行效果&#xff1a;接收文件端&#xff1a;发送文件端&#xff1a;连接WCF服务&#xff0c;选择要传输的文件文件传输成功&#xff1a;我们会在保存文件的默认路径&#x…

字符串的模式匹配--BF算法KMP算法

BF算法是基于主串指针回溯&#xff0c;重新与子串进行逐字符进行比较&#xff0c;主串为S什么要进行回溯呢&#xff0c;原因在于模式P中存在相同的字符或者说由字符&#xff08;串&#xff09;存在重复&#xff08;模式的部分匹配性质&#xff09;&#xff0c;设想如果模式P中字…

用SQL Server 监控 OS Server 的Task Management信息

用SQL Server 监控 OS Server 的Task Management信息 --原文来自于http://www.databasejournal.com/features/mssql/article.php/3562586/Monitor-CPU-Usage-of-All-Running-Processes---Part-II.htm 一&#xff1a; 监控程序部分 1. 在C 盘创一个文件夹&#xff1a;如 C…

匈牙利算法——最大匹配问题详解

2017年中兴提前批校招&#xff0c;就考了一题匈牙利算法。 匈牙利算法是由匈牙利数学家Edmonds于1965年提出&#xff0c;因而得名。匈牙利算法是基于Hall定理中充分性证明的思想&#xff0c;它是部图匹配最常见的算法&#xff0c;该算法的核心就是寻找增广路径&#xff0c;它是…

字符串匹配之KMP---全力解析

PS&#xff1a;文章是转载的 下方的微信公号不是我的 是原作者的。附上原文链接&#xff1a;字符串匹配之KMP jeliy王的博客 近日&#xff0c;一同学面试被问到字符串匹配算法&#xff0c;结果由于他使用了暴力法&#xff0c;直接就跪了(现在想想这样的面试官真的是不合格的&am…

用Dreamweaver实现ASP动态网站建设【8】

八、制作删除数据记录页 用上述学过的方法在Index.asp上创建“删除”连接。新建网页命名为delete.asp&#xff0c;并打开它&#xff0c;在其上创建一个七行二列的表格&#xff0c;并在左边的表格上填写相应的字段名&#xff0c;然后给网页绑定一个记录集&#xff0c;并对其字段…

大学计算机网络复习题

模拟试题 一、填空题 1、局域网中常用的拓扑结构主要有星型、 环形 、总线型三种。 2 、在当前的网络系统中&#xff0c;由于网络覆盖面积的大小、技术条件和工作环境不同&#xff0c;通常分为广域网、 局域网 、和城域网三种。 3、常用的通信介…

【讨论】从吉日的一段话说起+寻找WinForm架构的最佳实践

这两天园子里最火的莫过于吉日的白话反射&#xff0c;导致包子的批判&#xff0c;然后引来了老赵的两篇文章&#xff0c;然后又有若干人等一堆反射技术文章出世。可谓百花齐放&#xff0c;百家争鸣啊。喜欢这种氛围&#xff0c;呵呵。 今天我不谈反射&#xff0c;但和反射有关 …

Oracle分析函数一——函数列表

Oracle 分析函数 Oracle 分析函数——函数列表 SUM &#xff1a; 该函数计算组中表达式的累积和 MIN &#xff1a; 在一个组中的数据窗口中查找表达式的最小值 MAX &#xff1a; 在一个组中的数据窗口中查找表达式的最大值 AVG &#xff1a;…

用MATLAB实现神经网络

一 BP神经网络实现不使用MATLAB神经网络工具箱问题分析MATLAB实现代码运行结果绘制的图像 二 使用MATLAB的神经网络工具箱简易实现BP网络问题分析工具箱中的相关函数一些参考了MATLAB自带的英文手册mapminmax函数newff函数新版本关于nettrainParam的常用属性train函数sim函数 M…

Follow Me:CCIE RS--使用小凡模拟器搭建的CCIE拓扑图

我用小凡模拟器搭建了CCIE LAB 拓扑图有何不对的地方请指正转载于:https://blog.51cto.com/tanfo/216831

非线性最优化(二)——高斯牛顿法和Levengerg-Marquardt迭代

高斯牛顿法和Levengerg-Marquardt迭代都用来解决非线性最小二乘问题(nonlinear least square)。 From Wiki The Gauss–Newton algorithm is a method used to solve non-linear least squares problems. It is a modification of Newtons method for finding a minimum of a …

逆透视变换详解 及 代码实现(一)

逆透视变换详解 及 代码实现&#xff08;一&#xff09; 中主要是原理的说明&#xff1a; 一、世界坐标轴和摄像机坐标轴 从下图中可以看到&#xff0c;世界坐标为(X,Y,Z) 相机坐标为(Xc,Yc,Zc) 而世界坐标变换到相机坐标存在一个旋转矩阵变换R以及一个位移变换T。 根据上图…

C调用C++链接库

C调用C链接库&#xff1a; 1.编写C代码&#xff0c;编写函数的时候&#xff0c;需要加入对C的接口&#xff0c;也就是extern “c" 2.由于C不能直接用"class.function”的形式调用函数&#xff0c;所以C中需要为C写一个接口函数。例如本来要调用student类的talk函数&a…

逆透视变换详解 及 代码实现(二)

根据 逆透视变换详解 及 代码实现(一)的原理 下面我用车上拍摄的车道图像&#xff0c;采用逆透视变换得到的图像&#xff0c;给出代码前我们先看下处理结果。 首先是原始图像&#xff1a; 下图为逆透视变换图像&#xff1a; 下面说具体的实现吧&#xff01;&#xff01; 一、…

[赵星理]《简单男人》--歌曲温暖你的心,激励你前进

简单的男人&#xff0c;简单的歌曲&#xff0c;赵星理《简单男人》送给所有身负家庭责任的人&#xff0c;要让家越来越美&#xff0c;再苦再累也不能后退。加油&#xff01;简单男人词曲&#xff1a;赵星理演唱&#xff1a;赵星理累不累也不许落泪醉不醉苦辣都值得回味要让家越…

SCVMM

通过SCVMM实现并管理虚拟机高可用性 1、 添加群集主机2、 创建虚拟网络3、 创建虚拟机并实现高可用性接着上一篇文章&#xff0c;这次我们来看一下&#xff0c;如果通过SCVMM R2来实现虚拟机的高可用性。首先将群集主机添加到SCVMM 1、 登陆到计算机Win2008R2&#xff0c;打开S…