Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

using @interact to enter some information in election system

The following code gives the candidat who must be eliminated in a *Nanson election *.

def Nanson_one(cand, partisans,code):
    Scand=sorted(Set(cand))
    ncand=len(cand)
    All_pref=Arrangements(Scand,ncand).list()
    All_pref_with_index = [(i,All_pref[i]) for i in range(len(All_pref))]
    rank=[[(list(All_pref[i]).index(cand[j]))+1 for i in range(len(All_pref))] for j in range(len(cand))]
    score =  piecewise([([i,i], ncand+1-i) for i in range(ncand+1)])
    scores=[[partisans[i]*score(rank[j][i]) for i in range(len(All_pref))] for j in range(len(cand))]
    totscore=[add(scores[i]) for i in range(len(cand))]
    augmented_scores = [scores[i]+[totscore[i]] for i in range(len(cand))]
    ahr=[i for i in range(len(All_pref))]+["$\\text{Total}$"]
    minvote = min(totscore)
    candidat_éliminé=min(totscore)
    indice_candidat_éliminé=totscore.index(min(totscore)) 
    nom_candidat_éliminé=cand[indice_candidat_éliminé]
    if code == 1 :
         return table(augmented_scores,header_row=ahr,header_column=["$\\stackrel{\\normalsize Préf}{Cand}$","$\\boldsymbol{A}$","$\\boldsymbol{B}$","$\\boldsymbol{C}$","$\\boldsymbol{D}$"])
    elif code == 2 : 
          show(LE(r"\text{Le candidat éliminé est le candidat }"), nom_candidat_éliminé, LE(r"\text{ qui n'a reçu que }"),minvote,LE(r"\text{ votes.}"))
elif code == 3 : 
          return nom_candidat_éliminé

code = 1 gives the table of scores

code =2 gives the name of the candidat who must be excluded.

In what concerns passing the cand parameter, there is no difficulty it must be

cand = ["A","B","C","D"]

For partisans parameter it's much involved, since it correspond to the number of electors whose preferences are ABCD, ACBD, ACDB... For teaching purpose, it is necessary to construct a list of number whise entries correspond to the number of voters for ABCD...

I have seen that input is possible with @interactbut the display is elementary and works for small matrix. Here I need the some information (the ABCD's) be above the cell which must be documented (by default it is zero).

I can do it manualy as demonstrated by the following code :

cand = ["A","B","C","D"]
All_pref=Arrangements(Scand,ncand).list()
LE=LatexExpr
All_pref_with_index = [(i,All_pref[i]) for i in range(len(All_pref))]
show(LE(r"\text{Toutes les préférences sont possibles} "))
show(All_pref_with_index)
#Toutes les entrées de partisans sont mises à 0 puis on rajoute celles qu'on veut.
partisans=[0 for i in range(len(All_pref))]
partisans[1]=25
partisans[10]=15
partisans[12]=5
partisans[16]=12
partisans[23]=3
hr=[i for i in range(len(All_pref))]

show(LatexExpr(r"\text{Le nombre des électeurs qui adoptent les préférences sont :}"))
table([partisans], header_row=hr,header_column=[r"$ \text{Indice des préférences }",r"$\text{Nombre d'électeurs}"],frame=true)

but I think it will be better if my student can change thge number of electors in an @interact cell.

But if there is no solution it's not a big problem. Now my true problem is the following:

the result of Nanson_one(cand, partisans,3) is the name of the eliminated candidate. So I must drop its name from the list cand. Here is the code

cand.remove(Nanson_one(cand, partisans,3))

and this works. But if I try to allocate

cand1=cand.remove(Nanson_one(cand, partisans,3))

when I try to display it I obtain nothing.

Hope to have been clear and to have given all needed information.