-
Notifications
You must be signed in to change notification settings - Fork 0
/
loot.py
312 lines (247 loc) · 10.7 KB
/
loot.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Calculates the cumulative probability of obtaining all n items from a
a boss after some k runs. We assume that each item has an equal chance
of dropping. We resort to numerical estimations in doing so.
"""
__author__ = "Othman Alikhan"
__email__ = "[email protected]"
import string
import numpy as np
import matplotlib.pyplot as plt
class Item:
"""
Representation of an in-game item that can be dropped.
"""
itemsGenerated = 0
def __init__(self, probability, isWanted):
"""
Assigns an alphabet ID to the item generated and initializes.
:param probability: Number, the probability of the item being dropped.
:param isWanted: Boolean, whether this item is sought by the user.
"""
self.id = string.ascii_uppercase[Item.itemsGenerated]
Item.itemsGenerated += 1
self.probability = probability
self.isWanted = isWanted
self.hasDroppedRecently = False # Flag used to simplify code
self.totalDrops = 0
self.totalAttempts = 0
class Instance:
"""
Representation of an in-game instance which drops items.
"""
def __init__(self, itemsList):
"""
Constructs the instance. Extracts the total amount of wanted items
to be dropped. Also, extracts the probabilities for each item to be
dropped.
:param itemsList: List, containing Item objects.
"""
self.runNum = 1
self.totalAttempts = 0
self.totalWantedItems = 0
self.droppedWantedItems = 0
self.itemsList = itemsList
self.itemsProbabilityList = []
for item in itemsList:
self.itemsProbabilityList.append(item.probability)
if item.isWanted:
self.totalWantedItems += 1
def plotSimulationProbabilities(self, maxRuns, iterations=10000):
"""
Plots the probability and cumulative probability of successfully getting
all items from the boss against the number of runs on a 2D graph using
matplotlib.
:param maxRuns: Number, the maximum number of instance runs.
:param iterations: Number, used to reduce numerical errors.
"""
title = "Number of Drops From Boss={}" \
"\nNumber of Numerical Iterations={}" \
.format(len(self.itemsList), iterations)
fileName = "Items={}_Iterations={}"\
.format(len(self.itemsList), iterations)
# Initializing the y-axis tick marks
yticksList = []
for i in range(11):
yticksList.append(i/10)
# Generating the data
runList, runsProbabilityList, runsCumulativeProbabilityList = \
self.generateData(maxRuns, iterations)
# Plotting the data
fig = plt.figure()
fig.canvas.set_window_title(fileName)
fig.set_size_inches(10, 8)
subplot = fig.add_subplot(111)
plt.plot(runList,
runsProbabilityList,
color="r",
marker="x",
label="Probability For All Wanted Items In X Runs Exactly")
plt.plot(runList,
runsCumulativeProbabilityList,
color="g",
marker="x",
label="Probability For All Wanted Items In X Runs or Less")
# Shrinking graph to allow extra information to be placed on the it's
# right (items and their drop chances)
box = subplot.get_position()
subplot.set_position([box.x0, box.y0, 0.8*box.width, box.height])
for i, item in enumerate(self.itemsList):
plt.figtext(0.80, 0.84 - i/15,
"Item {}: {:<3.1f}%\nWanted={}"
.format(item.id, 100*item.probability, item.isWanted),
bbox=dict(facecolor="white", alpha=0.5),
fontweight='bold',
fontsize=12)
# Painting the columns on the right hand side below the legend
# that contains the probability tables
columns = list(zip(runList, runsCumulativeProbabilityList))
leftColumn = columns[:len(runList)//2]
rightColumn = columns[len(runList)//2:]
plt.figtext(0.76, 0.62,
"Probability For All Wanted\nItems in X Runs or Less",
weight='bold')
for i, (run, probability) in enumerate(leftColumn):
plt.figtext(0.76, 0.59 - i/50,
"{:<2}, {:.1%}".format(run, probability))
for i, (run, probability) in enumerate(rightColumn):
plt.figtext(0.88, 0.59 - i/50,
"{:<2}, {:.1%}".format(run, probability))
# Adjusting graph settings
plt.title(title, fontweight="bold")
plt.legend(loc='upper right', fontsize=12)
plt.ylabel('Probability')
plt.yticks(yticksList)
plt.ylim(plt.ylim()[0], 1.2)
plt.xlabel('Instance Runs')
plt.show()
def generateData(self, maxRuns, iterations=10000):
"""
Executes the consecutive runs simulation, obtains data, calculates
probabilities for each set of successful consecutive runs, then returns
the processed data ready to be plotted.
:param maxRuns: Number, the maximum number of instance runs.
:param iterations: Number, used to reduce numerical errors.
:return: Dictionary, mapping runs to dropped items.
"""
runsDroppedDict = self.simulateConsecutiveRuns(maxRuns, iterations)
return self.calculateSimulationProbabilities(runsDroppedDict)
def simulateConsecutiveRuns(self, maxRuns, iterations=10000):
"""
Per iteration, generates a consecutive sequence of runs until the upper
limit is reached or until all the items have dropped in the sequence.
Returns a dictionary that contains items of form (runNumber : Number
of times successfully obtained all wanted items)
:param maxRuns: Number, the maximum number of instance runs.
:param iterations: Number, used to reduce numerical errors.
:return: Dictionary, mapping runs to dropped items.
"""
runsDroppedDict = {i:0 for i in range(1, maxRuns+1)}
# Generating Data:
# The inner while-loop represents items dropping in consecutive runs up
# to a maximum of maxRuns runs. The outer-loop is solely to repeat this
# process multiple times for a better numerical solution.
for _ in range(iterations):
# Initialization per new series of runs
self.reset()
while(self.runNum <= maxRuns):
self.dropItem()
# Checks if all items have dropped yet
if self.haveAllItemsDropped():
runsDroppedDict[self.runNum] += 1
break
self.runNum += 1
return runsDroppedDict
def calculateSimulationProbabilities(self, runsDroppedDict):
"""
Calculates the probabilities from the data generated by the
simulation function. The runs, probabilities and cumulative
probabilities are calculated.
:param runsDroppedDict: Dictionary, mapping runs to dropped items.
"""
dimension = len(runsDroppedDict)
runsList = dimension*[0]
runsProbabilityList = dimension*[0]
runsCumulativeProbabilityList = dimension*[0]
# Calculating Data:
# The probabilities of successful runs
print("\nProbabilities:")
for i, (run, drops) in enumerate(runsDroppedDict.items()):
runsList[i] = run
runsProbabilityList[i] = drops / self.totalAttempts
print("Runs: {:<3} {:<3} Probability: {:<3.4f}"
.format(run, "||", runsProbabilityList[i]))
# The cumulative probabilities of successful runs
runsCumulativeProbabilityList =\
self.calculateCumulativeProbabilities(runsProbabilityList)
print("\nCumulative Probabilities:")
for i, _ in enumerate(runsCumulativeProbabilityList):
print("Runs: {:<3} {:<3} Cumulative Probability: {:<3.4f}"
.format(runsList[i], "||", runsCumulativeProbabilityList[i]))
# The estimated drop chance of each item
print("\nItem Drop Chances From Simulation Data:")
for item in self.itemsList:
print("Item '{}' drop chance in sample: {:.4f}"
.format(item.id, item.totalDrops/item.totalAttempts))
return runsList, runsProbabilityList, runsCumulativeProbabilityList
def dropItem(self):
"""
Selects an item to drop based on weighted probabilities and updates
the instance correspondingly.
"""
itemChosen = np.random.choice(self.itemsList,
p=self.itemsProbabilityList)
# Updates by checking if the item is wanted and has dropped before
if(itemChosen.hasDroppedRecently is False):
itemChosen.hasDroppedRecently = True
if(itemChosen.isWanted):
self.droppedWantedItems += 1
# Updates by incrementing the drop variables of the item objects
itemChosen.totalDrops += 1
for item in self.itemsList:
item.totalAttempts += 1
def haveAllItemsDropped(self):
"""
Checks whether all wanted items have dropped from the instance.
:return: Boolean, whether all items have dropped.
"""
if self.droppedWantedItems == self.totalWantedItems:
return True
else:
return False
def reset(self):
"""
Resets the instance to allow a new set of consecutive runs.
"""
self.totalAttempts += 1
self.runNum = 1
self.droppedWantedItems = 0
for item in self.itemsList:
item.hasDroppedRecently = False
def calculateCumulativeProbabilities(self, probabilityList):
"""
Calculates and returns the cumulative probabilities list when given a
probability list.
:param probabilityList: List, containing probabilities.
"""
cumulativeProbability = 0
cumulativeProbabilityList = []
for probability in probabilityList:
cumulativeProbability += probability
cumulativeProbabilityList.append(cumulativeProbability)
return cumulativeProbabilityList
def main():
"""
Creates items, then runs the simulation with the specified items and
subsequently plots.
"""
iterations = 10000
runs = 50
item1 = Item(0.3872, isWanted=False)
item2 = Item(0.3872, isWanted=True)
item3 = Item(0.2256, isWanted=True)
itemsList = [item1, item2, item3]
instance = Instance(itemsList)
instance.plotSimulationProbabilities(runs, iterations)
if __name__ == "__main__":
main()