Exemplo n.º 1
0
def get_distribution(N):
    #setting up versus array
    partitions = pg.partition_generator(N, 5, 0, N)
    num = len(partitions)
    cn = [[0] * num for i in range(num)]

    for i in range(num):
        for j in range(num):
            cn[i][j] = sim.get_probability(partitions[j], partitions[i])

    #setting up cvxpy
    dist = [0] * num
    for i in range(num):
        dist[i] = cp.Variable()
    objective = cp.Maximize(
        sum(cp.entr(dist[i])
            for i in range(len(partitions))))  #maximize entropy
    constraint1 = [
        sum([dist[j] * cn[i][j] for j in range(num)]) >= 0.5
        for i in range(num)
    ]
    constraint2 = [dist[i] >= 0 for i in range(num)]
    constraint3 = [sum(dist) == 1]
    problem = cp.Problem(objective, constraint1 + constraint2 + constraint3)
    problem.solve(solver=cp.ECOS)
    #printing distribution
    print("Entropy: ", problem.value)
    print("Ratio entropy/log(n): ", problem.value / np.log(num))
    print("Probability distribution: ")
    distribution_list = []
    for i in range(num):
        temp = (float)(dist[i].value)
        distribution_list.append(
            (max(0, round(temp, DIGITS_PRECISION)), partitions[i]))
    distribution_list.sort(reverse=True)
    counter = 0
    for a in distribution_list:
        counter = counter + 1
        print(counter, ". ", a[1], " p= ", a[0], sep='')

    print("Responses by Player Two --> Player One Probability of Win")
    response_list = []
    for i in range(num):
        temp = 0
        for j in range(num):
            temp = temp + ((float)(dist[j].value)) * cn[i][j]
        response_list.append((round(temp, 3), partitions[i]))

    response_list.sort()
    for a in response_list:
        print(a[1], " ---> ", a[0])
Exemplo n.º 2
0
def get_distribution(N):
	partitions=pg.partition_generator(N,5,0,N)
	num=len(partitions)

	cn=[[0]*num for i in range(num)]

	for i in range(num):
		for j in range(num):
			cn[i][j]=sim.get_probability(partitions[j],partitions[i])

	dist=[0]*num
	for i in range(num):
		dist[i]=cp.Variable()

	#original
	#objective=cp.Maximize(sum(dist))
	objective=cp.Minimize(sum(dist))
	constraint1=[sum([dist[j]*cn[i][j] for j in range(num)])>=0.5 for i in range(num)]
	constraint2=[dist[i]>=0 for i in range(num)]
	constraint3=[sum(dist)==1]
	problem=cp.Problem(objective,constraint1+constraint2+constraint3)
	problem.solve()

	optimal_distribution={tuple(partitions[i]): dist[i].value for i in range(num)}

	#print("Distribution: ")
	distribution_list=[]
	for i in range(num):
		temp=(float)(dist[i].value)
		distribution_list.append((round(temp,3),partitions[i]))
		#print(partitions[i]," p",i+1," = ",round(temp,3),sep='')
	distribution_list.sort(reverse=True)
	#for a in distribution_list:
		#print(a[1], " p= ", a[0])



	#print("Responses by Player Two --> Player One Probability of Win")
	response_list=[]
	for i in range(num):
		temp=0
		for j in range(num):
			temp=temp+((float)(dist[j].value))*cn[i][j]
		response_list.append((round(temp,3),partitions[i]))

	response_list.sort()
	#for a in response_list:
		#print(a[1]," ---> ", a[0])

	return optimal_distribution
