VWAR Simulator
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
from math import log
from random import random
from pprint import pprint
data = {
'lombardia' : [195,150],
'veneto' : [83,70],
'sardegna' : [34,100],
'faroe' : [33],
'cherkasy' : [19],
'vitebsk' : [18],
'lisbon' : [13],
'pontevedra' : [12],
'vladimir' : [11],
'moldavia' : [11]
}
iterations = 25
printIterations = True
printAverage = True
aliveRegions = 33
totalTerritories = 517
totalUsers = 1000
if sum([v[0] for v in data.values()]) > totalTerritories:
print("Errore numero territori")
exit()
if sum([v[1] if len(v)>1 else 0 for v in data.values()]) > totalUsers:
print("Errore numero utenti")
exit()
otherRegions = aliveRegions - len(data)
otherTerritories = totalTerritories - sum([v[0] for v in data.values()])
otherUsers = totalUsers - sum([ v[1] if len(v)>1 else 0 for v in data.values()])
incomplete = []
for k,v in data.items():
if len(v) == 1:
incomplete.append(k)
for i in range(otherRegions):
data['other%s' % i] = [
otherTerritories/(otherRegions) ,
otherUsers/(otherRegions+len(incomplete))
]
for k in incomplete:
data[k].append(otherUsers/(otherRegions+len(incomplete)))
def score(regions,people):
return regions * log(2+people)
scores = {k: score(*v) for k,v in data.items()}
total = sum(scores.values())
probs = {k: v/total for k,v in scores.items()}
items = []
for v in probs:
items.append((v,probs[v]))
cumul = []
for k,v in enumerate(items):
if k == 0:
cumul.append((v[0],v[1]))
else:
cumul.append((v[0],v[1] + cumul[k-1][1]))
def attack():
r = random()
for v in cumul:
if r < v[1]:
return v[0]
return False
attacksAverage = {}
attacksCount = 0
for y in range(iterations):
attacks = {k: 0 for k,v in scores.items()}
for i in range(24*3):
a = attack()
if a in attacks:
attacks[a] = attacks[a] + 1
attacks['altri'] = 0
for k,v in attacks.items():
if 'other' in k:
attacks['altri'] += v
for i in range(otherRegions):
attacks.pop('other%s'%i,None)
if printIterations: print(attacks)
for k,v in attacks.items():
if k in attacksAverage:
avg = (( attacksAverage[k] * attacksCount ) + v ) / ( attacksCount + 1 )
attacksAverage[k] = (float)("%.2f" % avg)
else:
attacksAverage[k] = v
attacksCount += 1
if printIterations: print()
if printAverage:
print("Media:")
print(attacksAverage)
Python
INFO