From dab6368c14732f24b13ca7855780d177b90d60ff Mon Sep 17 00:00:00 2001 From: A S Date: Sat, 16 Mar 2019 16:47:06 +0100 Subject: [PATCH] =?UTF-8?q?Fixed=20division=20by=20zero=20in=20sequential?= =?UTF-8?q?=20Phragm=C3=A9n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NPoS/simplePhragmén.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/NPoS/simplePhragmén.py b/NPoS/simplePhragmén.py index ff3140e..544f928 100644 --- a/NPoS/simplePhragmén.py +++ b/NPoS/simplePhragmén.py @@ -82,9 +82,12 @@ def seqPhragmén(votelist,numtoelect): candidate.backedstake=0 for nom in nomlist: - for edge in nom.edges: - edge.backingstake = nom.budget * edge.load/nom.load - edge.candidate.backedstake += edge.backingstake + for edge in nom.edges: + if nom.load > 0.0: + edge.backingstake = nom.budget * edge.load/nom.load + edge.candidate.backedstake += edge.backingstake + else: + edge.backingstake = 0 return (nomlist,electedcandidates) def approvalvoting(votelist,numtoelect): @@ -181,6 +184,27 @@ def example1(): print("Sequential Phragmén with post processing gives") printresult(nomlist, electedcandidates) +def example2(): + votelist = [ + ("10", 1000, ["10"]), + ("20", 1000, ["20"]), + ("30", 1000, ["30"]), + ("40", 1000, ["40"]), + ('2', 500, ['10', '20', '30']), + ('4', 500, ['10', '20', '40']) + ] + print("Votes ",votelist) + nomlist, electedcandidates = seqPhragmén(votelist,2) + print("Sequential Phragmén gives") + printresult(nomlist, electedcandidates) + nomlist, electedcandidates = approvalvoting(votelist,2) + print() + print("Approval voting gives") + printresult(nomlist, electedcandidates) + nomlist, electedcandidates = seqPhragménwithpostprocessing(votelist,2) + print("Sequential Phragmén with post processing gives") + printresult(nomlist, electedcandidates) + import unittest class electiontests(unittest.TestCase): def testexample1Phragmén(self):