Exemplo n.º 3
0
def get_distribution(N):
    #setting up versus array
    if ALREADY_CALCULATED == True:
        return dlr.dist_list_reader()
    partitions = pg.partition_generator(N, 5, 0, N)
    #partitions=pg.partition_generator(N,5,0,int(float(2*N/5+1)))
    num = len(partitions)
    cn = [[0] * num for i in range(num)]

    for i in range(num):
        for j in range(num):
            cn[i][j] = sim.get_probability(partitions[j], partitions[i])

    #setting up cvxpy
    dist = [0] * num
    for i in range(num):
        dist[i] = cp.Variable()
    objective = cp.Maximize(
        sum(cp.entr(dist[i])
            for i in range(len(partitions))))  #maximize entropy
    constraint1 = [
        sum([dist[j] * cn[i][j] for j in range(num)]) >= 0.5
        for i in range(num)
    ]
    constraint2 = [dist[i] >= 0 for i in range(num)]
    constraint3 = [sum(dist) == 1]
    problem = cp.Problem(objective, constraint1 + constraint2 + constraint3)
    problem.solve()
    #printing distribution
    print("Probability distribution: ")
    distribution_list = []
    for i in range(num):
        temp = (float)(dist[i].value)
        distribution_list.append(
            (max(0, round(temp, DIGITS_PRECISION)), partitions[i]))
    distribution_list.sort(reverse=True)
    for a in distribution_list:
        print(a[1], " p= ", a[0], sep='')
    return distribution_list
Exemplo n.º 4
0
def get_distribution(N, T):
    partitions = pg.partition_generator(N, 5, 0, N)
    num = len(partitions)

    cn = [[0] * num for i in range(num)]

    for i in range(num):
        for j in range(num):
            cn[i][j] = sim.get_probability(partitions[j], partitions[i])

    dist = [0] * num
    for i in range(num):
        dist[i] = cp.Variable()

    #Maximize probability of being at least X
    tlist = [0] * len(partitions)
    for i in range(len(partitions)):
        counter = 0
        for j in range(5):
            if partitions[i][j] >= T:
                counter = counter + 1
        tlist[i] = (counter / 5)
    objective = cp.Maximize(
        sum(dist[i] * tlist[i] for i in range(len(partitions)))
    )  #maximize chance that more than a certain amount (T) is sent to a front
    constraint1 = [
        sum([dist[j] * cn[i][j] for j in range(num)]) >= 0.5
        for i in range(num)
    ]
    constraint2 = [dist[i] >= 0 for i in range(num)]
    constraint3 = [sum(dist) == 1]
    problem = cp.Problem(objective, constraint1 + constraint2 + constraint3)
    problem.solve()

    optimal_distribution = {
        tuple(partitions[i]): dist[i].value
        for i in range(num)
    }
    ans = (float)(problem.value)
    print("T=", T, "  --> ", round(ans, 3))
    # distribution_list=[]
    # for i in range(num):
    # 	temp=(float)(dist[i].value)
    # 	distribution_list.append((round(temp,3),partitions[i]))
    # distribution_list.sort(reverse=True)
    # for a in distribution_list:
    # 	print(a[1], " p= ", a[0])

    # print("Responses by Player Two --> Player One Probability of Win")
    # response_list=[]
    # for i in range(num):
    # 	temp=0
    # 	for j in range(num):
    # 		temp=temp+((float)(dist[j].value))*cn[i][j]
    # 	response_list.append((round(temp,3),partitions[i]))

    # response_list.sort()
    # for a in response_list:
    # 	print(a[1]," ---> ", a[0])

    return optimal_distribution
