-
Notifications
You must be signed in to change notification settings - Fork 0
/
Evaluator.cpp
170 lines (156 loc) · 4.92 KB
/
Evaluator.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
161
162
163
164
165
166
167
168
169
170
/* =============================================================================
# FileName: Evaluator.cpp
# Desc: Base class for evaluation function
# Author: YanlongLi
# Email: [email protected]
# HomePage: http://www.yanlongli.me
# Created: 2017-10-14 22:09:02
# Version: 0.0.1
# LastChange: 2017-10-14 22:09:02
# History:
# 0.0.1 | YanlongLi | init
============================================================================= */
#include "Evaluator.h"
#include <cstdlib>
#include <iostream>
static double simpleEvaluation(const Board& board);
/*
* 2*(number_of_own_pieces_remaining)
*/
static double defensiveHeuristic(const Board& board, int player);
/*
* 2*(30 - number_of_opponent_pieces_remaining)
*/
static double offensiveHeuristic(const Board& board, int player);
/*
* average distance of n workers that far from base
*/
static double distanceFromBase(const Board& board, int n, int player);
/*
* Evaluator
*/
double Evaluator::randomFloat() {
return rand() / (float)RAND_MAX;
}
//////////////////////////////////////////////////////////////////////////////
///
/////////////////////////////////////////////////////////////////////////////
/*
* Simple Evaluator
*/
double SimpleEvaluator::maxerEvaluation(const Board& board) {
return simpleEvaluation(board) + randomFloat();
}
double SimpleEvaluator::minerEvaluation(const Board& board) {
return simpleEvaluation(board) + randomFloat();
}
/*
* DefensiveHeuristicEvaluator
*/
double DefensiveHeuristicEvaluator::maxerEvaluation(const Board& board) {
return defensiveHeuristic(board, 1) + randomFloat();
}
double DefensiveHeuristicEvaluator::minerEvaluation(const Board& board) {
return defensiveHeuristic(board, -1) + randomFloat();
}
/*
* OffensiveHeuristicEvaluator
*/
double OffensiveHeuristicEvaluator::maxerEvaluation(const Board& board) {
return offensiveHeuristic(board, 1) + randomFloat();
}
double OffensiveHeuristicEvaluator::minerEvaluation(const Board& board) {
return offensiveHeuristic(board, -1) + randomFloat();
}
/*
* DistanceFromBaseEvaluator
*/
DistanceFromBaseEvaluator::DistanceFromBaseEvaluator(int _top): top(_top) {
}
double DistanceFromBaseEvaluator::maxerEvaluation(const Board& board) {
return distanceFromBase(board, top, 1) + randomFloat();
}
double DistanceFromBaseEvaluator::minerEvaluation(const Board& board) {
return distanceFromBase(board, top, -1) + randomFloat();
}
//////////////////////////////////////////////////////////////////////////////
///
/////////////////////////////////////////////////////////////////////////////
/*
* help functions
*/
static double simpleEvaluation(const Board& board) {
int sum = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
sum += board.getValue(i, j);
return sum;
}
static double defensiveHeuristic(const Board& board, int player) {
if (player != 1 && player != -1) {
fprintf(stderr, "invalid player value: %d\n", player);
exit(-1);
}
int nOne = 0, nMinusOne = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board.getValue(i, j) == 1) nOne ++;
else if (board.getValue(i, j) == -1) nMinusOne ++;
}
}
return 2 * (player == 1 ? nOne : nMinusOne);
}
static double offensiveHeuristic(const Board& board, int player) {
if (player != 1 && player != -1) {
fprintf(stderr, "invalid player value: %d\n", player);
exit(-1);
}
int nOne = 0, nMinusOne = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board.getValue(i, j) == 1) nOne ++;
else if (board.getValue(i, j) == -1) nMinusOne ++;
}
}
return 2 * (player == 1 ? nMinusOne : nOne);
}
static double distanceFromBase(const Board& board, int n, int player) {
if (player != 1 && player != -1) {
fprintf(stderr, "invalid player value: %d\n", player);
exit(-1);
}
int count = 0, sum = 0;
if (player == 1) {
for (int k = N * N - 1; k >= 0; k--) {
int i = k / N, j = k % N;
if (board.getValue(i, j) == 1) {
count ++;
sum += j * j;
if (count == n) {
break;
}
}
}
if (count == 0) {
fprintf(stderr, "invalid board, no type %d worker\n", player);
exit(-1);
}
return sum / (double)count;
} else {
for (int k = 0; k < N * N; k++) {
int i = k / N, j = k % N;
if (board.getValue(i, j) == 1) {
count ++;
sum += (N - 1 - j) * (N - 1 - j);
if (count == n) {
break;
}
}
}
if (count == 0) {
fprintf(stderr, "invalid board, no type %d worker\n", player);
exit(-1);
}
return sum / (double)count;
}
}