-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
160 lines (152 loc) · 4.57 KB
/
main.cpp
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
#include <iostream>
#include <mpi.h>
#include <math.h>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include "reversi.h"
#include "treeNode.h"
using namespace std;
double score(double a, double b, double c);
pair<int, int> UCT(Reversi board, int iteration, int rank, int size);
int MC(Reversi board, int player, int rank, int size, int iteration);
double ucb(double a, double b, double c);
int bestIndex(TreeNode *root);
int main() {
int rank, size;
srand(time(NULL));
MPI_Init(0, 0);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
Reversi board;
int count = 0;
int iteration = 2000 / size;
double startTime = MPI_Wtime();
for (int i = 0; i < 1; ++i) {
if (MC(board, -1, rank, size, iteration) > 0) ++count;
if (rank == 0) {
cout << i+1 << ' ' << count << endl;
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (rank == 0) cout << MPI_Wtime() - startTime << endl;
MPI_Finalize();
return 0;
}
pair<int, int> UCT(Reversi board, int iteration, int rank, int size) {
pair<int, int> result;
clock_t startTime = clock();
TreeNode *root = new TreeNode(board, 0, NULL);
// UCT step
for (int round = 0; round < iteration; ++round) {
TreeNode *nextState = root->treePolicy();
double reward = nextState->defaultPolicy();
nextState->update(reward);
}
/*
while (clock() - startTime < seconds * CLOCKS_PER_SEC) {
TreeNode *nextState = root->treePolicy();
double reward = nextState->defaultPolicy();
nextState->update(reward);
}
*/
int childrenNum = root->childrenNum;
if (rank == 0) {
// Receive data from other proc
vector<vector<double> > data(size - 1, vector<double>(childrenNum * 2 + 1));
for (int receiver = 1; receiver < size; ++receiver) {
MPI_Status status;
MPI_Recv(&data[receiver - 1].front(), childrenNum * 2 + 1,
MPI_DOUBLE, receiver, 1, MPI_COMM_WORLD, &status);
}
// Average data
double sum = root->visitNum;
for (int i = 0; i < size - 1; ++i) {
sum += data[i][0];
}
for (int i = 1; i < childrenNum * 2 + 1; ++i) {
double rewardSum = 0, visitSum = 0;
if (i % 2 == 1) {
// Sum up visitNum
for (int j = 0; j < size - 1; ++j) {
visitSum += data[j][i];
}
visitSum += root->children[(i - 1) / 2]->visitNum;
root->children[(i - 1) / 2]->visitNum = visitSum / size;
} else {
// Sum up reward
for (int j = 0; j < size - 1; ++j) {
rewardSum += data[j][i];
}
rewardSum += root->children[i / 2 - 1]->rewards;
root->children[i / 2 - 1]->rewards = rewardSum / size;
}
}
} else {
// Send data to proc 0
vector<double> data = root->gatherData(0);
MPI_Request request;
MPI_Isend(&data.front(), childrenNum * 2 + 1, MPI_DOUBLE,
0, 1, MPI_COMM_WORLD, &request);
}
if (rank == 0) {
// int index = bestIndex(root);
// result = root->nextMoves[index];
result = root->bestMove();
}
root->deleteTree();
return result;
}
int MC(Reversi board, int player, int rank, int size, int iteration) {
board.setPlayer(player);
pair<int, int> move;
while (true) {
vector<pair<int, int> > nextMoves = board.getValidMoves();
if (nextMoves.empty()) break;
if (board.getPlayer() == player) {
move = UCT(board, iteration, rank, size);
} else {
move = nextMoves[rand() % nextMoves.size()];
}
// Send the next move to other proc
if (rank == 0) {
int sendingMove[2];
sendingMove[0] = move.first;
sendingMove[1] = move.second;
for (int receiver = 1; receiver < size; ++receiver) {
MPI_Request request;
MPI_Isend(&sendingMove, 2, MPI_INT, receiver, 0, MPI_COMM_WORLD, &request);
}
}
else {
// Receive the next move from proc 0
int receivingMove[2];
MPI_Status status;
MPI_Recv(&receivingMove, 2, MPI_INT, 0, 0, MPI_COMM_WORLD, &status);
move.first = receivingMove[0];
move.second = receivingMove[1];
}
board.makeMove(move.first, move.second);
board.turnOver();
}
return board.getScore(player);
}
double ucb(double a, double b, double c) {
double C = 0.2;
return 1 - a / b + C * sqrt(2 * log(c) / b);
}
int bestIndex(TreeNode *root) {
std::vector<TreeNode*> children = root->children;
double maxReward = -1e8;
int index = 0;
for (int i = 0; i < root->childrenNum; ++i) {
double curReward =
ucb(children[i]->rewards, children[i]->visitNum, root->visitNum);
if (curReward > maxReward) {
maxReward = curReward;
index = i;
}
}
return index;
}