-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.cpp
More file actions
137 lines (122 loc) · 4 KB
/
functions.cpp
File metadata and controls
137 lines (122 loc) · 4 KB
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
#include "functions.h"
using namespace std;
node* generateSearchTree(int depth, int branch_factor, node *root){
node *ind;
if(root == NULL){
ind = new node;
//ind->sibling = NULL;
ind->parent = NULL;
ind->min_or_max = true;
root = ind;
}
if(depth == 1){
root->children = NULL;
root->value = rand()%1024;
root->num_of_children = 0;
return root;
}
ind = new node[branch_factor];
root->children = ind;
root->num_of_children = branch_factor;
for(int i = 0; i < branch_factor; i++){
(ind+i)->parent = root;
(ind+i)->min_or_max = !(root->min_or_max);
generateSearchTree(depth - 1, branch_factor, (ind+i));
/*if(i < branch_factor){
ind->sibling = new node;
ind = ind->sibling;
}
else ind->sibling = NULL;*/
}
return root;
}
int AlphaBeta(node* currentNode, int alpha, int beta, bool maximisingPlayer){
int bestValue, childValue;
if(currentNode->children == 0){
bestValue = currentNode->value;
}
else if(maximisingPlayer){
bestValue = alpha;
// Recurse for all children of node.
node *index = currentNode->children;
for(int i = 0; i < currentNode->num_of_children; i++){
childValue = AlphaBeta(index+i, bestValue, beta, false);
bestValue = std::max(bestValue, childValue);
if(beta <= bestValue){
break;
}
//index = index->sibling;
}
}
else{
bestValue = beta;
node *index = currentNode->children;
// Recurse for all children of node.
for(int i = 0; i < currentNode->num_of_children; i++){
childValue = AlphaBeta(index+i, alpha, bestValue, true);
bestValue = std::min(bestValue, childValue);
if(bestValue <= alpha){
break;
}
//index = index->sibling;
}
}
return bestValue;
}
int MinMax(node* currentNode, int alpha, int beta){
int bestValue, childValue;
if(currentNode->children == 0){
return currentNode->min_or_max ? currentNode->value : currentNode->value * (-1);
}
bestValue = alpha;
// Recurse for all children of node.
node *index = currentNode->children;
for(int i = 0; i < currentNode->num_of_children; i++){
childValue = MinMax(index+i, -beta, -bestValue) * (-1);
bestValue = std::max(bestValue, childValue);
if(bestValue >= beta) return bestValue;
//index = index->sibling;
}
return bestValue;
}
int MinMax(node* currentNode){
int bestValue, childValue;
if (currentNode->children == 0)
return currentNode->min_or_max ? currentNode->value : currentNode->value * (-1);
bestValue = INT_MIN;
// Recurse for all children of node.
node *index = currentNode->children;
for(int i = 0; i < currentNode->num_of_children; i++){
childValue = MinMax(index)*(-1);
bestValue = std::max(bestValue, childValue);
//if(bestValue >= beta) return bestValue;
}
return bestValue;
}
void* parallel_(void* tmp){
alphabeta* target = (alphabeta*)tmp;
for (int i = target->idx; i < target->branch_factor; i+=target->num_of_thread){
//cout <<target->idx<<' '<<i<<' ' <<target->branch_factor<<' '<<target->num_of_thread<<endl;
int childValue = AlphaBeta(target->temp+i, INT_MIN, INT_MAX, false);
//cout<<target->idx<<":"<<childValue<<endl;
//target->result[target->idx] = std::max(target->result[target->idx], childValue);
if (childValue > *(target->result+target->idx*sizeof(int))) *(target->result+target->idx*sizeof(int)) = childValue;
//cout<<*(target->result+target->idx*sizeof(int))<<endl;
}
return NULL;
}
int newparaAlphaBeta(node* currentNode, int branch_factor, int num_of_thread, int* result, pthread_t* thr, alphabeta** t){
int bestValue = INT_MIN;
for(int i = 0; i < num_of_thread; i++){
pthread_create(&thr[i], NULL, ¶llel_, (void*)t[i]);
}
for(int i = 0; i < num_of_thread; i++)
pthread_join(thr[i], NULL);
for(int i = 0; i < num_of_thread; i++){
//cout<<*(result+i*sizeof(int))<<' ';
bestValue = std::max(bestValue, *(result+i*sizeof(int)));
}
//cout<<endl;
//pthread_barrier_destroy(&barr);
return bestValue;
}