-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic_population.py
227 lines (192 loc) · 8.47 KB
/
logic_population.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import random as rnd
import time
import yaml
import matplotlib.pyplot as plt
class Mistakes1D:
def __init__(self, error, population):
# size of population
self.population = population
# change error
self.chanceError = error
self.maxIterations = 100000
# arrangement of cells on the field
self.field = []
# coordinates for white and black cells
self.whiteCoordinates = []
self.blackCoordinates = []
# name of yaml data file
self.nameYAML = "mistakes.yaml"
def clearField(self):
"""Method for clearing class """
self.field = []
self.whiteCoordinates = []
self.blackCoordinates = []
def createRandomField(self):
"""Create a random arrangement of cells on the field """
self.clearField()
strFiled = f"{'w' * (self.population // 2)}{'b' * (self.population // 2)}"
self.field = list(strFiled)
rnd.shuffle(self.field)
# save coordinates of cells
for coord, cell in enumerate(self.field):
if cell == "w":
self.whiteCoordinates.append(coord)
else:
self.blackCoordinates.append(coord)
def createBadField(self):
"""Create a field with 0 happiness """
self.clearField()
for i in range(self.population):
if i % 2 == 0:
self.field.append("w")
else:
self.field.append("b")
# save coordinates of cells
for coord, cell in enumerate(self.field):
if cell == "w":
self.whiteCoordinates.append(coord)
else:
self.blackCoordinates.append(coord)
def getRandomCoord(self):
"""Get random coordinates """
whiteCoord = rnd.choice(self.whiteCoordinates)
blackCoord = rnd.choice(self.blackCoordinates)
return whiteCoord, blackCoord
def getNeighbours(self, coord):
"""Get neighbors by coordinates """
neighboursCoord = [coord - 1]
if coord + 1 == self.population:
neighboursCoord.append(0)
else:
neighboursCoord.append(coord + 1)
neighbours = [self.field[nCoord] for nCoord in neighboursCoord]
return neighbours
def getHappinessByCoordAndColor(self, coord, cell):
"""Calculate happiness for a coordinate and type cell
:param coord: coordinate cell
:param cell: type cell
:return: happiness for cell
"""
happiness = 0
temporaryNeighbours = self.getNeighbours(coord)
neighbours = []
for neighbour in temporaryNeighbours:
# substitute the values of neighbors
if rnd.random() < self.chanceError:
neighbour = "b" if neighbour == "w" else "w"
neighbours.append(neighbour)
for neighbour in neighbours:
if cell == neighbour:
happiness += 1
return happiness
def getFieldHappiness(self):
"""Calculate happiness for field """
happiness = 0
for coord, cell in enumerate(self.field):
neighbours = self.getNeighbours(coord)
for neighbour in neighbours:
if cell == neighbour:
happiness += 1
return happiness
def getMaxHappiness(self):
return (self.population - 4) * 2 + 4
def imitateLiveOfPopulation(self):
"""Main method for generating life """
maxHappiness = 0
maxFieldHappiness = self.getMaxHappiness()
iterations = 0
while (maxFieldHappiness != maxHappiness) and (iterations < self.maxIterations):
rndWhite, rndBlack = self.getRandomCoord()
# calculate current happiness for random cells
nowHappinessW = self.getHappinessByCoordAndColor(rndWhite, "w")
nowHappinessB = self.getHappinessByCoordAndColor(rndBlack, "b")
# calculate happiness for cells when changing location
newHappinessW = self.getHappinessByCoordAndColor(rndBlack, "w")
newHappinessB = self.getHappinessByCoordAndColor(rndWhite, "b")
# swap the coordinates if one cell will receive + happiness
# another cell will not lose happiness
if ((newHappinessW - nowHappinessW) >= 1 and (newHappinessB - nowHappinessB >= 0)) \
or ((newHappinessB - nowHappinessB) >= 1 and (newHappinessW - nowHappinessW) >= 0):
self.field[rndWhite], self.field[rndBlack] = self.field[rndBlack], self.field[rndWhite]
self.whiteCoordinates.remove(rndWhite)
self.whiteCoordinates.append(rndBlack)
self.blackCoordinates.remove(rndBlack)
self.blackCoordinates.append(rndWhite)
# calculate new filed happiness
maxHappiness = self.getFieldHappiness()
iterations += 1
return iterations
def testChancesMistakes(self):
"""Method for test chances error """
chances = [0.001, 0.0015, 0.005, 0.01, 0.015, 0.02, 0.05, 0.075, 0.1, 0.125, 0.175, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5]
dictChancesInfo = {}
for chance in chances:
self.chanceError = chance
iterations = []
for i in range(50):
print(i)
self.createBadField()
iterations.append(self.imitateLiveOfPopulation())
dictChancesInfo[chance] = sum(iterations) // len(iterations)
print(f"OK {chance}")
self.saveResultYAML(dictChancesInfo)
def saveResultYAML(self, data):
"""Save test data in YAML """
with open(self.nameYAML, "w", encoding="utf-8") as file:
yaml.dump(data, file)
def savePlotByYAML(self):
"""Create plot by YAML data file """
with open(self.nameYAML, "r", encoding="utf-8") as file:
data = yaml.safe_load(file)
xdata = list(data.keys())
xdata = [f"{x*100}%" for x in xdata]
ydata = list(data.values())
plt.bar(xdata, ydata, color='green')
plt.xlabel("% лжи")
plt.xticks(size=7, rotation=65)
plt.ylabel("Итерации")
plt.title("Зависимость итераций от % лжи")
plt.savefig("plot.jpeg")
def generatorImitateLiveOfPopulation(self):
"""Main method for generating life """
maxHappiness = 0
maxFieldHappiness = self.getMaxHappiness()
iterations = 0
oldHappiness = 0
while (maxFieldHappiness != maxHappiness) and (iterations < self.maxIterations):
wasChanged = False
rndWhite, rndBlack = self.getRandomCoord()
# calculate current happiness for random cells
nowHappinessW = self.getHappinessByCoordAndColor(rndWhite, "w")
nowHappinessB = self.getHappinessByCoordAndColor(rndBlack, "b")
# calculate happiness for cells when changing location
newHappinessW = self.getHappinessByCoordAndColor(rndBlack, "w")
newHappinessB = self.getHappinessByCoordAndColor(rndWhite, "b")
# swap the coordinates if one cell will receive + happiness
# another cell will not lose happiness
if ((newHappinessW - nowHappinessW) >= 1 and (newHappinessB - nowHappinessB >= 0)) \
or ((newHappinessB - nowHappinessB) >= 1 and (newHappinessW - nowHappinessW) >= 0):
wasChanged = True
self.field[rndWhite], self.field[rndBlack] = self.field[rndBlack], self.field[rndWhite]
self.whiteCoordinates.remove(rndWhite)
self.whiteCoordinates.append(rndBlack)
self.blackCoordinates.remove(rndBlack)
self.blackCoordinates.append(rndWhite)
oldHappiness = maxHappiness
# calculate new filed happiness
maxHappiness = self.getFieldHappiness()
iterations += 1
yield {"iterations": iterations, "wasChanged": wasChanged, "rndWhite": rndWhite, "rndBlack": rndBlack,
"error": self.chanceError,
"currentHappiness": maxHappiness, "oldHappiness": oldHappiness,
"maxHappiness": maxFieldHappiness}
if __name__ == "__main__":
# a = Mistakes1D(error=0.125, population=32)
# a.createBadField()
# if you want to test errors
# a.testChancesMistakes()
# if you want to run imitation
# a.imitateLiveOfPopulation()
# if you want to draw a graph
# a.savePlotByYAML()
pass