julien-boudry / Condorcet

Command line application and PHP library, providing an election engine with a high-level interface. Native support 20+ voting methods, easy to extend. Support simple elections with ease or billions of votes in low resource environment. Intensively tested and highly polyvalent.
https://www.condorcet.io
MIT License
119 stars 11 forks source link

$election->getResult() results in error when there are 0 votes #148

Closed LiamM32 closed 1 year ago

LiamM32 commented 1 year ago

This is a problem with the $election->getResult() function in the dev-4.5 branch. When there are 0 votes under the '%tagFilter' specified, calling this function results in an error.

Here is the line from my program that calls it: $filteredPairwise = $contest->getResult(methodOptions: ['%tagFilter' => true, 'withTag' => true, 'tags' => $country])->pairwise; $contest is an object of a class derived from the Election class. Here is the error output:

PHP Fatal error:  Uncaught CondorcetPHP\Condorcet\Throwable\ResultRequestedWithoutVotesException: The result cannot be requested without votes in /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Election.php:479
Stack trace:
#0 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/ElectionProcess/ResultsProcess.php(71): CondorcetPHP\Condorcet\Election->preparePairwiseAndCleanCompute()
#1 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/ElectionProcess/ResultsProcess.php(65): CondorcetPHP\Condorcet\Election->getResult()
#2 /home/liam/src/Eurovision_Condorcet/src/Method/EurovisionSchulze.php(27): CondorcetPHP\Condorcet\Election->getResult()
#3 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Algo/Methods/Schulze/Schulze_Core.php(108): EurovisionVoting\Method\EurovisionSchulze->schulzeVariant()
#4 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Algo/Methods/Schulze/Schulze_Core.php(43): CondorcetPHP\Condorcet\Algo\Methods\Schulze\Schulze_Core->makeStrongestPaths()
#5 /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/ElectionProcess/ResultsProcess.php(82): CondorcetPHP\Condorcet\Algo\Methods\Schulze\Schulze_Core->getResult()
#6 /home/liam/src/Eurovision_Condorcet/src/main.php(38): CondorcetPHP\Condorcet\Election->getResult()
#7 {main}
  thrown in /home/liam/src/Eurovision_Condorcet/vendor/julien-boudry/condorcet/src/Election.php on line 479

Commenting out the ->pairwise part of that line results in the same error.

julien-boudry commented 1 year ago

Good bug! Can argue that's not really a bug but a feature. But a feature designed for the normal election workflow, not really for this filter workflow. The problem is that I don't know what to do here, throw an exception is not bad, because I can't return a result without votes. Maybe another exception class and message.... maybe keep this one.

An easy workaround for you could be to catch the exception and do what must be done if caught. I have seen that you do something with this idea in your project (but with a check before, not catching the exception).

julien-boudry commented 1 year ago

Note that the Fatal error is to not catch the exception, not the exception herself.

LiamM32 commented 1 year ago

Aren't you planning to make a new function for getting pairwise comparisons? In that case, it should give a zero for both $i and $j when there are 0 votes for either.