-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIG-Problem2_MinMax
272 lines (216 loc) · 8.73 KB
/
AIG-Problem2_MinMax
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import Base.display;
export AbstractGame, Figure5_2, descision, alphabeta_full_search, alphabeta_search, display, albePlayer, startGame;
#Description of the game begins
function argmax(seq::T, fn::Function) where {T <: Vector}
local bElement = seq[1];
local bScrore = fn(bElement);
for element in seq
elementScore = fn(element);
if (elementScore > bScrore)
bElement = element;
bScrore = elementScore;
end
end
return bElement;
end
function if_(boolExpression::Bool, answer1::Any, answer2::Any)
if (boolExpression)
return answer1;
else
return answer2;
end
end
abstract type AbstractGame end;
#Struct can be considered as a class
struct Game <: AbstractGame
initial::String
function Game(initial_state::String)
return new(initial_state);
end
end
#Defining the action function without any implementation
function actions(game::T, state::String) where {T <: AbstractGame}
println("no implementation", typeof(game), "!");
nothing;
end
#Defining the result function without implementation
function result(game::T, state::String, move::String) where {T <: AbstractGame}
println("no implementation", typeof(game), "!");
nothing;
end
#Defining the utility function without implementation
function utility(game::T, state::String, player::String) where {T <: AbstractGame}
println("no implementation", typeof(game), "!");
nothing;
end
function termTest(game::T, state::String) where {T <: AbstractGame}
if (length(actions(game, state)) == 0)
return true;
else
return false;
end
end
#Defining the move function without implementation
function makeMove(game::T, state::String) where {T <: AbstractGame}
println("no implementation", typeof(game), "!");
nothing;
end
function display(game::T, state::String) where {T <: AbstractGame}
println(state);
end
#Class for figure 5.2 Game
struct Figure_52 <: AbstractGame
initial::String
nodes::Dict
utilities::Dict
#Refer to "A" as node and "A1" as edge and continueing letters as Child Nodes and there edges
function Figure_52()
return new("A", Dict([
Pair("A", Dict("A1"=>"B", "A2"=>"C", "A3"=>"D")),
Pair("B", Dict("B1"=>"B1", "B2"=>"B2", "B3"=>"B3")),
Pair("C", Dict("C1"=>"C1", "C2"=>"C2", "C3"=>"C3")),
Pair("D", Dict("D1"=>"D1", "D2"=>"D2", "D3"=>"D3")),
]),
Dict([
Pair("B1", 1), Pair("B2", 8), Pair("B3", 12),
Pair("C1", 6), Pair("C2", 7), Pair("C3", 10),
Pair("D1", 18), Pair("D2", 9), Pair("D3", 5),
]));
end
end
function actions(game::Figure_52, state::String)
return collect(keys(get(game.nodes, state, Dict())));
end
function result(game::Figure_52, state::String, move::String)
return game.nodes[state][move];
end
function utility(game::Figure_52, state::String, player::String)
if (player == "max")
return game.utilities[state];
else
return -game.utilities[state];
end
end
function termTest(game::Figure_52, state::String)
return !(state in ["A", "B", "C", "D"]);
end
function makeMove(game::Figure_52, state::String)
return if_((state in ["B", "C", "D"]), "min", "max");
end
function minmaxMaxValue(game::T, player::String, state::String) where {T <: AbstractGame}
if (termTest(game, state))
return utility(game, state, player)
end
local value::Float64 = -Inf64;
value = reduce(max, vcat(value, collect(minmaxMinValue(game, player, result(game, state, action))
for action in actions(game, state))));
return value;
end
function minmaxMinValue(game::T, player::String, state::String) where {T <: AbstractGame}
if (termTest(game, state))
return utility(game, state, player);
end
local value::Float64 = Inf64;
value = reduce(min, vcat(value, collect(minmaxMaxValue(game, player, result(game, state, action))
for action in actions(game, state))));
return value;
end
#Scroll down to the leaf node
function descision(state::String, game::T) where {T <: AbstractGame}
local player = makeMove(game, state);
return argmax(actions(game, state),
(function(action::String,; relevantGame::AbstractGame=game, relevantPlayer::String=player, relevantState::String=state)
return minmaxMinValue(relevantGame, relevantPlayer, result(relevantGame, relevantState, action));
end));
end
function albeFullSearch_maxValue(game::T, player::String, state::String, alpha::Number, beta::Number) where {T <: AbstractGame}
if (termTest(game, state))
return utility(game, state, player)
end
local value::Float64 = -Inf64;
for action in actions(game, state)
value = max(value, albeFullSearch_minValue(game, player, result(game, state, action), alpha, beta));
if (value >= beta)
return value;
end
alpha = max(alpha, value);
end
return value;
end
function albeFullSearch_minValue(game::T, player::String, state::String, alpha::Number, beta::Number) where {T <: AbstractGame}
if (termTest(game, state))
return utility(game, state, player);
end
local value::Float64 = Inf64;
for action in actions(game, state)
value = min(value, albeFullSearch_maxValue(game, player, result(game, state, action), alpha, beta));
if (value <= alpha)
return value;
end
beta = min(beta, value);
end
return value;
end
function albeSearch_maxValue(game::T, player::String, cutoff::Function, evaluFunction::Function, state::String, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff(state, depth))
return evaluFunction(state);
end
local value::Float64 = -Inf64;
for action in actions(game, state)
value = max(value, albeSearch_minValue(game, player, cutoff, evaluFunction, result(game, state, action), alpha, beta, depth + 1));
if (value >= beta)
return value;
end
alpha = max(alpha, value);
end
return value;
end
function albeSearch_minValue(game::T, player::String, cutoff::Function, evaluFunction::Function, state::String, alpha::Number, beta::Number, depth::Int64) where {T <: AbstractGame}
if (cutoff(state, depth))
return evaluFunction(state);
end
local value::Float64 = Inf64;
for action in actions(game, state)
value = min(value, albeSearch_maxValue(game, player, cutoff, evaluFunction, result(game, state, action), alpha, beta, depth + 1));
if (value >= alpha)
return value;
end
beta = min(alpha, value);
end
return value;
end
#Alphabeta search begins with pruning by cutting off edges
function alphabeta_search(state::String, game::T; d::Int64=4, cutoff::Union{Nothing, Function}=nothing, evaluFunction::Union{Nothing, Function}=nothing) where {T <: AbstractGame}
local player::String = makeMove(game, state);
if (typeof(cutoff) <: Nothing)
cutoff = (function(state::String, depth::Int64; dvar::Int64=d, relevantGame::AbstractGame=game)
return ((depth > dvar) || termTest(relevantGame, state));
end);
end
if (typeof(evaluFunction) <: Nothing)
evaluFunction = (function(state::String, ; relevantGame::AbstractGame=game, relevantPlayer::String=player)
return utility(relevantGame, state, relevantPlayer);
end);
end
return argmax(actions(game, state),
(function(action::String,; relevantGame::AbstractGame=game, relevantState::String=state, relevantPlayer::String=player, cutoff_test::Function=cutoff, eval_fn::Function=evaluFunction)
return albeSearch_minValue(relevantGame, relevantPlayer, cutoff_test, eval_fn, result(relevantGame, relevantState, action), -Inf64, Inf64, 0);
end));
end
function albePlayer(game::T, state::String) where {T <: AbstractGame}
return alphabeta_search(state, game);
end
function startGame(game::T, players::Vararg{Function}) where {T <: AbstractGame}
state = game.initial;
while (true)
for player in players
move = player(game, state);
state = result(game, state, move);
if (termTest(game, state))
return utility(game, state, makeMove(game, game.initial));
end
end
end
end
game = Figure_52()
println(startGame(game, albePlayer, albePlayer))