from candidates import *
from voters import *
from copeland import *
#from psvlambda import *
from exapp import *
from aux import *

NUM_EXPS    = 1000
NUM_ELECS   = 100
NUM_CANDS   = 3
NUM_VOTES   = 50

def generate_elections(no,c,nv,path):
    elections = []
    for i in range(0,no):
        e = generate_election(i,c,nv,path)
        elections.append(e)
    return elections        

def generate_election(no,c,nv,path):
    vs = voters(c, nv, None)
    name = path+"/blt%i.blt" % no
    vs.output_ballots(name)
    v = voters.parser(name)
    return v

if __name__ == "__main__":
    cws = []
    cls = []
    lw00 = []
    lw01 = []
    lw02 = []
    lw03 = []
    lw04 = []
    lw05 = []
    lw06 = []
    lw07 = []
    lw08 = []
    lw09 = []
    lw10 = []
    ll00 = []
    ll01 = []
    ll02 = []
    ll03 = []
    ll04 = []
    ll05 = []
    ll06 = []
    ll07 = []
    ll08 = []
    ll09 = []
    ll10 = []

    for i in range(0,NUM_EXPS):
        c = candidates(None, NUM_CANDS)
        elections = generate_elections(NUM_ELECS,c,NUM_VOTES,"random")

        #check for number of condorcet winners existing
        #print "Calculating the condorcet winners and losers"
        condorcet_winners = 0
        condorcet_losers = 0
        conw_exists_elec = []
        conl_exists_elec = []
        #print "Number of elections %i" % len(elections)
        for e in elections:
            co = copeland(e)
            co.score()
            for cand in c.candidates:
                if co.scores[cand] == NUM_CANDS-1 and len(co.winner()) == 1: #num of remaining cands
                    condorcet_winners += 1
                    conw_exists_elec.append((e, cand))
                if co.scores[cand] == 0: #condorcet loser
                    condorcet_losers += 1
                    conl_exists_elec.append((e, cand))
        #print condorcet_winners
        #print condorcet_losers
        cws.append(float(condorcet_winners)/NUM_ELECS)
        cls.append(float(condorcet_losers)/NUM_ELECS)

        
        #How often do the rules elect the condorcet winner?
        #print "How often do rules elect the Condorcet Winner?"
        w00 = 0
        w01 = 0
        w02 = 0
        w03 = 0
        w04 = 0
        w05 = 0
        w06 = 0
        w07 = 0
        w08 = 0
        w09 = 0
        w10 = 0
        for e,cand in conw_exists_elec:
            e00 = exapp(e, 0.0)
            e01 = exapp(e, 0.1)
            e02 = exapp(e, 0.2)
            e03 = exapp(e, 0.3)
            e04 = exapp(e, 0.4)
            e05 = exapp(e, 0.5)
            e06 = exapp(e, 0.6)
            e07 = exapp(e, 0.7)
            e08 = exapp(e, 0.8)
            e09 = exapp(e, 0.9)
            e10 = exapp(e, 1.0)
            if [cand] != e00.winner():
                w00 += 1
            if [cand] != e01.winner():
                w01 += 1
            if [cand] != e02.winner():
                w02 += 1
            if [cand] != e03.winner():
                w03 += 1
            if [cand] != e04.winner():
                w04 += 1
            if [cand] != e05.winner():
                w05 += 1
            if [cand] != e06.winner():
                w06 += 1
            if [cand] != e07.winner():
                w07 += 1
            if [cand] != e08.winner():
                w08 += 1
            if [cand] != e09.winner():
                w09 += 1
            if [cand] != e10.winner():
                w10 += 1

        lw00.append(float(w00)/condorcet_winners)
        lw01.append(float(w01)/condorcet_winners)
        lw02.append(float(w02)/condorcet_winners)
        lw03.append(float(w03)/condorcet_winners)
        lw04.append(float(w04)/condorcet_winners)
        lw05.append(float(w05)/condorcet_winners)
        lw06.append(float(w06)/condorcet_winners)
        lw07.append(float(w07)/condorcet_winners)
        lw08.append(float(w08)/condorcet_winners)
        lw09.append(float(w09)/condorcet_winners)
        lw10.append(float(w10)/condorcet_winners)

        #print wp, wb, wa, we, ws

        # How often do the rules elect the condorcet loser?
        #print "How often do rules elect the Condorcet Loser?"
        l00 = 0
        l01 = 0
        l02 = 0
        l03 = 0
        l04 = 0
        l05 = 0
        l06 = 0
        l07 = 0
        l08 = 0
        l09 = 0
        l10 = 0
        for e,cand in conl_exists_elec:
            e00 = exapp(e, 0.0)
            e01 = exapp(e, 0.1)
            e02 = exapp(e, 0.2)
            e03 = exapp(e, 0.3)
            e04 = exapp(e, 0.4)
            e05 = exapp(e, 0.5)
            e06 = exapp(e, 0.6)
            e07 = exapp(e, 0.7)
            e08 = exapp(e, 0.8)
            e09 = exapp(e, 0.9)
            e10 = exapp(e, 1.0)
            if [cand] == e00.winner():
                l00 += 1
            if [cand] == e01.winner():
                l01 += 1
            if [cand] == e02.winner():
                l02 += 1
            if [cand] == e03.winner():
                l03 += 1
            if [cand] == e04.winner():
                l04 += 1
            if [cand] == e05.winner():
                l05 += 1
            if [cand] == e06.winner():
                l06 += 1
            if [cand] == e07.winner():
                l07 += 1
            if [cand] == e08.winner():
                l08 += 1
            if [cand] == e09.winner():
                l09 += 1
            if [cand] == e10.winner():
                l10 += 1
        ll00.append(float(l00)/condorcet_losers)
        ll01.append(float(l01)/condorcet_losers)
        ll02.append(float(l02)/condorcet_losers)
        ll03.append(float(l03)/condorcet_losers)
        ll04.append(float(l04)/condorcet_losers)
        ll05.append(float(l05)/condorcet_losers)
        ll06.append(float(l06)/condorcet_losers)
        ll07.append(float(l07)/condorcet_losers)
        ll08.append(float(l08)/condorcet_losers)
        ll09.append(float(l09)/condorcet_losers)
        ll10.append(float(l10)/condorcet_losers)

        #print lp, lb, la, le, ls

    avg_cws = 0
    avg_cls = 0
    avge00w = 0
    avge01w = 0
    avge02w = 0
    avge03w = 0
    avge04w = 0
    avge05w = 0
    avge06w = 0
    avge07w = 0
    avge08w = 0
    avge09w = 0
    avge10w = 0
    avge00l = 0
    avge01l = 0
    avge02l = 0
    avge03l = 0
    avge04l = 0
    avge05l = 0
    avge06l = 0
    avge07l = 0
    avge08l = 0
    avge09l = 0
    avge10l = 0


    for i in range(0,NUM_EXPS):
        avg_cws += cws[i]
        avg_cls += cls[i]
        avge00w += lw00[i]
        avge01w += lw01[i]
        avge02w += lw02[i]
        avge03w += lw03[i]
        avge04w += lw04[i]
        avge05w += lw05[i]
        avge06w += lw06[i]
        avge07w += lw07[i]
        avge08w += lw08[i]
        avge09w += lw09[i]
        avge10w += lw10[i]
        avge00l += ll00[i]
        avge01l += ll01[i]
        avge02l += ll02[i]
        avge03l += ll03[i]
        avge04l += ll04[i]
        avge05l += ll05[i]
        avge06l += ll06[i]
        avge07l += ll07[i]
        avge08l += ll08[i]
        avge09l += ll09[i]
        avge10l += ll10[i]


    avg_cws = float(avg_cws)/(NUM_EXPS)
    sig_cws = sigma(avg_cws,cws)

    avg_cls = float(avg_cls)/(NUM_EXPS)
    sig_cls = sigma(avg_cls,cls)
    
    avge00w = float(avge00w)/NUM_EXPS
    sige00w = sigma(avge00w,lw00)
    avge01w = float(avge01w)/NUM_EXPS
    sige01w = sigma(avge01w,lw01)
    avge02w = float(avge02w)/NUM_EXPS
    sige02w = sigma(avge02w,lw02)
    avge03w = float(avge03w)/NUM_EXPS
    sige03w = sigma(avge03w,lw03)
    avge04w = float(avge04w)/NUM_EXPS
    sige04w = sigma(avge04w,lw04)
    avge05w = float(avge05w)/NUM_EXPS
    sige05w = sigma(avge05w,lw05)
    avge06w = float(avge06w)/NUM_EXPS
    sige06w = sigma(avge06w,lw06)
    avge07w = float(avge07w)/NUM_EXPS
    sige07w = sigma(avge07w,lw07)
    avge08w = float(avge08w)/NUM_EXPS
    sige08w = sigma(avge08w,lw08)
    avge09w = float(avge09w)/NUM_EXPS
    sige09w = sigma(avge09w,lw09)
    avge10w = float(avge10w)/NUM_EXPS
    sige10w = sigma(avge10w,lw10)

    avge00l = float(avge00l)/NUM_EXPS
    sige00l = sigma(avge00l,ll00)
    avge01l = float(avge01l)/NUM_EXPS
    sige01l = sigma(avge01l,ll01)
    avge02l = float(avge02l)/NUM_EXPS
    sige02l = sigma(avge02l,ll02)
    avge03l = float(avge03l)/NUM_EXPS
    sige03l = sigma(avge03l,ll03)
    avge04l = float(avge04l)/NUM_EXPS
    sige04l = sigma(avge04l,ll04)
    avge05l = float(avge05l)/NUM_EXPS
    sige05l = sigma(avge05l,ll05)
    avge06l = float(avge06l)/NUM_EXPS
    sige06l = sigma(avge06l,ll06)
    avge07l = float(avge07l)/NUM_EXPS
    sige07l = sigma(avge07l,ll07)
    avge08l = float(avge08l)/NUM_EXPS
    sige08l = sigma(avge08l,ll08)
    avge09l = float(avge09l)/NUM_EXPS
    sige09l = sigma(avge09l,ll09)
    avge10l = float(avge10l)/NUM_EXPS
    sige10l = sigma(avge10l,ll10)

    

    print "Number of candidates: %i" % NUM_CANDS
    print "Number of voters: %i" % NUM_VOTES
    print "Number of elections: %i" % (NUM_EXPS*NUM_ELECS)
    print
    print "Existence of Condorcet Winner: %f, %f" % (avg_cws*100, sig_cws*100)
    print "Existence of Condorcet Loser: %f, %f" % (avg_cls*100, sig_cls*100)
    print
    print "How often does a procedure not elect the Condorcet winner:"
    print 0.0, avge00w*100, sige00w*100
    print 0.1, avge01w*100, sige01w*100
    print 0.2, avge02w*100, sige02w*100
    print 0.3, avge03w*100, sige03w*100
    print 0.4, avge04w*100, sige04w*100
    print 0.5, avge05w*100, sige05w*100
    print 0.6, avge06w*100, sige06w*100
    print 0.7, avge07w*100, sige07w*100
    print 0.8, avge08w*100, sige08w*100
    print 0.9, avge09w*100, sige09w*100
    print 1.0, avge10w*100, sige10w*100
    print
    print "How often does a procedure elect the Condorcet loser:"
    print 0.0, avge00l*100, sige00l*100
    print 0.1, avge01l*100, sige01l*100
    print 0.2, avge02l*100, sige02l*100
    print 0.3, avge03l*100, sige03l*100
    print 0.4, avge04l*100, sige04l*100
    print 0.5, avge05l*100, sige05l*100
    print 0.6, avge06l*100, sige06l*100
    print 0.7, avge07l*100, sige07l*100
    print 0.8, avge08l*100, sige08l*100
    print 0.9, avge09l*100, sige09l*100
    print 1.0, avge10l*100, sige10l*100
    print


