蒙特卡洛算法C++代码

时间: 2023-08-27 15:06:53 浏览: 81
以下是一个简单的蒙特卡洛算法的C++代码示例,用于计算圆周率的近似值: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); // 初始化随机数种子 int n = 1000000; // 模拟次数 int inside = 0; // 圆内点数 for (int i = 0; i < n; i++) { double x = (double)rand() / RAND_MAX; // 生成[0,1]之间的随机数 double y = (double)rand() / RAND_MAX; if (x * x + y * y <= 1) // 判断是否在圆内 { inside++; } } double pi = (double)inside / n * 4; // 计算圆周率的近似值 cout << "Pi is approximately " << pi << endl; return 0; } ``` 该代码通过生成随机的点,判断这些点是否在圆内,从而计算出圆周率的近似值。
相关问题

亚马逊棋蒙特卡洛代码c++

以下是一个基于蒙特卡洛树搜索算法的亚马逊棋 AI 代码,使用 C++ 实现: ```c++ #include <bits/stdc++.h> using namespace std; const int N = 10, M = 100000; const int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; struct Move { int x, y, nx, ny, cnt; }; int n, m, t, a[N][N], f[N][N][N][N], vis[N][N], st[N][N]; int q[N * N][2], hh, tt, dx[M], dy[M], cnt[M]; int dist(int x, int y, int nx, int ny) { if (x == nx && y == ny) return 0; if (x == nx || y == ny || abs(x - nx) == abs(y - ny)) return max(abs(x - nx), abs(y - ny)); return 2 * max(abs(x - nx), abs(y - ny)); } int get(int x, int y) { return x * m + y; } vector<Move> get_moves(int player) { vector<Move> res; memset(vis, 0, sizeof vis); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (a[i][j] == player) { for (int k = 0; k < 8; ++k) { int nx = i + dir[k][0], ny = j + dir[k][1]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny]) continue; memset(st, 0, sizeof st); int hh = 0, tt = -1; q[++tt][0] = i, q[tt][1] = j, st[i][j] = 1; while (hh <= tt) { int x = q[hh][0], y = q[hh++][1]; for (int l = 0; l < 8; ++l) { int nx = x + dir[l][0], ny = y + dir[l][1]; if (nx < 0 || nx >= n || ny < 0 || ny >= m || st[nx][ny]) continue; int d = dist(i, j, nx, ny); if (d >= t) continue; if (a[nx][ny] == 3 - player) { tt = -1; break; } q[++tt][0] = nx, q[tt][1] = ny, st[nx][ny] = 1; } } if (tt == -1) continue; for (int l = 0; l <= tt; ++l) if (!vis[q[l][0]][q[l][1]]) { vis[q[l][0]][q[l][1]] = 1; res.push_back({i, j, q[l][0], q[l][1], tt + 1}); } } } return res; } int dfs(int player, int depth) { if (depth == 0) return 0; auto moves = get_moves(player); int res = -1; for (auto move : moves) { int x = move.x, y = move.y, nx = move.nx, ny = move.ny, cnt = move.cnt; a[nx][ny] = player, a[x][y] = 0; int t = dfs(3 - player, depth - 1); a[x][y] = player, a[nx][ny] = 0; if (t == -1) return -1; res = max(res, cnt - t); } return res; } void init() { memset(f, -1, sizeof f); queue<int> q; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (a[i][j] != 0) { f[i][j][i][j] = 0; q.push(get(i, j)); } while (q.size()) { int t = q.front(); q.pop(); int x = t / m, y = t % m; for (int k = 0; k < 8; ++k) { int nx = x + dir[k][0], ny = y + dir[k][1]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; if (f[x][y][nx][ny] == -1) { f[x][y][nx][ny] = f[x][y][x][y] + 1; q.push(get(nx, ny)); } } } } int eval() { vector<int> p1, p2; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if (a[i][j] == 1) p1.push_back(i * m + j); else if (a[i][j] == 2) p2.push_back(i * m + j); int res = 0; for (int i = 0; i < p1.size(); ++i) for (int j = 0; j < p2.size(); ++j) res += f[p1[i] / m][p1[i] % m][p2[j] / m][p2[j] % m]; return res; } int dfs2(int player, int depth, int lf, int rf) { if (depth == 0) return eval(); auto moves = get_moves(player); if (moves.empty()) return lf - rf; int res = -1e9; vector<int> cnts; for (auto move : moves) { int x = move.x, y = move.y, nx = move.nx, ny = move.ny, cnt = move.cnt; a[nx][ny] = player, a[x][y] = 0; cnts.push_back(cnt); res = max(res, -dfs2(3 - player, depth - 1, -rf, -lf)); a[x][y] = player, a[nx][ny] = 0; lf = max(lf, res - cnt); if (lf >= rf) break; } return res; } void MCTS(int player, int depth) { auto moves = get_moves(player); if (moves.empty()) return; int ucnt = moves.size(); int sum = 0; for (int i = 0; i < ucnt; ++i) { int x = moves[i].x, y = moves[i].y, nx = moves[i].nx, ny = moves[i].ny, cnt = moves[i].cnt; a[nx][ny] = player, a[x][y] = 0; int t = dfs(3 - player, depth - 1); a[nx][ny] = 0, a[x][y] = player; if (t != -1) cnts[i] += cnt - t, sum += cnt - t; } double mx = -1e9; int idx = -1; for (int i = 0; i < ucnt; ++i) { double val = 1.0 * cnts[i] / sum + sqrt(log(cnt[i]) / cnts[i]); if (val > mx) mx = val, idx = i; } int x = moves[idx].x, y = moves[idx].y, nx = moves[idx].nx, ny = moves[idx].ny; a[nx][ny] = player, a[x][y] = 0; } int main() { cin >> n >> m >> t; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> a[i][j]; init(); int depth = 0; while (1) { auto moves = get_moves(1); if (moves.empty()) { puts("Black loses"); break; } if (depth >= 3) { memset(cnt, 0, sizeof cnt); for (int i = 0; i < M; ++i) cnt[i] = 1; for (int i = 0; i < 100; ++i) { int t = dfs2(1, 3, -1e9, 1e9); if (t > 0) break; int idx = rand() % moves.size(); int x = moves[idx].x, y = moves[idx].y, nx = moves[idx].nx, ny = moves[idx].ny; a[nx][ny] = 1, a[x][y] = 0; } } else { for (int i = 0; i < 10000; ++i) MCTS(1, 5); int mx = -1e9, idx = -1; for (int i = 0; i < moves.size(); ++i) if (cnts[i] > mx) mx = cnts[i], idx = i; int x = moves[idx].x, y = moves[idx].y, nx = moves[idx].nx, ny = moves[idx].ny; a[nx][ny] = 1, a[x][y] = 0; } ++depth; auto moves2 = get_moves(2); if (moves2.empty()) { puts("White loses"); break; } int idx = rand() % moves2.size(); int x = moves2[idx].x, y = moves2[idx].y, nx = moves2[idx].nx, ny = moves2[idx].ny; a[nx][ny] = 2, a[x][y] = 0; } return 0; } ``` 该代码实现了亚马逊棋的基本算法,包括: - 蒙特卡洛树搜索算法; - 最短路算法; - 评估函数。 其中,`get_moves` 函数用于获取当前玩家所有合法的移动方式;`dfs` 函数用于在搜索树上搜索指定深度的节点,并返回该节点的得分(如果该节点无法到达终局状态,则返回 -1);`init` 函数用于预处理出最短路;`eval` 函数用于评估当前局面的得分;`dfs2` 函数用于在搜索树上搜索指定深度的节点,并返回该节点的得分;`MCTS` 函数用于执行一次蒙特卡洛树搜索,并更新访问次数和得分。

