-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaxn
More file actions
180 lines (147 loc) · 6.51 KB
/
maxn
File metadata and controls
180 lines (147 loc) · 6.51 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
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
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_ALGORITHM_MAXN_HPP
#define GTL_ALGORITHM_MAXN_HPP
// Summary: The MaxN search algorithm to find the best possible moves for an N player game with known information. [wip]
#if defined(_MSC_VER)
#pragma warning(push, 0)
#endif
#include <array>
#include <vector>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
namespace gtl {
template <typename state_type>
class abstract_state {
public:
constexpr static const unsigned int entities = state_type::entities;
public:
virtual unsigned int get_entity_id() const = 0;
virtual std::vector<state_type> get_options() const = 0;
virtual std::array<int, abstract_state::entities> get_scores() const = 0;
};
template <typename state_type>
class maxn final {
public:
constexpr static const unsigned int entities = state_type::entities;
private:
class evaluation_type {
public:
unsigned int entity;
std::vector<state_type> options;
std::vector<std::array<int, maxn::entities>> scores;
unsigned int current_option_index;
public:
evaluation_type() = default;
evaluation_type(const state_type& state, int max_score) {
this->entity = state.get_entity_id();
this->options = state.get_options();
std::array<int, maxn::entities> default_score;
default_score.fill(-(max_score + 1));
this->scores = std::vector<std::array<int, maxn::entities>>(this->options.size(), default_score);
this->current_option_index = 0;
}
};
public:
bool search(const state_type& state, unsigned int max_depth, int max_score, state_type& option) {
// Need to be able to create a larger score.
if (max_score <= -(max_score + 1)) {
// TODO: Should be an assert.
return false;
}
unsigned int depth = 0;
bool heading_down = true;
std::vector<evaluation_type> stack(max_depth);
// Insert initial state.
evaluation_type& root = stack[0];
root = evaluation_type(state, max_score);
++depth;
if (stack[0].options.empty()) {
return false;
}
while (true) {
unsigned int depth_parent = depth - 1;
// If we just moved down.
if (heading_down) {
// Handy variables.
evaluation_type& current = stack[depth];
evaluation_type& parent = stack[depth_parent];
const state_type& current_state = parent.options[parent.current_option_index];
// Check if we have reached max depth yet.
if (depth < max_depth) {
// Save to the stack for further processing.
current = evaluation_type(current_state, max_score);
// Check if there is more depth to search.
if (!current.options.empty()) {
// Keep heading down.
++depth;
continue;
}
}
// Can't/Won't go down, because either we're at max depth or have no moves, so score the state.
parent.scores[parent.current_option_index] = current_state.get_scores();
// We're going up now.
heading_down = false;
// Reduce the depth.
--depth;
}
else {
// Check if we have no more options left to try.
if (stack[depth].current_option_index == stack[depth].options.size() - 1) {
// If we're back at the top we're done!
if (depth == 0) {
break;
}
// Handy variables.
const evaluation_type& current = stack[depth];
evaluation_type& parent = stack[depth - 1];
// Calculate move on this level.
unsigned int index_max = 0;
for (unsigned int index = 1; index < current.options.size(); ++index) {
if (current.scores[index_max][current.entity] < current.scores[index][current.entity]) {
index_max = index;
}
// TODO: Could make it min opponents if ties are found.
}
// Save best to parent.
parent.scores[parent.current_option_index] = current.scores[index_max];
// Keep heading up.
--depth;
continue;
}
// Otherwise go down the next branch.
else {
// Finally increment the current option index of the current depth.
++(stack[depth].current_option_index);
// We're going down now.
heading_down = true;
// Increase the depth.
++depth;
}
}
}
unsigned int index_max = 0;
for (unsigned int index = 1; index < root.options.size(); ++index) {
if (root.scores[index_max][root.entity] < root.scores[index][root.entity]) {
index_max = index;
}
// TODO: Could make it min opponents if ties are found.
}
option = root.options[index_max];
return true;
}
};
}
#endif // GTL_ALGORITHM_MAXN_HPP