-
Notifications
You must be signed in to change notification settings - Fork 0
/
MINIMAX.pl
443 lines (371 loc) · 7.74 KB
/
MINIMAX.pl
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Storable qw(dclone);
##simply define one variable to begin
## can't define both
my $mini=1;
my $max;
####global scope####
my @A__=(0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,0,1,0,1,1,1);
my @B__=(0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,0,0,1);
my @C__=(0,0,0,1,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,0,1);
my @D__=(1,0,0,1,0,0,0,1,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0);
my $max_depth =64;
#list of victories branches
my @hs;
# array to contain pruneded parents
my @pruned;
my @tree;
#current branch being traversed
my @branch;
my %stock_values =(
'A'=>10,
'B'=>10,
'C'=>10,
'D'=>10
);
sub init
{
#max
my @player_1=('A', 'B');
#mini
my @player_2=('C', 'D');
push(@branch, \@player_1);
depth(\@tree, \@branch, 0, \@player_1, \@player_2);
}
sub depth
{
my $tree = shift;
my $branch = shift;
my $current_depth= shift;
my $player_1=shift;
my $player_2=shift;
# RETURNS IF PARENT IS IN PRUNED LIST
if(prune($branch, $current_depth))
{
return;
}
#EVALUATES STOCK VALUE BASED ON DEPTH
my ($stock_values, $flag) = stock_evaluate($player_1, $player_2, $current_depth, $branch);
#RETURNS IF COMBINED PROFILE OF A PLAYER IS EQUAL OR LESS THAN ZERO
#IF STOCK HAS A VALUE OF ZERO, OR A COMBINATION OF THE PLAYERS WORTH IS ZERO
if($flag)
{
record_results($branch, $stock_values, $current_depth);
return;
}
#IF MAX DEPTH HAS BEEN REAHED
if ($current_depth >= $max_depth)
{
record_results($branch, $stock_values, $current_depth);
return;
}
#generates one level at a time,
#this is actually breadth
$tree[$current_depth]->[0]= generate($player_1, $player_2);
#add onto our current branch the leftmost node
push($branch, $tree[$current_depth]->[0]->[0] );
depth(
$tree,
$branch,
$current_depth+1,
$tree[$current_depth]->[0]->[0],
compliment($tree[$current_depth]->[0]->[0]->[0], $tree[$current_depth]->[0]->[0]->[1])
);
#already explored leftmost node, remove it from the stack
shift($tree[$current_depth]->[0]);
my $i=0;
foreach(@{$tree[$current_depth]->[0]})
{
pop(@{$branch});
push(@{$branch}, $_);
depth(
# breadth
$_,
# current branch being traversed
$branch,
$current_depth+1,
#player one
$tree[$current_depth]->[0]->[$i],
#player 2
compliment($tree[$current_depth]->[0]->[$i]->[0], $tree[$current_depth]->[0]->[$i]->[1])
);
$i++;
}
pop(@branch);
return;
}
##increments stock or decrements based on value of prediction
sub revalute
{
my $stock_values = $_[4];
if($_[0])
{
$stock_values->{'A'} = $stock_values->{'A'} + 1;
} else
{
$stock_values->{'A'} = $stock_values->{'A'} - 1;
}
if($_[1])
{
$stock_values->{'B'} = $stock_values->{'B'} + 1;
} else
{
$stock_values->{'B'} = $stock_values->{'B'} - 1;
}
if($_[2])
{
$stock_values->{'C'} = $stock_values->{'C'} + 1;
} else
{
$stock_values->{'C'} = $stock_values->{'C'} - 1;
}
if($_[3])
{
$stock_values->{'D'} = $stock_values->{'D'} + 1;
} else
{
$stock_values->{'D'} = $stock_values->{'D'} - 1;
}
return $stock_values;
}
## based on depth this function creates new stock values
## this is to avoid the overhead of saving this hash in every state
## but really is quite sloppy
sub stock_evaluate
{
my $player_1 = shift;
my $player_2 = shift;
my $current_depth = shift;
my %is = %stock_values;
my $ref_val= \%is;
if($current_depth==0)
{
#no need to evalute stock worth
return ($ref_val, 0);
}
my $index=0;
for(my $i=0; $i<$current_depth; $i++)
{
if($index == 31)
{
$index=0;
}
$ref_val = revalute($A__[$index], $B__[$index], $C__[$index], $D__[$index], $ref_val);
if($i)
{
my $traded = arr_return($branch[$i], $branch[$i+1]);
$ref_val->{$traded->[0]} =$ref_val->{$traded->[0]}+1;
$ref_val->{$traded->[1]} =$ref_val->{$traded->[1]}+1;
}
$index++;
}
my $stock_1 = $ref_val->{$player_1->[0]};
my $stock_2 = $ref_val->{$player_1->[1]};
my $stock_3 = $ref_val->{$player_2->[0]};
my $stock_4 = $ref_val->{$player_2->[1]};
if (($stock_1+$stock_2) <= 0)
{
return ($ref_val,1);
}
if($stock_1<=0)
{
return($ref_val,1);
}
if($stock_2<=0)
{
return($ref_val,1);
}
if($stock_3<=0)
{
return($ref_val,1);
}
if($stock_4<=0)
{
return($ref_val,1);
}
if(($stock_3+$stock_4) <= 0)
{
return ($ref_val,1);
}
return ($ref_val,0);
}
## makes a copy of existing data (as a hash)
## pushes the data into an array of victorious moves
## also prints to a csv
sub record_results
{
my $tree= shift;
my @branch=@{$tree};
my $stock=shift;
my $current_depth = shift;
my @data_new = @{ dclone(\@branch) };
my %data_stock = %{ dclone($stock) };
my @last_node = $data_new[$current_depth];
my @parent =@{ dclone(\@branch) };
my $raw = $stock->{$last_node[0]->[0]} + $stock->{$last_node[0]->[1]};
my %has_new =(branch => \@data_new,
stock => \%data_stock,
value=> $raw,
parent=> $parent[1],
depth=> $current_depth
);
if(prune_time($parent[1], $raw))
{
# push(@pruned, $parent[1]);
}else
{
push (@hs, \%has_new);
open(my $fh, '>>', "results.csv");
print $fh "Value,".$raw."\n";
print $fh "depth,". $current_depth."\n";
print $fh "parent(one of the initial 4 moves ),". $parent[1]->[0].$parent[1]->[1]."\n";
print $fh "stock data,A=>,".$data_stock{A}.",B=>,".$data_stock{B}.",C=>,".$data_stock{C}.",B=>,".$data_stock{D};
print $fh "\n";
foreach(@data_new)
{
print $fh $_->[0]. $_->[1].","
}
print $fh "BRANCH END\n\n\n";
close $fh;
}
}
## for all existing values
## determine if the branches parent should be pruned
sub prune_time
{
my $parent = shift;
my $value = shift;
foreach(@hs)
{
# if (array_diff($parent,$_->{'parent'}))
# {
if($mini)
{
if($_->{'value'} < $value)
{
##the value found is greater than a known value
## it's useless lets prune
return 1;
}
}
if($max)
{
if($_->{'value'} > $value)
{
##the value found is less than a known value
## it's useless lets prune
return 1;
}
}
# }
}
return 0;
}
## if a given branches parent
## is in the "pruned" list
## this function will return true
sub prune
{
my $branch = shift;
my $current_depth=shift;
if($current_depth ==0)
{
return 0;
}
foreach(@pruned)
{
if(array_diff($branch[1],$_))
{
return 1;
}
}
return 0;
}
## given player one and two
## it produces the next possible trades
## for player one
## these are the child nodes of the previous trade
sub generate
{
my @tree;
my $player_1 = $_[0];
my $player_2 = $_[1];
foreach(@{$player_1})
{
my $unit = $_;
my @baby_tree;
foreach(@{$player_2})
{
my @ar;
push(@ar, $unit, $_);
push(@tree, \@ar);
}
}
return \@tree;
}
## provides the compliment of a given stock profile
## given a and b it produces c and d
##
sub compliment
{
my @list =('A','B','C','D');
my @compliments;
my $compa = $_[0];
my $compb = $_[1];
foreach(@list)
{
if(!($compa =~ /$_/) && !($compb =~ /$_/) )
{
push(@compliments, $_);
}
}
return(\@compliments);
}
## insures the trade took place correctly
## also determines if two arrays/nodes are identical
##
##
sub array_diff
{
my $arry_1 = shift;
my $arry_2 = shift;
my $num=0;
foreach(@{$arry_1})
{
my $unit = $_;
foreach(@{$arry_2})
{
if($unit eq $_)
{
$num++;
}
}
}
if($num == 2)
{
return 1;
}
return 0;
}
## insures the trade took place correctly
## returns the difference between two arrays/nodes
##
##
sub arr_return
{
my $list1=shift;
my $list2=shift;
my @diff;
my %repeats;
for (@{$list1}, @{$list2}) { $repeats{$_}++ }
for (keys %repeats) {
push @diff, $_ unless $repeats{$_} > 1;
}
return \@diff;
}
init();
print"\n\n\n\n";
print Dumper (\@hs);