Exemplo n.º 5
0
def solve(N):
    three = []
    for blah in pg.three_front_generator(N):
        if blah[2] <= 2 * N / 5 + 1:
            three.append(blah)
    #three=pg.three_front_generator(N)
    five = pg.partition_generator(N, 5, 0, N)
    num3 = len(three)
    num5 = len(five)
    dist = [0] * num3
    for i in range(num3):
        dist[i] = cp.Variable()
    res = [[0] * num5 for i in range(num3)]
    for i in range(num3):
        for j in range(num5):
            res[i][j] = getResults(three[i], five[j])

    #Settng up the problem
    w = cp.Variable()
    constraintList = []
    constraint1 = [sum(dist) == 1]
    constraint2 = [
        sum(dist[i] * (three[i][0] + three[i][1] + three[i][2])
            for i in range(num3)) == 0.6 * N
    ]
    constraint3 = [dist[i] >= 0 for i in range(num3)]
    constraint4 = [
        sum(res[i][j] * dist[i] for i in range(num3)) >= w for j in range(num5)
    ]
    #E[x]=E[x^2]+4E[xy]
    #constraint5=[sum(dist[i]*(three[i][0]+three[i][1]+three[i][2])/(3*N) for i in range(num3))==sum(dist[i]*(three[i][0]**2+three[i][1]**2+three[i][2]**2)/(3*N**2) for i in range(num3))+4*sum(dist[i]*(three[i][0]*three[i][1]+three[i][0]*three[i][2]+three[i][1]*three[i][2])/(3*N**2) for i in range(num3))]
    constraint5 = [
        getMoment(N, 1, 0, 0, three,
                  dist) == getMoment(N, 2, 0, 0, three, dist) +
        4 * getMoment(N, 1, 1, 0, three, dist)
    ]
    #E[x^2]=E[x^3]+4E[x^2y]
    #constraint6=[sum(dist[i]*(three[i][0]**2+three[i][1]**2+three[i][2]**2)/(3*N**2) for i in range(num3))==sum(dist[i]*(three[i][0]**3+three[i][1]**3+three[i][2]**3)/(3*N**3) for i in range(num3))+4*sum(dist[i]*(three[i][0]**2*three[i][1]+three[i][0]**2*three[i][2]+three[i][1]**2*three[i][0]+three[i][1]**2*three[i][2]+three[i][2]**2*(three[i][0]+three[i][1]))/(6*N**3) for i in range(num3))]
    constraint6 = [
        getMoment(N, 2, 0, 0, three,
                  dist) == getMoment(N, 3, 0, 0, three, dist) +
        4 * getMoment(N, 2, 1, 0, three, dist)
    ]
    #E[xy]=2E[x^2y]+3E[xyz]
    #constraint7=[sum(dist[i]*(three[i][0]*three[i][1]+three[i][0]*three[i][2]+three[i][1]*three[i][2])/(3*N**2) for i in range(num3))==2*sum(dist[i]*(three[i][0]*three[i][1]*(three[i][0]+three[i][1])+three[i][0]*three[i][2]*(three[i][0]+three[i][2])+three[i][1]*three[i][2]*(three[i][1]+three[i][2]))/(6*N**3) for i in range(num3))+3*sum(dist[i]*(three[i][0]*three[i][1]*three[i][2])/N**3 for i in range(num3))]
    constraint7 = [
        getMoment(N, 1, 1, 0, three,
                  dist) == 2 * getMoment(N, 2, 1, 0, three, dist) +
        3 * getMoment(N, 1, 1, 1, three, dist)
    ]

    #E[x^3]=E[x^4]+4E[x^3y]
    #constraint8=[sum(dist[i]*(three[i][0]**3+three[i][1]**3+three[i][2]**3)/(3*N**3) for i in range(num3))==sum(dist[i]*(three[i][0]**4+three[i][1]**4+three[i][2]**4)/(3*N**4) for i in range(num3))+4*sum(dist[i]*(three[i][0]*three[i][1]*(three[i][0]**2+three[i][1]**2)+three[i][0]*three[i][2]*(three[i][0]**2+three[i][2]**2)+three[i][1]*three[i][2]*(three[i][1]**2+three[i][2]**2))/(6*N**4) for i in range(num3))]
    constraint8 = [
        getMoment(N, 3, 0, 0, three,
                  dist) == getMoment(N, 4, 0, 0, three, dist) +
        4 * getMoment(N, 3, 1, 0, three, dist)
    ]
    #E[x^2y]=E[x^3y]+E[x^2y^2]+3E[x^2yz]
    #constraint9=[sum(dist[i]*(three[i][0]*three[i][1]*(three[i][0]+three[i][1])+three[i][0]*three[i][2]*(three[i][0]+three[i][2])+three[i][1]*three[i][2]*(three[i][1]+three[i][2]))/(6*N**3) for i in range(num3))==sum(dist[i]*(three[i][0]*three[i][1]*(three[i][0]**2+three[i][1]**2)+three[i][0]*three[i][2]*(three[i][0]**2+three[i][2]**2)+three[i][1]*three[i][2]*(three[i][1]**2+three[i][2]**2))/(6*N**4) for i in range(num3))+sum(dist[i]*(three[i][0]**2*three[i][1]**2+three[i][1]**2*three[i][2]**2+three[i][0]**2*three[i][2]**2)/(3*N**4) for i in range(num3))+3*sum(dist[i]*(three[i][0]*three[i][1]*three[i][2]*(three[i][0]+three[i][1]+three[i][2]))/(3*N**4) for i in range(num3))]
    constraint9 = [
        getMoment(N, 2, 1, 0, three,
                  dist) == getMoment(N, 3, 1, 0, three, dist) +
        getMoment(N, 2, 2, 0, three, dist) +
        3 * getMoment(N, 2, 1, 1, three, dist)
    ]

    constraintList += constraint1
    constraintList += constraint2
    constraintList += constraint3
    constraintList += constraint4
    constraintList += constraint5
    constraintList += constraint6
    constraintList += constraint7
    constraintList += constraint8
    constraintList += constraint9

    objective = cp.Maximize(w)
    problem = cp.Problem(
        objective, constraintList
    )  #constraint1+constraint2+constraint3+constraint4+constraint5+constraint6+constraint7+constraint8+constraint9)
    problem.solve(solver=cp.ECOS)
    #Printing results
    print(problem.value)
    distribution_list = []
    for i in range(num3):
        temp = (float)(dist[i].value)
        distribution_list.append((max(0, round(temp, 4)), three[i]))
    distribution_list.sort(reverse=True)
    counter = 0
    for a in distribution_list:
        counter = counter + 1
        print(counter, ". ", a[1], " p= ", a[0], sep='')
    CONSTdist = [0] * num3
    for i in range(num3):
        CONSTdist[i] = dist[i].value

    #Consructing a five front distribution that best models this three front distribution
    contributionMatrix = [[0] * num5 for i in range(num3)]
    for i in range(num3):
        for j in range(num5):
            contributionMatrix[i][j] = getContribution(three[i], five[j])
    cn = [[0] * num5 for i in range(num5)]
    for i in range(num5):
        for j in range(num5):
            cn[i][j] = sim.get_probability(five[j], five[i])
    constraintList2 = []
    maxDiff = cp.Variable()
    dist2 = [0] * num5
    for i in range(num5):
        dist2[i] = cp.Variable()

    #old constraints
    Constraint1 = [
        sum([dist2[j] * cn[i][j] for j in range(num5)]) >= 0.5
        for i in range(num5)
    ]
    Constraint2 = [dist2[i] >= 0 for i in range(num5)]
    Constraint3 = [sum(dist2) == 1]

    #new constraints
    #maxDiff should be positive
    Constraint4 = [maxDiff >= 0]
    #maximum absolute difference between a triplet from three front dist and extracted triplet from five front dist is maxDiff
    Constraint5 = [
        sum(dist2[i] * contributionMatrix[j][i]
            for i in range(num5)) - CONSTdist[j] <= maxDiff
        for j in range(num3)
    ]
    Constraint6 = [
        CONSTdist[j] - sum(dist2[i] * contributionMatrix[j][i]
                           for i in range(num5)) <= maxDiff
        for j in range(num3)
    ]

    constraintList2 += Constraint1
    constraintList2 += Constraint2
    constraintList2 += Constraint3
    constraintList2 += Constraint4
    constraintList2 += Constraint5
    constraintList2 += Constraint6
    objective = cp.Minimize(maxDiff)
    problem = cp.Problem(objective, constraintList2)
    problem.solve(solver=cp.ECOS)
    print("maxDiff: ", problem.value)

    #printing data
    print("\nCorresponding Five Front Distribution")
    distribution_list = []
    for i in range(num5):
        temp = (float)(dist2[i].value)
        distribution_list.append((max(0, round(temp, 5)), five[i]))
    distribution_list.sort(reverse=True)
    counter = 0
    for a in distribution_list:
        counter = counter + 1
        print(counter, ". ", a[1], " p= ", a[0], sep='')

    print("\nThree front distribution from above:")
    #sim.extract_three_tuples_from_known(N,distribution_list)
    for i in range(num3):
        val = round(
            sum(dist2[j].value * contributionMatrix[i][j]
                for j in range(num5)), 4)
        print(three[i], " From distribution: ", val, "Should be: ",
              np.round(CONSTdist[i], 4))