forked from ryubromley/MDM_3_Politics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameTheorySimulation.py
132 lines (109 loc) · 4.8 KB
/
GameTheorySimulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import random
import matplotlib.pyplot as plt
def GenerateRandomOpinions(numParties, numPlayers):
listOfPlayersOpinions = [] # list of lists containing initial preference info
for player in range(numPlayers):
playerOpinions = [0] * numParties
for party in range(numParties):
playerOpinions[party] = round(random.uniform(-10, 10),2) # opinions range from -10 to +10, to 2dp
if all(i < 0 for i in playerOpinions): #if person only has negative opinions
randomSwitch = random.randint(0, numParties-1)
playerOpinions[randomSwitch] = -playerOpinions[randomSwitch]
listOfPlayersOpinions.append(playerOpinions)
return listOfPlayersOpinions
def PartyPopularities(
listOfOpinions, numParties): # input list of list of opinions. output list with the number of people who prefer each party
partyPopularity = [0] * numParties
for player in range(len(listOfOpinions)):
opinions = listOfOpinions[player]
maxpref = -11
index = 0
for party in range(numParties):
if opinions[party] > maxpref:
maxpref = opinions[party]
index = party
partyPopularity[index] += 1
return partyPopularity
def GameTheoryODE(playersOpinions, partyPopularities): # returns a new list of party opinions
listOfOpinions = playersOpinions
#print(listOfOpinions)
maxvotes = max(partyPopularities)
alpha = 1
beta = 1
for party in range(len(partyPopularities)):
if partyPopularities[party] == maxvotes:
winningParty = party
for player in range(len(playersOpinions)):
opinions = playersOpinions[player]
newOpinions = playersOpinions[player]
if opinions[winningParty] == max(opinions): # If your prefered party is likely to win, you should always vote for them
newOpinions = opinions
elif opinions[winningParty] < 0: # use weighting of how much you hate that party
for party in range(len(partyPopularities)):
newOpinions[party] += (opinions[party] - opinions[winningParty]) * (partyPopularities[party]/len(playersOpinions)) * alpha
else:
for party in range(len(partyPopularities)):
newOpinions[party] += opinions[party] * (partyPopularities[party]/len(playersOpinions)) * beta
listOfOpinions[player] = newOpinions
#print(listOfOpinions)
return listOfOpinions
def GenerateRandomPoints(numPoints, numDimensions):
listOfPoints = []
for point in range(numPoints):
pointCentre = [0]*numDimensions
for dimension in range(numDimensions):
pointCentre[dimension] = round(random.uniform(-1, 1),2)
listOfPoints.append(pointCentre)
return listOfPoints
def CalculateDistanceToEachParty(point, listOfParties):
listOfDistances = []
for party in listOfParties:
distance = 0
for dim in range(len(point)):
distance += abs(point[dim] - party[dim])
listOfDistances.append(round(distance,2))
return listOfDistances
def PreferenceScoreFromDistance(distances):
listOfPreferences = []
minDistance = min(distances)
for dis in distances:
prefScore = 10 - (dis - minDistance)*6.5
listOfPreferences.append(round(prefScore,2))
return listOfPreferences
def run(numParties, numPlayers):
listOfPartyPop = []
#opinions = GenerateRandomOpinions(numParties, numPlayers)
people = GenerateRandomPoints(numPlayers, 4)
parties = GenerateRandomPoints(numParties, 4)
opinions = []
for person in people:
opinions.append(PreferenceScoreFromDistance(CalculateDistanceToEachParty(person, parties)))
partyPop = PartyPopularities(opinions, numParties)
listOfPartyPop.append(partyPop)
for i in range(26):
opinions = GameTheoryODE(opinions, partyPop)
partyPop = PartyPopularities(opinions, numParties)
listOfPartyPop.append(partyPop)
listOfParties = [1,2,3,4,5,6]
for i in range(25):
plt.bar(listOfParties, listOfPartyPop[i])
plt.ylim(0,round(6*numPlayers/10))
plt.title('Party Votes Changing with Tactical Voting')
plt.xlabel('Parties')
plt.ylabel('Number Of Votes')
plt.savefig('1'*i,bbox_inches='tight')
plt.clf()
import cv2
import numpy as np
import glob
img_array = []
for filename in glob.glob('C:/Users/GFOAT/PycharmProjects/*.png'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width,height)
img_array.append(img)
out = cv2.VideoWriter('project.avi',cv2.VideoWriter_fourcc(*'DIVX'), 1, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
run(6,1000)