c++亚马逊棋蒙特卡洛代码

以下是一个简单的 C++ Monte Carlo Tree Search 实现,用于玩亚马逊棋游戏: ``` #include <bits/stdc++.h> using namespace std; const int N = 10, INF = 1e9; const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1}; int n, m, turn, tot, cnt; int a[N][N], f[N * N][N * N], pos[N * N][2], val[N * N], vis[N * N], res[N * N]; double c1 = 1.0, c2 = sqrt(2.0); mt19937 rnd(time(NULL)); inline bool check(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m && !a[x][y]; } inline int calc(int x) { int ans = 0; for(int i = 0; i < 8; i++) { int nx = pos[x][0] + dx[i], ny = pos[x][1] + dy[i]; while(check(nx, ny)) ans++, nx += dx[i], ny += dy[i]; } return ans; } inline double calcval(int x) { return 1.0 * val[x] / (vis[x] + 1) + c1 * sqrt(log(tot) / (vis[x] + 1)); } inline int expand(int x) { int ans = 0; for(int i = 0; i < 8; i++) { int nx = pos[x][0] + dx[i], ny = pos[x][1] + dy[i]; while(check(nx, ny)) { ans++; f[x][++cnt] = nx * m + ny; val[cnt] = calc(cnt); nx += dx[i], ny += dy[i]; } } return ans; } inline int getmax() { int ans = 1; double mx = -INF; for(int i = 1; i <= cnt; i++) { if(!vis[i]) return i; double val = calcval(i); if(val > mx) mx = val, ans = i; } return ans; } inline int simulate(int x) { int ans = 0; for(int i = 0; i < 100; i++) { int nx = pos[x][0], ny = pos[x][1]; while(1) { int p = rnd() % 8, xx = nx + dx[p], yy = ny + dy[p]; if(check(xx, yy)) { nx = xx, ny = yy; break; } } int t = nx * m + ny; if(vis[t] || t == x) continue; vis[t] = 1; int num = expand(t); ans += num; } return ans; } inline int getbest(int x) { int ans = 1; double mx = -INF; for(int i = 1; i <= cnt; i++) { if(!vis[i]) continue; double val = 1.0 * res[i] / vis[i] + c2 * sqrt(log(tot) / vis[i]); if(val > mx) mx = val, ans = i; } return ans; } inline void backpropagate(int x, int v) { while(x) { vis[x]++, res[x] += v, v = -v, x = f[x][0]; } } inline void mcts() { int now = 1; while(1) { if(!expand(now)) break; int idx = getmax(); int num = simulate(idx); backpropagate(idx, num); turn = -turn; tot++; now = getbest(now); } } int main() { cin >> n >> m; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { cin >> a[i][j]; if(a[i][j] == 1) pos[++cnt][0] = i, pos[cnt][1] = j; } } mcts(); int ans = 1, mx = -INF; for(int i = 1; i <= cnt; i++) { if(!vis[i]) continue; if(res[i] * turn > mx) mx = res[i] * turn, ans = i; } cout << pos[ans][0] << " " << pos[ans][1] << endl; return 0; } ``` 该代码实现了一个简单的 Monte Carlo Tree Search 算法来玩亚马逊棋游戏。具体来说,它首先根据当前局面扩展一个节点,然后选择一个未被访问的子节点进行模拟,得到一个评估值,最后将评估值回溯到从该节点到根节点的所有节点。根据 MCTS 算法中的公式,每个节点的价值由它的胜率和探索程度决定,这里的实现中使用了 UCB1 公式。

相关推荐

最新推荐

recommend-type

C++递归算法实例代码

主要介绍了C++递归算法实例代码,还是比较不错的,运用了递归算法解决相关问题,这里分享给大家,需要的朋友可以参考下。
recommend-type

遗传算法c++代码,车间调度

主要是关于车间遗传算法的代码,仅仅参考,当然交流之用,我也是从别人那里拷来的
recommend-type

用C++实现DBSCAN聚类算法

本篇文章是对使用C++实现DBSCAN聚类算法的方法进行了详细的分析介绍,需要的朋友参考下
recommend-type

c++代码实现tea加密算法的实例详解

主要介绍了c++代码实现tea加密算法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

C++实现分水岭算法(Watershed Algorithm)

主要为大家详细介绍了C++实现分水岭算法Watershed Algorithm,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。