/
COP.pm
2422 lines (2091 loc) · 80.7 KB
/
COP.pm
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# Testing:
# gibsonized players get the bye
# odd contender groups
# class prize contenders in last round
# pair around an existing pairing
package TSH::Command::COP;
use strict;
use warnings;
use threads;
use Data::Dumper;
use File::Basename;
use Graph::Matching qw(max_weight_matching);
use TSH::Command::ShowPairings;
use TSH::PairingCommand;
use File::Copy;
our (@ISA) = qw(TSH::PairingCommand);
use constant PROHIBITIVE_WEIGHT => 1000000;
use constant BYE_PLAYER_ID => 0;
use constant INITIAL_FACTOR => 1000000;
use constant SINGULAR_CHILD_ROUNDS_REMAINING => 2;
use constant UNDEFINED_CLASS => 'UNDEFINED_CLASS';
=pod
=head1 NAME
TSH::Command::COP - implement the C<tsh> COP command
=head1 SYNOPSIS
my $command = new TSH::Command::COP;
my $argsp = $command->ArgumentTypes();
my $helptext = $command->Help();
my (@names) = $command->Names();
$command->Run($tournament, @parsed_arguments);
=head1 ABSTRACT
TSH::Command::COP is a subclass of TSH::Command.
=cut
=head1 DESCRIPTION
=over 4
=cut
sub initialise ($$$$);
sub new ($);
sub Run ($$@);
=item $parserp->initialise()
Used internally to (re)initialise the object.
=cut
sub initialise ($$$$) {
my $this = shift;
my $path = shift;
my $namesp = shift;
my $argtypesp = shift;
$this->{'help'} = <<'EOF';
Use the COP command to automatically pair a round.
EOF
$this->{'names'} = [qw(cop)];
$this->{'argtypes'} = [qw(BasedOnRound Division)];
# print "names=@$namesp argtypes=@$argtypesp\n";
return $this;
}
sub new ($) { return TSH::Utility::new(@_); }
=item $command->Run($tournament, @parsed_args)
Should run the command in the context of the given
tournament with the specified parsed arguments.
=cut
sub Run ($$@) {
my $this = shift;
my $tournament = shift;
my ( $sr, $dp ) = @_;
my $sr0 = $sr - 1;
# Create log directory
my $log_dir =
sprintf( "%s/cop_logs/", $tournament->Config()->{root_directory} );
mkdir $log_dir;
if ( !-e $log_dir ) {
$tournament->TellUser( 'eapfail',
"failed to create directory $log_dir" );
return 0;
}
my $last_paired_round0 = $dp->LastPairedRound0();
my $round_to_pair0 = $dp->FirstUnpairedRound0();
my $timestamp = get_timestamp();
my $division_name = $dp->Name();
my $sr1 = $sr0 + 1;
# +2 because:
# 0-index to 1-index for humans, and
# TSH pairs the next available round
my $round_to_pair1 = $round_to_pair0 + 1;
my $number_of_rounds = $dp->MaxRound0() + 1;
if ( $round_to_pair1 > $number_of_rounds ) {
$tournament->TellUser( 'ebigrd', $round_to_pair1, $number_of_rounds );
return 0;
}
my $log_filename =
"$log_dir$timestamp"
. "_$division_name"
. "_round_"
. $round_to_pair1
. "_based_on_$sr1" . ".log";
my $html_log_filename =
$log_dir . '../html/' . "$division_name$round_to_pair1" . '_cop.log';
# Extract TSH config vars
my $gibson_spread = $tournament->Config()->Value('gibson_spread');
if ( !defined $gibson_spread || scalar @{$gibson_spread} == 0 ) {
$tournament->TellUser( 'ebadconfigarray',
'gibson_spread', $gibson_spread );
return 0;
}
my $number_of_sims = $tournament->Config()->Value('simulations');
if ( !defined $number_of_sims ) {
$tournament->TellUser( 'ebadconfigentry', 'simulations' );
return 0;
}
my $always_wins_number_of_sims =
$tournament->Config()->Value('always_wins_simulations');
if ( !defined $always_wins_number_of_sims ) {
$tournament->TellUser( 'ebadconfigentry', 'always_wins_simulations' );
return 0;
}
my $control_loss_thresholds =
$tournament->Config()->Value('control_loss_thresholds');
if ( !defined $control_loss_thresholds
|| scalar @{$control_loss_thresholds} == 0 )
{
$tournament->TellUser( 'ebadconfigarray', 'control_loss_thresholds',
$control_loss_thresholds );
return 0;
}
my $hopefulness = $tournament->Config()->Value('hopefulness');
if ( !defined $hopefulness || scalar @{$hopefulness} == 0 ) {
$tournament->TellUser( 'ebadconfigarray', 'hopefulness', $hopefulness );
return 0;
}
my ( $lowest_ranked_payout, $lowest_ranked_class_payouts ) =
get_lowest_ranked_payouts( $tournament->Config(), $division_name );
if ( $lowest_ranked_payout < 0 ) {
$tournament->TellUser( 'ebadconfigentry', 'prizes' );
return 0;
}
my $control_loss_activation_round =
$tournament->Config()->Value('control_loss_activation_round');
if ( !defined $control_loss_activation_round ) {
$tournament->TellUser( 'ebadconfigentry',
'control_loss_activation_round' );
return 0;
}
my $number_of_threads = $tournament->Config()->Value('cop_threads');
if ( ( !( defined $number_of_threads ) ) || $number_of_threads < 1 ) {
$number_of_threads = 1;
}
my $disallow_repeat_byes = !!$tournament->Config()->Value('disallow_repeat_byes');
my %times_played = ();
my %previous_pairing_hash = ();
# Iterate through the players in a division
my @players = $dp->Players();
my $number_of_players = scalar @players;
my @tournament_players = ();
my $player_index = 0;
my $top_class;
my %prepaired_players = ();
for ( my $i = 0 ; $i < $number_of_players ; $i++ ) {
my $player = $players[$i];
my $player_class = $player->Class();
if ( !defined $player_class ) {
$player_class = UNDEFINED_CLASS;
}
my $player_id = $player->ID();
if ( $player_id == 1 ) {
$top_class = $player_class;
}
if ( !$player->Active() ) {
next;
}
for ( my $j = $i + 1 ; $j < $number_of_players ; $j++ ) {
my $opponent = $players[$j];
if ( !$opponent->Active() ) {
next;
}
my $opponent_id = $opponent->ID();
my $number_of_times_played =
$player->CountRoundRepeats( $opponent, $last_paired_round0 );
my $number_of_times_played_excluding_last_round =
$player->CountRoundRepeats( $opponent, $last_paired_round0 - 1 );
my $played_last_round = $number_of_times_played -
$number_of_times_played_excluding_last_round;
if ( $played_last_round > 1 ) {
$tournament->TellUser(
'eapfail',
sprintf(
"these players played more than once last round: %s and %s (%d)",
$player->PrettyName(), $opponent->PrettyName(),
$played_last_round
)
);
return 0;
}
my $times_played_key =
create_times_played_key( $player_id, $opponent_id );
$times_played{$times_played_key} = $number_of_times_played;
if ( $played_last_round == 1 ) {
$previous_pairing_hash{$times_played_key} = 1;
}
}
my $times_given_bye_key =
create_times_played_key( $player_id, BYE_PLAYER_ID );
# Count the byes by up to the based on round
# (There does not seem to be an existing Player method for this)
my $byes = 0;
for ( my $round = 0 ; $round <= $round_to_pair1 - 1 ; $round++ ) {
my $opponent = $player->{pairings}->[$round];
if ( ( defined $opponent ) && $opponent == 0 ) {
$byes++;
}
}
$times_played{$times_given_bye_key} = $byes;
push @tournament_players, new_tournament_player(
$player_id,
$player->PrettyName(),
$player_class,
$player_index,
# Wins count as 2 and draws count as 1 to
# keep everything in integers.
$player->RoundWins($sr0) * 2,
$player->RoundSpread($sr0), 0
);
$player_index++;
my $actual_opponent_id = $player->OpponentID($round_to_pair0);
if ( defined $actual_opponent_id ) {
$prepaired_players{$player_id} = $actual_opponent_id;
}
}
# Create the special config for cop
my $cop_config = {
log_filename => $log_filename,
html_log_filename => $html_log_filename,
number_of_sims => $number_of_sims,
number_of_threads => $number_of_threads,
number_of_rounds => $number_of_rounds,
round_to_pair => $round_to_pair0,
prepaired_players => \%prepaired_players,
always_wins_number_of_sims => $always_wins_number_of_sims,
control_loss_thresholds => extend_tsh_config_array(
$control_loss_thresholds, $number_of_rounds
),
control_loss_activation_round => $control_loss_activation_round - 1,
number_of_rounds_remaining => ( $number_of_rounds - 1 ) - $sr0,
lowest_ranked_payout => $lowest_ranked_payout,
lowest_ranked_class_payouts => $lowest_ranked_class_payouts,
top_class => $top_class,
cumulative_gibson_spreads =>
get_cumulative_gibson_spreads( $gibson_spread, $number_of_rounds ),
gibson_spreads =>
extend_tsh_config_array( $gibson_spread, $number_of_rounds ),
hopefulness =>
extend_tsh_config_array( $hopefulness, $number_of_rounds ),
bye_active => 0,
disallow_repeat_byes => $disallow_repeat_byes,
};
my ( $id_pairings, $warnings ) = cop(
$cop_config, \@tournament_players,
\%times_played, \%previous_pairing_hash
);
for ( my $i = 0 ; $i < scalar @{$warnings} ; $i++ ) {
$tournament->TellUser( 'eapfail', $warnings->[$i] );
}
my $setupp = $this->SetupForPairings(
'division' => $dp,
'source0' => $sr0
) or return 0;
my $target0 = $setupp->{'target0'};
for ( my $i = 0 ; $i < scalar @{$id_pairings} ; $i++ ) {
my $player_id = $id_pairings->[$i]->[0];
my $opponent_id = $id_pairings->[$i]->[1];
$dp->Pair( $player_id, $opponent_id, $target0, 1 );
}
$this->TidyAfterPairing($dp);
# Automatically show the pairings
my $show_pairings_command =
new TSH::Command::ShowPairings( 'noconsole' => 1 );
$show_pairings_command->Run( $tournament, $round_to_pair1, $dp );
}
=back
=cut
=head1 BUGS
The number of byes each player has is based on the most recent
round as opposed to the provided based on round.
=cut
sub log_info {
my ( $config, $content ) = @_;
if ( !$config->{log_filename} ) {
return;
}
my $fh;
my $file_opened = open( $fh, '>>', $config->{log_filename} );
if ( !$file_opened ) {
printf( "could not write to file %s: %s\n",
$config->{log_filename}, $! );
return;
}
print $fh $content;
close($fh);
}
sub copy_log_to_html_directory {
my ($config) = @_;
if ( !$config->{log_filename} ) {
log_info( $config,
"Could not copy to html directory: no html log filename specified\n"
);
return;
}
my $success = copy( $config->{log_filename}, $config->{html_log_filename} );
if ( !$success ) {
log_info(
$config,
sprintf(
"\nCould not copy %s to %s: %s\n",
$config->{log_filename},
$config->{html_log_filename}, $!
)
);
}
else {
log_info(
$config,
sprintf(
"\nSuccessfully copied %s to %s\n",
$config->{log_filename},
$config->{html_log_filename}
)
);
}
}
sub get_timestamp {
my ( $sec, $min, $hour, $day, $month, $year ) = localtime();
$year += 1900;
$month = sprintf( "%02d", $month + 1 );
$day = sprintf( "%02d", $day );
$hour = sprintf( "%02d", $hour );
$min = sprintf( "%02d", $min );
$sec = sprintf( "%02d", $sec );
my $timestamp = "$year\_$month\_$day\_$hour\_$min\_$sec";
return $timestamp;
}
# Create new tournament results
# The tournament results are a 2-d array
# represented as a 1-d array. Each row
# represents a player and each column
# represents the place that player achieved
# by the end of the tournament. The count
# is the number of simulations performed.
sub new_tournament_results {
my $number_of_players = shift;
my $self = {
number_of_players => $number_of_players,
array => [ (0) x ( $number_of_players * $number_of_players ) ],
count => 0
};
return $self;
}
sub record_tournament_results {
my ( $tournament_results, $tournament_players ) = @_;
for ( my $i = 0 ; $i < $tournament_results->{number_of_players} ; $i++ ) {
my $player = $tournament_players->[$i];
$tournament_results->{array}
->[ ( $tournament_results->{number_of_players} * $player->{index} ) +
$i ] += 1;
}
$tournament_results->{count} += 1;
}
sub get_tournament_result {
my ( $tournament_results, $player, $place ) = @_;
return $tournament_results->{array}
->[ ( $tournament_results->{number_of_players} * $player->{index} ) +
$place ];
}
# Player scores
sub new_player_scores {
my ( $name, $index, $opponent_indexes, $scores ) = @_;
my $self = {
name => $name,
index => $index,
opponent_indexes => $opponent_indexes,
scores => $scores
};
return $self;
}
# Tournament players
sub new_tournament_player {
my ( $id, $name, $class, $index, $wins, $spread, $is_bye ) = @_;
my $self = {
id => $id,
name => $name,
class => $class,
index => $index,
# The start_* fields are used to reset the players
# to their original record when a simulation is finished.
start_wins => $wins,
# Wins are worth 2 and draws are worth 1, to keep everything
# in integers.
wins => $wins,
start_spread => $spread,
spread => $spread,
is_bye => $is_bye,
};
return $self;
}
sub copy_tournament_players {
my ($tournament_players) = @_;
my $number_of_players = scalar(@$tournament_players);
my @new_tournament_players = ();
for ( my $i = 0 ; $i < $number_of_players ; $i++ ) {
my $tournament_player = $tournament_players->[$i];
push @new_tournament_players,
new_tournament_player(
$tournament_player->{id}, $tournament_player->{name},
$tournament_player->{class}, $tournament_player->{index},
$tournament_player->{wins}, $tournament_player->{spread},
$tournament_player->{is_bye}
);
}
return \@new_tournament_players;
}
sub reset_tournament_player {
my $tournament_player = shift;
$tournament_player->{wins} = $tournament_player->{start_wins};
$tournament_player->{spread} = $tournament_player->{start_spread};
}
sub add_bye_player {
my ( $tournament_players, $bye_player_index ) = @_;
# This display index can be the same as the original index
push @{$tournament_players},
new_tournament_player( BYE_PLAYER_ID, 'BYE', 'X', $bye_player_index, 0,
0, 1 );
}
# Extract lowest payout rank
sub get_lowest_ranked_payouts {
my ( $tsh_config, $division_name ) = @_;
my $prizes_config = $tsh_config->{prizes};
my $lowest_ranked_payout = -1;
my %lowest_ranked_class_payouts = ();
for ( my $i = 0 ; $i < scalar @{$prizes_config} ; $i++ ) {
my $prize_specification = $prizes_config->[$i];
my $class = $prize_specification->{class};
my $division = $prize_specification->{division};
my $type = $prize_specification->{type};
# Convert from 1-index to 0-index
if ( $division
&& uc($division) eq uc($division_name)
&& $type eq 'rank' )
{
my $place = $prize_specification->{subtype} - 1;
# This is a place or class prize
if (
( defined $class )
&& ( !defined $lowest_ranked_class_payouts{$class}
|| $place > $lowest_ranked_class_payouts{$class} )
)
{
$lowest_ranked_class_payouts{$class} = $place;
}
elsif ( ( !defined $class ) && $place > $lowest_ranked_payout ) {
$lowest_ranked_payout = $place;
}
}
}
return $lowest_ranked_payout, \%lowest_ranked_class_payouts;
}
# Gibson spread
sub get_cumulative_gibson_spreads {
my ( $gibson_spread, $number_of_rounds ) = @_;
my $number_of_gibson_spreads = scalar @{$gibson_spread};
my @cumulative_gibson_spreads = (0) x ($number_of_rounds);
my $last_gibson_spread;
for ( my $i = 0 ; $i < $number_of_rounds ; $i++ ) {
if ( $i == 0 ) {
$cumulative_gibson_spreads[$i] = $gibson_spread->[$i] * 2;
$last_gibson_spread = $gibson_spread->[$i] * 2;
}
else {
$cumulative_gibson_spreads[$i] =
$cumulative_gibson_spreads[ $i - 1 ];
if ( $i < $number_of_gibson_spreads ) {
$cumulative_gibson_spreads[$i] += $gibson_spread->[$i] * 2;
$last_gibson_spread = $gibson_spread->[$i] * 2;
}
else {
$cumulative_gibson_spreads[$i] += $last_gibson_spread;
}
}
}
return \@cumulative_gibson_spreads;
}
# Control loss
sub extend_tsh_config_array {
my ( $array, $number_of_rounds ) = @_;
my $number_of_entries = scalar @{$array};
my @full_array = (0) x ($number_of_rounds);
my $last_entry;
for ( my $i = 0 ; $i < $number_of_rounds ; $i++ ) {
if ( $i < $number_of_entries ) {
$full_array[$i] = $array->[$i];
$last_entry = $array->[$i];
}
else {
$full_array[$i] = $last_entry;
}
}
return \@full_array;
}
sub config_array_to_string {
my ($array_ref) = @_;
my $ret = '[';
for ( my $i = 0 ; $i < scalar(@$array_ref) ; $i++ ) {
$ret .= sprintf( "%4s", $array_ref->[$i] ) . ', ';
}
$ret = substr( $ret, 0, -2 );
$ret .= ']';
return $ret;
}
sub config_to_string {
my ($config) = @_;
my $ret = "COP Config:\n\n";
$ret .= sprintf( "%31s %s\n", "Log Filename:", $config->{log_filename} );
$ret .= sprintf( "%31s %s\n", "Pairing for Round:",
$config->{round_to_pair} + 1 );
$ret .= sprintf( "%31s %s\n",
"Total Number of Rounds:",
$config->{number_of_rounds} );
$ret .= sprintf( "%31s %s\n",
"Rounds Remaining:",
$config->{number_of_rounds_remaining} );
$ret .= sprintf( "%31s %s\n", "Simulations:", $config->{number_of_sims} );
$ret .= sprintf( "%31s %s\n",
"Always Wins Simulations:",
$config->{always_wins_number_of_sims} );
$ret .= sprintf( "%31s %s\n",
"Lowest Ranked Cash Payout:",
$config->{lowest_ranked_payout} + 1 );
foreach my $class ( sort keys %{ $config->{lowest_ranked_class_payouts} } )
{
$ret .= sprintf( "%31s %s\n",
sprintf( "Lowest Ranked Class Payout %s:", $class ),
$config->{lowest_ranked_class_payouts}->{$class} + 1 );
}
$ret .= sprintf( "%31s %s\n",
"Control Loss Activation Round:",
$config->{control_loss_activation_round} + 1 );
$ret .= sprintf( "%31s %s\n", "Threads:", $config->{number_of_threads} );
my $active_bye_text = 'INACTIVE';
if ( $config->{bye_active} ) {
$active_bye_text = 'ACTIVE';
}
$ret .= sprintf( "%31s %s\n", "Bye Active:", $active_bye_text );
my $active_drb_text = 'INACTIVE';
if ( $config->{disallow_repeat_byes} ) {
$active_drb_text = 'ACTIVE';
}
$ret .= sprintf( "%31s %s\n", "Disallow Repeat Byes:", $active_drb_text );
# Write a marker designating which array values are being used
# for this round.
$ret .= sprintf( "%31s %s",
'(* denotes values in use)',
( ' ' x ( $config->{number_of_rounds_remaining} - 1 ) )
. " *\n" );
$ret .= sprintf( "%31s %s\n",
"Cumulative Gibson Spreads:",
config_array_to_string( $config->{cumulative_gibson_spreads} ) );
$ret .= sprintf( "%31s %s\n",
"Gibson Spreads:",
config_array_to_string( $config->{gibson_spreads} ) );
$ret .= sprintf( "%31s %s\n",
"Control Loss Thresholds:",
config_array_to_string( $config->{control_loss_thresholds} ) );
$ret .= sprintf( "%31s %s\n",
"Hopefulness:", config_array_to_string( $config->{hopefulness} ) );
return $ret;
}
sub prepaired_players_to_string {
my ( $config, $tournament_players ) = @_;
if ( scalar keys %{ $config->{prepaired_players} } == 0 ) {
return "\n\nThere are no prepaired players\n\n";
}
my %player_id_to_tournament_player = ();
foreach my $player ( @{$tournament_players} ) {
$player_id_to_tournament_player{ $player->{id} } = $player;
}
my $ret = "\n\nPrepaired Players:\n\n";
foreach my $player_id ( sort keys %{ $config->{prepaired_players} } ) {
my $player = $player_id_to_tournament_player{$player_id};
my $opponent =
$player_id_to_tournament_player{ $config->{prepaired_players}
->{$player_id} };
if ( $player->{id} < $opponent->{id} ) {
$ret .= sprintf( "%s vs %s\n",
player_string( $player, -2 ),
player_string( $opponent, -2 ) );
}
}
return $ret;
}
sub previous_pairings_to_string {
my (
$config, $tournament_players,
$number_of_players, $previous_pairing_hash
) = @_;
my $res = "\nPrevious Pairings\n\n";
for ( my $i = 0 ; $i < $number_of_players ; $i++ ) {
my $player_i = $tournament_players->[$i];
for ( my $j = $i + 1 ; $j < $number_of_players ; $j++ ) {
my $player_j = $tournament_players->[$j];
my $times_played_key =
create_times_played_key( $player_i->{id}, $player_j->{id} );
if ( $previous_pairing_hash->{$times_played_key} ) {
$res .= sprintf( "%s vs %s\n",
player_string( $player_i, $i ),
player_string( $player_j, $j ) );
}
}
}
return $res . "\n\n";
}
# Pairing and simming
sub cop {
my (
$config, $tournament_players,
$times_played_hash, $previous_pairing_hash
) = @_;
log_info( $config,
prepaired_players_to_string( $config, $tournament_players ) );
if ( $config->{number_of_rounds_remaining} <= 0 ) {
return sprintf( "Invalid rounds remaining: %d\n",
$config->{number_of_rounds_remaining} );
}
my $number_of_players = scalar(@$tournament_players);
if ( $number_of_players % 2 == 1 ) {
add_bye_player( $tournament_players, $number_of_players );
$number_of_players = scalar(@$tournament_players);
$config->{bye_active} = 1;
}
log_info( $config, config_to_string($config) );
sort_tournament_players_by_record($tournament_players);
log_info(
$config,
sprintf( "\n\nStandings\n\n%s\n",
tournament_players_string($tournament_players) )
);
log_info(
$config,
previous_pairings_to_string(
$config, $tournament_players,
$number_of_players, $previous_pairing_hash
)
);
# Truncate the players for simulations.
# We don't need to simulate anyone who can't cash and
# having unnecessary players in the simulations
# degrades performance.
my $sim_tournament_players =
get_sim_tournament_players( $config, $tournament_players );
my $lowest_gibson_rank =
get_lowest_gibson_rank( $config, $sim_tournament_players );
# Use some large value for max factor
# so that the number of rounds remaining
# is always used as the factor.
my $factor_pair_results =
sim_factor_pair( $config, $sim_tournament_players, $lowest_gibson_rank,
INITIAL_FACTOR );
log_info(
$config,
results_string(
$config, $sim_tournament_players,
$factor_pair_results, INITIAL_FACTOR
)
);
my ( $stat_results, $abs_results, ) =
get_lowest_ranked_players_who_can_finish_in_nth( $config,
$factor_pair_results, $sim_tournament_players );
# Make the max factor the difference in rank of the lowest player
# who can get the highest ranked non-gibsonized rank and the rank
# itself at least once in the sims.
my $improved_factor_constant =
$abs_results->[ $lowest_gibson_rank + 1 ] - ( $lowest_gibson_rank + 1 );
my $improved_factor_pair_results = sim_factor_pair(
$config, $sim_tournament_players,
$lowest_gibson_rank, $improved_factor_constant
);
log_info(
$config,
results_string(
$config, $sim_tournament_players,
$improved_factor_pair_results, $improved_factor_constant
)
);
my $lowest_ranked_always_wins = -1;
my $control_loss = -1;
if ( $lowest_gibson_rank < 0 ) {
( $lowest_ranked_always_wins, $control_loss ) =
get_control_loss( $config, $improved_factor_pair_results,
$sim_tournament_players );
}
my $adjusted_control_loss_threshold = 0;
if ( ( $config->{number_of_rounds_remaining} - 1 ) <
scalar( @{ $config->{control_loss_thresholds} } ) )
{
$adjusted_control_loss_threshold =
$config->{control_loss_thresholds}
->[ $config->{number_of_rounds_remaining} - 1 ];
}
log_info(
$config,
sprintf( "\nAdjusted control loss threshold: %f\n",
$adjusted_control_loss_threshold,
)
);
log_info( $config, "\n\nGibsons\n\n" );
if ( $lowest_gibson_rank >= 0 ) {
log_info(
$config,
sprintf(
"\nLowest ranked gibsonized player: %d (%s)\n",
$lowest_gibson_rank,
$tournament_players->[$lowest_gibson_rank]->{name}
)
);
}
else {
log_info( $config, "\nNo one is gibsonized\n" );
}
sort_tournament_players_by_record($sim_tournament_players);
my (
$lowest_ranked_players_who_can_finish_in_nth_statistically,
$lowest_ranked_players_who_can_finish_in_nth_absolutely
)
= get_lowest_ranked_players_who_can_finish_in_nth( $config,
$improved_factor_pair_results, $sim_tournament_players );
my $lowest_ranked_player_who_can_cash_statistically =
$lowest_ranked_players_who_can_finish_in_nth_statistically
->[ $config->{lowest_ranked_payout} ];
my $lowest_ranked_player_who_can_cash_absolutely =
$lowest_ranked_players_who_can_finish_in_nth_absolutely
->[ $config->{lowest_ranked_payout} ];
log_info(
$config,
sprintf(
"\nLowest ranked player who can still cash statistically: %d (%s)",
$lowest_ranked_player_who_can_cash_statistically + 1,
$sim_tournament_players
->[$lowest_ranked_player_who_can_cash_statistically]->{name}
)
);
log_info(
$config,
sprintf(
"\nLowest ranked player who can still cash absolutely: %d (%s)",
$lowest_ranked_player_who_can_cash_absolutely + 1,
$sim_tournament_players
->[$lowest_ranked_player_who_can_cash_absolutely]->{name}
)
);
# Add 1 because TSH will pair the round after the last paired round
my $control_loss_active =
$config->{round_to_pair} >= $config->{control_loss_activation_round};
my $control_status_text = 'ACTIVE';
if ( !$control_loss_active ) {
$control_status_text = 'DISABLED';
}
# Get the number of repeats for each individual player
my %number_of_repeats = ();
my $destinys_child = -1;
my $control_loss_weight_used = 0;
for ( my $i = 0 ; $i < $number_of_players ; $i++ ) {
my $player_i = $tournament_players->[$i];
for ( my $j = $i + 1 ; $j < $number_of_players ; $j++ ) {
my $player_j = $tournament_players->[$j];
my $number_of_times_played =
get_number_of_times_played( $player_i->{id},
$player_j->{id}, $times_played_hash );
if ( !exists $number_of_repeats{ $player_i->{id} } ) {
$number_of_repeats{ $player_i->{id} } = 0;
}
if ( !exists $number_of_repeats{ $player_j->{id} } ) {
$number_of_repeats{ $player_j->{id} } = 0;
}
if ( $number_of_times_played > 1 ) {
my $repeats = $number_of_times_played - 1;
$number_of_repeats{ $player_i->{id} } += $repeats;
$number_of_repeats{ $player_j->{id} } += $repeats;
}
if ( $config->{number_of_rounds_remaining} == 1 ) {
# do nothing
}
elsif ( !$player_j->{is_bye} ) {
# If neither of these blocks are true, that means both
# players are gibsonized and we don't have to consider
# control loss or placement.
my $i_gibson_j_cash =
$i <= $lowest_gibson_rank
&& $j > $lowest_gibson_rank
&& $j <= $lowest_ranked_player_who_can_cash_absolutely
&& $j != ( $number_of_players - 1 );
my $neither_player_gibson =
$i > $lowest_gibson_rank && $j > $lowest_gibson_rank;
if ($i_gibson_j_cash) {
# do nothing
}
elsif ($neither_player_gibson) {
# Control loss weight
# Only applies to the player in first
# If:
# Control loss is active for this part of the tournament, and
# We are considering the player in first, and
# the control loss meets the threshold, and
# the opponent is lower ranked than the minimum of:
# the person who can get first in the sims and
# the lowest ranked always winning person
# or, if control loss threshold isn't met
# the person who can get first in the sims and
my $lowest_ranked_person_who_can_win =
$lowest_ranked_players_who_can_finish_in_nth_statistically
->[0];
if ( $lowest_ranked_person_who_can_win == 0 ) {
# This player is not gibsonized, but no one reached
# them in the simulations, so just make the lowest
# ranked person who can win the player in 2nd
$lowest_ranked_person_who_can_win = 1;
}
# Enforce destiny control for two players if there
# are more than 2 rounds left.
if ( $control_loss_active
&& $i == 0 )
{
if (
(
$control_loss >
$adjusted_control_loss_threshold && $j != min(
$lowest_ranked_person_who_can_win,
$lowest_ranked_always_wins
)