Support the Condorcet Election Format as input
The Condorcet Election Format (.cvotes) is a tentative to standardize election input (configuration & vote data) in a human-readable and editable format, but also a precise definition that is easy to parse for a program.
https://github.com/CondorcetVote/CondorcetElectionFormat
Since CIVS already has a format, one easy initial step would be write a converter from this format to the CIVS input format.
I guess there's some some PHP code to parse this? And interpret it? Not the language I'd pick for the job, to be honest.
There is one here:
- Code class: https://github.com/julien-boudry/Condorcet/blob/master/src/Tools/Converters/CondorcetElectionFormat.php
- Usage doc: https://www.condorcet.io/3.AsPhpLibrary/8.GoFurther/4.ElectionFilesFormats
But it is strongly coupled with the Condorcet library of the same project. It is therefore not a pure parser, as its purpose is to convert into an Election object but not expose raw data into data structure.
However, it would be easy to add a tiny wrapper to export it in Civs format. If it's in PHP, I could even take care of this abstraction which would perform these tasks: Input => CondorcetElection object => Export to Civs format (to define ?)
This is less elegant than a more agnostic parser. And possibly a PERL one.
And also, some protection must be planned, because the Condorcet PHP can manage billions of votes.
But in the context of a web service, limits must be set because it can be an attack, I have just to write the following:
A > B > C * 10000000000000000
The CIVS input format does support weighting votes. The tabulation algorithms don't, but that is a separate problem.
With the CondorcetElectionFormat, you have:
Weight:
A > B ^100 # One vote with a weight of 100
Quantifier:
A > B *100 # 100 identical votes
Both:
A > B ^10 *100 # 100 identical votes with for each a weight of 10
Weight can be unsupported with a warning (or better: silently converted to a quantifier, because it s the same for the results calculation). But the quantifier cannot be ignored because it's change the result.
The main problem should be that Civs format uses an order to identify a candidate, but Cef format uses the candidate's name. So, the conversion should know in which order the candidate are registered first. Actual Condorcet libraries can do it because internally the candidates have an Id, but it's a little bit hacky.
Another problem is the ties, Cef supports ties on a rank.
And also: implicit ranking. How absence of choice is implemented. With the Condorcet logic, it's filled as a last rank per default, unless implicit ranking is false. This choice can change the result.
An illustration of the conversion could be:
Cef:
#/Candidates: A ; B ; C ; D
#/Weight allowed: true
#/Implicit Ranking: true
A > B > C * 2
C > B > A ^2
B > A > C ^2 *2
Civs:
2X1,2,3,4
2X3,2,1,4
4X2,1,3,4
Or if Cef set: #/Implicit Ranking: false
Civs:
2X1,2,3,-
2X3,2,1,-
4X2,1,3,-
But the following, I don't know how to do it:
Cef:
#/Candidates: A ; B ; C ; D
A > B = C > D
CIVS will let you specify ties. Your example would be
1,2,2,3
Is there a comment system, where I can put additional information, such as the identity of the candidates?
Lines beginning with a # are treated as comments and ignored.
Just wrote a PR to add a Converter (export only) to the Civs format inside the Condorcet project: https://github.com/julien-boudry/Condorcet/pull/134
Can you read, especially the test file, and validate it?
Then, it should be easy to write a conversion routine from Cef to Civs. Something like this:
$election = (new CondorcetElectionFormat(<string or file input>))->setDataToAnElection();
CivsFormat::createFromElection(election: $election, file: null); // return string unless file is set
Or, a little bit more ambitious: add a Converter command to the Condorcet command line version.
Condorcet v4.4 just release with support of exporting to Civs format.
And also a new console command convert.
https://www.condorcet.io/2.AsCommandLineApplication/3.ImportFromElectionFormat?id=convert-formats
As command line application:
condorcet convert --from-condorcet-election-format --to-civs-format pathToInput pathToOutput
As PHP library:
$election = (new CondorcetElectionFormat(<string or file input>))->setDataToAnElection();
CivsFormat::createFromElection(election: $election, file: null); // return string unless file is set
Examples of conversions can be seen in the test file: https://github.com/julien-boudry/Condorcet/blob/master/Tests/src/Tools/Converters/CivsFormatTest.php
Better: should be the first alternative implementation in Perl of this standard.