Skip to content

Commit

Permalink
Merge pull request EGCETSII#26 from joszamama/G2-feature/HuntingtonHill
Browse files Browse the repository at this point in the history
Finalización HuntingtonHill y testeado
  • Loading branch information
beallasai authored Dec 20, 2021
2 parents 219d2fb + e933c10 commit ff69907
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 33 deletions.
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/decide/manage.py",
"args": [
"runserver",
"0.0.0.0:8000"
],
"django": true,
"justMyCode": false,
}
]
}
28 changes: 27 additions & 1 deletion decide/postproc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ def test_identity(self):

values = response.json()
self.assertEqual(values, expected_result)

def testHuntington(self):
data = {
'type': 'HUNTINGTONHILL',
'options': [
{'option':'A','number':1,'votes': 95000},
{'option':'B', 'number':2,'votes': 75000},
{'option':'C', 'number':3,'votes': 25000},
{'option':'D', 'number':4,'votes': 15000}
], 'numEscanyos': 8

}

expected_result = [
{'option':'A','number':1,'votes': 95000,'postproc':4},
{'option':'B', 'number':2,'votes': 75000,'postproc':3},
{'option':'C', 'number':3,'votes': 25000,'postproc':1},
{'option':'D', 'number':4,'votes': 15000,'postproc':0}
]
response = self.client.post('/postproc/', data, format='json')

self.assertEqual(response.status_code, 200)
values = response.json()


self.assertEqual(values, expected_result)


def testDHont1(self): #Fácil de comprobar manualmente
Expand Down Expand Up @@ -205,4 +231,4 @@ def testDHont6(self): #Escaños elevados
self.assertEqual(response.status_code, 200)

values = response.json()
self.assertEqual(values, expected_result)
self.assertEqual(values, expected_result)
65 changes: 33 additions & 32 deletions decide/postproc/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,34 @@ def identity(self, options):
out.sort(key=lambda x: -x['postproc'])
return Response(out)


def HuntingtonHill(self,numEscanos,options):
def HuntingtonHill(self,options,numEscanyos):

votosTotales = 0
for x in options:
votosTotales += x['votes']

if votosTotales > 0 and numEscanos > 0:
if votosTotales > 0 and numEscanyos > 0:

limite = votosTotales/numEscanos
limit = votosTotales/numEscanyos

#Crear parametros para metodo rounding rule
rounding = limite*0.001
lower = limite-rounding
upper = limite+rounding
#Vamos a aplicar la regla rounding
rounding = limit*0.001
lower = limit-rounding
upper = limit+rounding

numEscanosAsig = 0
numEscanyosAsig = 0

while(numEscanosAsig != numEscanos):
while(numEscanyosAsig != numEscanyos):

#si llegamos a aplicar rounding rule y no llegamos al numero igual de escanos,
#reseteamos de nuevo el numero de escanos asig y empezamos de nuevo
numEscanosAsig = 0
##si no se cumple la regla reseteamos cero
numEscanyosAsig = 0

for x in options:

if(x['votes']<limite):
if(x['votes']<limit):
x['postproc']=0
else:
cuota = x['votes']/limite
cuota = x['votes']/limit

if(isinstance(cuota,int)):
x['postproc']=cuota
Expand All @@ -62,22 +60,24 @@ def HuntingtonHill(self,numEscanos,options):
else:
x['postproc']=lQ

numEscanosAsig += x['postproc']
numEscanyosAsig += x['postproc']

#Huntington-Hill Rounding Rule
#For a quota q, let L denote its lower quota, U its upper quota, and G the
#Rounding Rule:


#For a quota q, let L denote it's lower quota, U its upper quota, and G the
#geometric mean of L and U. If then round q down to L, otherwise
#round q up to U.

if(numEscanosAsig < numEscanos):
limite = lower
lower = limite-rounding
upper = limite+rounding
if(numEscanyosAsig < numEscanyos):
limit = lower
lower = limit-rounding
upper = limit+rounding

else:
limite = upper
lower = limite-rounding
upper = limite+rounding
limit = upper
lower = limit-rounding
upper = limit+rounding
else:
for x in options:
x.update({'postproc' : 0})
Expand All @@ -86,14 +86,14 @@ def HuntingtonHill(self,numEscanos,options):
return Response(options)


def dHont(self, options, numEscanos):
def dHont(self, options, numEscanyos):

#Añadimos un campo para el contador de escaños asignados a cada opción
for op in options:
op['postproc'] = 0

#Para cada escaño recorremos todas las opciones usando la fórmula de d'Hont: número de votos de esa opción/(número de escaños asignados a esa opción + 1)
for escano in range(0, numEscanos):
for escano in range(0, numEscanyos):
#Lista de tamaño igual al número de opciones. Recuento al aplicar la fórmula a cada opción (ordenados en la misma forma)
recuento = []
for op in options:
Expand Down Expand Up @@ -123,15 +123,16 @@ def post(self, request):

t = request.data.get('type', 'IDENTITY')
opts = request.data.get('options', [])
numEscanos = request.data.get('numEscanos', 0)
numEscanyos = request.data.get('numEscanyos', 0)


if t == 'IDENTITY':
return self.identity(opts)

elif t == 'HUNTINGTONHILL':
return self.HuntingtonHill(options=opts, numEscanos=numEscanos)
elif t=='HUNTINGTONHILL':
return self.HuntingtonHill(options=opts, numEscanyos=numEscanyos)

elif t == 'DHONT':
return self.dHont(options=opts, numEscanos=numEscanos)
return self.dHont(options=opts, numEscanyos=numEscanyos)

return Response({})
13 changes: 13 additions & 0 deletions decide/voting/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,16 @@ def test_update_voting(self):
response = self.client.put('/voting/{}/'.format(voting.pk), data, format='json')
self.assertEqual(response.status_code, 400)
self.assertEqual(response.json(), 'Voting already tallied')

data = {
'name': 'Example',
'desc': 'Description example',
'tipo': 'HUNTINGTONHILL',
'numEscanos': '10',
'question': 'I am a ',
'question_opt': ['cow', 'cat', 'fish']
}

response = self.client.post('/voting/', data, format='json')
self.assertEqual(response.status_code, 201)

0 comments on commit ff69907

Please sign in to comment.