summaryrefslogtreecommitdiff
path: root/src/election.adb
blob: 2cedb3e859a33d8190acdadff7c86a0740b80f29 (plain)
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


with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;
with Ada.Text_IO;
use Ada.Text_IO;
with Rationals;


--  This source is licensed under Creative Commons CC0 v1.0.
--
--  To read the full text, see license.txt in the main directory of this repository
--  or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
--
--  For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/


package body Election is


    package SU renames Ada.Strings.Unbounded;




    Cand_Data  : Candidates.Containers.Candidate_Map;
    Pref_Data  : Bundle_Containers.Bundle_Map;
    Entries    : Entry_Vectors.Vector;
    Exhausted  : Extra_Data;
    Fractional : Extra_Data;
    Transfers  : Transfer_Vectors.Vector;


    Out_Dir      : SU.Unbounded_String;
    Next_Log_Num : Positive;
    Main_Log     : File_Type;
    Seats        : Natural;
    Vacancies    : Natural;
    Total_Papers : Natural;
    Quota        : Natural;
    Verbose      : Boolean;


    Work_To_Do : Boolean := False;




    function Droop_Quota
           (Votes : in Natural;
            Seats : in Natural)
        return Natural is
    begin
        return Votes / (Seats + 1) + 1;
    end Droop_Quota;




    --  needs to be refactored due to length
    procedure Setup
           (Candidate_Data           : in Candidates.Containers.Candidate_Map;
            Preference_File          : in String;
            Output_Dir, Main_Logfile : in String;
            Number_To_Elect          : in Natural;
            Is_Verbose               : in Boolean := False) is
    begin
        Verbose := Is_Verbose;

        if Verbose then
            Put_Line (Standard_Error, "Reading preference data...");
        end if;
        Bundle_Containers.Read_Bundles (Preference_File, Pref_Data);
        if Verbose then
            Put_Line (Standard_Error, "Done." & ASCII.LF);
            Put_Line (Standard_Error, "Setting up election...");
        end if;

        --  setup/extract relevant metadata from the vote bundle map
        for B in Pref_Data.Iterate loop
            declare
                Votes, Papers : Integer;
                This : Entry_Data;
            begin
                Given_Bundles.Count_Both
                   (Bundle_Containers.Bundle_Maps.Element (B).First_Element, Votes, Papers);
                This :=
                   (ID            => Bundle_Containers.Bundle_Maps.Key (B),
                    Vote_Change   => Votes,
                    Total_Votes   => Votes,
                    Paper_Change  => Papers,
                    Total_Papers  => Papers,
                    Status        => Running,
                    Changed       => False,
                    Order_Elected => 0);
                Entries.Append (This);
            end;
        end loop;

        Exhausted := (others => 0);
        Fractional := (others => 0);

        Transfers := Transfer_Vectors.Empty_Vector;
        Cand_Data := Candidate_Data;

        Out_Dir := SU.To_Unbounded_String (Output_Dir);
        Next_Log_Num := 1;
        Seats := Number_To_Elect;
        Vacancies := Number_To_Elect;

        Total_Papers := 0;
        for E of Entries loop
            Total_Papers := Total_Papers + E.Total_Papers;
        end loop;
        Quota := Droop_Quota (Total_Papers, Seats);

        Open (Main_Log, Append_File, Output_Dir & "/" & Main_Logfile);

        Work_To_Do := True;

        if Verbose then
            Put_Line (Standard_Error, "Done." & ASCII.LF);
        end if;
    end Setup;




    procedure Write_Log
    is
        use Ada.Strings;
        use Ada.Strings.Fixed;

        Header : String :=
            "Seats,Vacancies,Total Papers,Quota," &
            Candidates.Candidate_Header &
            ",Papers,Change,Votes,Transfer,Status,Changed,Order Elected";

        Logname : String :=
            SU.To_String (Out_Dir) & "/" & Trim (Integer'Image (Next_Log_Num), Both) & ".csv";

        Change_Str, Order_Str : SU.Unbounded_String;

        Logfile : File_Type;
    begin
        Create (Logfile, Out_File, Logname);

        Put_Line (Logfile, Header);
        for E of Entries loop
            if E.Changed then
                Change_Str := SU.To_Unbounded_String ("True");
            else
                Change_Str := SU.To_Unbounded_String (0);
            end if;

            if E.Order_Elected > 0 then
                Order_Str := SU.To_Unbounded_String (Trim (Integer'Image (E.Order_Elected), Both));
            else
                Order_Str := SU.To_Unbounded_String (0);
            end if;

            Put_Line (Logfile,
                    Trim (Integer'Image (Seats), Both) & "," &
                    Trim (Integer'Image (Vacancies), Both) & "," &
                    Trim (Integer'Image (Total_Papers), Both) & "," &
                    Trim (Integer'Image (Quota), Both) & "," &
                    Candidates.To_String (Cand_Data.Element (E.ID)) & "," &
                    Trim (Integer'Image (E.Total_Papers), Both) & "," &
                    Trim (Integer'Image (E.Paper_Change), Both) & "," &
                    Trim (Integer'Image (E.Total_Votes), Both) & "," &
                    Trim (Integer'Image (E.Vote_Change), Both) & "," &
                    Candidate_Status'Image (E.Status) & "," &
                    SU.To_String (Change_Str) & "," & SU.To_String (Order_Str));
        end loop;
        Put_Line (Logfile,
                ",,,,,,Fractional Loss,,," &
                Trim (Integer'Image (Fractional.Total_Papers), Both) & "," &
                Trim (Integer'Image (Fractional.Paper_Change), Both) & "," &
                ",,,,");
        Put_Line (Logfile,
                ",,,,,,Exhausted,,," &
                Trim (Integer'Image (Exhausted.Total_Papers), Both) & "," &
                Trim (Integer'Image (Exhausted.Paper_Change), Both) & "," &
                ",,,,");

        Close (Logfile);
    end Write_Log;




    type Entry_Position is record
        Index       : Positive;
        Total_Votes : Natural;
    end record;


    package Position_Vectors is new Ada.Containers.Vectors
           (Index_Type   => Positive,
            Element_Type => Entry_Position);


    function "<"
           (Left, Right : in Entry_Position)
        return Boolean is
    begin
        if Left.Total_Votes = Right.Total_Votes then
            return Left.Index < Right.Index;
        else
            return Left.Total_Votes < Right.Total_Votes;
        end if;
    end "<";


    package Position_Sorting is new Position_Vectors.Generic_Sorting;


    function Extract_Running_Entries
        return Position_Vectors.Vector
    is
        Result : Position_Vectors.Vector := Position_Vectors.Empty_Vector;
    begin
        for E in Entries.Iterate loop
            if Entry_Vectors.Element (E).Status = Running then
                Result.Append
                      ((Index       => Entry_Vectors.To_Index (E),
                        Total_Votes => Entry_Vectors.Element (E).Total_Votes));
            end if;
        end loop;
        return Result;
    end Extract_Running_Entries;




    procedure Elect
           (Index : Positive;
            ID    : Candidates.CandidateID)
    is
        use Ada.Strings;
        use Ada.Strings.Fixed;

        Log_Msg : SU.Unbounded_String;
    begin
        Log_Msg := SU.To_Unbounded_String
               (Candidates.Name_And_Party (Cand_Data.Reference (ID)) &
                " elected at logfile #" & Trim (Integer'Image (Next_Log_Num), Both));
        Put_Line (Main_Log, SU.To_String (Log_Msg));
        if Verbose then
            Put_Line (Standard_Error, SU.To_String (Log_Msg));
        end if;

        Entries.Reference (Index).Status := Elected;
        Entries.Reference (Index).Changed := True;
        Entries.Reference (Index).Order_Elected := Seats - Vacancies + 1;

        Vacancies := Vacancies - 1;
    end Elect;




    function Elect_Candidates
        return Boolean
    is
        Running : Position_Vectors.Vector := Extract_Running_Entries;

        Working_Position : Positive;
        Working_ID : Candidates.CandidateID;

        Number_Elected : Natural := 0;
    begin
        Position_Sorting.Sort (Running);

        for R in reverse Running.Iterate loop
            exit when Running.Reference (R).Total_Votes < Quota;
            Working_Position := Position_Vectors.Element (R).Index;
            Working_ID := Entries.Reference (Working_Position).ID;

            Elect (Working_Position, Working_ID);

            Number_Elected := Number_Elected + 1;
            Transfers.Append ( (From => Working_ID, Position => Working_Position) );
        end loop;

        return Number_Elected > 0;
    end Elect_Candidates;




    function Check_If_Done
        return Boolean
    is
        Total_Still_Running : Natural := 0;
    begin
        for E of Entries loop
            if E.Status = Running then
                Total_Still_Running := Total_Still_Running + 1;
            end if;
        end loop;
        if Vacancies = 0 or Total_Still_Running = 0 then
            Close (Main_Log);
            Work_To_Do := False;
            return True;
        else
            return False;
        end if;
    end Check_If_Done;




    --  need to refactor this due to length
    function Transfer_Votes
        return Boolean
    is
        use type Ada.Containers.Count_Type;
        use type Rationals.Fraction;

        Still_Running : Position_Vectors.Vector;
        Not_Considered : Candidates.Containers.CandidateID_Set;

        Working_Transfer : Pending_Transfer;
        Working_Position : Positive;
        Working_ID : Candidates.CandidateID;
        Working_Value : Rationals.Fraction;

        New_Bundle : Given_Bundles.Bundle;
        Votes_In, Papers_In : Natural;
    begin
        if Transfers.Length = 0 then
            return False;
        end if;

        Still_Running := Extract_Running_Entries;
        Not_Considered := Candidates.Containers.CandidateID_Sets.Empty_Set;
        for E of Entries loop
            if E.Status /= Running then
                Not_Considered.Insert (E.ID);
            end if;
        end loop;

        Working_Transfer := Transfers.First_Element;
        Transfers.Delete_First;

        Exhausted.Paper_Change := Entries.Reference (Working_Transfer.Position).Total_Papers;

        if Entries.Reference (Working_Transfer.Position).Status = Excluded then
            Entries.Reference (Working_Transfer.Position).Vote_Change :=
                  - Entries.Reference (Working_Transfer.Position).Total_Votes;
            Working_Value := 1 / 1;
            Entries.Reference (Working_Transfer.Position).Total_Votes := 0;
        else
            Entries.Reference (Working_Transfer.Position).Vote_Change :=
                    Quota - Entries.Reference (Working_Transfer.Position).Total_Votes;
            Working_Value := (Entries.Reference (Working_Transfer.Position).Total_Votes - Quota) /
                                Entries.Reference (Working_Transfer.Position).Total_Votes;
            Entries.Reference (Working_Transfer.Position).Total_Votes := Quota;
        end if;
        Entries.Reference (Working_Transfer.Position).Paper_Change :=
              - Entries.Reference (Working_Transfer.Position).Total_Papers;
        Entries.Reference (Working_Transfer.Position).Total_Papers := 0;

        for R in Still_Running.Iterate loop
            Working_Position := Still_Running.Reference (R).Index;
            Working_ID := Entries.Reference (Working_Position).ID;

            for B in Pref_Data.Reference (Working_Transfer.From).Iterate loop
                Given_Bundles.Transfer
                       (This     => Pref_Data.Reference (Working_Transfer.From).Reference (B),
                        From     => Working_Transfer.From,
                        To       => Working_ID,
                        Excluded => Not_Considered,
                        Value    => Working_Value,
                        Result   => New_Bundle);
                Given_Bundles.Count_Both (New_Bundle, Votes_In, Papers_In);
                if Votes_In > 0 then
                    Pref_Data.Reference (Working_ID).Append (New_Bundle);
                    Entries.Reference (Working_Position).Vote_Change :=
                            Entries.Reference (Working_Position).Vote_Change + Votes_In;
                    Entries.Reference (Working_Position).Paper_Change :=
                            Entries.Reference (Working_Position).Paper_Change + Papers_In;
                    Exhausted.Paper_Change := Exhausted.Paper_Change - Papers_In;
                elsif Papers_In > 0 then
                    Fractional.Paper_Change := Fractional.Paper_Change + Papers_In;
                    Exhausted.Paper_Change := Exhausted.Paper_Change - Papers_In;
                end if;
            end loop;

            Entries.Reference (Working_Position).Total_Votes :=
                    Entries.Reference (Working_Position).Total_Votes +
                    Entries.Reference (Working_Position).Vote_Change;
            Entries.Reference (Working_Position).Total_Papers :=
                    Entries.Reference (Working_Position).Total_Papers +
                    Entries.Reference (Working_Position).Paper_Change;
        end loop;

        Pref_Data.Replace (Working_Transfer.From, Bundle_Containers.Bundle_Vectors.Empty_Vector);

        Fractional.Total_Papers := Fractional.Total_Papers + Fractional.Paper_Change;
        Exhausted.Total_Papers := Exhausted.Total_Papers + Exhausted.Paper_Change;

        return True;
    end Transfer_Votes;




    function Check_No_Quota
        return Boolean
    is
        Running : Position_Vectors.Vector := Extract_Running_Entries;

        Working_Position : Positive;
        Working_ID : Candidates.CandidateID;
    begin
        if Integer (Running.Length) > Vacancies + 1 then
            return False;
        end if;

        Position_Sorting.Sort (Running);

        for R in reverse Running.Iterate loop
            exit when Vacancies = 0;
            Working_Position := Position_Vectors.Element (R).Index;
            Working_ID := Entries.Reference (Working_Position).ID;
            Elect (Working_Position, Working_ID);
        end loop;

        return True;
    end Check_No_Quota;




    function Exclude_Candidates
        return Boolean
    is
        use Ada.Strings;
        use Ada.Strings.Fixed;

        Running : Position_Vectors.Vector := Extract_Running_Entries;
        Log_Msg : SU.Unbounded_String;

        Working_ID : Candidates.CandidateID;
        Working_Position : Positive;
        Number_Excluded : Natural := 0;
        Votes_Excluded : Natural := 0;
        Votes_To_Be_Excluded : Natural;
        Applied_Breakpoint : Positive;
    begin
        Position_Sorting.Sort (Running);
        Applied_Breakpoint := Quota - Running.Reference (Running.Last_Index).Total_Votes;

        for R in Running.Iterate loop
            Votes_To_Be_Excluded := Running.Reference (R).Total_Votes;
            exit when Number_Excluded > 0 and Votes_Excluded + Votes_To_Be_Excluded > Applied_Breakpoint;

            Working_Position := Running.Reference (R).Index;
            Working_ID := Entries.Reference (Working_Position).ID;

            Entries.Reference (Working_Position).Status := Excluded;
            Entries.Reference (Working_Position).Changed := True;
            Transfers.Append ( (From => Working_ID, Position => Working_Position) );
            Votes_Excluded := Votes_Excluded + Votes_To_Be_Excluded;
            Number_Excluded := Number_Excluded + 1;
        end loop;

        if Number_Excluded > 1 then
            Log_Msg := SU.To_Unbounded_String
                   ("Bulk exclusion of " & Trim (Integer'Image (Number_Excluded), Both) &
                    " candidates at logfile #" & Trim (Integer'Image (Next_Log_Num), Both));
            Put_Line (Main_Log, SU.To_String (Log_Msg));
            if Verbose then
                Put_Line (Standard_Error, SU.To_String (Log_Msg));
            end if;
        end if;

        return Number_Excluded > 0;
    end Exclude_Candidates;




    procedure Run is
    begin
        while Work_To_Do loop
            Write_Log;
            Next_Log_Num := Next_Log_Num + 1;

            for E of Entries loop
                E.Vote_Change := 0;
                E.Paper_Change := 0;
                E.Changed := False;
            end loop;

            Fractional.Paper_Change := 0;
            Exhausted.Paper_Change := 0;

            if  not Elect_Candidates and then
                not Check_If_Done and then
                not Transfer_Votes and then
                not Check_No_Quota and then
                not Exclude_Candidates
            then
                --  if all of the above procedures can't do
                --  anything then something is wrong
                raise Program_Error;
            end if;
        end loop;
    end Run;




    function Is_Properly_Setup
        return Boolean is
    begin
        return Work_To_Do;
    end Is_Properly_Setup;


end Election;