-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_random_ballots.cpp
1862 lines (1505 loc) · 76.4 KB
/
generate_random_ballots.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
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
// generate_random_ballots.cpp
//
// This program generates random ballots for use with the
// votefair_ranking.cpp code, or other vote-counting
// software that uses numeric codes to supply ballots
// and numeric codes to indicate winners.
// See the ABOUT section for details.
//
//
// -----------------------------------------------
//
// COPYRIGHT & LICENSE
//
// (c) Copyright 2021 by Richard Fobes at www.VoteFair.org.
// You can redistribute and/or modify this VoteFairRanking software
// under the MIT software license terms that appear
// in the "license" file.
//
// Conversion of this code into another programming language
// is also covered by the above license terms.
//
//
// -----------------------------------------------
//
// VERSION
//
// Version 2.2
//
// This code works with an older (same-date)
// version of the votefair_ranking.cpp code.
// In 2022-May the votefair_ranking.cpp code was
// modified to remove the calculations for RCIPE
// and IRV because those methods are now
// calculated in the rcipe_stv.cpp code.
// So this code should be updated to use that
// new code (instead of using the older
// votefair_ranking.cpp code).
//
//
// -----------------------------------------------
//
// USAGE
//
// This program generates random ballots for use with the
// votefair_ranking.cpp code.
//
// The following sample code compiles and then executes
// this software under a typical Windows environment
// with the g++ compiler and the mingw32 library already
// installed.
//
// path=C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin\
//
// g++ generate_random_ballots.cpp -o generate_random_ballots
//
// .\generate_random_ballots > output_generate_random_ballots.txt
//
//
// -----------------------------------------------
//
// ABOUT
//
// This software generates random ballots for the
// votefair_ranking.cpp code and then calculates
// results for the following three tests:
//
// * Calculates how often each method yields the
// same winner as the Condorcet-Kemeny method
// (which deeply looks into ALL the ballot
// information).
//
// * Calculates how often each method yields a
// different winner if any one of the other
// non-winning candidates did not enter the
// race. Such failures fail the "independence
// of irrelevant alternatives" (IIA) criterion.
//
// * Calculates how often each method yields a
// different winner if a clone candidate enters
// the race. Such failures fail the
// "independence of clones" criterion.
//
//
// -----------------------------------------------
//
// LICENSE
//
// This generate_random_ballots software is licensed
// under the MIT License terms.
//
//
// -----------------------------------------------
// -----------------------------------------------
// Begin code.
// -----------------------------------------------
// -----------------------------------------------
// Specify libraries needed.
#include <cstring>
#include <string>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <random>
#include <chrono>
// -----------------------------------------------
// Change these values as needed to specify the
// number of ballots, and the number of clones
// added during the clone independence tests.
// The number of cloned choices does not
// include the original similar choice.
// NEITHER of these values can be zero!
int global_maximum_ballot_number = 11 ;
int global_number_of_clones = 2 ;
// -----------------------------------------------
// Specify the number of tests per choice count.
// This number is an estimate. The actual number
// of tests are indicated in the results.
int global_number_of_tests_per_choice_count = 10 ;
// -----------------------------------------------
// Declare the variables that specify which
// choice counts are used for the tests.
// NOTE:
// To find the code that specifies the choice
// counts, search for:
// "global_choice_count_list[ 1 ]"
// or
// "global_number_of_choice_counts_specified ="
int global_number_of_choice_counts_specified ;
int global_choice_count_list[ 20 ] ;
// -----------------------------------------------
// Define true and false constants (for easier
// conversion between programming languages).
const int global_false = 0 ;
const int global_true = 1 ;
// -----------------------------------------------
// Specify whether to suppress writing details
// to the log file. This suppression is useful
// when lots of cases are being run, and
// debugging is not needed.
// int global_true_or_false_show_details_in_log_file = global_true ;
int global_true_or_false_show_details_in_log_file = global_false ;
// -----------------------------------------------
// Ensure that the case numbers have the same
// number of digits. This number can be changed.
// Note that some tests use multiple cases.
int global_minimum_case_id = 100000 ;
// -----------------------------------------------
// Assign numbers and names to the vote-counting
// methods, and specify how many methods there
// are. This order determines both the order of
// the calculations (which has no effect on the
// results) and the layering of the data on the
// scatter plot.
int global_number_of_methods = 7 ;
const int global_method_plurality = 1 ;
const int global_method_borda = 2 ;
const int global_method_irv = 3 ;
const int global_method_star = 4 ;
const int global_method_rcipe = 5 ;
const int global_method_ipe = 6 ;
const int global_method_kemeny = 7 ;
// Ignore results for what was intended to be
// a RCIPE version 2 method, because it was
// too easily mistaken for being the "borda
// elimination" method.
const int global_method_psc = 0 ;
// Ignore results for IRV-BTR because the results
// are so similar to IRV, and the scatter plot
// becomes crowded.
const int global_method_irvbtr = 0 ;
// Ignore results for pseudo-method PLE
// which was used to verify how often every round
// of elimination included a Condorcet loser.
const int global_method_ple = 0 ;
// Ignore results for approval voting because
// unfair to meaningfully convert from ranked
// ballot to approval ballot.
const int global_method_approval = 0 ;
std::string global_name_for_method_kemeny = "C-K" ;
std::string global_name_for_method_ipe = "IPE" ;
std::string global_name_for_method_rcipe = "RCIPE" ;
std::string global_name_for_method_psc = "PSC" ;
std::string global_name_for_method_star = "STAR/sim/NT" ;
std::string global_name_for_method_irv = "IRV" ;
std::string global_name_for_method_borda = "Borda/NT" ;
std::string global_name_for_method_irvbtr = "IRV-BTR" ;
std::string global_name_for_method_approval = "Appr/NT" ;
std::string global_name_for_method_plurality = "Plur" ;
std::string global_name_for_method_condorcet = "COND" ;
std::string global_name_for_method_ple = "PLE" ;
// -----------------------------------------------
// (Do not change these numbers.)
// Specify constants.
const int global_question_number = 1 ;
// -----------------------------------------------
// Declare global variables.
int global_case_id ;
int global_case_type ;
int global_case_count_limit ;
int global_specified_choice_count ;
int global_maximum_choice_number ;
int global_choice_count_case_specific ;
int global_choice_omitted ;
int global_clone_choice_number_next ;
int global_vf_test_count ;
int global_condorcet_test_count ;
int global_iia_test_count ;
int global_clone_test_count ;
int global_count_of_cases_involving_tie ;
int global_pointer_to_choice_count_list ;
int global_flag_as_interesting ;
int global_choice_winner_from_method_condorcet ;
// -----------------------------------------------
// Declare global constants.
const int global_case_all_choices = 1 ;
const int global_case_choice_omitted = 2 ;
const int global_case_choice_omitted_final = 3 ;
const int global_case_clones_included = 4 ;
const int global_case_ignored = 5 ;
// -----------------------------------------------
// Declare frequently used variable names,
// but these are not intended to convey data
// between functions.
int method_id = 1 ;
int ballot_number = 0 ;
int choice_number = 0 ;
int ranking_level = 0 ;
int position_number = 0 ;
int pointer_number = 0 ;
float float_number = 0.0 ;
// -----------------------------------------------
// Declare the needed arrays.
std::string global_name_for_method[ 20 ] ;
std::string global_color_hex_for_method[ 20 ] ;
int global_choice_on_ballot_at_ranking_level[ 200 ][ 20 ] ;
int calculated_vf_result_match_for_method_and_choice_count[ 20 ][ 20 ] ;
int calculated_condorcet_result_match_for_method_and_choice_count[ 20 ][ 20 ] ;
int global_calculated_iia_result_match_for_method_and_choice_count[ 20 ][ 20 ] ;
float global_calculated_iia_result_match_with_tenths[ 20 ][ 20 ] ;
int global_calculated_clone_result_match_for_method_and_choice_count[ 20 ][ 20 ] ;
float global_calculated_clone_result_match_with_tenths[ 20 ][ 20 ] ;
float global_calculated_condorcet_result_match_with_tenths[ 20 ][ 20 ] ;
int global_choice_number_at_position[ 99 ] ;
int global_usage_count_for_choice_and_rank[ 99 ][ 99 ] ;
int global_choice_winner_all_choices_for_method[ 20 ] ;
int global_choice_winner_from_method[ 20 ] ;
int global_count_of_vf_tests_match_for_method[ 20 ] ;
int global_count_of_vf_tests_fail_match_for_method[ 20 ] ;
int global_count_of_vf_tests_tied_for_method[ 20 ] ;
int global_count_of_vf_tests_unexpected_for_method[ 20 ] ;
int global_count_of_condorcet_tests_match_for_method[ 20 ] ;
int global_count_of_condorcet_tests_fail_match_for_method[ 20 ] ;
int global_count_of_iia_tests_match_for_method[ 20 ] ;
int global_count_of_iia_group_match_for_method[ 20 ] ;
int global_count_of_iia_tests_fail_match_for_method[ 20 ] ;
int global_count_of_iia_group_fail_match_for_method[ 20 ] ;
int global_count_of_iia_tests_tied_for_method[ 20 ] ;
int global_count_of_iia_group_tied_for_method[ 20 ] ;
int global_count_of_iia_tests_unexpected_for_method[ 20 ] ;
int global_count_of_clone_tests_match_for_method[ 20 ] ;
int global_count_of_clone_tests_fail_match_for_method[ 20 ] ;
int global_count_of_clone_tests_clone_displaces_for_method[ 20 ] ;
int global_count_of_clone_tests_tied_for_method[ 20 ] ;
int global_count_of_clone_tests_clone_help_for_method[ 20 ] ;
int global_count_of_clone_tests_clone_hurt_for_method[ 20 ] ;
int global_count_of_clone_tests_unexpected_for_method[ 20 ] ;
// -----------------------------------------------
// Specify codes that identify the meaning of the
// next number in the coded output list.
std::string global_string_voteinfo_code_for_end_of_all_cases = "-2" ;
std::string global_string_voteinfo_code_for_case_number = "-3" ;
std::string global_string_voteinfo_code_for_question_number = "-4" ;
std::string global_string_voteinfo_code_for_number_of_choices = "-6" ;
std::string global_string_voteinfo_code_for_start_of_all_vote_info = "-7" ;
std::string global_string_voteinfo_code_for_end_of_all_vote_info = "-8" ;
std::string global_string_voteinfo_code_for_end_of_ballot = "-10" ;
std::string global_string_voteinfo_code_for_ballot_count = "-11" ;
std::string global_string_voteinfo_code_for_preference_level = "-12" ;
std::string global_string_voteinfo_code_for_request_instant_runoff_voting = "-50" ;
std::string global_string_voteinfo_code_for_request_instant_pairwise_elimination = "-51" ;
std::string global_string_voteinfo_code_for_request_rcipe_voting = "-52" ;
std::string global_string_voteinfo_code_for_request_star_voting = "-56" ;
std::string global_string_voteinfo_code_for_request_pairwise_loser_elimination = "-58" ;
std::string global_string_voteinfo_code_for_request_logging_off = "-65" ;
const int global_voteinfo_code_for_case_number = -3 ;
const int global_voteinfo_code_for_choice = -13 ;
const int global_voteinfo_code_for_tie = -14 ;
const int global_voteinfo_code_for_start_of_votefair_popularity_ranking_sequence_results = -15 ;
const int global_voteinfo_code_for_start_of_plurality_results = -36 ;
const int global_voteinfo_code_for_end_of_plurality_results = -37 ;
const int global_voteinfo_code_for_plurality_count = -38 ;
const int global_voteinfo_code_for_winner_instant_runoff_voting = -53 ;
const int global_voteinfo_code_for_winner_instant_pairwise_elimination = -54 ;
const int global_voteinfo_code_for_winner_rcipe_voting = -55 ;
const int global_voteinfo_code_for_winner_star_voting = -57 ;
const int global_voteinfo_code_for_winner_pairwise_loser_elimination = -59 ;
const int global_voteinfo_code_for_winner_irv_bottom_two_runoff = -60 ;
const int global_voteinfo_code_for_winner_borda_count = -61 ;
const int global_voteinfo_code_for_flag_as_interesting = -62 ;
const int global_voteinfo_code_for_winner_approval_voting = -63 ;
const int global_voteinfo_code_for_winner_condorcet = -64 ;
const int global_voteinfo_code_for_winner_pairwise_support_count = -66 ;
// -----------------------------------------------
// Declare an output file for writing log info.
std::ofstream log_out ;
// -----------------------------------------------
// Declare an input file for reading calculated
// results.
std::ifstream calc_results ;
// -----------------------------------------------
// Declare an output file for the scatter plot.
std::ofstream svg_out ;
// -----------------------------------------------
// Declare the random number generator.
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator ( seed ) ;
// -----------------------------------------------
// convert_text_to_integer
//
// This function is used instead of "std::to_string"
// for compatibility with older C++ "string" libraries
// that have a bug. The bug is that the "to_string"
// function is not recognized as being within the
// "std" library, even though it is defined there.
int convert_text_to_integer( char * supplied_text )
{
int equivalent_integer ;
try
{
equivalent_integer = atoi( supplied_text ) ;
}
catch( ... )
{
equivalent_integer = -99999 ;
}
return equivalent_integer ;
}
// -----------------------------------------------
// -----------------------------------------------
// generate_preferences
//
// This function generates random preferences
// for the ballots.
void generate_preferences( ) {
// -----------------------------------------------
// Initialization.
int count_for_ranking_level ;
int count_of_choices_not_yet_ranked ;
int count_of_choices_need_to_rank ;
for ( choice_number = 1 ; choice_number <= global_maximum_choice_number ; choice_number ++ )
{
for ( ranking_level = 1 ; ranking_level <= global_maximum_choice_number ; ranking_level ++ )
{
global_usage_count_for_choice_and_rank[ choice_number ][ ranking_level ] = 0 ;
}
}
// -----------------------------------------------
// Begin a loop that handles one ballot.
for ( ballot_number = 1 ; ballot_number <= global_maximum_ballot_number ; ballot_number ++ )
{
// -----------------------------------------------
// Put the choice numbers in a list so that
// they can be chosen at random without repeating
// any choice number.
for ( choice_number = 1 ; choice_number <= global_maximum_choice_number ; choice_number ++ )
{
global_choice_number_at_position[ choice_number ] = choice_number ;
}
// -----------------------------------------------
// Specify the number of choices that need to be
// randomly assigned to a ranking level.
// If clone independence is being measured, the
// ranking of the clones is just after choice
// number one, so the clone choices are not
// assigned randomly.
count_of_choices_need_to_rank = global_maximum_choice_number ;
// log_out << "{" ;
// -----------------------------------------------
// Begin a loop that handles each ranking level.
ranking_level = 1 ;
count_of_choices_not_yet_ranked = count_of_choices_need_to_rank ;
for ( count_for_ranking_level = 1 ; count_for_ranking_level <= count_of_choices_need_to_rank ; count_for_ranking_level ++ )
{
// -----------------------------------------------
// Randomly choose a choice number for this
// ranking level. Do not repeat any choice number
// already used at a previous ranking level.
std::uniform_int_distribution<int> distribution( 1 , count_of_choices_not_yet_ranked ) ;
position_number = distribution( generator ) ;
choice_number = global_choice_number_at_position[ position_number ] ;
global_choice_on_ballot_at_ranking_level[ ballot_number ][ ranking_level ] = choice_number ;
// log_out << choice_number ;
// -----------------------------------------------
// Keep track of choice number usage to allow
// verifying randomness.
global_usage_count_for_choice_and_rank[ choice_number ][ ranking_level ] ++ ;
// -----------------------------------------------
// Update the list of which choices still need to
// be assigned to a ranking level.
// log_out << " " << position_number << ":" ;
if ( position_number < count_of_choices_not_yet_ranked )
{
for ( pointer_number = position_number ; pointer_number <= ( global_maximum_choice_number - 1 ) ; pointer_number ++ )
{
global_choice_number_at_position[ pointer_number ] = global_choice_number_at_position[ pointer_number + 1 ] ;
// log_out << global_choice_number_at_position[ pointer_number ] ;
}
}
// log_out << ":" << " " ;
// -----------------------------------------------
// Repeat the loop that handles one ranking level.
ranking_level ++ ;
count_of_choices_not_yet_ranked -- ;
}
// log_out << "}" ;
// -----------------------------------------------
// Repeat the loop that handles one ballot.
}
// -----------------------------------------------
// If desired, write to a file the counts that
// can be looked at to verify that the preference
// info is really random.
// Use either "1 == 1" or "1 == 0" to generate or
// omit this output file.
if ( 1 == 1 )
{
std::fstream verifyfile;
verifyfile.open ( "temp_verify_randomness.txt" , std::fstream::app ) ;
for ( choice_number = 1 ; choice_number <= global_maximum_choice_number ; choice_number ++ )
{
for ( ranking_level = 1 ; ranking_level <= global_maximum_choice_number ; ranking_level ++ )
{
verifyfile << "choice " << choice_number << " rank " << ranking_level << " usage " << global_usage_count_for_choice_and_rank[ choice_number ][ ranking_level ] << std::endl ;
}
}
verifyfile.close( ) ;
}
// -----------------------------------------------
// End of function generate_preferences.
return ;
}
// -----------------------------------------------
// -----------------------------------------------
// handle_calculated_results
//
// Reads numbers and codes from the file
// written by the calculation program.
//
// -----------------------------------------------
// -----------------------------------------------
void handle_calculated_results( )
{
std::string input_line ;
std::string input_text_word ;
// -----------------------------------------------
// Initialization.
int method_id = 1 ;
int current_result_code = 0 ;
int previous_result_code = 0 ;
int next_result_code = 0 ;
int count_of_result_codes = 0 ;
int count_position_at_start_of_votefair_popularity_sequence = 0 ;
int count_position_at_start_of_plurality_result = 0 ;
int count_position_at_choice_number = 0 ;
int choice_number_adjustment = 0 ;
int true_or_false_within_plurality_result = global_false ;
int plurality_choice_number = 0 ;
int plurality_winner = global_voteinfo_code_for_tie ;
int highest_plurality_count = -1 ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
global_choice_winner_from_method[ method_id ] = 0 ;
}
global_choice_winner_from_method_condorcet = 0 ;
// -----------------------------------------------
// Begin loop to handle one line from the input file
// that contains the calculated results.
log_out << std::endl << "[" << global_case_id << "][t " << global_case_type << "][ch " << global_choice_count_case_specific << "][om " << global_choice_omitted << "]" ;
count_position_at_start_of_votefair_popularity_sequence = -10 ;
count_position_at_choice_number = -10 ;
for ( std::string input_line ; std::getline( calc_results , input_line ) ; )
{
std::size_t pointer_found = input_line.find_last_not_of( " \t\n\r" ) ;
if ( pointer_found != std::string::npos )
{
input_line.erase( pointer_found + 1 ) ;
} else
{
input_line.clear( ) ;
}
// log_out << "[input line: " << input_line << "]" << std::endl ;
char input_line_c_version[ 2000 ] = "" ;
std::size_t line_length = std::min( 2000 , (int) input_line.length() ) ;
std::size_t line_length_copied = input_line.copy( input_line_c_version , line_length , 0 ) ;
input_line_c_version[ line_length_copied ] = '\0' ;
// -----------------------------------------------
// Begin loop to get first/next space-delimited
// word of text from the input line.
char * pointer_to_word ;
// reminder: strtok modifies the string
pointer_to_word = strtok( input_line_c_version , " ,." ) ;
while ( pointer_to_word != NULL )
{
// -----------------------------------------------
// Attempt to convert the input word into an
// integer. If this conversion is not successful,
// indicate an error and write this error to the
// output files and then exit the program.
try
{
current_result_code = convert_text_to_integer( pointer_to_word ) ;
}
catch( ... )
{
log_out << "Error: invalid input word: " << pointer_to_word << std::endl ;
log_out << "[Warning: Input line contains non-numeric characters (" << pointer_to_word << "), so this case (" << global_case_id << ") cannot be calculated]" << std::endl ;
exit( EXIT_FAILURE ) ;
}
// -----------------------------------------------
// Handle the supplied vote-info number.
// log_out << "[" << current_result_code << "]" ;
if ( current_result_code == global_voteinfo_code_for_case_number )
{
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
global_choice_winner_from_method[ method_id ] = 0 ;
}
global_choice_winner_from_method_condorcet = 0 ;
} else if ( current_result_code == global_voteinfo_code_for_start_of_votefair_popularity_ranking_sequence_results )
{
count_position_at_start_of_votefair_popularity_sequence = count_of_result_codes ;
} else if ( current_result_code == global_voteinfo_code_for_flag_as_interesting )
{
global_flag_as_interesting ++ ;
} else if ( current_result_code == global_voteinfo_code_for_choice )
{
count_position_at_choice_number = count_of_result_codes ;
} else if ( ( count_of_result_codes == count_position_at_start_of_votefair_popularity_sequence + 2 ) && ( count_of_result_codes == count_position_at_choice_number + 1 ) )
{
global_choice_winner_from_method[ global_method_kemeny ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_kemeny ] << " " << global_choice_winner_from_method[ global_method_kemeny ] << "]" ;
} else if ( ( current_result_code == global_voteinfo_code_for_tie ) && ( count_of_result_codes == count_position_at_start_of_votefair_popularity_sequence + 3 ) )
{
global_choice_winner_from_method[ global_method_kemeny ] = 0 ;
log_out << "[" << global_name_for_method[ global_method_kemeny ] << "_tie]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_instant_pairwise_elimination )
{
global_choice_winner_from_method[ global_method_ipe ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_ipe ] << " " << global_choice_winner_from_method[ global_method_ipe ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_rcipe_voting )
{
global_choice_winner_from_method[ global_method_rcipe ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_rcipe ] << " " << global_choice_winner_from_method[ global_method_rcipe ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_pairwise_support_count )
{
global_choice_winner_from_method[ global_method_psc ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_psc ] << " " << global_choice_winner_from_method[ global_method_psc ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_instant_runoff_voting )
{
global_choice_winner_from_method[ global_method_irv ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_irv ] << " " << global_choice_winner_from_method[ global_method_irv ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_irv_bottom_two_runoff )
{
global_choice_winner_from_method[ global_method_irvbtr ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_irvbtr ] << " " << global_choice_winner_from_method[ global_method_irvbtr ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_borda_count )
{
global_choice_winner_from_method[ global_method_borda ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_borda ] << " " << global_choice_winner_from_method[ global_method_borda ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_star_voting )
{
global_choice_winner_from_method[ global_method_star ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_star ] << " " << global_choice_winner_from_method[ global_method_star ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_approval_voting )
{
global_choice_winner_from_method[ global_method_approval ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_approval ] << " " << global_choice_winner_from_method[ global_method_approval ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_pairwise_loser_elimination )
{
global_choice_winner_from_method[ global_method_ple ] = current_result_code ;
log_out << "[" << global_name_for_method[ global_method_ple ] << " " << global_choice_winner_from_method[ global_method_ple ] << "]" ;
} else if ( previous_result_code == global_voteinfo_code_for_winner_condorcet )
{
global_choice_winner_from_method_condorcet = current_result_code ;
log_out << "[COND " << global_choice_winner_from_method_condorcet << "]" ;
} else if ( current_result_code == global_voteinfo_code_for_start_of_plurality_results )
{
true_or_false_within_plurality_result = global_true ;
} else if ( ( true_or_false_within_plurality_result == global_true ) && ( count_of_result_codes == count_position_at_choice_number + 1 ) )
{
plurality_choice_number = current_result_code ;
} else if ( ( true_or_false_within_plurality_result == global_true ) && ( previous_result_code == global_voteinfo_code_for_plurality_count ) )
{
if ( current_result_code > highest_plurality_count )
{
plurality_winner = plurality_choice_number ;
highest_plurality_count = current_result_code ;
} else if ( current_result_code == highest_plurality_count )
{
plurality_winner = global_voteinfo_code_for_tie ;
}
} else if ( current_result_code == global_voteinfo_code_for_end_of_plurality_results )
{
global_choice_winner_from_method[ global_method_plurality ] = plurality_winner ;
log_out << "[" << global_name_for_method[ global_method_plurality ] << " " << global_choice_winner_from_method[ global_method_plurality ] << "]" ;
true_or_false_within_plurality_result = global_false ;
}
// -----------------------------------------------
// Repeat the loop for the next word (within the line).
count_of_result_codes ++ ;
previous_result_code = current_result_code ;
pointer_to_word = strtok( NULL, " ,." ) ;
}
// -----------------------------------------------
// Repeat the loop for the next line of data from
// the input file.
}
// -----------------------------------------------
// If this case uses all the choices and a tie
// occurs in VoteFair popularity ranking, or in
// plurality counting, or in IRV, the case is
// ignored because this would not be a meaningful
// test.
if ( ( global_case_type == global_case_all_choices ) && ( ( global_choice_winner_from_method[ global_method_kemeny ] < 1 ) || ( global_choice_winner_from_method[ global_method_plurality ] < 1 ) || ( global_choice_winner_from_method[ global_method_irv ] < 1 ) ) )
{
global_count_of_cases_involving_tie ++ ;
global_case_type = global_case_ignored ;
log_out << "[ignored]" ;
return ;
}
// -----------------------------------------------
// If this case included all the choices, save
// the winner based on each method.
if ( global_case_type == global_case_all_choices )
{
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
global_choice_winner_all_choices_for_method[ method_id ] = global_choice_winner_from_method[ method_id ] ;
}
}
// -----------------------------------------------
// Count the number of cases that match, or fail
// to match, the winner according to VoteFair
// popularity ranking. Also count ties.
if ( global_case_type == global_case_all_choices )
{
global_vf_test_count ++ ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
if ( global_choice_winner_from_method[ method_id ] == global_choice_winner_from_method[ global_method_kemeny ] )
{
global_count_of_vf_tests_match_for_method[ method_id ] ++ ;
} else if ( global_choice_winner_from_method[ method_id ] > 0 )
{
global_count_of_vf_tests_fail_match_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " fails]" ;
} else
{
global_count_of_vf_tests_tied_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " tied]" ;
}
}
}
// -----------------------------------------------
// Count the number of cases that match, or fail
// to match, the Condorcet winner.
if ( global_case_type == global_case_all_choices )
{
if ( global_choice_winner_from_method_condorcet > 0 )
{
global_condorcet_test_count ++ ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
if ( global_choice_winner_from_method[ method_id ] == global_choice_winner_from_method_condorcet )
{
global_count_of_condorcet_tests_match_for_method[ method_id ] ++ ;
} else
{
global_count_of_condorcet_tests_fail_match_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " fails cond.]" ;
}
}
}
}
// -----------------------------------------------
// When testing for independence of irrelevant
// alternatives, if the winner of the case with
// all the choices is not the omitted choice and
// is not the same as the winner without the
// omitted choice, then count this case as a
// failure for the counting method.
if ( ( global_case_type == global_case_choice_omitted ) || ( global_case_type == global_case_choice_omitted_final ) )
{
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
if ( global_choice_winner_all_choices_for_method[ method_id ] == global_choice_omitted )
{
continue ;
}
choice_number_adjustment = 0 ;
if ( ( global_choice_winner_from_method[ method_id ] >= global_choice_omitted ) )
{
choice_number_adjustment = 1 ;
}
if ( global_choice_winner_from_method[ method_id ] + choice_number_adjustment == global_choice_winner_all_choices_for_method[ method_id ] )
{
global_count_of_iia_group_match_for_method[ method_id ] ++ ;
} else if ( global_choice_winner_from_method[ method_id ] < 1 )
{
global_count_of_iia_group_tied_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " IIA tied]" ;
} else
{
global_count_of_iia_group_fail_match_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " IIA fails]" ;
}
}
}
// -----------------------------------------------
// For testing independence of irrelevant
// alternatives, determine the overall result of
// the test according to what happened when
// testing the removal of each choice (one at a
// time). The test fails if the winner changes
// as a result of omitting any choice other than
// the original winning choice.
// If the test involves only two choices, the
// test is always successful because removing
// either choice will always change the result,
// which is not a meaningful result.
if ( ( global_maximum_choice_number == 2 ) && ( global_case_type == global_case_all_choices ) )
{
global_iia_test_count ++ ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
global_count_of_iia_tests_match_for_method[ method_id ] ++ ;
}
}
if ( global_case_type == global_case_choice_omitted_final )
{
global_iia_test_count ++ ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
if ( global_count_of_iia_group_fail_match_for_method[ method_id ] > 0 )
{
global_count_of_iia_tests_fail_match_for_method[ method_id ] ++ ;
} else if ( global_count_of_iia_group_match_for_method[ method_id ] > 0 )
{
global_count_of_iia_tests_match_for_method[ method_id ] ++ ;
} else
{
global_count_of_iia_tests_tied_for_method[ method_id ] ++ ;
}
}
}
// -----------------------------------------------
// If testing clone independence, determine
// whether the winner is the same as the case
// with all the choices. If not, track
// whether the added clone helps or hurts the
// similar choice.
// Also do a separate count for failures in which
// the clone wins instead of the similar choice.
if ( global_case_type == global_case_clones_included )
{
global_clone_test_count ++ ;
for ( method_id = 1 ; method_id <= global_number_of_methods ; method_id ++ )
{
if ( ( global_choice_winner_from_method[ method_id ] > 0 ) && ( global_choice_winner_from_method[ method_id ] == global_choice_winner_all_choices_for_method[ method_id ] ) )
{
global_count_of_clone_tests_match_for_method[ method_id ] ++ ;
} else if ( global_choice_winner_from_method[ method_id ] < 1 )
{
global_count_of_clone_tests_tied_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " CL tied]" ;
} else if ( ( global_choice_winner_from_method[ method_id ] > global_maximum_choice_number ) && ( global_choice_winner_all_choices_for_method[ method_id ] == 1 ) )
{
global_count_of_clone_tests_clone_displaces_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " displaces]" ;
} else
{
global_count_of_clone_tests_fail_match_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " CL fails]" ;
if ( global_choice_winner_from_method[ method_id ] == 1 )
{
global_count_of_clone_tests_clone_help_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " helps similar]" ;
} else if ( global_choice_winner_all_choices_for_method[ method_id ] == 1 )
{
global_count_of_clone_tests_clone_hurt_for_method[ method_id ] ++ ;
log_out << "[" << global_name_for_method[ method_id ] << " hurts similar]" ;
}
}
}
}
// -----------------------------------------------
// Identify interesting cases:
if ( ( global_choice_winner_from_method[ global_method_ple ] < 0 ) && ( global_choice_winner_from_method[ global_method_rcipe ] > 0 ) && ( global_choice_winner_from_method[ global_method_ipe ] > 0 ) && ( global_choice_winner_from_method[ global_method_irv ] > 0 ) && ( global_choice_winner_from_method[ global_method_kemeny ] == global_choice_winner_from_method[ global_method_rcipe ] ) && ( global_choice_winner_from_method[ global_method_rcipe ] != global_choice_winner_from_method[ global_method_irv ] ) )
{
log_out << "[interesting]" ;
if ( global_choice_winner_from_method[ global_method_plurality ] != global_choice_winner_from_method[ global_method_kemeny ] )
{
log_out << "[very]" ;
}
}
if ( global_flag_as_interesting > 0 )
{
log_out << "[flagged_interesting]" ;
global_flag_as_interesting = 0 ;
}
// -----------------------------------------------
// End of function handle_calculated_results
return ;
}